webxr_ar_plane_detection.html 4.0 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. let controller;
  29. init();
  30. animate();
  31. const planesAdded = new Set();
  32. function init() {
  33. const container = document.createElement( 'div' );
  34. document.body.appendChild( container );
  35. scene = new THREE.Scene();
  36. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 20 );
  37. const light = new THREE.HemisphereLight( 0xffffff, 0xbbbbff, 1 );
  38. light.position.set( 0.5, 1, 0.25 );
  39. scene.add( light );
  40. //
  41. renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
  42. renderer.setPixelRatio( window.devicePixelRatio );
  43. renderer.setSize( window.innerWidth, window.innerHeight );
  44. renderer.xr.enabled = true;
  45. container.appendChild( renderer.domElement );
  46. //
  47. document.body.appendChild( ARButton.createButton( renderer, {
  48. requiredFeatures: ['plane-detection']
  49. } ) );
  50. //
  51. window.addEventListener( 'resize', onWindowResize );
  52. renderer.xr.addEventListener( 'sessionstart', function () {
  53. camera.position.set( 0, 0, 0 );
  54. } );
  55. renderer.xr.addEventListener( 'planeadded', function (e) {
  56. console.log( "plane added", e.data )
  57. } );
  58. renderer.xr.addEventListener( 'planeremoved', function (e) {
  59. console.log( "plane removed", e.data )
  60. } );
  61. renderer.xr.addEventListener( 'planechanged', function (e) {
  62. console.log( "plane changed", e.data)
  63. } );
  64. renderer.xr.addEventListener( 'planesdetected', function (e) {
  65. const detectedPlanes = e.data;
  66. const referenceSpace = renderer.xr.getReferenceSpace();
  67. console.log( `Detected ${detectedPlanes.size} planes` );
  68. detectedPlanes.forEach( plane => {
  69. if ( planesAdded.has( plane ) ) return;
  70. planesAdded.add( plane );
  71. const frame = renderer.xr.getFrame();
  72. const planePose = frame.getPose( plane.planeSpace, referenceSpace );
  73. const planeGeometry = new THREE.BufferGeometry();
  74. const polygon = plane.polygon;
  75. let minX = Number.MAX_SAFE_INTEGER;
  76. let maxX = Number.MIN_SAFE_INTEGER;
  77. let minZ = Number.MAX_SAFE_INTEGER;
  78. let maxZ = Number.MIN_SAFE_INTEGER;
  79. polygon.forEach( point => {
  80. minX = Math.min( minX, point.x );
  81. maxX = Math.max( maxX, point.x );
  82. minZ = Math.min( minZ, point.z );
  83. maxZ = Math.max( maxZ, point.z );
  84. } );
  85. const width = maxX - minX;
  86. const height = maxZ - minZ;
  87. const boxMesh = new THREE.Mesh(
  88. new THREE.BoxGeometry( width, 0.01, height ),
  89. new THREE.MeshBasicMaterial( { color: 0xffffff * Math.random() } )
  90. );
  91. boxMesh.matrixAutoUpdate = false;
  92. boxMesh.matrix.fromArray( planePose.transform.matrix );
  93. scene.add( boxMesh );
  94. } );
  95. } );
  96. }
  97. function onWindowResize() {
  98. camera.aspect = window.innerWidth / window.innerHeight;
  99. camera.updateProjectionMatrix();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. }
  102. //
  103. function animate() {
  104. renderer.setAnimationLoop( render );
  105. }
  106. function render() {
  107. renderer.render( scene, camera );
  108. }
  109. </script>
  110. </body>
  111. </html>