LightNode.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import Node from '../core/Node.js';
  2. import Object3DNode from '../accessors/Object3DNode.js';
  3. import PositionNode from '../accessors/PositionNode.js';
  4. import ColorNode from '../inputs/ColorNode.js';
  5. import FloatNode from '../inputs/FloatNode.js';
  6. import OperatorNode from '../math/OperatorNode.js';
  7. import MathNode from '../math/MathNode.js';
  8. import { NodeUpdateType } from '../core/constants.js';
  9. import { getDistanceAttenuation } from '../functions/BSDFs.js';
  10. import { Color } from 'three';
  11. class LightNode extends Node {
  12. constructor( light = null ) {
  13. super( 'vec3' );
  14. this.updateType = NodeUpdateType.Object;
  15. this.light = light;
  16. this.colorNode = new ColorNode( new Color() );
  17. this.lightCutoffDistanceNode = new FloatNode( 0 );
  18. this.lightDecayExponentNode = new FloatNode( 0 );
  19. }
  20. update( /* frame */ ) {
  21. this.colorNode.value.copy( this.light.color ).multiplyScalar( this.light.intensity );
  22. this.lightCutoffDistanceNode.value = this.light.distance;
  23. this.lightDecayExponentNode.value = this.light.decay;
  24. }
  25. generate( builder ) {
  26. const lightPositionView = new Object3DNode( Object3DNode.VIEW_POSITION );
  27. const positionView = new PositionNode( PositionNode.VIEW );
  28. const lVector = new OperatorNode( '-', lightPositionView, positionView );
  29. const lightDirection = new MathNode( MathNode.NORMALIZE, lVector );
  30. const lightDistance = new MathNode( MathNode.LENGTH, lVector );
  31. const lightAttenuation = getDistanceAttenuation( {
  32. lightDistance,
  33. cutoffDistance: this.lightCutoffDistanceNode,
  34. decayExponent: this.lightDecayExponentNode
  35. } );
  36. const lightColor = new OperatorNode( '*', this.colorNode, lightAttenuation );
  37. lightPositionView.object3d = this.light;
  38. const lightingModelFunction = builder.context.lightingModel;
  39. if ( lightingModelFunction !== undefined ) {
  40. const directDiffuse = builder.context.directDiffuse;
  41. const directSpecular = builder.context.directSpecular;
  42. lightingModelFunction( {
  43. lightDirection,
  44. lightColor,
  45. directDiffuse,
  46. directSpecular
  47. }, builder );
  48. }
  49. }
  50. }
  51. export default LightNode;