BabylonJS/Babylon.js · error · Error

`Uniforms must have a type and a name: '${uniformLine}'`

Error message

`Uniforms must have a type and a name: '${uniformLine}'`

What it means

After locating a uniform line, ParseFragmentShader parses it with /^uniform\s+(\w+)\s+(\w+)\s*;?/. If the regex does not produce type and name captures, the declaration is malformed and this error reports the offending line verbatim.

Source

Thrown at packages/dev/smartFilters/src/utils/buildTools/shaderConverter.ts:139

    fragmentShader = fragmentShaderWithoutHeader;
    const blockType = header?.[SmartFilterBlockTypeKey] || undefined;
    const namespace = header?.namespace || null;

    // Read the uniforms
    const uniforms: UniformMetadata[] = [];
    const uniformRegExp = new RegExp(/(\/\/\s*\{.*\}\s*(?:\r\n|\r|\n)+)?(uniform .*)/gm);
    const uniformGroups = fragmentShader.matchAll(uniformRegExp);
    for (const matches of uniformGroups) {
        const annotationJSON = matches[1];
        const uniformLine = matches[2];

        if (!uniformLine) {
            throw new Error("Uniform line not found");
        }

        const uniformLineMatches = new RegExp(/^uniform\s+(\w+)\s+(\w+)\s*;?/gm).exec(uniformLine);
        if (!uniformLineMatches || uniformLineMatches.length < 3) {
            throw new Error(`Uniforms must have a type and a name: '${uniformLine}'`);
        }
        const uniformTypeString = uniformLineMatches[1];
        const uniformName = uniformLineMatches[2];

        if (!uniformTypeString) {
            throw new Error(`Uniforms must have a type: '${uniformLine}'`);
        }
        if (!uniformName) {
            throw new Error(`Uniforms must have a name: '${uniformLine}'`);
        }

        // Convert to ConnectionPointType
        let type: ConnectionPointType;
        switch (uniformTypeString) {
            case "float":
                type = ConnectionPointType.Float;
                break;
            case "sampler2D":

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Rewrite the uniform as a simple single-line 'uniform <type> <name>;' declaration.
  2. Replace array/struct uniforms with individual scalar/vector uniforms supported by the tool.
  3. Check the quoted line in the error message to find and fix the exact malformed statement.

Example fix

// before
uniform float weights[8];
// after
uniform float weight0;
uniform float weight1;
Defensive patterns

Strategy: validation

Validate before calling

/^uniform\s+\w+\s+\w+\s*;?$/m.test(src) || lines.filter(l => /\buniform\b/.test(l) && !/^\s*\/\//.test(l)).forEach(l => console.error(`Malformed uniform: ${l.trim()}`));

Type guard

const isWellFormedUniform = (line: string): boolean => /^uniform\s+\w+\s+\w+\s*;?$/.test(line.trim());

Try / catch

try { await ConvertShader(path, ...); } catch (e) { const m = e.message.match(/Uniforms must have a type and a name: '(.*)'/); if (m) { rewriteUniform(m[1]); } else { throw e; } }

Prevention

When it happens

Trigger: A 'uniform' statement that doesn't match 'uniform <type> <name>' — e.g. multi-line declarations, missing name, or non-word characters in type/name (arrays 'uniform float a[4];', structs).

Common situations: GLSL array or struct uniforms unsupported by the converter; uniforms split across lines; exotic GLSL type names; copy-pasted declarations with unusual spacing.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/e3f1100c603e169e. Report an issue: GitHub.