Capítulo 747 de 859

Chapter 747: NodeBuilder

Core Idea

Base class for builders which generate a shader program based on a 3D object and its node material definition.

Key Concepts

  • activeStacks : Array.<StackNode>: The active stack nodes.
  • attributes : Array.<NodeAttribute>: This array holds the node attributes of this builder created via AttributeNode.
  • bindGroups : Array.<BindGroup>: Reference to the array of bind groups.
  • bindings : Object: This dictionary holds the bindings for each shader stage.
  • bindingsIndexes : Object: This dictionary maintains the binding indices per bind group.
  • bufferAttributes : Array.<NodeAttribute>: This array holds the node attributes of this builder created via BufferAttributeNode.
  • buildStage : 'setup' | 'analyze' | 'generate': The current build stage.
  • cache : NodeCache: The builder's cache.
  • camera : Camera: The camera the 3D object is rendered with.
  • chaining : Array.<Node>: A chain of nodes. Used to check recursive calls in node-graph.

Reference Tables

Constructor

Signature
new NodeBuilder( object : Object3D, renderer : Renderer, parser : NodeParser )

Properties

PropertyDescription
.activeStacks : Array.<StackNode>The active stack nodes.
.attributes : Array.<NodeAttribute>This array holds the node attributes of this builder created via AttributeNode.
.bindGroups : Array.<BindGroup>Reference to the array of bind groups.
.bindings : ObjectThis dictionary holds the bindings for each shader stage.
.bindingsIndexes : ObjectThis dictionary maintains the binding indices per bind group.
.bufferAttributes : Array.<NodeAttribute>This array holds the node attributes of this builder created via BufferAttributeNode.
`.buildStage : 'setup''analyze'
.cache : NodeCacheThe builder's cache.
.camera : CameraThe camera the 3D object is rendered with.
.chaining : Array.<Node>A chain of nodes. Used to check recursive calls in node-graph.
.clippingContext : ClippingContextThe current clipping context.
.codes : Object.<string, Array.<NodeCode>>This dictionary holds the (native) node codes of this builder. The codes are maintained in an array for each shader stage.
.computeShader : stringThe generated compute shader.
.context : ObjectThe builder's context.
.currentFunctionNode : FunctionNodeReference to the current function node.
.currentNode : NodeA reference the current node which is the last node in the chain of nodes.
.declarations : ObjectThis dictionary holds the declarations for each shader stage.
.environmentNode : NodeA reference to the current environment node.
.flow : ObjectCurrent code flow. All code generated in this stack will be stored in .flow.
.flowCode : Object.<string, string>Nodes code from .flowNodes.
.flowNodes : Object.<string, Array.<Node>>Nodes used in the primary flow of code generation.
.fnCall : NodeThe current TSL function(Fn) call node.
.fogNode : NodeA reference to the current fog node.
.fragmentShader : stringThe generated fragment shader.
.geometry : BufferGeometryThe geometry of the 3D object.
.globalCache : NodeCacheSince the NodeBuilder#cache might be temporarily overwritten by other caches, this member retains the reference to the builder's own cache.
.hardwareClipping : booleanWhether the built material uses hardware clipping or not.
.hashNodes : Object.<number, Node>A dictionary that assigns each node to a unique hash.
.lightsNode : LightsNodeA reference to the current lights node.
.material : MaterialThe material of the 3D object.
.nodes : Set.<Node>A list of all nodes the builder is processing for this 3D object.
.object : Object3DThe 3D object.
.observer : NodeMaterialObserverA reference to a node material observer.
.parser : NodeParserA reference to a node parser.
.renderer : RendererThe current renderer.
.scene : SceneThe scene the 3D object belongs to.
.sequentialNodes : Set.<Node>A list of all nodes the builder is processing in sequential order.
`.shaderStage : 'vertex''fragment'
.stack : StackNodeThe current stack. This reflects the current process in the code block hierarchy, it is useful to know if the current process is inside a conditional for example.
.stacks : Array.<StackNode>List of stack nodes. The current stack hierarchy is stored in an array.
.structs : ObjectThis dictionary holds the output structs of the builder. The structs are maintained in an array for each shader stage.
.subBuildReturns the current sub-build layer.
.subBuildFn : stringThe current sub-build TSL function(Fn).
.subBuildLayers : Array.<SubBuildNode>The sub-build layers.
.tab : stringA tab value. Used for shader string generation.
.types : ObjectThis dictionary holds the types of the builder.
.uniforms : ObjectThis dictionary holds the node uniforms of the builder. The uniforms are maintained in an array for each shader stage.
.updateAfterNodes : Array.<Node>A list of all nodes which Node#updateAfter method should be executed.
.updateBeforeNodes : Array.<Node>A list of all nodes which Node#updateBefore method should be executed.
.updateNodes : Array.<Node>A list of all nodes which Node#update method should be executed.
`.vars : Object.<string, (Array.<NodeVar>number)>`
.varyings : Array.<NodeVarying>This array holds the node varyings of this builder.
.vertexShader : stringThe generated vertex shader.

Methods

MethodDescription
.addChain( node : Node )Adds the given node to the internal node chain. This is used to check recursive calls in node-graph.
.addContext( context : Object ) : ObjectAdds context data to the builder's current context.
`.addFlow( shaderStage : 'vertex''fragment'
.addFlowCode( code : string ) : NodeBuilderAdds a code to the current code flow.
.addFlowCodeHierarchy( node : Node, nodeBlock : Node )Adds a code flow based on the code-block hierarchy. This is used so that code-blocks like If,Else create their variables locally if the Node is only used inside one of these conditionals in the curre…
.addFlowTab() : NodeBuilderAdd tab in the code that will be generated so that other snippets respect the current tabulation. Typically used in codes with If,Else.
.addInclude( node : Node ) : voidIncludes a node in the current function node.
.addLineFlowCode( code : string, node : Node ) : NodeBuilderAdd a inline-code to the current flow.
.addLineFlowCodeBlock( node : Node, code : string, nodeBlock : Node )Add a inline-code to the current flow code-block.
.addNode( node : Node )Adds a node to this builder.
.addSequentialNode( node : Node )It is used to add Nodes that will be used as FRAME and RENDER events, and need to follow a certain sequence in the calls to work correctly. This function should be called after 'setup()' in the 'buil…
.addStack() : StackNodeAdds a stack node to the internal stack.
.addSubBuild( subBuild : SubBuildNode )Adds a sub-build layer to the node builder.
.build() : NodeBuilderCentral build method which controls the build for the given object.
.buildAsync() : Promise.<NodeBuilder> (async)Async version of build() that yields to main thread between shader stages. Use this in compileAsync() to prevent blocking the main thread.
.buildCode() (abstract)Controls the code build of the shader stages.
.buildFunctionCode( shaderNode : ShaderNodeInternal ) : string (abstract)Builds the given shader node.
.buildFunctionNode( shaderNode : ShaderNodeInternal ) : FunctionNodeReturns the native shader operator name for a given generic name. It is a similar type of method like NodeBuilder#getMethod.
.buildUpdateNodes()Checks the update types of nodes
.changeComponentType( type : string, newComponentType : string ) : stringFor a given type this method changes the component type to the given value. E.g. vec4 should be changed to the new component type uint which results in uvec4.
.createCubeRenderTarget( size : number, options : Object ) : CubeRenderTargetFactory method for creating an instance of CubeRenderTarget with the given dimensions and options.
.createRenderTarget( width : number, height : number, options : Object ) : RenderTargetFactory method for creating an instance of RenderTarget with the given dimensions and options.
`.flowBuildStage( node : Node, buildStage : string, output : Nodestring ) : Node
.flowChildNode( node : Node, output : string ) : ObjectGenerates a code flow based on a child Node.
.flowNode( node : Node ) : ObjectExecutes the node flow based on a root node to generate the final shader code.
`.flowNodeFromShaderStage( shaderStage : 'vertex''fragment'
.flowShaderNode( shaderNode : ShaderNodeInternal ) : ObjectGenerates a code flow based on a TSL function: Fn().
.flowStagesNode( node : Node, output : string ) : ObjectRuns the node flow through all the steps of creation, 'setup', 'analyze', 'generate'.
.format( snippet : string, fromType : string, toType : string ) : stringFormats the given shader snippet from a given type into another one. E.g. this method might be used to convert a simple float string "1.0" into a vec3 representation: "vec3<f32>( 1.0 )".
.generateArray( type : string, count : number, values : Array.<Node> ) : stringGenerates the array shader string for the given type and value.
.generateArrayDeclaration( type : string, count : number ) : stringGenerates the array declaration string.
.generateConst( type : string, value : any ) : stringGenerates the shader string for the given type and value.
.generateStruct( type : string, membersLayout : Array.<Object>, values : Array.<Node> ) : stringGenerates the struct shader string.
.generateTexture( texture : Texture, textureProperty : string, uvSnippet : string ) : string (abstract)Generates a texture sample shader string for the given texture data.
.generateTextureLod( texture : Texture, textureProperty : string, uvSnippet : string, depthSnippet : string, levelSnippet : string ) : string (abstract)Generates a texture LOD shader string for the given texture data.
.getActiveStack() : StackNodeReturns the active stack.
.getAttribute( name : string, type : string ) : NodeAttributeReturns a node attribute for the given name and type.
`.getAttributes( shaderStage : 'vertex''fragment'
.getAttributesArray() : Array.<NodeAttribute>Returns an array holding all node attributes of this node builder.
.getBaseStack() : StackNodeReturns the base stack.
`.getBindGroupArray( groupName : string, shaderStage : 'vertex''fragment'
.getBindings() : Array.<BindGroup>Returns a list bindings of all shader stages separated by groups.
.getBufferAttributeFromNode( node : BufferAttributeNode, type : string, name : string ) : NodeAttributeReturns an instance of NodeAttribute for the given buffer attribute node.
`.getBuildStage() : 'setup''analyze'
.getCache() : NodeCacheReturns the builder's current cache.
.getCacheFromNode( node : Node, parent : boolean ) : NodeCacheReturns a cache for the given node.
`.getClosestSubBuild( data : NodeSet.<string>
`.getCodeFromNode( node : CodeNode, type : string, shaderStage : 'vertex''fragment'
`.getCodes( shaderStage : 'vertex''fragment'
.getComponentType( type : string ) : stringReturns the component type for a given type.
.getComponentTypeFromTexture( texture : Texture ) : stringReturns the component type of a given texture.
.getContext() : ObjectReturns the builder's current context.
`.getDataFromNode( node : Node, shaderStage : 'vertex''fragment'
.getDrawIndex() : string (abstract)Returns the drawIndex input variable as a native shader string. Only relevant for WebGL and its WEBGL_multi_draw extension.
.getElementType( type : string ) : stringReturns the element type for a given type.
`.getFlowData( node : Node, shaderStage : 'vertex''fragment'
.getFragCoord() : string (abstract)Returns the fragCoord input variable as a native shader string.
.getFrontFacing() : string (abstract)Returns the frontFacing input variable as a native shader string.
.getFunctionOperator( op : string ) : string (abstract)Returns the native shader operator name for a given generic name. It is a similar type of method like NodeBuilder#getMethod.
.getHash() : stringReturns the hash of this node builder.
.getInstanceIndex() : string (abstract)Contextually returns either the vertex stage instance index builtin or the linearized index of an compute invocation within a grid of workgroups.
.getIntegerType( type : string ) : stringReturns the integer type pendant for the given type.
.getMethod( method : string ) : string (abstract)Returns the native shader method name for a given generic name. E.g. the method name textureDimensions matches the WGSL name but must be resolved to textureSize in GLSL.
.getNodeFromHash( hash : number ) : NodeReturns a node for the given hash, see NodeBuilder#setHashNode.
`.getNodeProperties( node : Node, shaderStage : 'vertex''fragment'
.getNodeUniform( uniformNode : NodeUniform, type : string ) : UniformReturns a uniform representation which is later used for UBO generation and rendering.
.getOutputStructName() : string (abstract)Returns the output struct name which is required by OutputStructNode.
.getOutputStructTypeFromNode( node : OutputStructNode, membersLayout : Array.<Object> ) : StructTypeReturns an instance of StructType for the given output struct node.
.getOutputType( index : number ) : stringReturns the type of the color output based on the renderer's render target.
`.getPropertyName( node : Node, shaderStage : 'vertex''fragment'
`.getShaderStage() : 'vertex''fragment'
.getSharedContext() : ObjectGets a context used in shader construction that can be shared across different materials. This is necessary since the renderer cache can reuse shaders generated in one material and use them in anothe…
.getSharedDataFromNode( node : Node ) : ObjectReturns shared data object for the given node.
.getSignature() : stringReturns a signature with the engine's current revision.
`.getStructTypeFromNode( node : OutputStructNode, membersLayout : Array.<Object>, name : string, shaderStage : 'vertex''fragment'
`.getStructTypeNode( name : string, shaderStage : 'vertex''fragment'
.getSubBuildOutput( node : Node ) : stringReturns the output node of a sub-build layer.
.getSubBuildProperty( property : string, node : Node ) : stringReturns the sub-build property name for the given property and node.
.getTernary( condSnippet : string, ifSnippet : string, elseSnippet : string ) : string (abstract)Returns the native snippet for a ternary operation. E.g. GLSL would output a ternary op as cond ? x : y whereas WGSL would output it as select(y, x, cond)
.getType( type : string ) : stringIt might be necessary to convert certain data types to different ones so this method can be used to hide the conversion.
.getTypeFromArray( array : TypedArray ) : stringReturns the type for a given typed array.
.getTypeFromAttribute( attribute : BufferAttribute ) : stringReturns the type for a given buffer attribute.
.getTypeFromLength( length : number, componentType : string ) : stringReturns the data type for the given the length and component type.
.getTypeLength( type : string ) : numberReturns the length for the given data type.
.getUniformBufferLimit() : numberReturns the maximum number of bytes available for uniform buffers.
`.getUniformFromNode( node : UniformNode, type : string, shaderStage : 'vertex''fragment'
`.getUniforms( shaderStage : 'vertex''fragment'
.getVar( type : string, name : string, count : number ) : stringReturns a single variable definition as a shader string for the given variable type and name.
`.getVarFromNode( node : VarNode, name : string, type : string, shaderStage : 'vertex''fragment'
`.getVars( shaderStage : 'vertex''fragment'
`.getVaryingFromNode( node : VaryingNodePropertyNode, name : string, type : string, interpolationType : string, interpolationSampling : string ) : NodeVar`
`.getVaryings( shaderStage : 'vertex''fragment'
.getVectorFromMatrix( type : string ) : stringReturns the vector type for a given matrix type.
.getVectorType( type : string ) : stringReturns the vector type for a given type.
.getVertexIndex() : string (abstract)Returns the vertexIndex input variable as a native shader string.
.hasGeometryAttribute( name : string ) : booleanWhether the given attribute name is defined in the geometry or not.
.hasWriteUsage( node : Node ) : booleanReturns whether the given node has been written to in any shader stage.
.includes( node : Node ) : booleanWhether the given node is included in the internal array of nodes or not.
.increaseUsage( node : Node ) : numberCalling this method increases the usage count for the given node by one.
.isAvailable( name : string ) : boolean (abstract)Whether the requested feature is available or not.
.isContextAssign() : booleanReturns whether the builder is currently in an assignment context.
.isDeterministic( node : Node ) : booleanReturns whether a Node or its flow is deterministic, useful for use in const.
.isFilteredTexture( texture : Texture ) : booleanWhether the given texture is filtered or not.
.isFlatShading() : booleanWhether the material is using flat shading or not.
.isFlipY() : boolean (abstract)Whether to flip texture data along its vertical axis or not. WebGL needs this method evaluate to true, WebGPU to false.
.isInteger( type : string ) : booleanReturns the type is an integer type.
.isMatrix( type : string ) : booleanWhether the given type is a matrix type or not.
.isOpaque() : booleanWhether the material is opaque or not.
.isReference( type : string ) : booleanWhether the given type is a reference type or not.
.isVector( type : string ) : booleanWhether the given type is a vector type or not.
.needsPreviousData() : booleanReturns true if data from the previous frame are required. Relevant when computing motion vectors with VelocityNode.
.needsToWorkingColorSpace( texture : Texture ) : boolean (abstract)Checks if the given texture requires a manual conversion to the working color space.
.prebuild()Prebuild the node builder.
.registerDeclaration( node : Object )Registers a node declaration in the current shader stage.
.removeActiveStack( stack : StackNode )Removes the active stack from the internal stack.
.removeChain( node : Node )Removes the given node from the internal node chain.
.removeFlowTab() : NodeBuilderRemoves a tab.
.removeStack() : StackNodeRemoves the last stack node from the internal stack.
.removeSubBuild() : SubBuildNodeRemoves the last sub-build layer from the node builder.
.setActiveStack( stack : StackNode )Adds an active stack to the internal stack.
`.setBuildStage( buildStage : 'setup''analyze'
.setCache( cache : NodeCache )Sets builder's cache.
.setContext( context : Object )Sets builder's context.
.setHashNode( node : Node, hash : number )The builder maintains each node in a hash-based dictionary. This method sets the given node (value) with the given hash (key) into this dictionary.
`.setShaderStage( shaderStage : 'vertex''fragment'
.sortBindingGroups()Sorts the bind groups and updates NodeBuilder#bindingsIndexes.

Key Takeaways

  1. Most relevant properties: activeStacks, attributes, bindGroups, bindings.
  2. Key methods: addChain, addContext, addFlow, addFlowCode.

Connects To