webxr_ar_hittest.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js ar - hit test</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <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+)
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { ARButton } from './jsm/webxr/ARButton.js';
  16. var container;
  17. var camera, scene, renderer;
  18. var controller;
  19. var matrix, ray, reticle;
  20. init();
  21. animate();
  22. function init() {
  23. container = document.createElement( 'div' );
  24. document.body.appendChild( container );
  25. scene = new THREE.Scene();
  26. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 20 );
  27. var light = new THREE.HemisphereLight( 0xffffff, 0xbbbbff, 1 );
  28. light.position.set( 0.5, 1, 0.25 );
  29. scene.add( light );
  30. //
  31. renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
  32. renderer.setPixelRatio( window.devicePixelRatio );
  33. renderer.setSize( window.innerWidth, window.innerHeight );
  34. renderer.xr.enabled = true;
  35. container.appendChild( renderer.domElement );
  36. //
  37. document.body.appendChild( ARButton.createButton( renderer ) );
  38. //
  39. var geometry = new THREE.CylinderBufferGeometry( 0.1, 0.1, 0.2, 32 ).translate( 0, 0.1, 0 );
  40. function onSelect() {
  41. if ( reticle.visible ) {
  42. var material = new THREE.MeshPhongMaterial( { color: 0xffffff * Math.random() } );
  43. var mesh = new THREE.Mesh( geometry, material );
  44. mesh.position.setFromMatrixPosition( reticle.matrix );
  45. mesh.scale.y = Math.random() * 2 + 1;
  46. scene.add( mesh );
  47. }
  48. }
  49. controller = renderer.xr.getController( 0 );
  50. controller.addEventListener( 'select', onSelect );
  51. scene.add( controller );
  52. matrix = new THREE.Matrix4();
  53. ray = new THREE.Ray();
  54. reticle = new THREE.Mesh(
  55. new THREE.RingBufferGeometry( 0.15, 0.2, 32 ).rotateX( - Math.PI / 2 ),
  56. new THREE.MeshBasicMaterial()
  57. );
  58. reticle.matrixAutoUpdate = false;
  59. reticle.visible = false;
  60. scene.add( reticle );
  61. //
  62. window.addEventListener( 'resize', onWindowResize, false );
  63. }
  64. function onWindowResize() {
  65. camera.aspect = window.innerWidth / window.innerHeight;
  66. camera.updateProjectionMatrix();
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. }
  69. //
  70. function animate() {
  71. renderer.setAnimationLoop( render );
  72. }
  73. function render( timestamp, frame ) {
  74. if ( frame ) {
  75. var referenceSpace = renderer.xr.getReferenceSpace();
  76. var session = renderer.xr.getSession();
  77. var pose = frame.getViewerPose( referenceSpace );
  78. if ( pose ) {
  79. matrix.fromArray( pose.transform.matrix );
  80. ray.origin.set( 0, 0, 0 );
  81. ray.direction.set( 0, 0, - 1 );
  82. ray.applyMatrix4( matrix );
  83. var xrRay = new XRRay( ray.origin, ray.direction );
  84. session.requestHitTest( xrRay, referenceSpace )
  85. .then( function ( results ) {
  86. if ( results.length ) {
  87. var hitResult = results[ 0 ];
  88. reticle.visible = true;
  89. reticle.matrix.fromArray( hitResult.hitMatrix );
  90. } else {
  91. reticle.visible = false;
  92. }
  93. } );
  94. }
  95. }
  96. renderer.render( scene, camera );
  97. }
  98. </script>
  99. </body>
  100. </html>