LightContextNode.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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, output ) {
  13. const type = this.getType( 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. const totalLightSnippet = `( ${reflectedLight}.directDiffuse + ${reflectedLight}.directSpecular )`;
  26. // add code
  27. super.generate( builder, output );
  28. return builder.format( totalLightSnippet, type, output );
  29. }
  30. }
  31. export default LightContextNode;