|
1 | 1 | 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]; |
6 | 4 | } |
7 | 5 |
|
| 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 | + */ |
8 | 16 | export function findUniformName(source: string | undefined, word: string) { |
9 | 17 | return findName(source, "uniform", word); |
10 | 18 | } |
11 | 19 |
|
| 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 | + */ |
12 | 30 | export function findVaryingName(source: string | undefined, word: string) { |
13 | 31 | return findName(source, "varying", word) || findName(source, "in", word); |
14 | 32 | } |
15 | 33 |
|
| 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 | + */ |
16 | 44 | export function findAttributeName(source: string | undefined, word: string) { |
17 | 45 | return findName(source, "attribute", word) || findName(source, "in", word); |
18 | 46 | } |
0 commit comments