AnalyticLightNode.js 5.7 KB

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