PointLightNode.js 1.5 KB

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