123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js ar - hit test</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> ar - hit test<br/>Enable chrome://flags/#webxr-ar-module<br/>Enable chrome://flags/#webxr-hit-test<br/>(Chrome Android 80+)
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { ARButton } from './jsm/webxr/ARButton.js';
- var container;
- var camera, scene, renderer;
- var controller;
- var matrix, ray, reticle;
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- scene = new THREE.Scene();
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 20 );
- var light = new THREE.HemisphereLight( 0xffffff, 0xbbbbff, 1 );
- light.position.set( 0.5, 1, 0.25 );
- scene.add( light );
- //
- renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.xr.enabled = true;
- container.appendChild( renderer.domElement );
- //
- document.body.appendChild( ARButton.createButton( renderer ) );
- //
- var geometry = new THREE.CylinderBufferGeometry( 0.1, 0.1, 0.2, 32 ).translate( 0, 0.1, 0 );
- function onSelect() {
- if ( reticle.visible ) {
- var material = new THREE.MeshPhongMaterial( { color: 0xffffff * Math.random() } );
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.setFromMatrixPosition( reticle.matrix );
- mesh.scale.y = Math.random() * 2 + 1;
- scene.add( mesh );
- }
- }
- controller = renderer.xr.getController( 0 );
- controller.addEventListener( 'select', onSelect );
- scene.add( controller );
- matrix = new THREE.Matrix4();
- ray = new THREE.Ray();
- reticle = new THREE.Mesh(
- new THREE.RingBufferGeometry( 0.15, 0.2, 32 ).rotateX( - Math.PI / 2 ),
- new THREE.MeshBasicMaterial()
- );
- reticle.matrixAutoUpdate = false;
- reticle.visible = false;
- scene.add( reticle );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- renderer.setAnimationLoop( render );
- }
- function render( timestamp, frame ) {
- if ( frame ) {
- var referenceSpace = renderer.xr.getReferenceSpace();
- var session = renderer.xr.getSession();
- var pose = frame.getViewerPose( referenceSpace );
- if ( pose ) {
- matrix.fromArray( pose.transform.matrix );
- ray.origin.set( 0, 0, 0 );
- ray.direction.set( 0, 0, - 1 );
- ray.applyMatrix4( matrix );
- var xrRay = new XRRay( ray.origin, ray.direction );
- session.requestHitTest( xrRay, referenceSpace )
- .then( function ( results ) {
- if ( results.length ) {
- var hitResult = results[ 0 ];
- reticle.visible = true;
- reticle.matrix.fromArray( hitResult.hitMatrix );
- } else {
- reticle.visible = false;
- }
- } );
- }
- }
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|