Fix compiling edge-cases

This commit is contained in:
Andras Schmelczer 2026-06-10 21:36:52 +01:00
parent bd2307f782
commit 4a6a25a081
2 changed files with 21 additions and 9 deletions

View file

@ -79,11 +79,16 @@ export class ParallelCompiler {
let replaceHappened: boolean; let replaceHappened: boolean;
do { do {
replaceHappened = false; replaceHappened = false;
processedSource = processedSource.replace(/{(.+)}/gm, (_, name: string): string => { processedSource = processedSource.replace(
/{(\w+)}/gm,
(_, name: string): string => {
replaceHappened = true; replaceHappened = true;
const value = substitutions[name]; if (!(name in substitutions)) {
return numberToGlslFloat(value); throw new Error(`Unknown shader substitution: '{${name}}'`);
}); }
return numberToGlslFloat(substitutions[name]);
}
);
} while (replaceHappened); } while (replaceHappened);
const shader = this.gl.createShader(type)!; const shader = this.gl.createShader(type)!;
@ -142,8 +147,7 @@ export class ParallelCompiler {
console.error( console.error(
formatLog( formatLog(
'parallel-compiler', 'parallel-compiler',
`Error: ${error}\nSource (line ${line}):\n${ `Error: ${error}\nSource (line ${line}):\n${shader.source.split('\n')[line - 1]
shader.source.split('\n')[line - 1]
}` }`
) )
); );

View file

@ -3,5 +3,13 @@
* *
* Returns non-numbers as is. * Returns non-numbers as is.
*/ */
export const numberToGlslFloat = (value: number | string): string => export const numberToGlslFloat = (value: number | string): string => {
Number.isInteger(value) ? `${value}.0` : value.toString(); if (typeof value !== 'number') {
return String(value);
}
const asString = value.toString();
// Very large integers stringify in exponent notation (1e21)
return Number.isInteger(value) && !asString.includes('e') ? `${asString}.0` : asString;
};