From 4a6a25a081b841a80fc9cf1f7e3b8f1a7f500d35 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Wed, 10 Jun 2026 21:36:52 +0100 Subject: [PATCH] Fix compiling edge-cases --- .../graphics-library/parallel-compiler.ts | 18 +++++++++++------- src/helper/number-to-glsl-float.ts | 12 ++++++++++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/graphics/graphics-library/parallel-compiler.ts b/src/graphics/graphics-library/parallel-compiler.ts index bb170cb..714c423 100644 --- a/src/graphics/graphics-library/parallel-compiler.ts +++ b/src/graphics/graphics-library/parallel-compiler.ts @@ -79,11 +79,16 @@ export class ParallelCompiler { let replaceHappened: boolean; do { replaceHappened = false; - processedSource = processedSource.replace(/{(.+)}/gm, (_, name: string): string => { - replaceHappened = true; - const value = substitutions[name]; - return numberToGlslFloat(value); - }); + processedSource = processedSource.replace( + /{(\w+)}/gm, + (_, name: string): string => { + replaceHappened = true; + if (!(name in substitutions)) { + throw new Error(`Unknown shader substitution: '{${name}}'`); + } + return numberToGlslFloat(substitutions[name]); + } + ); } while (replaceHappened); const shader = this.gl.createShader(type)!; @@ -142,8 +147,7 @@ export class ParallelCompiler { console.error( formatLog( 'parallel-compiler', - `Error: ${error}\nSource (line ${line}):\n${ - shader.source.split('\n')[line - 1] + `Error: ${error}\nSource (line ${line}):\n${shader.source.split('\n')[line - 1] }` ) ); diff --git a/src/helper/number-to-glsl-float.ts b/src/helper/number-to-glsl-float.ts index 8f2ec3d..5495c0f 100644 --- a/src/helper/number-to-glsl-float.ts +++ b/src/helper/number-to-glsl-float.ts @@ -3,5 +3,13 @@ * * Returns non-numbers as is. */ -export const numberToGlslFloat = (value: number | string): string => - Number.isInteger(value) ? `${value}.0` : value.toString(); +export const numberToGlslFloat = (value: number | string): string => { + 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; +};