Why can't I define constants right at the beginning in wgslFn?

Hi, hoisting are enabled in WGSL so it wouldn’t be a problem if you put your constants below the initial (main) function, e.g:

fn myFunc() -> f32 {

	return myConst;

}

const myConst : f32 = 1.0;

You can also use snippet, which is code that is not pre-processed and will be embedded in the final code of your shader, e.g:

// import { wgslFn, code } from 'three/nodes';
// ...

const snippet = code( `
const myConst : f32 = 1.0;
` );

// wgslFn( code, [ ...includes ] )
const myFunc = wgslFn( `
fn myFunc() -> f32 {
	return myConst;
}
`, [ snippet ] );

This is useful when using the same code (snippet) in more than one function.

3 Likes