12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import ContextNode from '../core/ContextNode.js';
- import { temp } from '../core/VarNode.js';
- import { add } from '../math/OperatorNode.js';
- import { mix } from '../math/MathNode.js';
- import { addNodeClass } from '../core/Node.js';
- import { addNodeElement, nodeProxy, float, vec3 } from '../shadernode/ShaderNode.js';
- class LightingContextNode extends ContextNode {
- constructor( node, lightingModelNode = null, backdropNode = null, backdropAlphaNode = null ) {
- super( node );
- this.lightingModelNode = lightingModelNode;
- this.backdropNode = backdropNode;
- this.backdropAlphaNode = backdropAlphaNode;
- }
- getNodeType( /*builder*/ ) {
- return 'vec3';
- }
- construct( builder ) {
- const { lightingModelNode, backdropNode, backdropAlphaNode } = this;
- const context = this.context = {}; // reset context
- const properties = builder.getNodeProperties( this );
- const directDiffuse = temp( vec3() ),
- directSpecular = temp( vec3() ),
- indirectDiffuse = temp( vec3() ),
- indirectSpecular = temp( vec3() );
- let totalDiffuse = add( directDiffuse, indirectDiffuse );
- if ( backdropNode !== null ) {
- totalDiffuse = vec3( backdropAlphaNode !== null ? mix( totalDiffuse, backdropNode, backdropAlphaNode ) : backdropNode );
- }
- const totalSpecular = add( directSpecular, indirectSpecular );
- const total = add( totalDiffuse, totalSpecular );
- const reflectedLight = {
- directDiffuse,
- directSpecular,
- indirectDiffuse,
- indirectSpecular,
- total
- };
- const lighting = {
- radiance: temp( vec3() ),
- irradiance: temp( vec3() ),
- iblIrradiance: temp( vec3() ),
- ambientOcclusion: temp( float( 1 ) )
- };
- Object.assign( properties, reflectedLight, lighting );
- Object.assign( context, lighting );
- context.reflectedLight = reflectedLight;
- context.lightingModelNode = lightingModelNode || context.lightingModelNode;
- if ( lightingModelNode && lightingModelNode.indirectDiffuse ) lightingModelNode.indirectDiffuse.call( context );
- if ( lightingModelNode && lightingModelNode.indirectSpecular ) lightingModelNode.indirectSpecular.call( context );
- if ( lightingModelNode && lightingModelNode.ambientOcclusion ) lightingModelNode.ambientOcclusion.call( context );
- return super.construct( builder );
- }
- generate( builder ) {
- const { context } = this;
- const type = this.getNodeType( builder );
- super.generate( builder, type );
- return context.reflectedLight.total.build( builder, type );
- }
- }
- export default LightingContextNode;
- export const lightingContext = nodeProxy( LightingContextNode );
- addNodeElement( 'lightingContext', lightingContext );
- addNodeClass( LightingContextNode );
|