HemisphereLightNode.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import AnalyticLightNode from './AnalyticLightNode.js';
  2. import LightsNode from './LightsNode.js';
  3. import Object3DNode from '../accessors/Object3DNode.js';
  4. import { uniform, add, mul, dot, mix, normalize, normalView } from '../shadernode/ShaderNodeElements.js';
  5. import { Color, HemisphereLight } from 'three';
  6. class HemisphereLightNode extends AnalyticLightNode {
  7. constructor( light = null ) {
  8. super( light );
  9. this.lightPositionNode = new Object3DNode( Object3DNode.POSITION );
  10. this.lightDirectionNode = normalize( this.lightPositionNode );
  11. this.groundColorNode = uniform( new Color() );
  12. }
  13. update( frame ) {
  14. const { light } = this;
  15. super.update( frame );
  16. this.lightPositionNode.object3d = light;
  17. this.groundColorNode.value.copy( light.groundColor ).multiplyScalar( light.intensity );
  18. }
  19. generate( builder ) {
  20. const { colorNode, groundColorNode, lightDirectionNode } = this;
  21. const dotNL = dot( normalView, lightDirectionNode );
  22. const hemiDiffuseWeight = add( mul( 0.5, dotNL ), 0.5 );
  23. const irradiance = mix( groundColorNode, colorNode, hemiDiffuseWeight );
  24. builder.context.irradiance.add( irradiance );
  25. }
  26. }
  27. LightsNode.setReference( HemisphereLight, HemisphereLightNode );
  28. export default HemisphereLightNode;