ShadowMapViewer.js 5.4 KB

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