This guide provides a brief overview of the basic components of a web-based VR application made with three.js.


First, you have to include [link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/webxr/VRButton.js VRButton.js] into your project.

import { VRButton } from 'three/examples/jsm/webxr/VRButton.js';

*VRButton.createButton()* does two important things: It creates a button which indicates VR compatibility. Besides, it initiates a VR session if the user activates the button. The only thing you have to do is to add the following line of code to your app.

document.body.appendChild( VRButton.createButton( renderer ) );

Next, you have to tell your instance of *WebGLRenderer* to enable XR rendering.

renderer.xr.enabled = true;

Finally, you have to adjust your animation loop since we can't use our well known *window.requestAnimationFrame()* function. For VR projects we use [page:WebGLRenderer.setAnimationLoop setAnimationLoop]. The minimal code looks like this:

renderer.setAnimationLoop( function () { renderer.render( scene, camera ); } );

Next Steps

Have a look at one of the official WebVR examples to see this workflow in action.

[example:webxr_vr_ballshooter WebXR / VR / ballshooter]
[example:webxr_vr_cubes WebXR / VR / cubes]
[example:webxr_vr_dragging WebXR / VR / dragging]
[example:webxr_vr_paint WebXR / VR / paint]
[example:webxr_vr_panorama_depth WebXR / VR / panorama_depth]
[example:webxr_vr_panorama WebXR / VR / panorama]
[example:webxr_vr_rollercoaster WebXR / VR / rollercoaster]
[example:webxr_vr_sandbox WebXR / VR / sandbox]
[example:webxr_vr_sculpt WebXR / VR / sculpt]
[example:webxr_vr_video WebXR / VR / video]