XREstimatedLight.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 object 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. /**
  90. * This class can be used to represent the environmental light of
  91. * a XR session. It relies on the WebXR Lighting Estimation API.
  92. *
  93. * @augments Group
  94. * @three_import import { XREstimatedLight } from 'three/addons/webxr/XREstimatedLight.js';
  95. */
  96. export class XREstimatedLight extends Group {
  97. /**
  98. * Constructs a new light.
  99. *
  100. * @param {WebGLRenderer} renderer - The renderer.
  101. * @param {boolean} [environmentEstimation=true] - Whether to use environment estimation or not.
  102. */
  103. constructor( renderer, environmentEstimation = true ) {
  104. super();
  105. /**
  106. * The light probe that represents the estimated light.
  107. *
  108. * @type {LightProbe}
  109. */
  110. this.lightProbe = new LightProbe();
  111. this.lightProbe.intensity = 0;
  112. this.add( this.lightProbe );
  113. /**
  114. * Represents the primary light from the XR environment.
  115. *
  116. * @type {DirectionalLight}
  117. */
  118. this.directionalLight = new DirectionalLight();
  119. this.directionalLight.intensity = 0;
  120. this.add( this.directionalLight );
  121. /**
  122. * Will be set to a cube map in the SessionLightProbe if environment estimation is
  123. * available and requested.
  124. *
  125. * @type {?Texture}
  126. * @default null
  127. */
  128. this.environment = null;
  129. let sessionLightProbe = null;
  130. let estimationStarted = false;
  131. renderer.xr.addEventListener( 'sessionstart', () => {
  132. const session = renderer.xr.getSession();
  133. if ( 'requestLightProbe' in session ) {
  134. session.requestLightProbe( {
  135. reflectionFormat: session.preferredReflectionFormat
  136. } ).then( ( probe ) => {
  137. sessionLightProbe = new SessionLightProbe( this, renderer, probe, environmentEstimation, () => {
  138. estimationStarted = true;
  139. // Fired to indicate that the estimated lighting values are now being updated.
  140. this.dispatchEvent( { type: 'estimationstart' } );
  141. } );
  142. } );
  143. }
  144. } );
  145. renderer.xr.addEventListener( 'sessionend', () => {
  146. if ( sessionLightProbe ) {
  147. sessionLightProbe.dispose();
  148. sessionLightProbe = null;
  149. }
  150. if ( estimationStarted ) {
  151. // Fired to indicate that the estimated lighting values are no longer being updated.
  152. this.dispatchEvent( { type: 'estimationend' } );
  153. }
  154. } );
  155. /**
  156. * Frees the GPU-related resources allocated by this instance. Call this
  157. * method whenever this instance is no longer used in your app.
  158. */
  159. this.dispose = () => {
  160. if ( sessionLightProbe ) {
  161. sessionLightProbe.dispose();
  162. sessionLightProbe = null;
  163. }
  164. this.remove( this.lightProbe );
  165. this.lightProbe = null;
  166. this.remove( this.directionalLight );
  167. this.directionalLight = null;
  168. this.environment = null;
  169. };
  170. }
  171. }