|
| 1 | +import * as THREE from 'three'; |
| 2 | +import { VRButton } from 'three/addons/webxr/VRButton.js'; |
| 3 | + |
| 4 | +// Scene Setup |
| 5 | +const scene = new THREE.Scene(); |
| 6 | +scene.background = new THREE.Color(0x111111); |
| 7 | +scene.fog = new THREE.FogExp2(0xffffff, 0.02); |
| 8 | + |
| 9 | +const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 100); |
| 10 | +camera.position.set(0, 3, 10); |
| 11 | + |
| 12 | +const renderer = new THREE.WebGLRenderer(); |
| 13 | +renderer.xr.enabled = true; |
| 14 | +renderer.setSize(window.innerWidth, window.innerHeight); |
| 15 | + |
| 16 | +// Append DOM elements |
| 17 | +document.body.appendChild( renderer.domElement ); |
| 18 | +document.body.appendChild( VRButton.createButton( renderer ) ); |
| 19 | + |
| 20 | +// Add Light |
| 21 | +const spotLight = new THREE.SpotLight(0xffffff, 10, 50, Math.PI / 4, 0.3); |
| 22 | +spotLight.position.set(0, 10, 5); |
| 23 | +spotLight.target.position.set(0, 0, 0); |
| 24 | +scene.add(spotLight); |
| 25 | +scene.add(spotLight.target); |
| 26 | + |
| 27 | +// Add Visible Object |
| 28 | +const geometry = new THREE.SphereGeometry(1, 32, 32); |
| 29 | +const material = new THREE.MeshStandardMaterial({ color: 0xffaa00 }); |
| 30 | +const sphere = new THREE.Mesh(geometry, material); |
| 31 | +sphere.position.set(0, 1, 0); |
| 32 | +scene.add(sphere); |
| 33 | + |
| 34 | +// if (navigator.xr) { |
| 35 | +// const session = await navigator.xr.requestSession( "immersive-ar", { |
| 36 | +// requiredFeatures: [ "plane-detection" ] |
| 37 | +// } ).then( (session) => { |
| 38 | +// console.log( 'Depth sensing enabled:', session ); |
| 39 | +// } ); |
| 40 | +// } |
| 41 | + |
| 42 | +// const session = await navigator.xr.requestSession('immersive-ar', { |
| 43 | +// requiredFeatures: ['mesh-detection'] |
| 44 | +// }).then((session) => { |
| 45 | +// console.log('Scene Understanding Enabled!', session); |
| 46 | +// }); |
| 47 | + |
| 48 | +// Animate |
| 49 | +function animate() { |
| 50 | + requestAnimationFrame(animate); |
| 51 | + renderer.render(scene, camera); |
| 52 | +} |
| 53 | +animate(); |
| 54 | + |
| 55 | +if ("xr" in window.navigator) { |
| 56 | + /* WebXR can be used! */ |
| 57 | + console.log("a"); |
| 58 | +} else { |
| 59 | + /* WebXR isn't available */ |
| 60 | + console.log("b"); |
| 61 | +} |
| 62 | + |
| 63 | +try { |
| 64 | + const supported = await navigator.xr.isSessionSupported('immersive-ar'); |
| 65 | + if (supported) { |
| 66 | + const session = await navigator.xr.requestSession('immersive-ar', { requiredFeatures: ['plane-detection'] }) |
| 67 | + .catch(() => console.warn("Plane detection not supported on this device.")); |
| 68 | + if (session) { |
| 69 | + console.log("XR session with plane detection started."); |
| 70 | + } |
| 71 | + } else { |
| 72 | + console.warn("XR immersive AR not supported on this device."); |
| 73 | + } |
| 74 | +} catch (err) { |
| 75 | + console.error("Error checking XR support:", err); |
| 76 | +} |
| 77 | + |
| 78 | +// async function checkXRSupport() { |
| 79 | +// if (navigator.xr) { |
| 80 | +// try { |
| 81 | +// const supported = await navigator.xr.isSessionSupported('immersive-ar'); |
| 82 | +// if (supported) { |
| 83 | +// const session = await navigator.xr.requestSession('immersive-ar', { requiredFeatures: ['plane-detection'] }) |
| 84 | +// .catch(() => console.warn("Plane detection not supported on this device.")); |
| 85 | +// if (session) { |
| 86 | +// console.log("XR session with plane detection started."); |
| 87 | +// } |
| 88 | +// } else { |
| 89 | +// console.warn("XR immersive AR not supported on this device."); |
| 90 | +// } |
| 91 | +// } catch (err) { |
| 92 | +// console.error("Error checking XR support:", err); |
| 93 | +// } |
| 94 | +// } else { |
| 95 | +// console.warn("WebXR not supported in this browser."); |
| 96 | +// } |
| 97 | +// } |
| 98 | + |
| 99 | +// // Call this function in response to a user action (e.g., button click) |
| 100 | +// document.getElementById("startXR").addEventListener("click", checkXRSupport); |
0 commit comments