From 0ea049e3450e03399a5c2efc78af7fb9237f7865 Mon Sep 17 00:00:00 2001 From: aashu2006 Date: Thu, 12 Feb 2026 02:51:56 +0530 Subject: [PATCH 1/3] Forward optional scope parameter in build*Shader methods for instance mode support --- src/webgl/material.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/webgl/material.js b/src/webgl/material.js index 32d0c86d3f..e55343095e 100644 --- a/src/webgl/material.js +++ b/src/webgl/material.js @@ -739,8 +739,8 @@ function material(p5, fn) { * @param {Object} hooks An object specifying p5.strands hooks in GLSL. * @returns {p5.Shader} The material shader */ - fn.buildFilterShader = function (callback) { - return this.baseFilterShader().modify(callback); + fn.buildFilterShader = function (callback, scope) { + return this.baseFilterShader().modify(callback, scope); }; /** @@ -1567,8 +1567,8 @@ function material(p5, fn) { * @param {Object} hooks An object specifying p5.strands hooks in GLSL. * @returns {p5.Shader} The material shader. */ - fn.buildMaterialShader = function (cb) { - return this.baseMaterialShader().modify(cb); + fn.buildMaterialShader = function (cb, scope) { + return this.baseMaterialShader().modify(cb, scope); }; /** @@ -1783,8 +1783,8 @@ function material(p5, fn) { * @param {Object} hooks An object specifying p5.strands hooks in GLSL. * @returns {p5.Shader} The normal shader. */ - fn.buildNormalShader = function (cb) { - return this.baseNormalShader().modify(cb); + fn.buildNormalShader = function (cb, scope) { + return this.baseNormalShader().modify(cb, scope); }; /** @@ -1947,8 +1947,8 @@ function material(p5, fn) { * @param {Object} hooks An object specifying p5.strands hooks in GLSL. * @returns {p5.Shader} The color shader. */ - fn.buildColorShader = function (cb) { - return this.baseColorShader().modify(cb); + fn.buildColorShader = function (cb, scope) { + return this.baseColorShader().modify(cb, scope); }; /** @@ -2202,8 +2202,8 @@ function material(p5, fn) { * @param {Object} hooks An object specifying p5.strands hooks in GLSL. * @returns {p5.Shader} The stroke shader. */ - fn.buildStrokeShader = function (cb) { - return this.baseStrokeShader().modify(cb); + fn.buildStrokeShader = function (cb, scope) { + return this.baseStrokeShader().modify(cb, scope); }; /** From 0455a6c783d89f1c45768f32b38a44f43222aaa0 Mon Sep 17 00:00:00 2001 From: aashu2006 Date: Thu, 12 Feb 2026 10:47:13 +0530 Subject: [PATCH 2/3] Add unit test and document optional scope parameter in build*Shader methods --- src/webgl/material.js | 10 ++++++++++ test/unit/webgl/p5.Shader.scope.js | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 test/unit/webgl/p5.Shader.scope.js diff --git a/src/webgl/material.js b/src/webgl/material.js index e55343095e..c3dff5b766 100644 --- a/src/webgl/material.js +++ b/src/webgl/material.js @@ -732,11 +732,13 @@ function material(p5, fn) { * @beta * @submodule p5.strands * @param {Function} callback A function building a p5.strands shader. + * @param {Object} [scope] An optional scope object passed to .modify(). * @returns {p5.Shader} The material shader */ /** * @method buildFilterShader * @param {Object} hooks An object specifying p5.strands hooks in GLSL. + * @param {Object} [scope] An optional scope object passed to .modify(). * @returns {p5.Shader} The material shader */ fn.buildFilterShader = function (callback, scope) { @@ -1560,11 +1562,13 @@ function material(p5, fn) { * @submodule p5.strands * @beta * @param {Function} callback A function building a p5.strands shader. + * @param {Object} [scope] An optional scope object passed to .modify(). * @returns {p5.Shader} The material shader. */ /** * @method buildMaterialShader * @param {Object} hooks An object specifying p5.strands hooks in GLSL. + * @param {Object} [scope] An optional scope object passed to .modify(). * @returns {p5.Shader} The material shader. */ fn.buildMaterialShader = function (cb, scope) { @@ -1776,11 +1780,13 @@ function material(p5, fn) { * @submodule p5.strands * @beta * @param {Function} callback A function building a p5.strands shader. + * @param {Object} [scope] An optional scope object passed to .modify(). * @returns {p5.Shader} The normal shader. */ /** * @method buildNormalShader * @param {Object} hooks An object specifying p5.strands hooks in GLSL. + * @param {Object} [scope] An optional scope object passed to .modify(). * @returns {p5.Shader} The normal shader. */ fn.buildNormalShader = function (cb, scope) { @@ -1940,11 +1946,13 @@ function material(p5, fn) { * @submodule p5.strands * @beta * @param {Function} callback A function building a p5.strands shader. + * @param {Object} [scope] An optional scope object passed to .modify(). * @returns {p5.Shader} The color shader. */ /** * @method buildColorShader * @param {Object} hooks An object specifying p5.strands hooks in GLSL. + * @param {Object} [scope] An optional scope object passed to .modify(). * @returns {p5.Shader} The color shader. */ fn.buildColorShader = function (cb, scope) { @@ -2195,11 +2203,13 @@ function material(p5, fn) { * @submodule p5.strands * @beta * @param {Function} callback A function building a p5.strands shader. + * @param {Object} [scope] An optional scope object passed to .modify(). * @returns {p5.Shader} The stroke shader. */ /** * @method buildStrokeShader * @param {Object} hooks An object specifying p5.strands hooks in GLSL. + * @param {Object} [scope] An optional scope object passed to .modify(). * @returns {p5.Shader} The stroke shader. */ fn.buildStrokeShader = function (cb, scope) { diff --git a/test/unit/webgl/p5.Shader.scope.js b/test/unit/webgl/p5.Shader.scope.js new file mode 100644 index 0000000000..5dbe37905e --- /dev/null +++ b/test/unit/webgl/p5.Shader.scope.js @@ -0,0 +1,25 @@ +import p5 from '../../../src/app.js'; + +suite('p5.Shader', function() { + suite('buildMaterialShader scope forwarding', function() { + test('forwards scope to modify in instance mode', function(done) { + new p5((sketch) => { + sketch.setup = function() { + sketch.createCanvas(10, 10, sketch.WEBGL); + + const scope = { sketch }; + let receivedSketch = null; + + sketch.buildMaterialShader(function({ sketch: s }) { + receivedSketch = s; + }, scope); + + assert.strictEqual(receivedSketch, sketch); + + sketch.remove(); + done(); + }; + }); + }); + }); +}); From bec8391708bab478087c89a38f45034136061113 Mon Sep 17 00:00:00 2001 From: aashu2006 Date: Wed, 18 Feb 2026 14:07:22 +0530 Subject: [PATCH 3/3] Move scope test into p5.Shader.js and align with existing strands test style --- test/unit/webgl/p5.Shader.js | 15 +++++++++++++++ test/unit/webgl/p5.Shader.scope.js | 25 ------------------------- 2 files changed, 15 insertions(+), 25 deletions(-) delete mode 100644 test/unit/webgl/p5.Shader.scope.js diff --git a/test/unit/webgl/p5.Shader.js b/test/unit/webgl/p5.Shader.js index 75a634e08c..3003037011 100644 --- a/test/unit/webgl/p5.Shader.js +++ b/test/unit/webgl/p5.Shader.js @@ -459,6 +459,21 @@ suite('p5.Shader', function() { }).not.toThrowError(); }); + test('buildMaterialShader forwards scope to modify', () => { + myp5.createCanvas(5, 5, myp5.WEBGL); + expect(() => { + const myShader = myp5.buildMaterialShader(() => { + myp5.getPixelInputs(inputs => { + inputs.color = [1, 0, 0, 1]; + return inputs; + }); + }, { myp5 }); + myp5.noStroke(); + myp5.shader(myShader); + myp5.plane(myp5.width, myp5.height); + }).not.toThrowError(); + }); + test('returns numbers for builtin globals outside hooks and a strandNode when called inside hooks', () => { myp5.createCanvas(5, 5, myp5.WEBGL); myp5.baseMaterialShader().modify(() => { diff --git a/test/unit/webgl/p5.Shader.scope.js b/test/unit/webgl/p5.Shader.scope.js deleted file mode 100644 index 5dbe37905e..0000000000 --- a/test/unit/webgl/p5.Shader.scope.js +++ /dev/null @@ -1,25 +0,0 @@ -import p5 from '../../../src/app.js'; - -suite('p5.Shader', function() { - suite('buildMaterialShader scope forwarding', function() { - test('forwards scope to modify in instance mode', function(done) { - new p5((sketch) => { - sketch.setup = function() { - sketch.createCanvas(10, 10, sketch.WEBGL); - - const scope = { sketch }; - let receivedSketch = null; - - sketch.buildMaterialShader(function({ sketch: s }) { - receivedSketch = s; - }, scope); - - assert.strictEqual(receivedSketch, sketch); - - sketch.remove(); - done(); - }; - }); - }); - }); -});