diff --git a/src/materials/nodes/NodeMaterial.js b/src/materials/nodes/NodeMaterial.js index 361a15d25479ce..a6a736c6d2ac96 100644 --- a/src/materials/nodes/NodeMaterial.js +++ b/src/materials/nodes/NodeMaterial.js @@ -25,7 +25,6 @@ import { modelViewMatrix } from '../../nodes/accessors/ModelNode.js'; import { vertexColor } from '../../nodes/accessors/VertexColorNode.js'; import { premultiplyAlpha } from '../../nodes/display/BlendModes.js'; import { subBuild } from '../../nodes/core/SubBuildNode.js'; -import { error } from '../../utils.js'; /** * Base class for all node materials. @@ -485,32 +484,6 @@ class NodeMaterial extends Material { const renderer = builder.renderer; const renderTarget = renderer.getRenderTarget(); - // < CONTEXT > - - if ( renderer.contextNode.isContextNode === true ) { - - builder.context = { ...builder.context, ...renderer.contextNode.getFlowContextData() }; - - } else { - - error( 'NodeMaterial: "renderer.contextNode" must be an instance of `context()`.' ); - - } - - if ( this.contextNode !== null ) { - - if ( this.contextNode.isContextNode === true ) { - - builder.context = { ...builder.context, ...this.contextNode.getFlowContextData() }; - - } else { - - error( 'NodeMaterial: "material.contextNode" must be an instance of `context()`.' ); - - } - - } - // < VERTEX STAGE > builder.addStack(); diff --git a/src/nodes/core/NodeBuilder.js b/src/nodes/core/NodeBuilder.js index 08d1cc24f2e0df..b76a701ad00693 100644 --- a/src/nodes/core/NodeBuilder.js +++ b/src/nodes/core/NodeBuilder.js @@ -2939,13 +2939,41 @@ class NodeBuilder { } /** - * Central build method which controls the build for the given object. - * - * @return {NodeBuilder} A reference to this node builder. + * Prebuild the node builder. */ - build() { + prebuild() { + + const { object, renderer, material } = this; + + // < renderer.contextNode > + + if ( renderer.contextNode.isContextNode === true ) { + + this.context = { ...this.context, ...renderer.contextNode.getFlowContextData() }; + + } else { + + error( 'NodeBuilder: "renderer.contextNode" must be an instance of `context()`.' ); + + } + + // < material.contextNode > - const { object, material, renderer } = this; + if ( material && material.contextNode ) { + + if ( material.contextNode.isContextNode === true ) { + + this.context = { ...this.context, ...material.contextNode.getFlowContextData() }; + + } else { + + error( 'NodeBuilder: "material.contextNode" must be an instance of `context()`.' ); + + } + + } + + // < nodeMaterial > if ( material !== null ) { @@ -2953,7 +2981,7 @@ class NodeBuilder { if ( nodeMaterial === null ) { - error( `NodeMaterial: Material "${ material.type }" is not compatible.` ); + error( `NodeBuilder: Material "${ material.type }" is not compatible.` ); nodeMaterial = new NodeMaterial(); @@ -2967,6 +2995,17 @@ class NodeBuilder { } + } + + /** + * Central build method which controls the build for the given object. + * + * @return {NodeBuilder} A reference to this node builder. + */ + build() { + + this.prebuild(); + // setup() -> stage 1: create possible new nodes and/or return an output reference node // analyze() -> stage 2: analyze nodes to possible optimization and validation // generate() -> stage 3: generate shader @@ -3025,27 +3064,7 @@ class NodeBuilder { */ async buildAsync() { - const { object, material, renderer } = this; - - if ( material !== null ) { - - let nodeMaterial = renderer.library.fromMaterial( material ); - - if ( nodeMaterial === null ) { - - error( `NodeMaterial: Material "${ material.type }" is not compatible.` ); - - nodeMaterial = new NodeMaterial(); - - } - - nodeMaterial.build( this ); - - } else { - - this.addFlow( 'compute', object ); - - } + this.prebuild(); // setup() -> stage 1: create possible new nodes and/or return an output reference node // analyze() -> stage 2: analyze nodes to possible optimization and validation