import ContextNode from '../core/ContextNode.js'; import { nodeProxy, float, vec3 } from '../tsl/TSLBase.js'; /** * `LightingContextNode` represents an extension of the {@link ContextNode} module * by adding lighting specific context data. It represents the runtime context of * {@link LightsNode}. * * @augments ContextNode */ class LightingContextNode extends ContextNode { static get type() { return 'LightingContextNode'; } /** * Constructs a new lighting context node. * * @param {LightsNode} lightsNode - The lights node. * @param {?LightingModel} [lightingModel=null] - The current lighting model. * @param {?Node} [backdropNode=null] - A backdrop node. * @param {?Node} [backdropAlphaNode=null] - A backdrop alpha node. */ constructor( lightsNode, lightingModel = null, backdropNode = null, backdropAlphaNode = null ) { super( lightsNode ); /** * The current lighting model. * * @type {?LightingModel} * @default null */ this.lightingModel = lightingModel; /** * A backdrop node. * * @type {?Node} * @default null */ this.backdropNode = backdropNode; /** * A backdrop alpha node. * * @type {?Node} * @default null */ this.backdropAlphaNode = backdropAlphaNode; this._value = null; } /** * Returns a lighting context object. * * @return {{ * radiance: Node, * irradiance: Node, * iblIrradiance: Node, * ambientOcclusion: Node, * reflectedLight: {directDiffuse: Node, directSpecular: Node, indirectDiffuse: Node, indirectSpecular: Node}, * backdrop: Node, * backdropAlpha: Node * }} The lighting context object. */ getContext() { const { backdropNode, backdropAlphaNode } = this; const directDiffuse = vec3().toVar( 'directDiffuse' ), directSpecular = vec3().toVar( 'directSpecular' ), indirectDiffuse = vec3().toVar( 'indirectDiffuse' ), indirectSpecular = vec3().toVar( 'indirectSpecular' ); const reflectedLight = { directDiffuse, directSpecular, indirectDiffuse, indirectSpecular }; const context = { radiance: vec3().toVar( 'radiance' ), irradiance: vec3().toVar( 'irradiance' ), iblIrradiance: vec3().toVar( 'iblIrradiance' ), ambientOcclusion: float( 1 ).toVar( 'ambientOcclusion' ), reflectedLight, backdrop: backdropNode, backdropAlpha: backdropAlphaNode }; return context; } setup( builder ) { this.value = this._value || ( this._value = this.getContext() ); this.value.lightingModel = this.lightingModel || builder.context.lightingModel; return super.setup( builder ); } } export default LightingContextNode; export const lightingContext = /*@__PURE__*/ nodeProxy( LightingContextNode );