webxr_ar_lighting.html 4.7 KB

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