ShadowMapViewerGPU.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. 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 currentAutoClear;
  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 NodeMaterial();
  53. const shadowMapUniform = texture( new Texture() );
  54. material.fragmentNode = shadowMapUniform;
  55. const plane = new PlaneGeometry( frame.width, frame.height );
  56. const mesh = new Mesh( plane, material );
  57. scene.add( mesh );
  58. //Label for light's name
  59. let labelCanvas, labelMesh;
  60. if ( doRenderLabel ) {
  61. labelCanvas = document.createElement( 'canvas' );
  62. const context = labelCanvas.getContext( '2d' );
  63. context.font = 'Bold 20px Arial';
  64. const labelWidth = context.measureText( light.name ).width;
  65. labelCanvas.width = labelWidth;
  66. labelCanvas.height = 25; //25 to account for g, p, etc.
  67. context.font = 'Bold 20px Arial';
  68. context.fillStyle = 'rgba( 255, 0, 0, 1 )';
  69. context.fillText( light.name, 0, 20 );
  70. const labelTexture = new CanvasTexture( labelCanvas );
  71. const labelMaterial = new MeshBasicMaterial( { map: labelTexture, side: DoubleSide, transparent: true } );
  72. const labelPlane = new PlaneGeometry( labelCanvas.width, labelCanvas.height );
  73. labelMesh = new Mesh( labelPlane, labelMaterial );
  74. scene.add( labelMesh );
  75. }
  76. function resetPosition() {
  77. scope.position.set( scope.position.x, scope.position.y );
  78. }
  79. /**
  80. * Whether to display the shadow map viewer or not.
  81. *
  82. * @type {boolean}
  83. * @default true
  84. */
  85. this.enabled = true;
  86. /**
  87. * The size of the viewer. When changing this property, make sure
  88. * to call {@link ShadowMapViewer#update}.
  89. *
  90. * @type {{width:number,height:number}}
  91. * @default true
  92. */
  93. this.size = {
  94. width: frame.width,
  95. height: frame.height,
  96. set: function ( width, height ) {
  97. this.width = width;
  98. this.height = height;
  99. mesh.scale.set( this.width / frame.width, this.height / frame.height, 1 );
  100. //Reset the position as it is off when we scale stuff
  101. resetPosition();
  102. }
  103. };
  104. /**
  105. * The position of the viewer. When changing this property, make sure
  106. * to call {@link ShadowMapViewer#update}.
  107. *
  108. * @type {{width:number,height:number}}
  109. * @default true
  110. */
  111. this.position = {
  112. x: frame.x,
  113. y: frame.y,
  114. set: function ( x, y ) {
  115. this.x = x;
  116. this.y = y;
  117. const width = scope.size.width;
  118. const height = scope.size.height;
  119. mesh.position.set( - window.innerWidth / 2 + width / 2 + this.x, window.innerHeight / 2 - height / 2 - this.y, 0 );
  120. if ( doRenderLabel ) labelMesh.position.set( mesh.position.x, mesh.position.y - scope.size.height / 2 + labelCanvas.height / 2, 0 );
  121. }
  122. };
  123. /**
  124. * Renders the viewer. This method must be called in the app's animation loop.
  125. *
  126. * @param {WebGPURenderer} renderer - The renderer.
  127. */
  128. this.render = function ( renderer ) {
  129. if ( this.enabled ) {
  130. //Because a light's .shadowMap is only initialised after the first render pass
  131. //we have to make sure the correct map is sent into the shader, otherwise we
  132. //always end up with the scene's first added shadow casting light's shadowMap
  133. //in the shader
  134. //See: https://github.com/mrdoob/three.js/issues/5932
  135. shadowMapUniform.value = light.shadow.map.texture;
  136. currentAutoClear = renderer.autoClear;
  137. renderer.autoClear = false; // To allow render overlay
  138. renderer.clearDepth();
  139. renderer.render( scene, camera );
  140. renderer.autoClear = currentAutoClear;
  141. }
  142. };
  143. /**
  144. * Resizes the viewer. This method should be called whenever the app's
  145. * window is resized.
  146. */
  147. this.updateForWindowResize = function () {
  148. if ( this.enabled ) {
  149. camera.left = window.innerWidth / - 2;
  150. camera.right = window.innerWidth / 2;
  151. camera.top = window.innerHeight / 2;
  152. camera.bottom = window.innerHeight / - 2;
  153. camera.updateProjectionMatrix();
  154. this.update();
  155. }
  156. };
  157. /**
  158. * Updates the viewer.
  159. */
  160. this.update = function () {
  161. this.position.set( this.position.x, this.position.y );
  162. this.size.set( this.size.width, this.size.height );
  163. };
  164. //Force an update to set position/size
  165. this.update();
  166. }
  167. }
  168. export { ShadowMapViewer };
粤ICP备19079148号