AnalyticLightNode.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import LightingNode from './LightingNode.js';
  2. import { NodeUpdateType } from '../core/constants.js';
  3. import { uniform } from '../core/UniformNode.js';
  4. import { addNodeClass } from '../core/Node.js';
  5. import { /*vec2,*/ vec3 } from '../shadernode/ShaderNode.js';
  6. import { reference } from '../accessors/ReferenceNode.js';
  7. import { texture } from '../accessors/TextureNode.js';
  8. import { positionWorld } from '../accessors/PositionNode.js';
  9. import { normalWorld } from '../accessors/NormalNode.js';
  10. import { WebGPUCoordinateSystem } from 'three';
  11. //import { add } from '../math/OperatorNode.js';
  12. import { Color, DepthTexture, NearestFilter, LessCompare } from 'three';
  13. let depthMaterial = null;
  14. class AnalyticLightNode extends LightingNode {
  15. constructor( light = null ) {
  16. super();
  17. this.updateType = NodeUpdateType.FRAME;
  18. this.light = light;
  19. this.rtt = null;
  20. this.shadowNode = null;
  21. this.color = new Color();
  22. this._defaultColorNode = uniform( this.color );
  23. this.colorNode = this._defaultColorNode;
  24. this.isAnalyticLightNode = true;
  25. }
  26. getCacheKey() {
  27. return super.getCacheKey() + '-' + ( this.light.id + '-' + ( this.light.castShadow ? '1' : '0' ) );
  28. }
  29. getHash() {
  30. return this.light.uuid;
  31. }
  32. setupShadow( builder ) {
  33. let shadowNode = this.shadowNode;
  34. if ( shadowNode === null ) {
  35. if ( depthMaterial === null ) depthMaterial = builder.createNodeMaterial( 'MeshBasicNodeMaterial' );
  36. const shadow = this.light.shadow;
  37. const rtt = builder.getRenderTarget( shadow.mapSize.width, shadow.mapSize.height );
  38. const depthTexture = new DepthTexture();
  39. depthTexture.minFilter = NearestFilter;
  40. depthTexture.magFilter = NearestFilter;
  41. depthTexture.image.width = shadow.mapSize.width;
  42. depthTexture.image.height = shadow.mapSize.height;
  43. depthTexture.compareFunction = LessCompare;
  44. rtt.depthTexture = depthTexture;
  45. shadow.camera.updateProjectionMatrix();
  46. //
  47. const bias = reference( 'bias', 'float', shadow );
  48. const normalBias = reference( 'normalBias', 'float', shadow );
  49. let shadowCoord = uniform( shadow.matrix ).mul( positionWorld.add( normalWorld.mul( normalBias ) ) );
  50. shadowCoord = shadowCoord.xyz.div( shadowCoord.w );
  51. const frustumTest = shadowCoord.x.greaterThanEqual( 0 )
  52. .and( shadowCoord.x.lessThanEqual( 1 ) )
  53. .and( shadowCoord.y.greaterThanEqual( 0 ) )
  54. .and( shadowCoord.y.lessThanEqual( 1 ) )
  55. .and( shadowCoord.z.lessThanEqual( 1 ) );
  56. let coordZ = shadowCoord.z.add( bias );
  57. if ( builder.renderer.coordinateSystem === WebGPUCoordinateSystem ) {
  58. coordZ = coordZ.mul( 2 ).sub( 1 ); // WebGPU: Convertion [ 0, 1 ] to [ - 1, 1 ]
  59. }
  60. shadowCoord = vec3(
  61. shadowCoord.x,
  62. shadowCoord.y.oneMinus(), // follow webgpu standards
  63. coordZ
  64. );
  65. const textureCompare = ( depthTexture, shadowCoord, compare ) => texture( depthTexture, shadowCoord ).compare( compare );
  66. //const textureCompare = ( depthTexture, shadowCoord, compare ) => compare.step( texture( depthTexture, shadowCoord ) );
  67. // BasicShadowMap
  68. shadowNode = textureCompare( depthTexture, shadowCoord.xy, shadowCoord.z );
  69. // PCFShadowMap
  70. /*
  71. const mapSize = reference( 'mapSize', 'vec2', shadow );
  72. const radius = reference( 'radius', 'float', shadow );
  73. const texelSize = vec2( 1 ).div( mapSize );
  74. const dx0 = texelSize.x.negate().mul( radius );
  75. const dy0 = texelSize.y.negate().mul( radius );
  76. const dx1 = texelSize.x.mul( radius );
  77. const dy1 = texelSize.y.mul( radius );
  78. const dx2 = dx0.mul( 2 );
  79. const dy2 = dy0.mul( 2 );
  80. const dx3 = dx1.mul( 2 );
  81. const dy3 = dy1.mul( 2 );
  82. shadowNode = add(
  83. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx0, dy0 ) ), shadowCoord.z ),
  84. textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy0 ) ), shadowCoord.z ),
  85. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx1, dy0 ) ), shadowCoord.z ),
  86. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx2, dy2 ) ), shadowCoord.z ),
  87. textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy2 ) ), shadowCoord.z ),
  88. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx3, dy2 ) ), shadowCoord.z ),
  89. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx0, 0 ) ), shadowCoord.z ),
  90. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx2, 0 ) ), shadowCoord.z ),
  91. textureCompare( depthTexture, shadowCoord.xy, shadowCoord.z ),
  92. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx3, 0 ) ), shadowCoord.z ),
  93. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx1, 0 ) ), shadowCoord.z ),
  94. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx2, dy3 ) ), shadowCoord.z ),
  95. textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy3 ) ), shadowCoord.z ),
  96. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx3, dy3 ) ), shadowCoord.z ),
  97. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx0, dy1 ) ), shadowCoord.z ),
  98. textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy1 ) ), shadowCoord.z ),
  99. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx1, dy1 ) ), shadowCoord.z )
  100. ).mul( 1 / 17 );
  101. */
  102. //
  103. this.rtt = rtt;
  104. this.colorNode = this.colorNode.mul( frustumTest.mix( 1, shadowNode ) );
  105. this.shadowNode = shadowNode;
  106. //
  107. this.updateBeforeType = NodeUpdateType.RENDER;
  108. }
  109. }
  110. setup( builder ) {
  111. if ( this.light.castShadow ) this.setupShadow( builder );
  112. else if ( this.shadowNode !== null ) this.disposeShadow();
  113. }
  114. updateShadow( frame ) {
  115. const { rtt, light } = this;
  116. const { renderer, scene } = frame;
  117. const currentOverrideMaterial = scene.overrideMaterial;
  118. scene.overrideMaterial = depthMaterial;
  119. rtt.setSize( light.shadow.mapSize.width, light.shadow.mapSize.height );
  120. light.shadow.updateMatrices( light );
  121. const currentRenderTarget = renderer.getRenderTarget();
  122. const currentRenderObjectFunction = renderer.getRenderObjectFunction();
  123. renderer.setRenderObjectFunction( ( object, ...params ) => {
  124. if ( object.castShadow === true ) {
  125. renderer.renderObject( object, ...params );
  126. }
  127. } );
  128. renderer.setRenderTarget( rtt );
  129. renderer.render( scene, light.shadow.camera );
  130. renderer.setRenderTarget( currentRenderTarget );
  131. renderer.setRenderObjectFunction( currentRenderObjectFunction );
  132. scene.overrideMaterial = currentOverrideMaterial;
  133. }
  134. disposeShadow() {
  135. this.rtt.dispose();
  136. this.shadowNode = null;
  137. this.rtt = null;
  138. this.colorNode = this._defaultColorNode;
  139. }
  140. updateBefore( frame ) {
  141. const { light } = this;
  142. if ( light.castShadow ) this.updateShadow( frame );
  143. }
  144. update( /*frame*/ ) {
  145. const { light } = this;
  146. this.color.copy( light.color ).multiplyScalar( light.intensity );
  147. }
  148. }
  149. export default AnalyticLightNode;
  150. addNodeClass( 'AnalyticLightNode', AnalyticLightNode );