ShadowMapViewer.js 4.7 KB

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