Skip to content

Commit

Permalink
TSL: Fix include one function under another. (#26844)
Browse files Browse the repository at this point in the history
* TSL: Fix include one function under another.

* Add include example
  • Loading branch information
sunag authored Sep 26, 2023
1 parent ca5c7cb commit 9fe5ed2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
23 changes: 17 additions & 6 deletions examples/jsm/nodes/code/FunctionNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

};

Expand Down
14 changes: 12 additions & 2 deletions examples/webgpu_materials.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@

// Custom WGSL ( desaturate filter )

const desaturateWGSLNode = wgslFn( `
const desaturateWGSLFn = wgslFn( `
fn desaturate( color:vec3<f32> ) -> vec3<f32> {
let lum = vec3<f32>( 0.299, 0.587, 0.114 );
Expand All @@ -176,8 +176,18 @@
}
` );

// include example

const someWGSLFn = wgslFn( `
fn someFn( color:vec3<f32> ) -> vec3<f32> {
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 )
Expand Down

0 comments on commit 9fe5ed2

Please sign in to comment.