LightContextNode.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import ContextNode from '../core/ContextNode.js';
  2. import StructNode from '../core/StructNode.js';
  3. import { PhysicalLightingModel } from '../functions/BSDFs.js';
  4. const reflectedLightStruct = new StructNode( {
  5. directDiffuse: 'vec3',
  6. directSpecular: 'vec3'
  7. }, 'ReflectedLight' );
  8. class LightContextNode extends ContextNode {
  9. constructor( node ) {
  10. super( node, 'vec3' );
  11. }
  12. generate( builder ) {
  13. const type = this.getNodeType( builder );
  14. const material = builder.material;
  15. let lightingModel = null;
  16. if ( material.isMeshStandardMaterial === true ) {
  17. lightingModel = PhysicalLightingModel;
  18. }
  19. const reflectedLightNode = reflectedLightStruct.create();
  20. const reflectedLight = reflectedLightNode.build( builder, 'var' );
  21. this.setContextValue( 'reflectedLight', reflectedLightNode );
  22. if ( lightingModel !== null ) {
  23. this.setContextValue( 'lightingModel', lightingModel );
  24. }
  25. // add code
  26. super.generate( builder, type );
  27. return `( ${reflectedLight}.directDiffuse + ${reflectedLight}.directSpecular )`;
  28. }
  29. }
  30. export default LightContextNode;