LightingContextNode.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import ContextNode from '../core/ContextNode.js';
  2. import { addNodeClass } from '../core/Node.js';
  3. import { addNodeElement, nodeProxy, float, vec3 } from '../shadernode/ShaderNode.js';
  4. class LightingContextNode extends ContextNode {
  5. constructor( node, lightingModel = null, backdropNode = null, backdropAlphaNode = null ) {
  6. super( node );
  7. this.lightingModel = lightingModel;
  8. this.backdropNode = backdropNode;
  9. this.backdropAlphaNode = backdropAlphaNode;
  10. this._context = null;
  11. }
  12. getContext() {
  13. const { backdropNode, backdropAlphaNode } = this;
  14. const directDiffuse = vec3().temp( 'directDiffuse' ),
  15. directSpecular = vec3().temp( 'directSpecular' ),
  16. indirectDiffuse = vec3().temp( 'indirectDiffuse' ),
  17. indirectSpecular = vec3().temp( 'indirectSpecular' );
  18. const reflectedLight = {
  19. directDiffuse,
  20. directSpecular,
  21. indirectDiffuse,
  22. indirectSpecular
  23. };
  24. const context = {
  25. radiance: vec3().temp( 'radiance' ),
  26. irradiance: vec3().temp( 'irradiance' ),
  27. iblIrradiance: vec3().temp( 'iblIrradiance' ),
  28. ambientOcclusion: float( 1 ).temp( 'ambientOcclusion' ),
  29. reflectedLight,
  30. backdrop: backdropNode,
  31. backdropAlpha: backdropAlphaNode
  32. };
  33. return context;
  34. }
  35. setup( builder ) {
  36. this.context = this._context || ( this._context = this.getContext() );
  37. this.context.lightingModel = this.lightingModel || builder.context.lightingModel;
  38. return super.setup( builder );
  39. }
  40. }
  41. export default LightingContextNode;
  42. export const lightingContext = nodeProxy( LightingContextNode );
  43. addNodeElement( 'lightingContext', lightingContext );
  44. addNodeClass( 'LightingContextNode', LightingContextNode );