ShadowMapViewer.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import {
  2. DoubleSide,
  3. LinearFilter,
  4. Mesh,
  5. MeshBasicMaterial,
  6. OrthographicCamera,
  7. PlaneGeometry,
  8. Scene,
  9. ShaderMaterial,
  10. Texture,
  11. UniformsUtils
  12. } from '../../../build/three.module.js';
  13. import { UnpackDepthRGBAShader } from '../shaders/UnpackDepthRGBAShader.js';
  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. * Example usage:
  20. * 1) Import ShadowMapViewer into your app.
  21. *
  22. * 2) Create a shadow casting light and name it optionally:
  23. * let light = new DirectionalLight( 0xffffff, 1 );
  24. * light.castShadow = true;
  25. * light.name = 'Sun';
  26. *
  27. * 3) Create a shadow map viewer for that light and set its size and position optionally:
  28. * let shadowMapViewer = new ShadowMapViewer( light );
  29. * shadowMapViewer.size.set( 128, 128 ); //width, height default: 256, 256
  30. * shadowMapViewer.position.set( 10, 10 ); //x, y in pixel default: 0, 0 (top left corner)
  31. *
  32. * 4) Render the shadow map viewer in your render loop:
  33. * shadowMapViewer.render( renderer );
  34. *
  35. * 5) Optionally: Update the shadow map viewer on window resize:
  36. * shadowMapViewer.updateForWindowResize();
  37. *
  38. * 6) If you set the position or size members directly, you need to call shadowMapViewer.update();
  39. */
  40. class ShadowMapViewer {
  41. constructor( light ) {
  42. //- Internals
  43. const scope = this;
  44. const doRenderLabel = ( light.name !== undefined && light.name !== '' );
  45. let userAutoClearSetting;
  46. //Holds the initial position and dimension of the HUD
  47. const frame = {
  48. x: 10,
  49. y: 10,
  50. width: 256,
  51. height: 256
  52. };
  53. const camera = new OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, 1, 10 );
  54. camera.position.set( 0, 0, 2 );
  55. const scene = new Scene();
  56. //HUD for shadow map
  57. const shader = UnpackDepthRGBAShader;
  58. const uniforms = UniformsUtils.clone( shader.uniforms );
  59. const material = new ShaderMaterial( {
  60. uniforms: uniforms,
  61. vertexShader: shader.vertexShader,
  62. fragmentShader: shader.fragmentShader
  63. } );
  64. const plane = new PlaneGeometry( frame.width, frame.height );
  65. const mesh = new Mesh( plane, material );
  66. scene.add( mesh );
  67. //Label for light's name
  68. let labelCanvas, labelMesh;
  69. if ( doRenderLabel ) {
  70. labelCanvas = document.createElement( 'canvas' );
  71. const context = labelCanvas.getContext( '2d' );
  72. context.font = 'Bold 20px Arial';
  73. const labelWidth = context.measureText( light.name ).width;
  74. labelCanvas.width = labelWidth;
  75. labelCanvas.height = 25; //25 to account for g, p, etc.
  76. context.font = 'Bold 20px Arial';
  77. context.fillStyle = 'rgba( 255, 0, 0, 1 )';
  78. context.fillText( light.name, 0, 20 );
  79. const labelTexture = new Texture( labelCanvas );
  80. labelTexture.magFilter = LinearFilter;
  81. labelTexture.minFilter = LinearFilter;
  82. labelTexture.needsUpdate = true;
  83. const labelMaterial = new MeshBasicMaterial( { map: labelTexture, side: DoubleSide } );
  84. labelMaterial.transparent = true;
  85. const labelPlane = new PlaneGeometry( labelCanvas.width, labelCanvas.height );
  86. labelMesh = new Mesh( labelPlane, labelMaterial );
  87. scene.add( labelMesh );
  88. }
  89. function resetPosition() {
  90. scope.position.set( scope.position.x, scope.position.y );
  91. }
  92. //- API
  93. // Set to false to disable displaying this shadow map
  94. this.enabled = true;
  95. // Set the size of the displayed shadow map on the HUD
  96. this.size = {
  97. width: frame.width,
  98. height: frame.height,
  99. set: function ( width, height ) {
  100. this.width = width;
  101. this.height = height;
  102. mesh.scale.set( this.width / frame.width, this.height / frame.height, 1 );
  103. //Reset the position as it is off when we scale stuff
  104. resetPosition();
  105. }
  106. };
  107. // Set the position of the displayed shadow map on the HUD
  108. this.position = {
  109. x: frame.x,
  110. y: frame.y,
  111. set: function ( x, y ) {
  112. this.x = x;
  113. this.y = y;
  114. const width = scope.size.width;
  115. const height = scope.size.height;
  116. mesh.position.set( - window.innerWidth / 2 + width / 2 + this.x, window.innerHeight / 2 - height / 2 - this.y, 0 );
  117. if ( doRenderLabel ) labelMesh.position.set( mesh.position.x, mesh.position.y - scope.size.height / 2 + labelCanvas.height / 2, 0 );
  118. }
  119. };
  120. this.render = function ( renderer ) {
  121. if ( this.enabled ) {
  122. //Because a light's .shadowMap is only initialised after the first render pass
  123. //we have to make sure the correct map is sent into the shader, otherwise we
  124. //always end up with the scene's first added shadow casting light's shadowMap
  125. //in the shader
  126. //See: https://github.com/mrdoob/three.js/issues/5932
  127. uniforms.tDiffuse.value = light.shadow.map.texture;
  128. userAutoClearSetting = renderer.autoClear;
  129. renderer.autoClear = false; // To allow render overlay
  130. renderer.clearDepth();
  131. renderer.render( scene, camera );
  132. renderer.autoClear = userAutoClearSetting; //Restore user's setting
  133. }
  134. };
  135. this.updateForWindowResize = function () {
  136. if ( this.enabled ) {
  137. camera.left = window.innerWidth / - 2;
  138. camera.right = window.innerWidth / 2;
  139. camera.top = window.innerHeight / 2;
  140. camera.bottom = window.innerHeight / - 2;
  141. camera.updateProjectionMatrix();
  142. this.update();
  143. }
  144. };
  145. this.update = function () {
  146. this.position.set( this.position.x, this.position.y );
  147. this.size.set( this.size.width, this.size.height );
  148. };
  149. //Force an update to set position/size
  150. this.update();
  151. }
  152. }
  153. export { ShadowMapViewer };