2
0

ShadowMapViewer.js 5.2 KB

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