BabylonJS/Babylon.js · error · Error
"No function name found in shader code"
Error message
"No function name found in shader code"
What it means
ExtractFunctions scans the shader with GetFunctionHeaderRegEx; a header match whose capture group 1 (the function name) is empty means the regex matched something that looks like a function header but has no identifier. The parser cannot register a nameless function, so it throws. This is rare — usually a symptom of unusual syntax near a function definition.
Source
Thrown at packages/dev/smartFilters/src/utils/buildTools/shaderConverter.ts:357
mainFunctionName: string;
} {
const extractedFunctions: ShaderFunction[] = [];
let mainFunctionName: string | undefined;
let pos = 0;
const getFunctionHeaderRegEx = new RegExp(GetFunctionHeaderRegExString, GetFunctionHeaderRegExOptions);
while (pos < fragment.length) {
// Match the next available function header in the fragment code
getFunctionHeaderRegEx.lastIndex = pos;
const match = getFunctionHeaderRegEx.exec(fragment);
if (!match) {
break;
}
const functionName = match[1];
if (!functionName) {
throw new Error("No function name found in shader code");
}
const functionParams = match[2] || "";
// Store start index of the function definition
const startIndex = match.index;
// Balance braces to find end of function, starting just after the opening `{`
let endIndex = match.index + match[0].length;
let depth = 1;
while (depth > 0 && endIndex < fragment.length) {
if (fragment[endIndex] === "{") {
depth++;
} else if (fragment[endIndex] === "}") {
depth--;
}
endIndex++;
}
View on GitHub (pinned to 0592b347b8)
Solutions
- Inspect the shader near the reported location and give the function a proper name: `void doWork() { ... }`.
- Resolve/expand any preprocessor macros that might hide the function name before conversion.
- Simplify exotic syntax around function declarations (no attributes/annotations directly confusing the header pattern).
Example fix
// before
void () {
gl_FragColor = vec4(1.0);
}
// after
void fillColor() {
gl_FragColor = vec4(1.0);
} Defensive patterns
Strategy: validation
Validate before calling
const headers = [...shader.matchAll(/\b\w+\s+(\w*)\s*\([^)]*\)\s*\{/g)];
if (headers.some(m => !m[1])) throw new Error("Shader contains a function header with an empty name"); Type guard
function allFunctionsNamed(shader: string): boolean {
return [...shader.matchAll(/\b\w+\s+(\w*)\s*\([^)]*\)\s*\{/g)].every(m => m[1].length > 0);
} Try / catch
try {
const info = ParseFragmentShader(blockName, namespace, shader);
} catch (e) {
if (e instanceof Error && e.message === "No function name found in shader code") {
// locate the unnamed function near the reported position and name it
} else throw e;
} Prevention
- Always name functions; never emit placeholder/anonymous headers from codegen.
- Expand or remove macros around function declarations before conversion.
- Run a GLSL syntax validator on shaders as a pre-build step.
When it happens
Trigger: ExtractFunctions (via ParseFragmentShader) on shader code where the function-header regex matches a construct with an empty name capture, e.g. malformed syntax like `void () { }`, macro-expanded code that erased the identifier, or regex edge cases with anonymous-looking constructs.
Common situations: Shaders generated by codegen/templates with broken placeholders (`void %s(...)` unresolved); heavy macro usage that confuses the header regex; typos like a missing function name after the return type.
Related errors
- "Const line not found"
- `Consts must have a name, type, and a default value: '${cons
- `Consts must have a name: '${constLine}'`
- `Consts must have a value: '${constLine}'`
- `Unsupported const property type: '${type}'`
AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30).
Data as JSON: /api/errors/c1fc4f409fdde646.
Report an issue: GitHub.