AnalyticLightNode.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 } 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 { step } from '../math/MathNode.js';
  10. import { cond } from '../math/CondNode.js';
  11. import { Color, DepthTexture, NearestFilter } 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. constructShadow( 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 = THREE.LessCompare;
  38. rtt.depthTexture = depthTexture;
  39. shadow.camera.updateProjectionMatrix();
  40. //
  41. const bias = reference( 'bias', 'float', shadow );
  42. //const diffuseFactor = normalView.dot( objectViewPosition( this.light ).sub( positionView ).normalize().negate() );
  43. //bias = mix( bias, 0, diffuseFactor );
  44. let shadowCoord = uniform( shadow.matrix ).mul( positionWorld );
  45. shadowCoord = shadowCoord.xyz.div( shadowCoord.w );
  46. shadowCoord = vec3(
  47. shadowCoord.x,
  48. shadowCoord.y.oneMinus(),
  49. shadowCoord.z
  50. );
  51. // @TODO: Optimize using WebGPU compare-sampler
  52. let depth = texture( depthTexture, shadowCoord.xy );
  53. depth = depth.mul( .5 ).add( .5 ).add( bias );
  54. shadowNode = cond( shadowCoord.z.lessThan( depth ).or( shadowCoord.y.lessThan( .000001 ) /*@TODO: find the cause and remove it soon */ ), 1, 0 );
  55. //shadowNode = step( shadowCoord.z, depth );
  56. //
  57. this.rtt = rtt;
  58. this.colorNode = this.colorNode.mul( shadowNode );
  59. this.shadowNode = shadowNode;
  60. //
  61. this.updateBeforeType = NodeUpdateType.RENDER;
  62. }
  63. }
  64. construct( builder ) {
  65. if ( this.light.castShadow ) this.constructShadow( builder );
  66. }
  67. updateShadow( frame ) {
  68. const { rtt, light } = this;
  69. const { renderer, scene } = frame;
  70. scene.overrideMaterial = depthMaterial;
  71. rtt.setSize( light.shadow.mapSize.width, light.shadow.mapSize.height );
  72. light.shadow.updateMatrices( light );
  73. renderer.setRenderTarget( rtt );
  74. renderer.render( scene, light.shadow.camera );
  75. renderer.setRenderTarget( null );
  76. scene.overrideMaterial = null;
  77. }
  78. updateBefore( frame ) {
  79. const { light } = this;
  80. if ( light.castShadow ) this.updateShadow( frame );
  81. }
  82. update( /*frame*/ ) {
  83. const { light } = this;
  84. this.color.copy( light.color ).multiplyScalar( light.intensity );
  85. }
  86. }
  87. export default AnalyticLightNode;
  88. addNodeClass( AnalyticLightNode );