Skip to content

Commit 9e0b550

Browse files
authored
Merge pull request #8523 from aashu2006/fix-build-shader-instance-mode
Fix build*Shader methods in instance mode by forwarding optional scope
2 parents 1ca3781 + bec8391 commit 9e0b550

2 files changed

Lines changed: 35 additions & 10 deletions

File tree

src/webgl/material.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -732,15 +732,17 @@ function material(p5, fn) {
732732
* @beta
733733
* @submodule p5.strands
734734
* @param {Function} callback A function building a p5.strands shader.
735+
* @param {Object} [scope] An optional scope object passed to .modify().
735736
* @returns {p5.Shader} The material shader
736737
*/
737738
/**
738739
* @method buildFilterShader
739740
* @param {Object} hooks An object specifying p5.strands hooks in GLSL.
741+
* @param {Object} [scope] An optional scope object passed to .modify().
740742
* @returns {p5.Shader} The material shader
741743
*/
742-
fn.buildFilterShader = function (callback) {
743-
return this.baseFilterShader().modify(callback);
744+
fn.buildFilterShader = function (callback, scope) {
745+
return this.baseFilterShader().modify(callback, scope);
744746
};
745747

746748
/**
@@ -1560,15 +1562,17 @@ function material(p5, fn) {
15601562
* @submodule p5.strands
15611563
* @beta
15621564
* @param {Function} callback A function building a p5.strands shader.
1565+
* @param {Object} [scope] An optional scope object passed to .modify().
15631566
* @returns {p5.Shader} The material shader.
15641567
*/
15651568
/**
15661569
* @method buildMaterialShader
15671570
* @param {Object} hooks An object specifying p5.strands hooks in GLSL.
1571+
* @param {Object} [scope] An optional scope object passed to .modify().
15681572
* @returns {p5.Shader} The material shader.
15691573
*/
1570-
fn.buildMaterialShader = function (cb) {
1571-
return this.baseMaterialShader().modify(cb);
1574+
fn.buildMaterialShader = function (cb, scope) {
1575+
return this.baseMaterialShader().modify(cb, scope);
15721576
};
15731577

15741578
/**
@@ -1776,15 +1780,17 @@ function material(p5, fn) {
17761780
* @submodule p5.strands
17771781
* @beta
17781782
* @param {Function} callback A function building a p5.strands shader.
1783+
* @param {Object} [scope] An optional scope object passed to .modify().
17791784
* @returns {p5.Shader} The normal shader.
17801785
*/
17811786
/**
17821787
* @method buildNormalShader
17831788
* @param {Object} hooks An object specifying p5.strands hooks in GLSL.
1789+
* @param {Object} [scope] An optional scope object passed to .modify().
17841790
* @returns {p5.Shader} The normal shader.
17851791
*/
1786-
fn.buildNormalShader = function (cb) {
1787-
return this.baseNormalShader().modify(cb);
1792+
fn.buildNormalShader = function (cb, scope) {
1793+
return this.baseNormalShader().modify(cb, scope);
17881794
};
17891795

17901796
/**
@@ -1940,15 +1946,17 @@ function material(p5, fn) {
19401946
* @submodule p5.strands
19411947
* @beta
19421948
* @param {Function} callback A function building a p5.strands shader.
1949+
* @param {Object} [scope] An optional scope object passed to .modify().
19431950
* @returns {p5.Shader} The color shader.
19441951
*/
19451952
/**
19461953
* @method buildColorShader
19471954
* @param {Object} hooks An object specifying p5.strands hooks in GLSL.
1955+
* @param {Object} [scope] An optional scope object passed to .modify().
19481956
* @returns {p5.Shader} The color shader.
19491957
*/
1950-
fn.buildColorShader = function (cb) {
1951-
return this.baseColorShader().modify(cb);
1958+
fn.buildColorShader = function (cb, scope) {
1959+
return this.baseColorShader().modify(cb, scope);
19521960
};
19531961

19541962
/**
@@ -2195,15 +2203,17 @@ function material(p5, fn) {
21952203
* @submodule p5.strands
21962204
* @beta
21972205
* @param {Function} callback A function building a p5.strands shader.
2206+
* @param {Object} [scope] An optional scope object passed to .modify().
21982207
* @returns {p5.Shader} The stroke shader.
21992208
*/
22002209
/**
22012210
* @method buildStrokeShader
22022211
* @param {Object} hooks An object specifying p5.strands hooks in GLSL.
2212+
* @param {Object} [scope] An optional scope object passed to .modify().
22032213
* @returns {p5.Shader} The stroke shader.
22042214
*/
2205-
fn.buildStrokeShader = function (cb) {
2206-
return this.baseStrokeShader().modify(cb);
2215+
fn.buildStrokeShader = function (cb, scope) {
2216+
return this.baseStrokeShader().modify(cb, scope);
22072217
};
22082218

22092219
/**

test/unit/webgl/p5.Shader.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,21 @@ suite('p5.Shader', function() {
459459
}).not.toThrowError();
460460
});
461461

462+
test('buildMaterialShader forwards scope to modify', () => {
463+
myp5.createCanvas(5, 5, myp5.WEBGL);
464+
expect(() => {
465+
const myShader = myp5.buildMaterialShader(() => {
466+
myp5.getPixelInputs(inputs => {
467+
inputs.color = [1, 0, 0, 1];
468+
return inputs;
469+
});
470+
}, { myp5 });
471+
myp5.noStroke();
472+
myp5.shader(myShader);
473+
myp5.plane(myp5.width, myp5.height);
474+
}).not.toThrowError();
475+
});
476+
462477
test('returns numbers for builtin globals outside hooks and a strandNode when called inside hooks', () => {
463478
myp5.createCanvas(5, 5, myp5.WEBGL);
464479
myp5.baseMaterialShader().modify(() => {

0 commit comments

Comments
 (0)