PointLightNode.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import AnalyticLightNode from './AnalyticLightNode.js';
  2. import { addLightNode } from './LightsNode.js';
  3. import { getDistanceAttenuation } from './LightUtils.js';
  4. import { uniform } from '../core/UniformNode.js';
  5. import { objectViewPosition } from '../accessors/Object3DNode.js';
  6. import { positionView } from '../accessors/PositionNode.js';
  7. import { addNodeClass } from '../core/Node.js';
  8. import { PointLight } from 'three';
  9. class PointLightNode extends AnalyticLightNode {
  10. constructor( light = null ) {
  11. super( light );
  12. this.cutoffDistanceNode = uniform( 0 );
  13. this.decayExponentNode = uniform( 0 );
  14. }
  15. update( frame ) {
  16. const { light } = this;
  17. super.update( frame );
  18. this.cutoffDistanceNode.value = light.distance;
  19. this.decayExponentNode.value = light.decay;
  20. }
  21. setup( builder ) {
  22. const { colorNode, cutoffDistanceNode, decayExponentNode, light } = this;
  23. const lightingModel = builder.context.lightingModel;
  24. const lVector = objectViewPosition( light ).sub( positionView ); // @TODO: Add it into LightNode
  25. const lightDirection = lVector.normalize();
  26. const lightDistance = lVector.length();
  27. const lightAttenuation = getDistanceAttenuation( {
  28. lightDistance,
  29. cutoffDistance: cutoffDistanceNode,
  30. decayExponent: decayExponentNode
  31. } );
  32. const lightColor = colorNode.mul( lightAttenuation );
  33. const reflectedLight = builder.context.reflectedLight;
  34. lightingModel.direct( {
  35. lightDirection,
  36. lightColor,
  37. reflectedLight
  38. }, builder.stack, builder );
  39. }
  40. }
  41. export default PointLightNode;
  42. addNodeClass( 'PointLightNode', PointLightNode );
  43. addLightNode( PointLight, PointLightNode );