HemisphereLightNode.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import AnalyticLightNode from './AnalyticLightNode.js';
  2. import { addLightNode } from './LightsNode.js';
  3. import { uniform } from '../core/UniformNode.js';
  4. import { mix } from '../math/MathNode.js';
  5. import { normalView } from '../accessors/NormalNode.js';
  6. import { objectPosition } from '../accessors/Object3DNode.js';
  7. import { addNodeClass } from '../core/Node.js';
  8. import { Color, HemisphereLight } from 'three';
  9. class HemisphereLightNode extends AnalyticLightNode {
  10. constructor( light = null ) {
  11. super( light );
  12. this.lightPositionNode = objectPosition( light );
  13. this.lightDirectionNode = this.lightPositionNode.normalize();
  14. this.groundColorNode = uniform( new Color() );
  15. }
  16. update( frame ) {
  17. const { light } = this;
  18. super.update( frame );
  19. this.lightPositionNode.object3d = light;
  20. this.groundColorNode.value.copy( light.groundColor ).multiplyScalar( light.intensity );
  21. }
  22. setup( builder ) {
  23. const { colorNode, groundColorNode, lightDirectionNode } = this;
  24. const dotNL = normalView.dot( lightDirectionNode );
  25. const hemiDiffuseWeight = dotNL.mul( 0.5 ).add( 0.5 );
  26. const irradiance = mix( groundColorNode, colorNode, hemiDiffuseWeight );
  27. builder.context.irradiance.addAssign( irradiance );
  28. }
  29. }
  30. export default HemisphereLightNode;
  31. addNodeClass( 'HemisphereLightNode', HemisphereLightNode );
  32. addLightNode( HemisphereLight, HemisphereLightNode );