Skip to content

Commit 6af2036

Browse files
committed
fix: automatic uniform / attribute name recognition broken with minified shader code
1 parent 47ade94 commit 6af2036

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

lib/src/internal/findName.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
11
function findName(source: string | undefined, keyword: string, word: string) {
2-
return source
3-
?.split("\n")
4-
.find((line) => new RegExp(`^${keyword}.*${word};`, "i").test(line.trim()))
5-
?.match(/(\w+);/)?.[1];
2+
const re = new RegExp(`\\b${keyword}\\b\\s+\\w+\\s+(\\w*${word}\\w*)\\b`, "i");
3+
return source?.match(re)?.[1];
64
}
75

6+
/**
7+
* Finds the name of a uniform variable in a GLSL shader source string.
8+
*
9+
* This function searches for a declaration of a uniform variable that contains the specified `word`.
10+
* It returns the full name of the uniform variable if found.
11+
*
12+
* @param source - The GLSL shader source code as a string, or `undefined`.
13+
* @param word - The substring to look for within the uniform variable's name.
14+
* @returns The name of the uniform variable, or `undefined` if not found.
15+
*/
816
export function findUniformName(source: string | undefined, word: string) {
917
return findName(source, "uniform", word);
1018
}
1119

20+
/**
21+
* Finds the name of a varying (or 'in' in GLSL 300 ES) variable in a GLSL shader source string.
22+
*
23+
* This function searches for a declaration of a varying or 'in' variable that contains the specified `word`.
24+
* It returns the full name of the variable if found.
25+
*
26+
* @param source - The GLSL shader source code as a string, or `undefined`.
27+
* @param word - The substring to look for within the varying/in variable's name.
28+
* @returns The name of the varying/in variable, or `undefined` if not found.
29+
*/
1230
export function findVaryingName(source: string | undefined, word: string) {
1331
return findName(source, "varying", word) || findName(source, "in", word);
1432
}
1533

34+
/**
35+
* Finds the name of an attribute (or 'in' in GLSL 300 ES) variable in a GLSL shader source string.
36+
*
37+
* This function searches for a declaration of an attribute or 'in' variable that contains the specified `word`.
38+
* It returns the full name of the variable if found.
39+
*
40+
* @param source - The GLSL shader source code as a string, or `undefined`.
41+
* @param word - The substring to look for within the attribute/in variable's name.
42+
* @returns The name of the attribute/in variable, or `undefined` if not found.
43+
*/
1644
export function findAttributeName(source: string | undefined, word: string) {
1745
return findName(source, "attribute", word) || findName(source, "in", word);
1846
}

0 commit comments

Comments
 (0)