AnalyticLightNode.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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, vec4 } 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 overrideMaterial = 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 ( overrideMaterial === null ) {
  36. overrideMaterial = builder.createNodeMaterial();
  37. overrideMaterial.fragmentNode = vec4( 1 );
  38. }
  39. const shadow = this.light.shadow;
  40. const rtt = builder.getRenderTarget( shadow.mapSize.width, shadow.mapSize.height );
  41. const depthTexture = new DepthTexture();
  42. depthTexture.minFilter = NearestFilter;
  43. depthTexture.magFilter = NearestFilter;
  44. depthTexture.image.width = shadow.mapSize.width;
  45. depthTexture.image.height = shadow.mapSize.height;
  46. depthTexture.compareFunction = LessCompare;
  47. rtt.depthTexture = depthTexture;
  48. shadow.camera.updateProjectionMatrix();
  49. //
  50. const bias = reference( 'bias', 'float', shadow );
  51. const normalBias = reference( 'normalBias', 'float', shadow );
  52. let shadowCoord = uniform( shadow.matrix ).mul( positionWorld.add( normalWorld.mul( normalBias ) ) );
  53. shadowCoord = shadowCoord.xyz.div( shadowCoord.w );
  54. const frustumTest = shadowCoord.x.greaterThanEqual( 0 )
  55. .and( shadowCoord.x.lessThanEqual( 1 ) )
  56. .and( shadowCoord.y.greaterThanEqual( 0 ) )
  57. .and( shadowCoord.y.lessThanEqual( 1 ) )
  58. .and( shadowCoord.z.lessThanEqual( 1 ) );
  59. let coordZ = shadowCoord.z.add( bias );
  60. if ( builder.renderer.coordinateSystem === WebGPUCoordinateSystem ) {
  61. coordZ = coordZ.mul( 2 ).sub( 1 ); // WebGPU: Convertion [ 0, 1 ] to [ - 1, 1 ]
  62. }
  63. shadowCoord = vec3(
  64. shadowCoord.x,
  65. shadowCoord.y.oneMinus(), // follow webgpu standards
  66. coordZ
  67. );
  68. const textureCompare = ( depthTexture, shadowCoord, compare ) => texture( depthTexture, shadowCoord ).compare( compare );
  69. //const textureCompare = ( depthTexture, shadowCoord, compare ) => compare.step( texture( depthTexture, shadowCoord ) );
  70. // BasicShadowMap
  71. shadowNode = textureCompare( depthTexture, shadowCoord.xy, shadowCoord.z );
  72. // PCFShadowMap
  73. /*
  74. const mapSize = reference( 'mapSize', 'vec2', shadow );
  75. const radius = reference( 'radius', 'float', shadow );
  76. const texelSize = vec2( 1 ).div( mapSize );
  77. const dx0 = texelSize.x.negate().mul( radius );
  78. const dy0 = texelSize.y.negate().mul( radius );
  79. const dx1 = texelSize.x.mul( radius );
  80. const dy1 = texelSize.y.mul( radius );
  81. const dx2 = dx0.mul( 2 );
  82. const dy2 = dy0.mul( 2 );
  83. const dx3 = dx1.mul( 2 );
  84. const dy3 = dy1.mul( 2 );
  85. shadowNode = add(
  86. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx0, dy0 ) ), shadowCoord.z ),
  87. textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy0 ) ), shadowCoord.z ),
  88. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx1, dy0 ) ), shadowCoord.z ),
  89. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx2, dy2 ) ), shadowCoord.z ),
  90. textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy2 ) ), shadowCoord.z ),
  91. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx3, dy2 ) ), shadowCoord.z ),
  92. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx0, 0 ) ), shadowCoord.z ),
  93. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx2, 0 ) ), shadowCoord.z ),
  94. textureCompare( depthTexture, shadowCoord.xy, shadowCoord.z ),
  95. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx3, 0 ) ), shadowCoord.z ),
  96. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx1, 0 ) ), shadowCoord.z ),
  97. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx2, dy3 ) ), shadowCoord.z ),
  98. textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy3 ) ), shadowCoord.z ),
  99. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx3, dy3 ) ), shadowCoord.z ),
  100. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx0, dy1 ) ), shadowCoord.z ),
  101. textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy1 ) ), shadowCoord.z ),
  102. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx1, dy1 ) ), shadowCoord.z )
  103. ).mul( 1 / 17 );
  104. */
  105. //
  106. this.rtt = rtt;
  107. this.colorNode = this.colorNode.mul( frustumTest.mix( 1, shadowNode ) );
  108. this.shadowNode = shadowNode;
  109. //
  110. this.updateBeforeType = NodeUpdateType.RENDER;
  111. }
  112. }
  113. setup( builder ) {
  114. if ( this.light.castShadow ) this.setupShadow( builder );
  115. else if ( this.shadowNode !== null ) this.disposeShadow();
  116. }
  117. updateShadow( frame ) {
  118. const { rtt, light } = this;
  119. const { renderer, scene } = frame;
  120. const currentOverrideMaterial = scene.overrideMaterial;
  121. scene.overrideMaterial = overrideMaterial;
  122. rtt.setSize( light.shadow.mapSize.width, light.shadow.mapSize.height );
  123. light.shadow.updateMatrices( light );
  124. const currentRenderTarget = renderer.getRenderTarget();
  125. const currentRenderObjectFunction = renderer.getRenderObjectFunction();
  126. renderer.setRenderObjectFunction( ( object, ...params ) => {
  127. if ( object.castShadow === true ) {
  128. renderer.renderObject( object, ...params );
  129. }
  130. } );
  131. renderer.setRenderTarget( rtt );
  132. renderer.render( scene, light.shadow.camera );
  133. renderer.setRenderTarget( currentRenderTarget );
  134. renderer.setRenderObjectFunction( currentRenderObjectFunction );
  135. scene.overrideMaterial = currentOverrideMaterial;
  136. }
  137. disposeShadow() {
  138. this.rtt.dispose();
  139. this.shadowNode = null;
  140. this.rtt = null;
  141. this.colorNode = this._defaultColorNode;
  142. }
  143. updateBefore( frame ) {
  144. const { light } = this;
  145. if ( light.castShadow ) this.updateShadow( frame );
  146. }
  147. update( /*frame*/ ) {
  148. const { light } = this;
  149. this.color.copy( light.color ).multiplyScalar( light.intensity );
  150. }
  151. }
  152. export default AnalyticLightNode;
  153. addNodeClass( 'AnalyticLightNode', AnalyticLightNode );