From 9fe5ed2d71467efe9de0d5ca2a76e1b90e60147b Mon Sep 17 00:00:00 2001 From: sunag Date: Tue, 26 Sep 2023 16:10:10 -0300 Subject: [PATCH] TSL: Fix include one function under another. (#26844) * TSL: Fix include one function under another. * Add include example --- examples/jsm/nodes/code/FunctionNode.js | 23 +++++++++++++++++------ examples/webgpu_materials.html | 14 ++++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/examples/jsm/nodes/code/FunctionNode.js b/examples/jsm/nodes/code/FunctionNode.js index 6adf5bafdde859..870d2b31526c86 100644 --- a/examples/jsm/nodes/code/FunctionNode.js +++ b/examples/jsm/nodes/code/FunctionNode.js @@ -99,17 +99,28 @@ class FunctionNode extends CodeNode { export default FunctionNode; -const nativeFn = ( code, includes, language = '' ) => { +const nativeFn = ( code, includes = [], language = '' ) => { - let functionNode = null; + for ( let i = 0; i < includes.length; i ++ ) { - return ( ...params ) => { + const include = includes[ i ]; - if ( functionNode === null ) functionNode = nodeObject( new FunctionNode( code, includes, language ) ); + // TSL Function: glslFn, wgslFn - return functionNode.call( ...params ); + if ( typeof include === 'function' ) { - }; + includes[ i ] = include.functionNode; + + } + + } + + const functionNode = nodeObject( new FunctionNode( code, includes, language ) ); + + const fn = ( ...params ) => functionNode.call( ...params ); + fn.functionNode = functionNode; + + return fn; }; diff --git a/examples/webgpu_materials.html b/examples/webgpu_materials.html index 5ea4eb0681df48..611c8999df3612 100644 --- a/examples/webgpu_materials.html +++ b/examples/webgpu_materials.html @@ -166,7 +166,7 @@ // Custom WGSL ( desaturate filter ) - const desaturateWGSLNode = wgslFn( ` + const desaturateWGSLFn = wgslFn( ` fn desaturate( color:vec3 ) -> vec3 { let lum = vec3( 0.299, 0.587, 0.114 ); @@ -176,8 +176,18 @@ } ` ); + // include example + + const someWGSLFn = wgslFn( ` + fn someFn( color:vec3 ) -> vec3 { + + return desaturate( color ); + + } + `, [ desaturateWGSLFn ] ); + material = new MeshBasicNodeMaterial(); - material.colorNode = desaturateWGSLNode( { color: texture( uvTexture ) } ); + material.colorNode = someWGSLFn( { color: texture( uvTexture ) } ); materials.push( material ); // Custom WGSL ( get texture from keywords )