webxr_ar_lighting.html 4.6 KB

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