diff --git a/public/index.html b/public/index.html index ee35392947722acc200960877de4b3f211e39965..5287b2757919ee7a7c4002093a55110e25fdb51c 100644 --- a/public/index.html +++ b/public/index.html @@ -8,6 +8,7 @@ <!-- three.js --> <script src="https://unpkg.com/three@0.126.0/build/three.js"></script> + <script src="https://unpkg.com/three@0.126.0/examples/js/loaders/GLTFLoader.js"></script> </head> <body> @@ -72,6 +73,33 @@ session.updateRenderState({ // near the viewer's position at the time the session was created. const referenceSpace = await session.requestReferenceSpace('local'); +// A 'local' reference space has a native origin that is located +// near the viewer's position at the time the session was created. +const referenceSpace = await session.requestReferenceSpace('local'); + +// Create another XRReferenceSpace that has the viewer as the origin. +const viewerSpace = await session.requestReferenceSpace('viewer'); +// Perform hit testing using the viewer as origin. +const hitTestSource = await session.requestHitTestSource({ space: viewerSpace }); + + +const loader = new THREE.GLTFLoader(); +let reticle; +loader.load("https://immersive-web.github.io/webxr-samples/media/gltf/reticle/reticle.gltf", function(gltf) { + reticle = gltf.scene; + reticle.visible = false; + scene.add(reticle); +}) + +let flower; +loader.load("https://immersive-web.github.io/webxr-samples/media/gltf/sunflower/sunflower.gltf", function(gltf) { + flower = gltf.scene; +}); + +// Create a render loop that allows us to draw on the AR view. +const onXRFrame = (time, frame) => { + + // Create a render loop that allows us to draw on the AR view. const onXRFrame = (time, frame) => { // Queue up the next draw request. @@ -94,6 +122,16 @@ const onXRFrame = (time, frame) => { camera.matrix.fromArray(view.transform.matrix) camera.projectionMatrix.fromArray(view.projectionMatrix); camera.updateMatrixWorld(true); + + +const hitTestResults = frame.getHitTestResults(hitTestSource); +if (hitTestResults.length > 0 && reticle) { + const hitPose = hitTestResults[0].getPose(referenceSpace); + reticle.visible = true; + reticle.position.set(hitPose.transform.position.x, hitPose.transform.position.y, hitPose.transform.position.z) + reticle.updateMatrixWorld(true); +} + // Render the scene with THREE.WebGLRenderer. renderer.render(scene, camera)