A THREE.js-based starting point for VR experiences that work well in both Google Cardboard and other VR headsets. Also provides a fallback for experiencing the same content without requiring a VR device.
This project relies heavily on the webvr-polyfill to provide VR support even if the WebVR spec is not implemented.
The easiest way to start is to fork this repository or copy its contents into a new directory.
Alternatively, you can start from scratch. The key parts that the boilerplate provides are:
For example,
var effect = new THREE.VREffect(renderer);
var manager = new WebVRManager(renderer, effect);
The manager handles going in and out of VR mode. Instead of calling
renderer.render() or effect.render(), you call manager.render(), which
renders in monocular view by default, or side-by-side binocular view when in VR
mode.
The polyfill and boilerplate are also available via bower. Easy install:
bower install webvr-boilerplate
Features:
Bugs and known issues:
All configuration is done through the global window.WebVRConfig object. You
can use the following properties:
FORCE_DISTORTION (Boolean): Set this to true to enable barrel distortion
for cardboard devices, even if the device has unknown display properties.PREVENT_DISTORTION (Boolean): Set this to true to prevent barrel
distortion for cardboard devices, even if it is a known device. Do this if you
encounter issues with barrel distortion on cardboard devices.DISTORTION_BGCOLOR (Object): Use this to change the background color used
in the barrel distortion shader pass (cardboard devices). Pass an object with
x, y, z and w properties (type number, ranged 0..1).Here is an example of WebVRConfig usage:
WebVRConfig = {
// Forces cardboard distortion in VR mode.
//FORCE_DISTORTION: true, // Default: false.
// Prevents cardboard distortion in VR mode
//PREVENT_DISTORTION: true, // Default: false.
// Override the cardboard distortion background color.
//DISTORTION_BGCOLOR: {x: 1, y: 0, z: 0, w: 1}, // Default: (0,0,0,1).
// Show eye centers (for debugging).
//SHOW_EYE_CENTERS: true, // Default: false.
};
In addition to render(), WebVRManager provides a sparse API surface:
isVRMode() (Boolean): True if and only if currently in VR mode.getViewer() (Object): Information about the Cardboard-like viewer that
is currently selected. Viewers are pre-defined in device-info.js.As well as emitting the following events:
modechange: When the user changes the current mode.viewerchange: When the user selects a new viewer.$ claude mcp add webvr-boilerplate \
-- python -m otcore.mcp_server <graph>