PointLightNode.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. construct( builder ) {
  22. const { colorNode, cutoffDistanceNode, decayExponentNode, light } = this;
  23. const lVector = objectViewPosition( light ).sub( positionView ); // @TODO: Add it into LightNode
  24. const lightDirection = lVector.normalize();
  25. const lightDistance = lVector.length();
  26. const lightAttenuation = getDistanceAttenuation.call( {
  27. lightDistance,
  28. cutoffDistance: cutoffDistanceNode,
  29. decayExponent: decayExponentNode
  30. } );
  31. const lightColor = colorNode.mul( lightAttenuation );
  32. const lightingModelFunctionNode = builder.context.lightingModelNode;
  33. const reflectedLight = builder.context.reflectedLight;
  34. if ( lightingModelFunctionNode && lightingModelFunctionNode.direct ) {
  35. lightingModelFunctionNode.direct.call( {
  36. lightDirection,
  37. lightColor,
  38. reflectedLight
  39. }, builder );
  40. }
  41. }
  42. }
  43. export default PointLightNode;
  44. addLightNode( PointLight, PointLightNode );
  45. addNodeClass( PointLightNode );