PunctualLightNode.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import AnalyticLightNode from './AnalyticLightNode.js';
  2. import LightsNode from './LightsNode.js';
  3. import Object3DNode from '../accessors/Object3DNode.js';
  4. import getDistanceAttenuation from '../functions/light/getDistanceAttenuation.js';
  5. import { uniform, mul, normalize, length, sub, positionView } from '../shadernode/ShaderNodeElements.js';
  6. import { PointLight } from 'three';
  7. class PunctualLightNode extends AnalyticLightNode {
  8. constructor( light = null ) {
  9. super( light );
  10. this.cutoffDistanceNode = uniform( 0 );
  11. this.decayExponentNode = uniform( 0 );
  12. }
  13. update( frame ) {
  14. const { light } = this;
  15. super.update( frame );
  16. this.cutoffDistanceNode.value = light.distance;
  17. this.decayExponentNode.value = light.decay;
  18. }
  19. construct( builder ) {
  20. const { colorNode, cutoffDistanceNode, decayExponentNode } = this;
  21. const lightPositionViewNode = new Object3DNode( Object3DNode.VIEW_POSITION, this.light );
  22. const lVector = sub( lightPositionViewNode, positionView );
  23. const lightDirection = normalize( lVector );
  24. const lightDistance = length( lVector );
  25. const lightAttenuation = getDistanceAttenuation.call( {
  26. lightDistance,
  27. cutoffDistance: cutoffDistanceNode,
  28. decayExponent: decayExponentNode
  29. } );
  30. const lightColor = mul( colorNode, lightAttenuation );
  31. const lightingModelFunctionNode = builder.context.lightingModelNode;
  32. const reflectedLight = builder.context.reflectedLight;
  33. if ( lightingModelFunctionNode?.direct ) {
  34. lightingModelFunctionNode.direct.call( {
  35. lightDirection,
  36. lightColor,
  37. reflectedLight
  38. }, builder );
  39. }
  40. }
  41. }
  42. LightsNode.setReference( PointLight, PunctualLightNode );
  43. export default PunctualLightNode;