webxr-basics.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>VR</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – VR">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="lesson-title">
  24. <h1>VR</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <p>Making a VR app in three.js is pretty simple. You basically just have to tell
  29. three.js you want to use WebXR. If you think about it a few things about WebXR
  30. should be clear. Which way the camera is pointing is supplied by the VR system
  31. itself since the user turns their head to choose a direction to look. Similarly
  32. the field of view and aspect will be supplied by the VR system since each system
  33. has a different field of view and display aspect.</p>
  34. <p>Let's take an example from the article on <a href="responsive.html">making a responsive webpage</a>
  35. and make it support VR.</p>
  36. <p>Before we get started you're going to need a VR capable device like an Android
  37. smartphone, Google Daydream, Oculus Go, Oculus Rift, Vive, Samsung Gear VR., an
  38. iPhone with a <a href="https://apps.apple.com/us/app/webxr-viewer/id1295998056">WebXR browser</a>.</p>
  39. <p>Next, if you are running locally you need to run a simple web server like is
  40. covered in <a href="setup.html">the article on setting up</a>. </p>
  41. <p>If the device you are using to view VR is not the same computer you're running
  42. on you need to serve your webpage via https or else the browser will not allow using
  43. the WebXR API. The server mentioned in <a href="setup.html">the article on setting up</a>
  44. called <a href="https://greggman.github.io/servez">Servez</a> has an option to use https.
  45. Check it and start the server. </p>
  46. <div class="threejs_center"><img src="../resources/images/servez-https.png" class="nobg" style="width: 912px;"></div>
  47. <p>The note the URLs. You need the one that is your computer's local ipaddress.
  48. It will usually start with <code class="notranslate" translate="no">192</code>, <code class="notranslate" translate="no">172</code> or <code class="notranslate" translate="no">10</code>. Type that full address, including the <code class="notranslate" translate="no">https://</code> part
  49. into your VR device's browser. Note: Your computer and your VR device need to be on the same local network
  50. or WiFi and you probably need to be on a home network. note: Many cafes are setup to disallow this kind of
  51. machine to machine connection.</p>
  52. <p>You'll be greeted with an error something like the one below. Click "advanced" and then click
  53. <em>proceed</em>.</p>
  54. <div class="threejs_center"><img src="../resources/images/https-warning.gif"></div>
  55. <p>Now you can run your examples.</p>
  56. <p>If you're really going to do WebXR development another thing you should learn about is
  57. <a href="https://developers.google.com/web/tools/chrome-devtools/remote-debugging/">remote debugging</a>
  58. so that you can see console warnings, errors, and of course actually
  59. <a href="debugging-javascript.html">debug your code</a>.</p>
  60. <p>If you just want to see the code work below you can just run the code from
  61. this site.</p>
  62. <p>The first thing we need to do is include the VR support after
  63. including three.js</p>
  64. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import * as THREE from 'three';
  65. +import {VRButton} from 'three/addons/webxr/VRButton.js';
  66. </pre>
  67. <p>Then we need to enable three.js's WebXR support and add its
  68. VR button to our page</p>
  69. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function main() {
  70. const canvas = document.querySelector('#c');
  71. const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  72. + renderer.xr.enabled = true;
  73. + document.body.appendChild(VRButton.createButton(renderer));
  74. </pre>
  75. <p>We need to let three.js run our render loop. Until now we have used a
  76. <code class="notranslate" translate="no">requestAnimationFrame</code> loop but to support VR we need to let three.js handle
  77. our render loop for us. We can do that by calling
  78. <a href="/docs/#api/en/renderers/WebGLRenderer.setAnimationLoop"><code class="notranslate" translate="no">WebGLRenderer.setAnimationLoop</code></a> and passing a function to call for the loop.</p>
  79. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render(time) {
  80. time *= 0.001;
  81. if (resizeRendererToDisplaySize(renderer)) {
  82. const canvas = renderer.domElement;
  83. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  84. camera.updateProjectionMatrix();
  85. }
  86. cubes.forEach((cube, ndx) =&gt; {
  87. const speed = 1 + ndx * .1;
  88. const rot = time * speed;
  89. cube.rotation.x = rot;
  90. cube.rotation.y = rot;
  91. });
  92. renderer.render(scene, camera);
  93. - requestAnimationFrame(render);
  94. }
  95. -requestAnimationFrame(render);
  96. +renderer.setAnimationLoop(render);
  97. </pre>
  98. <p>There is one more detail. We should probably set a camera height
  99. that's kind of average for a standing user.</p>
  100. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  101. +camera.position.set(0, 1.6, 0);
  102. </pre>
  103. <p>and move the cubes up to be in front of the camera</p>
  104. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const cube = new THREE.Mesh(geometry, material);
  105. scene.add(cube);
  106. cube.position.x = x;
  107. +cube.position.y = 1.6;
  108. +cube.position.z = -2;
  109. </pre>
  110. <p>We set them to <code class="notranslate" translate="no">z = -2</code> since the camera will now be at <code class="notranslate" translate="no">z = 0</code> and
  111. camera defaults to looking down the -z axis.</p>
  112. <p>This brings up an extremely important point. <strong>Units in VR are in meters</strong>.
  113. In other words <strong>One Unit = One Meter</strong>. This means the camera is 1.6 meters above 0.
  114. The cube's centers are 2 meters in front of the camera. Each cube
  115. is 1x1x1 meter large. This is important because VR needs to adjust things to the
  116. user <em>in the real world</em>. That means we need the units used in three.js to match
  117. the user's own movements.</p>
  118. <p>And with that we should get 3 spinning cubes in front
  119. of the camera with a button to enter VR.</p>
  120. <p></p><div translate="no" class="threejs_example_container notranslate">
  121. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/webxr-basic.html"></iframe></div>
  122. <a class="threejs_center" href="/manual/examples/webxr-basic.html" target="_blank">click here to open in a separate window</a>
  123. </div>
  124. <p></p>
  125. <p>I find that VR works better if we have something surrounding the camera like
  126. room for reference so let's add a simple grid cubemap like we covered in
  127. <a href="backgrounds.html">the article on backgrounds</a>. We'll just use the same grid
  128. texture for each side of the cube which will give as a grid room.</p>
  129. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const scene = new THREE.Scene();
  130. +{
  131. + const loader = new THREE.CubeTextureLoader();
  132. + const texture = loader.load([
  133. + 'resources/images/grid-1024.png',
  134. + 'resources/images/grid-1024.png',
  135. + 'resources/images/grid-1024.png',
  136. + 'resources/images/grid-1024.png',
  137. + 'resources/images/grid-1024.png',
  138. + 'resources/images/grid-1024.png',
  139. + ]);
  140. + scene.background = texture;
  141. +}
  142. </pre>
  143. <p>That's better.</p>
  144. <p></p><div translate="no" class="threejs_example_container notranslate">
  145. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/webxr-basic-w-background.html"></iframe></div>
  146. <a class="threejs_center" href="/manual/examples/webxr-basic-w-background.html" target="_blank">click here to open in a separate window</a>
  147. </div>
  148. <p></p>
  149. <p>Note: To actually see VR you will need a WebXR compatible device.
  150. I believe most Android Phones can support WebXR using Chrome or Firefox.
  151. For iOS you might be able to use this <a href="https://apps.apple.com/us/app/webxr-viewer/id1295998056">WebXR App</a>
  152. though in general WebXR support on iOS is unsupported as of May 2019.</p>
  153. <p>To use WebXR on Android or iPhone you'll need a <em>VR Headset</em>
  154. for phones. You can get them for anywhere from $5 for one made of cardboard
  155. to $100. Unfortunately I don't know which ones to recommend. I've purchased
  156. 6 of them over the years and they are all of varying quality. I've
  157. never paid more than about $25.</p>
  158. <p>Just to mention some of the issues</p>
  159. <ol>
  160. <li><p>Do they fit your phone</p>
  161. <p>Phones come in a variety of sizes and so the VR headsets need to match.
  162. Many headsets claim to match a large variety of sizes. My experience
  163. is the more sizes they match the worse they actually are since instead
  164. of being designed for a specific size they have to make compromises
  165. to match more sizes. Unfortunately multi-size headsets are the most common type.</p>
  166. </li>
  167. <li><p>Can they focus for your face</p>
  168. <p>Some devices have more adjustments than others. Generally there
  169. are at most 2 adjustments. How far the lenses are from your eyes
  170. and how far apart the lenses are.</p>
  171. </li>
  172. <li><p>Are they too reflective</p>
  173. <p>Many headsets of a cone of plastic from your eye to the phone.
  174. If that plastic is shinny or reflective then it will act like
  175. a mirror reflecting the screen and be very distracting.</p>
  176. <p>Few if any of the reviews seem to cover this issue.</p>
  177. </li>
  178. <li><p>Are the comfortable on your face.</p>
  179. <p>Most of the devices rest on your nose like a pair of glasses.
  180. That can hurt after a few minutes. Some have straps that go around
  181. your head. Others have a 3rd strap that goes over your head. These
  182. may or may not help keep the device at the right place.</p>
  183. <p>It turns out for most (all?) devices, you eyes need to be centered
  184. with the lenses. If the lenses are slightly above or below your
  185. eyes the image gets out of focus. This can be very frustrating
  186. as things might start in focus but 45-60 seconds later the device
  187. has shifted up or down 1 millimeter and you suddenly realize you've
  188. been struggling to focus on a blurry image.</p>
  189. </li>
  190. <li><p>Can they support your glasses.</p>
  191. <p>If you wear eye glasses then you'll need to read the reviews to see
  192. if a particular headset works well with eye glasses.</p>
  193. </li>
  194. </ol>
  195. <p>I really can't make any recommendations unfortunately. <a href="https://vr.google.com/cardboard/get-cardboard/">Google has some
  196. cheap recommendations made from cardboard</a>
  197. some of them as low as $5 so maybe start there and if you enjoy it
  198. then consider upgrading. $5 is like the price of 1 coffee so seriously, give it try!</p>
  199. <p>There are also 3 basic types of devices.</p>
  200. <ol>
  201. <li><p>3 degrees of freedom (3dof), no input device</p>
  202. <p>This is generally the phone style although sometimes you can
  203. buy a 3rd party input device. The 3 degrees of freedom
  204. mean you can look up/down (1), left/right(2) and you can tilt
  205. your head left and right (3).</p>
  206. </li>
  207. <li><p>3 degrees of freedom (3dof) with 1 input device (3dof)</p>
  208. <p>This is basically Google Daydream and Oculus GO</p>
  209. <p>These also allow 3 degrees of freedom and include a small
  210. controller that acts like a laser pointer inside VR.
  211. The laser pointer also only has 3 degrees of freedom. The
  212. system can tell which way the input device is pointing but
  213. it can not tell where the device is.</p>
  214. </li>
  215. <li><p>6 degrees of freedom (6dof) with input devices (6dof)</p>
  216. <p>These are <em>the real deal</em> haha. 6 degrees of freedom
  217. means not only do these device know which way you are looking
  218. but they also know where your head actually is. That means
  219. if you move from left to right or forward and back or stand up / sit down
  220. the devices can register this and everything in VR moves accordingly.
  221. It's spookily and amazingly real feeling. With a good demo
  222. you'll be blown away or at least I was and still am.</p>
  223. <p>Further these devices usually include 2 controllers, one
  224. for each hand and the system can tell exactly where your
  225. hands are and which way they are oriented and so you can
  226. manipulate things in VR by just reaching out, touching,
  227. pushing, twisting, etc...</p>
  228. <p>6 degree of freedom devices include the Vive and Vive Pro,
  229. the Oculus Rift and Quest, and I believe all of the Windows MR devices.</p>
  230. </li>
  231. </ol>
  232. <p>With all that covered I don't for sure know which devices will work with WebXR.
  233. I'm 99% sure that most Android phones will work when running Chrome. You may
  234. need to turn on WebXR support in <a href="about:flags"><code class="notranslate" translate="no">about:flags</code></a>. I also know Google
  235. Daydream will also work and similarly you need to enable WebXR support in
  236. <a href="about:flags"><code class="notranslate" translate="no">about:flags</code></a>. Oculus Rift, Vive, and Vive Pro will work via
  237. Chrome or Firefox. I'm less sure about Oculus Go and Oculus Quest as both of
  238. them use custom OSes but according to the internet they both appear to work.</p>
  239. <p>Okay, after that long detour about VR Devices and WebXR
  240. there's some things to cover</p>
  241. <ul>
  242. <li><p>Supporting both VR and Non-VR</p>
  243. <p>AFAICT, at least as of r112, there is no easy way to support
  244. both VR and non-VR modes with three.js. Ideally
  245. if not in VR mode you'd be able to control the camera using
  246. whatever means you want, for example the <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a>,
  247. and you'd get some kind of event when switching into and
  248. out of VR mode so that you could turn the controls on/off.</p>
  249. </li>
  250. </ul>
  251. <p>If three.js adds some support to do both I'll try to update
  252. this article. Until then you might need 2 versions of your
  253. site OR pass in a flag in the URL, something like</p>
  254. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">https://mysite.com/mycooldemo?allowvr=true
  255. </pre><p>Then we could add some links in to switch modes</p>
  256. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;body&gt;
  257. &lt;canvas id="c"&gt;&lt;/canvas&gt;
  258. + &lt;div class="mode"&gt;
  259. + &lt;a href="?allowvr=true" id="vr"&gt;Allow VR&lt;/a&gt;
  260. + &lt;a href="?" id="nonvr"&gt;Use Non-VR Mode&lt;/a&gt;
  261. + &lt;/div&gt;
  262. &lt;/body&gt;
  263. </pre>
  264. <p>and some CSS to position them</p>
  265. <pre class="prettyprint showlinemods notranslate lang-css" translate="no">body {
  266. margin: 0;
  267. }
  268. #c {
  269. width: 100%;
  270. height: 100%;
  271. display: block;
  272. }
  273. +.mode {
  274. + position: absolute;
  275. + right: 1em;
  276. + top: 1em;
  277. +}
  278. </pre>
  279. <p>in your code you could use that parameter like this</p>
  280. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function main() {
  281. const canvas = document.querySelector('#c');
  282. const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  283. - renderer.xr.enabled = true;
  284. - document.body.appendChild(VRButton.createButton(renderer));
  285. const fov = 75;
  286. const aspect = 2; // the canvas default
  287. const near = 0.1;
  288. const far = 5;
  289. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  290. camera.position.set(0, 1.6, 0);
  291. + const params = (new URL(document.location)).searchParams;
  292. + const allowvr = params.get('allowvr') === 'true';
  293. + if (allowvr) {
  294. + renderer.xr.enabled = true;
  295. + document.body.appendChild(VRButton.createButton(renderer));
  296. + document.querySelector('#vr').style.display = 'none';
  297. + } else {
  298. + // no VR, add some controls
  299. + const controls = new OrbitControls(camera, canvas);
  300. + controls.target.set(0, 1.6, -2);
  301. + controls.update();
  302. + document.querySelector('#nonvr').style.display = 'none';
  303. + }
  304. </pre>
  305. <p>Whether that's good or bad I don't know. I have a feeling the differences
  306. between what's needed for VR and what's needed for non-VR are often
  307. very different so for all but the most simple things maybe 2 separate pages
  308. are better? You'll have to decide.</p>
  309. <p>Note for various reasons this will not work in the live editor
  310. on this site so if you want to check it out
  311. <a href="../examples/webxr-basic-vr-optional.html" target="_blank">click here</a>.
  312. It should start in non-VR mode and you can use the mouse or fingers to move
  313. the camera. Clicking "Allow VR" should switch to allow VR mode and you should
  314. be able to click "Enter VR" if you're on a VR device.</p>
  315. <ul>
  316. <li><p>Deciding on the level of VR support</p>
  317. <p>Above we covered 3 types of VR devices. </p>
  318. <ul>
  319. <li>3DOF no input</li>
  320. <li>3DOF + 3DOF input</li>
  321. <li>6DOF + 6DOF input</li>
  322. </ul>
  323. <p>You need to decide how much effort you're willing to put in
  324. to support each type of device.</p>
  325. <p>For example the simplest device has no input. The best you can
  326. generally do is make it so there are some buttons or objects in the user's view
  327. and if the user aligns some marker in the center of the display
  328. on those objects for 1/2 a second or so then that button is clicked.
  329. A common UX is to display a small timer that will appear over the object indicating
  330. if you keep the marker there for a moment the object/button will be selected.</p>
  331. <p>Since there is no other input that's about the best you can do</p>
  332. <p>The next level up you have one 3DOF input device. Generally it
  333. can point at things and the user has at least 2 buttons. The Daydream
  334. also has a touchpad which provides normal touch inputs.</p>
  335. <p>In any case if a user has this type of device it's far more
  336. comfortable for the user to by able to point at things with
  337. their controller than it is to make them do it with their
  338. head by looking at things.</p>
  339. <p>A similar level to that might be 3DOF or 6DOF device with a
  340. game console controller. You'll have to decide what to do here.
  341. I suspect the most common thing is the user still has to look
  342. to point and the controller is just used for buttons.</p>
  343. <p>The last level is a user with a 6DOF headset and 2 6DOF controllers.
  344. Those users will find an experience that is only 3DOF to often
  345. be frustrating. Similarly they usually expect to be able to
  346. virtually manipulate things with their hands in VR so you'll
  347. have to decide if you want to support that or not.</p>
  348. </li>
  349. </ul>
  350. <p>As you can see getting started in VR is pretty easy but
  351. actually making something shippable in VR will require
  352. lots of decision making and design.</p>
  353. <p>This was a pretty brief intro into VR with three.js. We'll
  354. cover some of the input methods in <a href="webxr-look-to-select.html">future articles</a>.</p>
  355. </div>
  356. </div>
  357. </div>
  358. <script src="../resources/prettify.js"></script>
  359. <script src="../resources/lesson.js"></script>
  360. </body></html>