ShadowMapViewer.js 5.6 KB

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