|
1 | 1 | import * as THREE from 'three';
|
2 |
| -import { VRButton } from 'three/addons/webxr/VRButton.js'; |
| 2 | +// import { VRButton } from 'three/addons/webxr/VRButton.js'; |
| 3 | +import { ARButton } from 'three/examples/jsm/webxr/ARButton.js'; |
| 4 | +import { setupSimulation, updateSimulation } from './src/scene'; |
3 | 5 |
|
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); |
| 6 | +// Set up camera |
| 7 | +const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.01, 5000 ); |
| 8 | +camera.position.set( 0, 2, 5 ); |
11 | 9 |
|
| 10 | +// Set up renderer |
12 | 11 | const renderer = new THREE.WebGLRenderer();
|
13 | 12 | renderer.xr.enabled = true;
|
14 |
| -renderer.setSize(window.innerWidth, window.innerHeight); |
| 13 | +renderer.setSize( window.innerWidth, window.innerHeight ); |
| 14 | +renderer.setAnimationLoop( animate ); // Start running animation loop |
15 | 15 |
|
16 | 16 | // Append DOM elements
|
17 | 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); |
| 18 | +// document.body.appendChild( VRButton.createButton( renderer ) ); |
| 19 | +document.body.appendChild( ARButton.createButton( renderer ) ); |
26 | 20 |
|
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 | 21 |
|
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 |
| -// }); |
| 22 | +// |
| 23 | +const scene = new THREE.Scene(); |
| 24 | +scene.background = null; |
| 25 | +// const sphere = setupSimulation( scene ); |
| 26 | +const particles = setupSimulation( scene ); |
47 | 27 |
|
48 |
| -// Animate |
49 | 28 | function animate() {
|
50 |
| - requestAnimationFrame(animate); |
51 |
| - renderer.render(scene, camera); |
| 29 | + // requestAnimationFrame( animate ); // ?? |
| 30 | + // updateSimulation( sphere ); |
| 31 | + updateSimulation( particles ); |
| 32 | + renderer.render( scene, camera ); |
52 | 33 | }
|
53 |
| -animate(); |
54 | 34 |
|
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'); |
| 35 | +navigator.xr.isSessionSupported('immersive-ar').then(supported => { |
65 | 36 | 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."); |
| 37 | + navigator.xr.requestSession('immersive-ar', { |
| 38 | + requiredFeatures: ['plane-detection'] |
| 39 | + }).then(session => { |
| 40 | + renderer.xr.setSession(session); |
| 41 | + }).catch(err => { |
| 42 | + console.warn("Feature not supported:", err); |
| 43 | + }); |
73 | 44 | }
|
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); |
| 45 | +}); |
0 commit comments