ShadowMapViewer.js 6.1 KB

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