How to get a global function on the NodeMaterial WGSL graph?

I’m testing out Three.js NodeMaterial with the WebGPU backend. I know the feature is experimental right now. Is there a way to define a global function that can be called in any WGSL code, for example:

const testGlobalFunctionCall = func( `
fn testGlobalFunctionCall( color:vec3<f32> ) -> f32 {
    let gf = myGlobalFunction(); //<---- global function
    return 1.0;
  }
  `)

Hi @michealmyers81

You can use includes, this will include the dependent function every time the function is used.

const myGlobalFunction= func( `
fn myGlobalFunction() -> f32 {
    return 1.0;
}
`);

const testGlobalFunctionCall = func( `fn testGlobalFunctionCall( color:vec3<f32> ) -> f32 {
    let gf = myGlobalFunction(); //<---- global function
    return 1.0;
  }`, [ myGlobalFunction ] );
2 Likes

awesome thank you. I’m hoping someone writes a short documentation on the wgsl node material soon.

1 Like