XREstimatedLight.js 5.9 KB

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