2
0

ShadowMapViewer.js 5.5 KB

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