SpotLightNode.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import AnalyticLightNode from './AnalyticLightNode.js';
  2. import { addLightNode } from './LightsNode.js';
  3. import getDistanceAttenuation from '../functions/light/getDistanceAttenuation.js';
  4. import getDirectionVector from '../functions/light/getDirectionVector.js';
  5. import { uniform } from '../core/UniformNode.js';
  6. import { smoothstep } from '../math/MathNode.js';
  7. import { objectViewPosition } from '../accessors/Object3DNode.js';
  8. import { positionView } from '../accessors/PositionNode.js';
  9. import { addNodeClass } from '../core/Node.js';
  10. import { Vector3, SpotLight } from 'three';
  11. class SpotLightNode extends AnalyticLightNode {
  12. constructor( light = null ) {
  13. super( light );
  14. this.directionNode = uniform( new Vector3() );
  15. this.coneCosNode = uniform( 0 );
  16. this.penumbraCosNode = uniform( 0 );
  17. this.cutoffDistanceNode = uniform( 0 );
  18. this.decayExponentNode = uniform( 0 );
  19. }
  20. update( frame ) {
  21. super.update( frame );
  22. const { light } = this;
  23. getDirectionVector( light, frame.camera, this.directionNode.value );
  24. this.coneCosNode.value = Math.cos( light.angle );
  25. this.penumbraCosNode.value = Math.cos( light.angle * ( 1 - light.penumbra ) );
  26. this.cutoffDistanceNode.value = light.distance;
  27. this.decayExponentNode.value = light.decay;
  28. }
  29. getSpotAttenuation( angleCosine ) {
  30. const { coneCosNode, penumbraCosNode } = this;
  31. return smoothstep( coneCosNode, penumbraCosNode, angleCosine );
  32. }
  33. construct( builder ) {
  34. const { colorNode, cutoffDistanceNode, decayExponentNode, light } = this;
  35. const lVector = objectViewPosition( light ).sub( positionView );
  36. const lightDirection = lVector.normalize();
  37. const angleCos = lightDirection.dot( this.directionNode );
  38. const spotAttenuation = this.getSpotAttenuation( angleCos );
  39. const lightDistance = lVector.length();
  40. const lightAttenuation = getDistanceAttenuation.call( {
  41. lightDistance,
  42. cutoffDistance: cutoffDistanceNode,
  43. decayExponent: decayExponentNode
  44. } );
  45. const finalColor = colorNode.mul( spotAttenuation ).mul( lightAttenuation );
  46. const lightColor = spotAttenuation.greaterThan( 0 ).cond( finalColor, 0 );
  47. const lightingModelFunctionNode = builder.context.lightingModelNode;
  48. const reflectedLight = builder.context.reflectedLight;
  49. if ( lightingModelFunctionNode && lightingModelFunctionNode.direct ) {
  50. lightingModelFunctionNode.direct.call( {
  51. lightDirection,
  52. lightColor,
  53. reflectedLight
  54. }, builder );
  55. }
  56. }
  57. }
  58. export default SpotLightNode;
  59. addLightNode( SpotLight, SpotLightNode );
  60. addNodeClass( SpotLightNode );