ShadowMapViewerGPU.js 5.6 KB

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