2
0

ShadowMapViewer.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. * var 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. * var 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. var ShadowMapViewer = function ( light ) {
  41. //- Internals
  42. var scope = this;
  43. var doRenderLabel = ( light.name !== undefined && light.name !== '' );
  44. var userAutoClearSetting;
  45. //Holds the initial position and dimension of the HUD
  46. var frame = {
  47. x: 10,
  48. y: 10,
  49. width: 256,
  50. height: 256
  51. };
  52. var 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. var scene = new Scene();
  55. //HUD for shadow map
  56. var shader = UnpackDepthRGBAShader;
  57. var uniforms = UniformsUtils.clone( shader.uniforms );
  58. var material = new ShaderMaterial( {
  59. uniforms: uniforms,
  60. vertexShader: shader.vertexShader,
  61. fragmentShader: shader.fragmentShader
  62. } );
  63. var plane = new PlaneGeometry( frame.width, frame.height );
  64. var mesh = new Mesh( plane, material );
  65. scene.add( mesh );
  66. //Label for light's name
  67. var labelCanvas, labelMesh;
  68. if ( doRenderLabel ) {
  69. labelCanvas = document.createElement( 'canvas' );
  70. var context = labelCanvas.getContext( '2d' );
  71. context.font = 'Bold 20px Arial';
  72. var 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. var labelTexture = new Texture( labelCanvas );
  79. labelTexture.magFilter = LinearFilter;
  80. labelTexture.minFilter = LinearFilter;
  81. labelTexture.needsUpdate = true;
  82. var labelMaterial = new MeshBasicMaterial( { map: labelTexture, side: DoubleSide } );
  83. labelMaterial.transparent = true;
  84. var labelPlane = new PlaneGeometry( labelCanvas.width, labelCanvas.height );
  85. labelMesh = new Mesh( labelPlane, labelMaterial );
  86. scene.add( labelMesh );
  87. }
  88. function resetPosition() {
  89. scope.position.set( scope.position.x, scope.position.y );
  90. }
  91. //- API
  92. // Set to false to disable displaying this shadow map
  93. this.enabled = true;
  94. // Set the size of the displayed shadow map on the HUD
  95. this.size = {
  96. width: frame.width,
  97. height: frame.height,
  98. set: function ( width, height ) {
  99. this.width = width;
  100. this.height = height;
  101. mesh.scale.set( this.width / frame.width, this.height / frame.height, 1 );
  102. //Reset the position as it is off when we scale stuff
  103. resetPosition();
  104. }
  105. };
  106. // Set the position of the displayed shadow map on the HUD
  107. this.position = {
  108. x: frame.x,
  109. y: frame.y,
  110. set: function ( x, y ) {
  111. this.x = x;
  112. this.y = y;
  113. var width = scope.size.width;
  114. var height = scope.size.height;
  115. mesh.position.set( - window.innerWidth / 2 + width / 2 + this.x, window.innerHeight / 2 - height / 2 - this.y, 0 );
  116. if ( doRenderLabel ) labelMesh.position.set( mesh.position.x, mesh.position.y - scope.size.height / 2 + labelCanvas.height / 2, 0 );
  117. }
  118. };
  119. this.render = function ( renderer ) {
  120. if ( this.enabled ) {
  121. //Because a light's .shadowMap is only initialised after the first render pass
  122. //we have to make sure the correct map is sent into the shader, otherwise we
  123. //always end up with the scene's first added shadow casting light's shadowMap
  124. //in the shader
  125. //See: https://github.com/mrdoob/three.js/issues/5932
  126. uniforms.tDiffuse.value = light.shadow.map.texture;
  127. userAutoClearSetting = renderer.autoClear;
  128. renderer.autoClear = false; // To allow render overlay
  129. renderer.clearDepth();
  130. renderer.render( scene, camera );
  131. renderer.autoClear = userAutoClearSetting; //Restore user's setting
  132. }
  133. };
  134. this.updateForWindowResize = function () {
  135. if ( this.enabled ) {
  136. camera.left = window.innerWidth / - 2;
  137. camera.right = window.innerWidth / 2;
  138. camera.top = window.innerHeight / 2;
  139. camera.bottom = window.innerHeight / - 2;
  140. camera.updateProjectionMatrix();
  141. this.update();
  142. }
  143. };
  144. this.update = function () {
  145. this.position.set( this.position.x, this.position.y );
  146. this.size.set( this.size.width, this.size.height );
  147. };
  148. //Force an update to set position/size
  149. this.update();
  150. };
  151. ShadowMapViewer.prototype.constructor = ShadowMapViewer;
  152. export { ShadowMapViewer };