webxr_ar_lighting.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js ar - lighting estimation</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 - Lighting Estimation<br/>
  12. (Chrome Android 90+)
  13. </div>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  25. import { ARButton } from 'three/addons/webxr/ARButton.js';
  26. import { XREstimatedLight } from 'three/addons/webxr/XREstimatedLight.js';
  27. let camera, scene, renderer;
  28. let controller;
  29. let defaultEnvironment;
  30. init();
  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 defaultLight = new THREE.HemisphereLight( 0xffffff, 0xbbbbff, 1 );
  37. defaultLight.position.set( 0.5, 1, 0.25 );
  38. scene.add( defaultLight );
  39. //
  40. renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
  41. renderer.setPixelRatio( window.devicePixelRatio );
  42. renderer.setSize( window.innerWidth, window.innerHeight );
  43. renderer.setAnimationLoop( animate );
  44. renderer.xr.enabled = true;
  45. container.appendChild( renderer.domElement );
  46. // Don't add the XREstimatedLight to the scene initially.
  47. // It doesn't have any estimated lighting values until an AR session starts.
  48. const xrLight = new XREstimatedLight( renderer );
  49. xrLight.addEventListener( 'estimationstart', () => {
  50. // Swap the default light out for the estimated one one we start getting some estimated values.
  51. scene.add( xrLight );
  52. scene.remove( defaultLight );
  53. // The estimated lighting also provides an environment cubemap, which we can apply here.
  54. if ( xrLight.environment ) {
  55. scene.environment = xrLight.environment;
  56. }
  57. } );
  58. xrLight.addEventListener( 'estimationend', () => {
  59. // Swap the lights back when we stop receiving estimated values.
  60. scene.add( defaultLight );
  61. scene.remove( xrLight );
  62. // Revert back to the default environment.
  63. scene.environment = defaultEnvironment;
  64. } );
  65. //
  66. new RGBELoader()
  67. .setPath( 'textures/equirectangular/' )
  68. .load( 'royal_esplanade_1k.hdr', function ( texture ) {
  69. texture.mapping = THREE.EquirectangularReflectionMapping;
  70. defaultEnvironment = texture;
  71. scene.environment = defaultEnvironment;
  72. } );
  73. //
  74. // In order for lighting estimation to work, 'light-estimation' must be included as either an optional or required feature.
  75. document.body.appendChild( ARButton.createButton( renderer, { optionalFeatures: [ 'light-estimation' ] } ) );
  76. //
  77. const ballGeometry = new THREE.SphereGeometry( 0.175, 32, 32 );
  78. const ballGroup = new THREE.Group();
  79. ballGroup.position.z = - 2;
  80. const rows = 3;
  81. const cols = 3;
  82. for ( let i = 0; i < rows; i ++ ) {
  83. for ( let j = 0; j < cols; j ++ ) {
  84. const ballMaterial = new THREE.MeshStandardMaterial( {
  85. color: 0xdddddd,
  86. roughness: i / rows,
  87. metalness: j / cols
  88. } );
  89. const ballMesh = new THREE.Mesh( ballGeometry, ballMaterial );
  90. ballMesh.position.set( ( i + 0.5 - rows * 0.5 ) * 0.4, ( j + 0.5 - cols * 0.5 ) * 0.4, 0 );
  91. ballGroup.add( ballMesh );
  92. }
  93. }
  94. scene.add( ballGroup );
  95. //
  96. function onSelect() {
  97. ballGroup.position.set( 0, 0, - 2 ).applyMatrix4( controller.matrixWorld );
  98. ballGroup.quaternion.setFromRotationMatrix( controller.matrixWorld );
  99. }
  100. controller = renderer.xr.getController( 0 );
  101. controller.addEventListener( 'select', onSelect );
  102. scene.add( controller );
  103. //
  104. window.addEventListener( 'resize', onWindowResize );
  105. }
  106. function onWindowResize() {
  107. camera.aspect = window.innerWidth / window.innerHeight;
  108. camera.updateProjectionMatrix();
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. }
  111. //
  112. function animate() {
  113. renderer.render( scene, camera );
  114. }
  115. </script>
  116. </body>
  117. </html>