AnalyticLightNode.js 6.2 KB

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