XREstimatedLight.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import {
  2. DirectionalLight,
  3. Group,
  4. LightProbe,
  5. WebGLCubeRenderTarget
  6. } from '../../../build/three.module.js';
  7. class SessionLightProbe {
  8. constructor( xrLight, renderer, lightProbe, environmentEstimation, estimationStartCallback ) {
  9. this.xrLight = xrLight;
  10. this.renderer = renderer;
  11. this.lightProbe = lightProbe;
  12. this.xrWebGLBinding = null;
  13. this.estimationStartCallback = estimationStartCallback;
  14. this.frameCallback = this.onXRFrame.bind( this );
  15. const session = renderer.xr.getSession();
  16. // If the XRWebGLBinding class is available then we can also query an
  17. // estimated reflection cube map.
  18. if ( environmentEstimation && 'XRWebGLBinding' in window ) {
  19. // This is the simplest way I know of to initialize a WebGL cubemap in Three.
  20. const cubeRenderTarget = new WebGLCubeRenderTarget( 16 );
  21. xrLight.environment = cubeRenderTarget.texture;
  22. const gl = renderer.getContext();
  23. // Ensure that we have any extensions needed to use the preferred cube map format.
  24. switch ( session.preferredReflectionFormat ) {
  25. case 'srgba8':
  26. gl.getExtension( 'EXT_sRGB' );
  27. break;
  28. case 'rgba16f':
  29. gl.getExtension( 'OES_texture_half_float' );
  30. break;
  31. }
  32. this.xrWebGLBinding = new XRWebGLBinding( session, gl );
  33. this.lightProbe.addEventListener( 'reflectionchange', () => {
  34. this.updateReflection();
  35. } );
  36. }
  37. // Start monitoring the XR animation frame loop to look for lighting
  38. // estimation changes.
  39. session.requestAnimationFrame( this.frameCallback );
  40. }
  41. updateReflection() {
  42. const textureProperties = this.renderer.properties.get( this.xrLight.environment );
  43. if ( textureProperties ) {
  44. const cubeMap = this.xrWebGLBinding.getReflectionCubeMap( this.lightProbe );
  45. if ( cubeMap ) {
  46. textureProperties.__webglTexture = cubeMap;
  47. }
  48. }
  49. }
  50. onXRFrame( time, xrFrame ) {
  51. // If either this obejct or the XREstimatedLight has been destroyed, stop
  52. // running the frame loop.
  53. if ( ! this.xrLight ) {
  54. return;
  55. }
  56. const session = xrFrame.session;
  57. session.requestAnimationFrame( this.frameCallback );
  58. const lightEstimate = xrFrame.getLightEstimate( this.lightProbe );
  59. if ( lightEstimate ) {
  60. // We can copy the estimate's spherical harmonics array directly into the light probe.
  61. this.xrLight.lightProbe.sh.fromArray( lightEstimate.sphericalHarmonicsCoefficients );
  62. this.xrLight.lightProbe.intensity = 1.0;
  63. // For the directional light we have to normalize the color and set the scalar as the
  64. // intensity, since WebXR can return color values that exceed 1.0.
  65. const intensityScalar = Math.max( 1.0,
  66. Math.max( lightEstimate.primaryLightIntensity.x,
  67. Math.max( lightEstimate.primaryLightIntensity.y,
  68. lightEstimate.primaryLightIntensity.z ) ) );
  69. this.xrLight.directionalLight.color.setRGB(
  70. lightEstimate.primaryLightIntensity.x / intensityScalar,
  71. lightEstimate.primaryLightIntensity.y / intensityScalar,
  72. lightEstimate.primaryLightIntensity.z / intensityScalar );
  73. this.xrLight.directionalLight.intensity = intensityScalar;
  74. this.xrLight.directionalLight.position.copy( lightEstimate.primaryLightDirection );
  75. if ( this.estimationStartCallback ) {
  76. this.estimationStartCallback();
  77. this.estimationStartCallback = null;
  78. }
  79. }
  80. }
  81. dispose() {
  82. this.xrLight = null;
  83. this.renderer = null;
  84. this.lightProbe = null;
  85. this.xrWebGLBinding = null;
  86. }
  87. }
  88. export class XREstimatedLight extends Group {
  89. constructor( renderer, environmentEstimation = true ) {
  90. super();
  91. this.lightProbe = new LightProbe();
  92. this.lightProbe.intensity = 0;
  93. this.add( this.lightProbe );
  94. this.directionalLight = new DirectionalLight();
  95. this.directionalLight.intensity = 0;
  96. this.add( this.directionalLight );
  97. // Will be set to a cube map in the SessionLightProbe is environment estimation is
  98. // available and requested.
  99. this.environment = null;
  100. let sessionLightProbe = null;
  101. let estimationStarted = false;
  102. renderer.xr.addEventListener( 'sessionstart', () => {
  103. const session = renderer.xr.getSession();
  104. if ( 'requestLightProbe' in session ) {
  105. session.requestLightProbe( {
  106. reflectionFormat: session.preferredReflectionFormat
  107. } ).then( ( probe ) => {
  108. sessionLightProbe = new SessionLightProbe( this, renderer, probe, environmentEstimation, () => {
  109. estimationStarted = true;
  110. // Fired to indicate that the estimated lighting values are now being updated.
  111. this.dispatchEvent( { type: 'estimationstart' } );
  112. } );
  113. } );
  114. }
  115. } );
  116. renderer.xr.addEventListener( 'sessionend', () => {
  117. if ( sessionLightProbe ) {
  118. sessionLightProbe.dispose();
  119. sessionLightProbe = null;
  120. }
  121. if ( estimationStarted ) {
  122. // Fired to indicate that the estimated lighting values are no longer being updated.
  123. this.dispatchEvent( { type: 'estimationend' } );
  124. }
  125. } );
  126. // Done inline to provide access to sessionLightProbe.
  127. this.dispose = () => {
  128. if ( sessionLightProbe ) {
  129. sessionLightProbe.dispose();
  130. sessionLightProbe = null;
  131. }
  132. this.remove( this.lightProbe );
  133. this.lightProbe = null;
  134. this.remove( this.directionalLight );
  135. this.directionalLight = null;
  136. this.environment = null;
  137. };
  138. }
  139. }