ShadowMapViewerGPU.js 5.1 KB

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