XREstimatedLight.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import {
  2. DirectionalLight,
  3. Group,
  4. LightProbe,
  5. WebGLCubeRenderTarget
  6. } from 'three';
  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. this.xrLight.environment.needsPMREMUpdate = true;
  48. }
  49. }
  50. }
  51. onXRFrame( time, xrFrame ) {
  52. // If either this obejct or the XREstimatedLight has been destroyed, stop
  53. // running the frame loop.
  54. if ( ! this.xrLight ) {
  55. return;
  56. }
  57. const session = xrFrame.session;
  58. session.requestAnimationFrame( this.frameCallback );
  59. const lightEstimate = xrFrame.getLightEstimate( this.lightProbe );
  60. if ( lightEstimate ) {
  61. // We can copy the estimate's spherical harmonics array directly into the light probe.
  62. this.xrLight.lightProbe.sh.fromArray( lightEstimate.sphericalHarmonicsCoefficients );
  63. this.xrLight.lightProbe.intensity = 1.0;
  64. // For the directional light we have to normalize the color and set the scalar as the
  65. // intensity, since WebXR can return color values that exceed 1.0.
  66. const intensityScalar = Math.max( 1.0,
  67. Math.max( lightEstimate.primaryLightIntensity.x,
  68. Math.max( lightEstimate.primaryLightIntensity.y,
  69. lightEstimate.primaryLightIntensity.z ) ) );
  70. this.xrLight.directionalLight.color.setRGB(
  71. lightEstimate.primaryLightIntensity.x / intensityScalar,
  72. lightEstimate.primaryLightIntensity.y / intensityScalar,
  73. lightEstimate.primaryLightIntensity.z / intensityScalar );
  74. this.xrLight.directionalLight.intensity = intensityScalar;
  75. this.xrLight.directionalLight.position.copy( lightEstimate.primaryLightDirection );
  76. if ( this.estimationStartCallback ) {
  77. this.estimationStartCallback();
  78. this.estimationStartCallback = null;
  79. }
  80. }
  81. }
  82. dispose() {
  83. this.xrLight = null;
  84. this.renderer = null;
  85. this.lightProbe = null;
  86. this.xrWebGLBinding = null;
  87. }
  88. }
  89. export class XREstimatedLight extends Group {
  90. constructor( renderer, environmentEstimation = true ) {
  91. super();
  92. this.lightProbe = new LightProbe();
  93. this.lightProbe.intensity = 0;
  94. this.add( this.lightProbe );
  95. this.directionalLight = new DirectionalLight();
  96. this.directionalLight.intensity = 0;
  97. this.add( this.directionalLight );
  98. // Will be set to a cube map in the SessionLightProbe is environment estimation is
  99. // available and requested.
  100. this.environment = null;
  101. let sessionLightProbe = null;
  102. let estimationStarted = false;
  103. renderer.xr.addEventListener( 'sessionstart', () => {
  104. const session = renderer.xr.getSession();
  105. if ( 'requestLightProbe' in session ) {
  106. session.requestLightProbe( {
  107. reflectionFormat: session.preferredReflectionFormat
  108. } ).then( ( probe ) => {
  109. sessionLightProbe = new SessionLightProbe( this, renderer, probe, environmentEstimation, () => {
  110. estimationStarted = true;
  111. // Fired to indicate that the estimated lighting values are now being updated.
  112. this.dispatchEvent( { type: 'estimationstart' } );
  113. } );
  114. } );
  115. }
  116. } );
  117. renderer.xr.addEventListener( 'sessionend', () => {
  118. if ( sessionLightProbe ) {
  119. sessionLightProbe.dispose();
  120. sessionLightProbe = null;
  121. }
  122. if ( estimationStarted ) {
  123. // Fired to indicate that the estimated lighting values are no longer being updated.
  124. this.dispatchEvent( { type: 'estimationend' } );
  125. }
  126. } );
  127. // Done inline to provide access to sessionLightProbe.
  128. this.dispose = () => {
  129. if ( sessionLightProbe ) {
  130. sessionLightProbe.dispose();
  131. sessionLightProbe = null;
  132. }
  133. this.remove( this.lightProbe );
  134. this.lightProbe = null;
  135. this.remove( this.directionalLight );
  136. this.directionalLight = null;
  137. this.environment = null;
  138. };
  139. }
  140. }