PointLightNode.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. shadowMask: this.shadowMaskNode
  39. }, builder.stack, builder );
  40. }
  41. }
  42. export default PointLightNode;
  43. addNodeClass( 'PointLightNode', PointLightNode );
  44. addLightNode( PointLight, PointLightNode );