Three.js is a powerful JavaScript library that simplifies the creation of 3D graphics in web browsers, making it a popular choice for developing immersive Virtual Reality (VR) and Augmented Reality (AR) experiences.
Three.js integrates seamlessly with the WebXR Device API, which is the web standard for accessing VR and AR capabilities directly in browsers. This allows for the creation of applications that can run across various VR headsets and AR-enabled mobile devices without requiring native app installations or plugins.
For VR development, Three.js enables the creation of fully immersive 3D environments. It supports stereoscopic rendering for VR headsets, tracks the position and orientation of the headset, and processes user input from motion controllers. Three.js provides built-in features, such as a "Enter VR" button, to easily transition a 3D scene into an immersive VR experience.
Three.js facilitates AR development by allowing digital content to be overlaid onto the real world. This can be achieved by integrating with frameworks like AR.js or by utilizing the WebXR Device API's AR mode. This capability opens up possibilities for applications such as virtual product try-ons or interactive educational tools that blend digital and physical elements.
This is a conceptual example of a basic WebXR VR scene. A full working example requires a WebXR-enabled browser and a VR headset.
import * as THREE from 'three';
import { VRButton } from 'three/addons/webxr/VRButton.js';
// Scene, Camera, Renderer setup (assuming these are already defined)
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Enable XR
renderer.xr.enabled = true;
// Add VR button to the DOM
document.body.appendChild(VRButton.createButton(renderer));
// Add a cube to the scene
const geometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.set(0, 1.6, 0); // Position camera for VR (eye level)
// Animation loop for VR
renderer.setAnimationLoop(function () {
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
});
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});