Device compatibility (mobile, VR)

Back to Modules

Device Compatibility: Mobile and VR with Three.js

Three.js offers strong compatibility with various devices, including mobile and virtual reality (VR) platforms. This enables developers to create immersive 3D experiences that are accessible across a wide range of hardware.

Mobile Compatibility

Three.js applications run directly in modern web browsers, making them inherently compatible with mobile devices. However, optimizing for mobile performance is crucial due to limited processing power and battery life.

Considerations for Mobile:

VR Compatibility (WebXR)

Three.js is a leading library for creating VR experiences on the web, primarily through its integration with the WebXR Device API. WebXR is a web standard that enables immersive virtual reality (VR) and augmented reality (AR) applications to run directly in web browsers, without needing external plug-ins.

Key Aspects of Three.js Mobile VR Compatibility:

Example: Basic WebXR VR Scene (Conceptual)

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);
});