webxr_ar_plane_detection.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js ar - plane detection</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 - plane detection<br/>(Chrome Android 81+)
  12. </div>
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { ARButton } from 'three/addons/webxr/ARButton.js';
  27. let camera, scene, renderer;
  28. init();
  29. animate();
  30. const planesAdded = new Set();
  31. function init() {
  32. const container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. scene = new THREE.Scene();
  35. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 20 );
  36. const light = new THREE.HemisphereLight( 0xffffff, 0xbbbbff, 1 );
  37. light.position.set( 0.5, 1, 0.25 );
  38. scene.add( light );
  39. //
  40. renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
  41. renderer.setPixelRatio( window.devicePixelRatio );
  42. renderer.setSize( window.innerWidth, window.innerHeight );
  43. renderer.xr.enabled = true;
  44. container.appendChild( renderer.domElement );
  45. //
  46. document.body.appendChild( ARButton.createButton( renderer, {
  47. requiredFeatures: [ 'plane-detection' ]
  48. } ) );
  49. //
  50. window.addEventListener( 'resize', onWindowResize );
  51. renderer.xr.addEventListener( 'sessionstart', function () {
  52. camera.position.set( 0, 0, 0 );
  53. } );
  54. renderer.xr.addEventListener( 'planeadded', function ( e ) {
  55. console.log( 'plane added', e.data );
  56. } );
  57. renderer.xr.addEventListener( 'planeremoved', function ( e ) {
  58. console.log( 'plane removed', e.data );
  59. } );
  60. renderer.xr.addEventListener( 'planechanged', function ( e ) {
  61. console.log( 'plane changed', e.data );
  62. } );
  63. renderer.xr.addEventListener( 'planesdetected', function ( e ) {
  64. const detectedPlanes = e.data;
  65. const referenceSpace = renderer.xr.getReferenceSpace();
  66. console.log( `Detected ${detectedPlanes.size} planes` );
  67. detectedPlanes.forEach( plane => {
  68. if ( planesAdded.has( plane ) ) return;
  69. planesAdded.add( plane );
  70. const frame = renderer.xr.getFrame();
  71. const planePose = frame.getPose( plane.planeSpace, referenceSpace );
  72. const polygon = plane.polygon;
  73. let minX = Number.MAX_SAFE_INTEGER;
  74. let maxX = Number.MIN_SAFE_INTEGER;
  75. let minZ = Number.MAX_SAFE_INTEGER;
  76. let maxZ = Number.MIN_SAFE_INTEGER;
  77. polygon.forEach( point => {
  78. minX = Math.min( minX, point.x );
  79. maxX = Math.max( maxX, point.x );
  80. minZ = Math.min( minZ, point.z );
  81. maxZ = Math.max( maxZ, point.z );
  82. } );
  83. const width = maxX - minX;
  84. const height = maxZ - minZ;
  85. const boxMesh = new THREE.Mesh(
  86. new THREE.BoxGeometry( width, 0.01, height ),
  87. new THREE.MeshBasicMaterial( { color: 0xffffff * Math.random() } )
  88. );
  89. boxMesh.matrixAutoUpdate = false;
  90. boxMesh.matrix.fromArray( planePose.transform.matrix );
  91. scene.add( boxMesh );
  92. } );
  93. } );
  94. }
  95. function onWindowResize() {
  96. camera.aspect = window.innerWidth / window.innerHeight;
  97. camera.updateProjectionMatrix();
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. }
  100. //
  101. function animate() {
  102. renderer.setAnimationLoop( render );
  103. }
  104. function render() {
  105. renderer.render( scene, camera );
  106. }
  107. </script>
  108. </body>
  109. </html>