ShadowMapViewer.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import {
  2. DoubleSide,
  3. CanvasTexture,
  4. Mesh,
  5. MeshBasicMaterial,
  6. OrthographicCamera,
  7. PlaneGeometry,
  8. Scene,
  9. ShaderMaterial,
  10. UniformsUtils
  11. } from 'three';
  12. import { UnpackDepthRGBAShader } from '../shaders/UnpackDepthRGBAShader.js';
  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 WebGLRenderer}. When using {@link WebGPURenderer},
  19. * import the class from `ShadowMapViewerGPU.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/ShadowMapViewer.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 userAutoClearSetting;
  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 shader = UnpackDepthRGBAShader;
  55. const uniforms = UniformsUtils.clone( shader.uniforms );
  56. const material = new ShaderMaterial( {
  57. uniforms: uniforms,
  58. vertexShader: shader.vertexShader,
  59. fragmentShader: shader.fragmentShader
  60. } );
  61. const plane = new PlaneGeometry( frame.width, frame.height );
  62. const mesh = new Mesh( plane, material );
  63. scene.add( mesh );
  64. //Label for light's name
  65. let labelCanvas, labelMesh;
  66. if ( doRenderLabel ) {
  67. labelCanvas = document.createElement( 'canvas' );
  68. const context = labelCanvas.getContext( '2d' );
  69. context.font = 'Bold 20px Arial';
  70. const labelWidth = context.measureText( light.name ).width;
  71. labelCanvas.width = labelWidth;
  72. labelCanvas.height = 25; //25 to account for g, p, etc.
  73. context.font = 'Bold 20px Arial';
  74. context.fillStyle = 'rgba( 255, 0, 0, 1 )';
  75. context.fillText( light.name, 0, 20 );
  76. const labelTexture = new CanvasTexture( labelCanvas );
  77. const labelMaterial = new MeshBasicMaterial( { map: labelTexture, side: DoubleSide, transparent: true } );
  78. const labelPlane = new PlaneGeometry( labelCanvas.width, labelCanvas.height );
  79. labelMesh = new Mesh( labelPlane, labelMaterial );
  80. scene.add( labelMesh );
  81. }
  82. function resetPosition() {
  83. scope.position.set( scope.position.x, scope.position.y );
  84. }
  85. /**
  86. * Whether to display the shadow map viewer or not.
  87. *
  88. * @type {boolean}
  89. * @default true
  90. */
  91. this.enabled = true;
  92. /**
  93. * The size of the viewer. When changing this property, make sure
  94. * to call {@link ShadowMapViewer#update}.
  95. *
  96. * @type {{width:number,height:number}}
  97. * @default true
  98. */
  99. this.size = {
  100. width: frame.width,
  101. height: frame.height,
  102. set: function ( width, height ) {
  103. this.width = width;
  104. this.height = height;
  105. mesh.scale.set( this.width / frame.width, this.height / frame.height, 1 );
  106. //Reset the position as it is off when we scale stuff
  107. resetPosition();
  108. }
  109. };
  110. /**
  111. * The position of the viewer. When changing this property, make sure
  112. * to call {@link ShadowMapViewer#update}.
  113. *
  114. * @type {{x:number,y:number, set:function(number,number)}}
  115. * @default true
  116. */
  117. this.position = {
  118. x: frame.x,
  119. y: frame.y,
  120. set: function ( x, y ) {
  121. this.x = x;
  122. this.y = y;
  123. const width = scope.size.width;
  124. const height = scope.size.height;
  125. mesh.position.set( - window.innerWidth / 2 + width / 2 + this.x, window.innerHeight / 2 - height / 2 - this.y, 0 );
  126. if ( doRenderLabel ) labelMesh.position.set( mesh.position.x, mesh.position.y - scope.size.height / 2 + labelCanvas.height / 2, 0 );
  127. }
  128. };
  129. /**
  130. * Renders the viewer. This method must be called in the app's animation loop.
  131. *
  132. * @param {WebGLRenderer} renderer - The renderer.
  133. */
  134. this.render = function ( renderer ) {
  135. if ( this.enabled ) {
  136. //Because a light's .shadowMap is only initialised after the first render pass
  137. //we have to make sure the correct map is sent into the shader, otherwise we
  138. //always end up with the scene's first added shadow casting light's shadowMap
  139. //in the shader
  140. //See: https://github.com/mrdoob/three.js/issues/5932
  141. uniforms.tDiffuse.value = light.shadow.map.texture;
  142. userAutoClearSetting = renderer.autoClear;
  143. renderer.autoClear = false; // To allow render overlay
  144. renderer.clearDepth();
  145. renderer.render( scene, camera );
  146. renderer.autoClear = userAutoClearSetting; //Restore user's setting
  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 };