1
0

ShadowMapViewer.js 5.3 KB

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