AnalyticLightNode.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 { 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 { mix } from '../math/MathNode.js';
  12. import { Color, DepthTexture, NearestFilter, LessCompare, NoToneMapping } 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.shadowMaskNode = null;
  22. this.color = new Color();
  23. this._defaultColorNode = uniform( this.color );
  24. this.colorNode = this._defaultColorNode;
  25. this.isAnalyticLightNode = true;
  26. }
  27. getCacheKey() {
  28. return super.getCacheKey() + '-' + ( this.light.id + '-' + ( this.light.castShadow ? '1' : '0' ) );
  29. }
  30. getHash() {
  31. return this.light.uuid;
  32. }
  33. setupShadow( builder ) {
  34. const { object } = builder;
  35. if ( object.receiveShadow === false ) return;
  36. let shadowNode = this.shadowNode;
  37. if ( shadowNode === null ) {
  38. if ( overrideMaterial === null ) {
  39. overrideMaterial = builder.createNodeMaterial();
  40. overrideMaterial.fragmentNode = vec4( 0, 0, 0, 1 );
  41. overrideMaterial.isShadowNodeMaterial = true; // Use to avoid other overrideMaterial override material.fragmentNode unintentionally when using material.shadowNode
  42. }
  43. const shadow = this.light.shadow;
  44. const rtt = builder.createRenderTarget( shadow.mapSize.width, shadow.mapSize.height );
  45. const depthTexture = new DepthTexture();
  46. depthTexture.minFilter = NearestFilter;
  47. depthTexture.magFilter = NearestFilter;
  48. depthTexture.image.width = shadow.mapSize.width;
  49. depthTexture.image.height = shadow.mapSize.height;
  50. depthTexture.compareFunction = LessCompare;
  51. rtt.depthTexture = depthTexture;
  52. shadow.camera.updateProjectionMatrix();
  53. //
  54. const shadowIntensity = reference( 'intensity', 'float', shadow );
  55. const bias = reference( 'bias', 'float', shadow );
  56. const normalBias = reference( 'normalBias', 'float', shadow );
  57. const position = object.material.shadowPositionNode || positionWorld;
  58. let shadowCoord = uniform( shadow.matrix ).mul( position.add( normalWorld.mul( normalBias ) ) );
  59. shadowCoord = shadowCoord.xyz.div( shadowCoord.w );
  60. const frustumTest = shadowCoord.x.greaterThanEqual( 0 )
  61. .and( shadowCoord.x.lessThanEqual( 1 ) )
  62. .and( shadowCoord.y.greaterThanEqual( 0 ) )
  63. .and( shadowCoord.y.lessThanEqual( 1 ) )
  64. .and( shadowCoord.z.lessThanEqual( 1 ) );
  65. let coordZ = shadowCoord.z.add( bias );
  66. if ( builder.renderer.coordinateSystem === WebGPUCoordinateSystem ) {
  67. coordZ = coordZ.mul( 2 ).sub( 1 ); // WebGPU: Convertion [ 0, 1 ] to [ - 1, 1 ]
  68. }
  69. shadowCoord = vec3(
  70. shadowCoord.x,
  71. shadowCoord.y.oneMinus(), // follow webgpu standards
  72. coordZ
  73. );
  74. const textureCompare = ( depthTexture, shadowCoord, compare ) => texture( depthTexture, shadowCoord ).compare( compare );
  75. //const textureCompare = ( depthTexture, shadowCoord, compare ) => compare.step( texture( depthTexture, shadowCoord ) );
  76. // BasicShadowMap
  77. shadowNode = textureCompare( depthTexture, shadowCoord.xy, shadowCoord.z );
  78. // PCFShadowMap
  79. /*
  80. const mapSize = reference( 'mapSize', 'vec2', shadow );
  81. const radius = reference( 'radius', 'float', shadow );
  82. const texelSize = vec2( 1 ).div( mapSize );
  83. const dx0 = texelSize.x.negate().mul( radius );
  84. const dy0 = texelSize.y.negate().mul( radius );
  85. const dx1 = texelSize.x.mul( radius );
  86. const dy1 = texelSize.y.mul( radius );
  87. const dx2 = dx0.mul( 2 );
  88. const dy2 = dy0.mul( 2 );
  89. const dx3 = dx1.mul( 2 );
  90. const dy3 = dy1.mul( 2 );
  91. shadowNode = add(
  92. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx0, dy0 ) ), shadowCoord.z ),
  93. textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy0 ) ), shadowCoord.z ),
  94. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx1, dy0 ) ), shadowCoord.z ),
  95. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx2, dy2 ) ), shadowCoord.z ),
  96. textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy2 ) ), shadowCoord.z ),
  97. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx3, dy2 ) ), shadowCoord.z ),
  98. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx0, 0 ) ), shadowCoord.z ),
  99. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx2, 0 ) ), shadowCoord.z ),
  100. textureCompare( depthTexture, shadowCoord.xy, shadowCoord.z ),
  101. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx3, 0 ) ), shadowCoord.z ),
  102. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx1, 0 ) ), shadowCoord.z ),
  103. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx2, dy3 ) ), shadowCoord.z ),
  104. textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy3 ) ), shadowCoord.z ),
  105. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx3, dy3 ) ), shadowCoord.z ),
  106. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx0, dy1 ) ), shadowCoord.z ),
  107. textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy1 ) ), shadowCoord.z ),
  108. textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx1, dy1 ) ), shadowCoord.z )
  109. ).mul( 1 / 17 );
  110. */
  111. //
  112. const shadowColor = texture( rtt.texture, shadowCoord );
  113. const shadowMaskNode = frustumTest.mix( 1, shadowNode.mix( shadowColor.a.mix( 1, shadowColor ), 1 ) );
  114. this.rtt = rtt;
  115. this.colorNode = this.colorNode.mul( mix( 1, shadowMaskNode, shadowIntensity ) );
  116. this.shadowNode = shadowNode;
  117. this.shadowMaskNode = shadowMaskNode;
  118. //
  119. this.updateBeforeType = NodeUpdateType.RENDER;
  120. }
  121. }
  122. setup( builder ) {
  123. if ( this.light.castShadow ) this.setupShadow( builder );
  124. else if ( this.shadowNode !== null ) this.disposeShadow();
  125. }
  126. updateShadow( frame ) {
  127. const { rtt, light } = this;
  128. const { renderer, scene, camera } = frame;
  129. const currentOverrideMaterial = scene.overrideMaterial;
  130. scene.overrideMaterial = overrideMaterial;
  131. rtt.setSize( light.shadow.mapSize.width, light.shadow.mapSize.height );
  132. light.shadow.updateMatrices( light );
  133. light.shadow.camera.layers.mask = camera.layers.mask;
  134. const currentToneMapping = renderer.toneMapping;
  135. const currentRenderTarget = renderer.getRenderTarget();
  136. const currentRenderObjectFunction = renderer.getRenderObjectFunction();
  137. renderer.setRenderObjectFunction( ( object, ...params ) => {
  138. if ( object.castShadow === true ) {
  139. renderer.renderObject( object, ...params );
  140. }
  141. } );
  142. renderer.setRenderTarget( rtt );
  143. renderer.toneMapping = NoToneMapping;
  144. renderer.render( scene, light.shadow.camera );
  145. renderer.setRenderTarget( currentRenderTarget );
  146. renderer.setRenderObjectFunction( currentRenderObjectFunction );
  147. renderer.toneMapping = currentToneMapping;
  148. scene.overrideMaterial = currentOverrideMaterial;
  149. }
  150. disposeShadow() {
  151. this.rtt.dispose();
  152. this.shadowNode = null;
  153. this.shadowMaskNode = null;
  154. this.rtt = null;
  155. this.colorNode = this._defaultColorNode;
  156. }
  157. updateBefore( frame ) {
  158. const { light } = this;
  159. if ( light.castShadow ) this.updateShadow( frame );
  160. }
  161. update( /*frame*/ ) {
  162. const { light } = this;
  163. this.color.copy( light.color ).multiplyScalar( light.intensity );
  164. }
  165. }
  166. export default AnalyticLightNode;
  167. addNodeClass( 'AnalyticLightNode', AnalyticLightNode );