AnalyticLightNode.js 3.2 KB

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