diff --git a/src/webgl/material.js b/src/webgl/material.js index 32d0c86d3f..c3dff5b766 100644 --- a/src/webgl/material.js +++ b/src/webgl/material.js @@ -732,15 +732,17 @@ 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) { - return this.baseFilterShader().modify(callback); + fn.buildFilterShader = function (callback, scope) { + return this.baseFilterShader().modify(callback, scope); }; /** @@ -1560,15 +1562,17 @@ 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) { - return this.baseMaterialShader().modify(cb); + fn.buildMaterialShader = function (cb, scope) { + return this.baseMaterialShader().modify(cb, scope); }; /** @@ -1776,15 +1780,17 @@ 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) { - return this.baseNormalShader().modify(cb); + fn.buildNormalShader = function (cb, scope) { + return this.baseNormalShader().modify(cb, scope); }; /** @@ -1940,15 +1946,17 @@ 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) { - return this.baseColorShader().modify(cb); + fn.buildColorShader = function (cb, scope) { + return this.baseColorShader().modify(cb, scope); }; /** @@ -2195,15 +2203,17 @@ 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) { - return this.baseStrokeShader().modify(cb); + fn.buildStrokeShader = function (cb, scope) { + return this.baseStrokeShader().modify(cb, scope); }; /** 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(() => {