mrdoob/three.js · error · Error

THREE.FunctionNode: Function is not a GLSL code.

Error message

THREE.FunctionNode: Function is not a GLSL code.

What it means

Thrown by the GLSL node-function parser when the source string does not begin with a valid GLSL function declaration. The regex requires `[precision?] returnType name?(params)` with exactly 5 capture groups; anything that fails to match (missing return type, missing parens, no params delimiter, JS/TS code, or WGSL) reaches the else branch.

Source

Thrown at src/nodes/parsers/GLSLNodeFunction.js:98

		const type = declaration[ 2 ];

		const precision = declaration[ 1 ] !== undefined ? declaration[ 1 ] : '';

		const headerCode = pragmaMainIndex !== - 1 ? source.slice( 0, pragmaMainIndex ) : '';

		return {
			type,
			inputs,
			name,
			precision,
			inputsCode,
			blockCode,
			headerCode
		};

	} else {

		throw new Error( 'THREE.FunctionNode: Function is not a GLSL code.' );

	}

};

/**
 * This class represents a GLSL node function.
 *
 * @augments NodeFunction
 */
class GLSLNodeFunction extends NodeFunction {

	/**
	 * Constructs a new GLSL node function.
	 *
	 * @param {string} source - The GLSL source.
	 */
	constructor( source ) {

View on GitHub (pinned to da05705fa3)

Solutions

  1. Ensure the GLSL string starts with a function signature like `float myFn( float x ) { ... }`.
  2. Use the default TSL parser for TSL/JS function bodies; only pass true GLSL to the GLSL parser.
  3. Strip leading comments/whitespace/structs before the function signature, or place them after `#pragma main`.

Example fix

// before
const f = Fn( `struct S{...}; float run(float x){return x;}` );

// after
const f = Fn( `float run( float x ) { return x; }` );
Defensive patterns

Strategy: validation

Validate before calling

const GLSL_FN_RE = /^\s*(highp|mediump|lowp)?\s*([a-z_0-9]+)\s*([a-z_0-9]+)?\s*\(([\s\S]*?)\)/i;
function isValidGlslFn( src ) {
  const m = ( src.indexOf( '#pragma main' ) !== -1 ? src.slice( src.indexOf( '#pragma main' ) + 11 ) : src ).match( GLSL_FN_RE );
  return m !== null && m.length === 5;
}

Type guard

const looksLikeGlslFunction = ( src ) => isValidGlslFn( src );

Prevention

When it happens

Trigger: Passing JavaScript/TypeScript or WGSL source to `Fn(..., { parser: GLSLNodeFunction })`; a GLSL string that starts with a struct/layout declaration instead of the function signature; malformed/empty string; missing `#pragma main` placement that leaves the signature unparsable.

Common situations: Authoring `Fn` with inline GLSL but forgetting the return type or parameter list; mixing TSL and raw GLSL incorrectly; copy-pasting GLSL that has leading comments/newlines the regex cannot skip; version change in parser strictness.

Related errors


AI-assisted analysis of mrdoob/three.js@da05705fa3 (2026-08-12). Data as JSON: /api/errors/ec610a5812401155. Report an issue: GitHub.