ShadowMapViewer.js 5.3 KB

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