Skip to content

Commit 9fe5ed2

Browse files
authoredSep 26, 2023
TSL: Fix include one function under another. (#26844)
* TSL: Fix include one function under another. * Add include example
1 parent ca5c7cb commit 9fe5ed2

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed
 

‎examples/jsm/nodes/code/FunctionNode.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,28 @@ class FunctionNode extends CodeNode {
9999

100100
export default FunctionNode;
101101

102-
const nativeFn = ( code, includes, language = '' ) => {
102+
const nativeFn = ( code, includes = [], language = '' ) => {
103103

104-
let functionNode = null;
104+
for ( let i = 0; i < includes.length; i ++ ) {
105105

106-
return ( ...params ) => {
106+
const include = includes[ i ];
107107

108-
if ( functionNode === null ) functionNode = nodeObject( new FunctionNode( code, includes, language ) );
108+
// TSL Function: glslFn, wgslFn
109109

110-
return functionNode.call( ...params );
110+
if ( typeof include === 'function' ) {
111111

112-
};
112+
includes[ i ] = include.functionNode;
113+
114+
}
115+
116+
}
117+
118+
const functionNode = nodeObject( new FunctionNode( code, includes, language ) );
119+
120+
const fn = ( ...params ) => functionNode.call( ...params );
121+
fn.functionNode = functionNode;
122+
123+
return fn;
113124

114125
};
115126

‎examples/webgpu_materials.html

+12-2
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166

167167
// Custom WGSL ( desaturate filter )
168168

169-
const desaturateWGSLNode = wgslFn( `
169+
const desaturateWGSLFn = wgslFn( `
170170
fn desaturate( color:vec3<f32> ) -> vec3<f32> {
171171
172172
let lum = vec3<f32>( 0.299, 0.587, 0.114 );
@@ -176,8 +176,18 @@
176176
}
177177
` );
178178

179+
// include example
180+
181+
const someWGSLFn = wgslFn( `
182+
fn someFn( color:vec3<f32> ) -> vec3<f32> {
183+
184+
return desaturate( color );
185+
186+
}
187+
`, [ desaturateWGSLFn ] );
188+
179189
material = new MeshBasicNodeMaterial();
180-
material.colorNode = desaturateWGSLNode( { color: texture( uvTexture ) } );
190+
material.colorNode = someWGSLFn( { color: texture( uvTexture ) } );
181191
materials.push( material );
182192

183193
// Custom WGSL ( get texture from keywords )

0 commit comments

Comments
 (0)
Please sign in to comment.