From 6f53a78d9893ef99d2094c2dbb8c7233df30d23f Mon Sep 17 00:00:00 2001 From: Aayushdev18 Date: Fri, 24 Oct 2025 19:19:46 +0530 Subject: [PATCH 1/3] Add detection for outside variable references in p5.strands uniforms - Detect common problematic variables (mouseX, windowWidth, state_, pixelate) in uniform functions - Provide helpful error message when outside variables are referenced - Addresses GSoC issue #8172 for better user experience --- src/webgl/p5.Shader.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/webgl/p5.Shader.js b/src/webgl/p5.Shader.js index a82f112361..959dc7fba2 100644 --- a/src/webgl/p5.Shader.js +++ b/src/webgl/p5.Shader.js @@ -560,6 +560,14 @@ p5.Shader = class { const initializer = this.hooks.uniforms[key]; let value; if (initializer instanceof Function) { + // Check for common outside variable references + const funcString = initializer.toString(); + if (funcString.includes('mouseX') || funcString.includes('windowWidth') || + funcString.includes('state_') || funcString.includes('pixelate')) { + p5._friendlyError( + `p5.strands: Variable referenced in uniform "${name}" is not accessible in shader context.` + ); + } value = initializer(); } else { value = initializer; From 0d03053fb355d61eeb20d83e341c9c4cee40ce83 Mon Sep 17 00:00:00 2001 From: Aayushdev18 Date: Fri, 24 Oct 2025 19:47:26 +0530 Subject: [PATCH 2/3] Fix linting error: remove trailing spaces --- src/webgl/p5.Shader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webgl/p5.Shader.js b/src/webgl/p5.Shader.js index 959dc7fba2..e934b9ecf2 100644 --- a/src/webgl/p5.Shader.js +++ b/src/webgl/p5.Shader.js @@ -562,7 +562,7 @@ p5.Shader = class { if (initializer instanceof Function) { // Check for common outside variable references const funcString = initializer.toString(); - if (funcString.includes('mouseX') || funcString.includes('windowWidth') || + if (funcString.includes('mouseX') || funcString.includes('windowWidth') || funcString.includes('state_') || funcString.includes('pixelate')) { p5._friendlyError( `p5.strands: Variable referenced in uniform "${name}" is not accessible in shader context.` From 9bfcba521382d9ac47cea20fcae8a0b968b03169 Mon Sep 17 00:00:00 2001 From: Aayushdev18 Date: Mon, 27 Oct 2025 03:30:05 +0530 Subject: [PATCH 3/3] Remove hardcoded variable detection, add TODO for proper implementation Following reviewer feedback, removed the approach of checking for specific variable names (mouseX, windowWidth, etc.) as it's not robust. Added TODO indicating this should be implemented in strands_transpiler.js during the transpilation phase by tracking variable declarations and detecting references to variables outside the strand's scope. --- src/webgl/p5.Shader.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/webgl/p5.Shader.js b/src/webgl/p5.Shader.js index e934b9ecf2..06077a8602 100644 --- a/src/webgl/p5.Shader.js +++ b/src/webgl/p5.Shader.js @@ -560,14 +560,10 @@ p5.Shader = class { const initializer = this.hooks.uniforms[key]; let value; if (initializer instanceof Function) { - // Check for common outside variable references - const funcString = initializer.toString(); - if (funcString.includes('mouseX') || funcString.includes('windowWidth') || - funcString.includes('state_') || funcString.includes('pixelate')) { - p5._friendlyError( - `p5.strands: Variable referenced in uniform "${name}" is not accessible in shader context.` - ); - } + // TODO: Detect outside variable references in uniform initializers + // This should be done during transpilation in strands_transpiler.js + // by analyzing which variables are declared in the strand's scope + // and checking if referenced variables in uniforms are in that scope value = initializer(); } else { value = initializer;