ShadowMapViewerGPU.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import {
  2. DoubleSide,
  3. CanvasTexture,
  4. Mesh,
  5. MeshBasicMaterial,
  6. NodeMaterial,
  7. OrthographicCamera,
  8. PlaneGeometry,
  9. Scene,
  10. DepthTexture,
  11. Vector2
  12. } from 'three';
  13. import { uv, uniform, textureLoad } from 'three/tsl';
  14. /**
  15. * This is a helper for visualising a given light's shadow map.
  16. * It works for shadow casting lights: DirectionalLight and SpotLight.
  17. * It renders out the shadow map and displays it on a HUD.
  18. *
  19. * This module can only be used with {@link WebGPURenderer}. When using {@link WebGLRenderer},
  20. * import the class from `ShadowMapViewer.js`.
  21. *
  22. * ```js
  23. * const lightShadowMapViewer = new ShadowMapViewer( light );
  24. * lightShadowMapViewer.position.x = 10;
  25. * lightShadowMapViewer.position.y = SCREEN_HEIGHT - ( SHADOW_MAP_HEIGHT / 4 ) - 10;
  26. * lightShadowMapViewer.size.width = SHADOW_MAP_WIDTH / 4;
  27. * lightShadowMapViewer.size.height = SHADOW_MAP_HEIGHT / 4;
  28. * lightShadowMapViewer.update();
  29. * ```
  30. *
  31. * @three_import import { ShadowMapViewer } from 'three/addons/utils/ShadowMapViewerGPU.js';
  32. */
  33. class ShadowMapViewer {
  34. /**
  35. * Constructs a new shadow map viewer.
  36. *
  37. * @param {Light} light - The shadow casting light.
  38. */
  39. constructor( light ) {
  40. //- Internals
  41. const scope = this;
  42. const doRenderLabel = ( light.name !== undefined && light.name !== '' );
  43. let currentAutoClear;
  44. //Holds the initial position and dimension of the HUD
  45. const frame = {
  46. x: 10,
  47. y: 10,
  48. width: 256,
  49. height: 256
  50. };
  51. const camera = new OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, 1, 10 );
  52. camera.position.set( 0, 0, 2 );
  53. const scene = new Scene();
  54. //HUD for shadow map
  55. const material = new NodeMaterial();
  56. const textureDimension = uniform( new Vector2() );
  57. const shadowMapUniform = textureLoad( new DepthTexture(), uv().flipY().mul( textureDimension ) );
  58. material.fragmentNode = shadowMapUniform.x.oneMinus();
  59. const plane = new PlaneGeometry( frame.width, frame.height );
  60. const mesh = new Mesh( plane, material );
  61. scene.add( mesh );
  62. //Label for light's name
  63. let labelCanvas, labelMesh;
  64. if ( doRenderLabel ) {
  65. labelCanvas = document.createElement( 'canvas' );
  66. const context = labelCanvas.getContext( '2d' );
  67. context.font = 'Bold 20px Arial';
  68. const labelWidth = context.measureText( light.name ).width;
  69. labelCanvas.width = labelWidth;
  70. labelCanvas.height = 25; //25 to account for g, p, etc.
  71. context.font = 'Bold 20px Arial';
  72. context.fillStyle = 'rgba( 255, 0, 0, 1 )';
  73. context.fillText( light.name, 0, 20 );
  74. const labelTexture = new CanvasTexture( labelCanvas );
  75. const labelMaterial = new MeshBasicMaterial( { map: labelTexture, side: DoubleSide, transparent: true } );
  76. const labelPlane = new PlaneGeometry( labelCanvas.width, labelCanvas.height );
  77. labelMesh = new Mesh( labelPlane, labelMaterial );
  78. scene.add( labelMesh );
  79. }
  80. function resetPosition() {
  81. scope.position.set( scope.position.x, scope.position.y );
  82. }
  83. /**
  84. * Whether to display the shadow map viewer or not.
  85. *
  86. * @type {boolean}
  87. * @default true
  88. */
  89. this.enabled = true;
  90. /**
  91. * The size of the viewer. When changing this property, make sure
  92. * to call {@link ShadowMapViewer#update}.
  93. *
  94. * @type {{width:number,height:number}}
  95. * @default true
  96. */
  97. this.size = {
  98. width: frame.width,
  99. height: frame.height,
  100. set: function ( width, height ) {
  101. this.width = width;
  102. this.height = height;
  103. mesh.scale.set( this.width / frame.width, this.height / frame.height, 1 );
  104. //Reset the position as it is off when we scale stuff
  105. resetPosition();
  106. }
  107. };
  108. /**
  109. * The position of the viewer. When changing this property, make sure
  110. * to call {@link ShadowMapViewer#update}.
  111. *
  112. * @type {{width:number,height:number}}
  113. * @default true
  114. */
  115. this.position = {
  116. x: frame.x,
  117. y: frame.y,
  118. set: function ( x, y ) {
  119. this.x = x;
  120. this.y = y;
  121. const width = scope.size.width;
  122. const height = scope.size.height;
  123. mesh.position.set( - window.innerWidth / 2 + width / 2 + this.x, window.innerHeight / 2 - height / 2 - this.y, 0 );
  124. if ( doRenderLabel ) labelMesh.position.set( mesh.position.x, mesh.position.y - scope.size.height / 2 + labelCanvas.height / 2, 0 );
  125. }
  126. };
  127. /**
  128. * Renders the viewer. This method must be called in the app's animation loop.
  129. *
  130. * @param {WebGPURenderer} renderer - The renderer.
  131. */
  132. this.render = function ( renderer ) {
  133. if ( this.enabled ) {
  134. //Because a light's .shadowMap is only initialised after the first render pass
  135. //we have to make sure the correct map is sent into the shader, otherwise we
  136. //always end up with the scene's first added shadow casting light's shadowMap
  137. //in the shader
  138. //See: https://github.com/mrdoob/three.js/issues/5932
  139. const depthTexture = light.shadow.map.depthTexture;
  140. shadowMapUniform.value = depthTexture;
  141. textureDimension.value.set( depthTexture.width, depthTexture.height );
  142. currentAutoClear = renderer.autoClear;
  143. renderer.autoClear = false; // To allow render overlay
  144. renderer.clearDepth();
  145. renderer.render( scene, camera );
  146. renderer.autoClear = currentAutoClear;
  147. }
  148. };
  149. /**
  150. * Resizes the viewer. This method should be called whenever the app's
  151. * window is resized.
  152. */
  153. this.updateForWindowResize = function () {
  154. if ( this.enabled ) {
  155. camera.left = window.innerWidth / - 2;
  156. camera.right = window.innerWidth / 2;
  157. camera.top = window.innerHeight / 2;
  158. camera.bottom = window.innerHeight / - 2;
  159. camera.updateProjectionMatrix();
  160. this.update();
  161. }
  162. };
  163. /**
  164. * Updates the viewer.
  165. */
  166. this.update = function () {
  167. this.position.set( this.position.x, this.position.y );
  168. this.size.set( this.size.width, this.size.height );
  169. };
  170. //Force an update to set position/size
  171. this.update();
  172. }
  173. }
  174. export { ShadowMapViewer };
粤ICP备19079148号