webxr_ar_plane_detection.html 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { ARButton } from 'three/addons/webxr/ARButton.js';
  24. import { XRPlanes } from 'three/addons/webxr/XRPlanes.js';
  25. //
  26. const renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
  27. renderer.setPixelRatio( window.devicePixelRatio );
  28. renderer.setSize( window.innerWidth, window.innerHeight );
  29. renderer.setAnimationLoop( animate );
  30. renderer.xr.enabled = true;
  31. document.body.appendChild( renderer.domElement );
  32. document.body.appendChild( ARButton.createButton( renderer, {
  33. requiredFeatures: [ 'plane-detection' ]
  34. } ) );
  35. window.addEventListener( 'resize', onWindowResize );
  36. //
  37. const scene = new THREE.Scene();
  38. const planes = new XRPlanes( renderer );
  39. scene.add( planes );
  40. const camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 20 );
  41. const light = new THREE.HemisphereLight( 0xffffff, 0xbbbbff, 3 );
  42. light.position.set( 0.5, 1, 0.25 );
  43. scene.add( light );
  44. //
  45. function onWindowResize() {
  46. camera.aspect = window.innerWidth / window.innerHeight;
  47. camera.updateProjectionMatrix();
  48. renderer.setSize( window.innerWidth, window.innerHeight );
  49. }
  50. function animate() {
  51. renderer.render( scene, camera );
  52. }
  53. </script>
  54. </body>
  55. </html>