ShadowMapViewer.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. * var 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. * var 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. var ShadowMapViewer = function ( light ) {
  29. //- Internals
  30. var scope = this;
  31. var doRenderLabel = light.name !== undefined && light.name !== '';
  32. var userAutoClearSetting; //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(); //HUD for shadow map
  42. var shader = THREE.UnpackDepthRGBAShader;
  43. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  44. var material = new THREE.ShaderMaterial( {
  45. uniforms: uniforms,
  46. vertexShader: shader.vertexShader,
  47. fragmentShader: shader.fragmentShader
  48. } );
  49. var plane = new THREE.PlaneGeometry( frame.width, frame.height );
  50. var mesh = new THREE.Mesh( plane, material );
  51. scene.add( mesh ); //Label for light's name
  52. var labelCanvas, labelMesh;
  53. if ( doRenderLabel ) {
  54. labelCanvas = document.createElement( 'canvas' );
  55. var context = labelCanvas.getContext( '2d' );
  56. context.font = 'Bold 20px Arial';
  57. var labelWidth = context.measureText( light.name ).width;
  58. labelCanvas.width = labelWidth;
  59. labelCanvas.height = 25; //25 to account for g, p, etc.
  60. context.font = 'Bold 20px Arial';
  61. context.fillStyle = 'rgba( 255, 0, 0, 1 )';
  62. context.fillText( light.name, 0, 20 );
  63. var labelTexture = new THREE.Texture( labelCanvas );
  64. labelTexture.magFilter = THREE.LinearFilter;
  65. labelTexture.minFilter = THREE.LinearFilter;
  66. labelTexture.needsUpdate = true;
  67. var labelMaterial = new THREE.MeshBasicMaterial( {
  68. map: labelTexture,
  69. side: THREE.DoubleSide
  70. } );
  71. labelMaterial.transparent = true;
  72. var labelPlane = new THREE.PlaneGeometry( labelCanvas.width, labelCanvas.height );
  73. labelMesh = new THREE.Mesh( labelPlane, labelMaterial );
  74. scene.add( labelMesh );
  75. }
  76. function resetPosition() {
  77. scope.position.set( scope.position.x, scope.position.y );
  78. } //- API
  79. // Set to false to disable displaying this shadow map
  80. this.enabled = true; // Set the size of the displayed shadow map on the HUD
  81. this.size = {
  82. width: frame.width,
  83. height: frame.height,
  84. set: function ( width, height ) {
  85. this.width = width;
  86. this.height = height;
  87. mesh.scale.set( this.width / frame.width, this.height / frame.height, 1 ); //Reset the position as it is off when we scale stuff
  88. resetPosition();
  89. }
  90. }; // Set the position of the displayed shadow map on the HUD
  91. this.position = {
  92. x: frame.x,
  93. y: frame.y,
  94. set: function ( x, y ) {
  95. this.x = x;
  96. this.y = y;
  97. var width = scope.size.width;
  98. var height = scope.size.height;
  99. mesh.position.set( - window.innerWidth / 2 + width / 2 + this.x, window.innerHeight / 2 - height / 2 - this.y, 0 );
  100. if ( doRenderLabel ) labelMesh.position.set( mesh.position.x, mesh.position.y - scope.size.height / 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.shadow.map.texture;
  111. userAutoClearSetting = renderer.autoClear;
  112. renderer.autoClear = false; // To allow render overlay
  113. renderer.clearDepth();
  114. renderer.render( scene, camera );
  115. renderer.autoClear = userAutoClearSetting; //Restore user's setting
  116. }
  117. };
  118. this.updateForWindowResize = function () {
  119. if ( this.enabled ) {
  120. camera.left = window.innerWidth / - 2;
  121. camera.right = window.innerWidth / 2;
  122. camera.top = window.innerHeight / 2;
  123. camera.bottom = window.innerHeight / - 2;
  124. camera.updateProjectionMatrix();
  125. this.update();
  126. }
  127. };
  128. this.update = function () {
  129. this.position.set( this.position.x, this.position.y );
  130. this.size.set( this.size.width, this.size.height );
  131. }; //Force an update to set position/size
  132. this.update();
  133. };
  134. ShadowMapViewer.prototype.constructor = ShadowMapViewer;
  135. THREE.ShadowMapViewer = ShadowMapViewer;
  136. } )();