Capítulo 416 de 859

Chapter 416: FunctionNode

Core Idea

This class represents a native shader function. It can be used to implement certain aspects of a node material with native shader code. There are two predefined TSL functions for easier usage.

  • wgslFn: Creates a WGSL function node.
  • glslFn: Creates a GLSL function node.

A basic example with one include looks like so:

Code Examples

const desaturateWGSLFn = wgslFn( `
	fn desaturate( color:vec3<f32> ) -> vec3<f32> {
		let lum = vec3<f32>( 0.299, 0.587, 0.114 );
		return vec3<f32>( dot( lum, color ) );
	}`
);
const someWGSLFn = wgslFn( `
	fn someFn( color:vec3<f32> ) -> vec3<f32> {
		return desaturate( color );
	}
`, [ desaturateWGSLFn ] );
material.colorNode = someWGSLFn( { color: texture( map ) } );
  • What it demonstrates: Typical usage of FunctionNode.

Reference Tables

Constructor

Signature
`new FunctionNode( code : string, includes : Array.<Node>, language : 'js'

Methods

MethodDescription
.generateNodeType( builder : NodeBuilder ) : stringReturns the type of this function node.
.getInputs( builder : NodeBuilder ) : Array.<NodeFunctionInput>Returns the inputs of this function node.
.getMemberType( builder : NodeBuilder, name : string ) : stringReturns the type of a member of this function node.
.getNodeFunction( builder : NodeBuilder ) : NodeFunctionReturns the node function for this function node.

Key Takeaways

  1. FunctionNode extends: EventDispatcher → Node → CodeNode
  2. Key methods: generateNodeType, getInputs, getMemberType, getNodeFunction.

Connects To