Skip to content

Commit 382e3ef

Browse files
committed
fix uSampler override: simplify with user-shader check
Apply review feedback: instead of tracking _userSetUniforms with _isInternalSetUniform flag, only override uSampler when shader is built-in or texture is active. Removes _userSetUniforms Set, _isInternalSetUniform flag, and per-frame clearing in _update().
1 parent 012b46e commit 382e3ef

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

src/core/p5.Renderer3D.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,12 @@ export class Renderer3D extends Renderer {
15011501
// the next time a shader is used. However, the texture() function
15021502
// works differently and is global p5 state. If the p5 state has
15031503
// been cleared, we also need to clear the value in uSampler to match.
1504-
fillShader.setUniform("uSampler", this.states._tex || empty);
1504+
const isUserFillShader =
1505+
fillShader === this.states.userFillShader ||
1506+
fillShader === this.states.userImageShader;
1507+
if (this.states._tex || !isUserFillShader) {
1508+
fillShader.setUniform("uSampler", this.states._tex || empty);
1509+
}
15051510
fillShader.setUniform(
15061511
"uTint",
15071512
this.states.tint?._getRGBA([255, 255, 255, 255]) ?? [255, 255, 255, 255]

test/unit/visual/cases/webgl.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,4 +1754,28 @@ visualTest('randomGaussian() in a fragment loop averages to the mean', (p5, scre
17541754
screenshot();
17551755
});
17561756
});
1757+
1758+
visualTest(
1759+
'user-set uSampler on custom shader is not overridden',
1760+
function(p5, screenshot) {
1761+
p5.createCanvas(50, 50, p5.WEBGL);
1762+
1763+
const myShader = p5.createFilterShader(`precision highp float;
1764+
uniform sampler2D uSampler;
1765+
varying vec2 vTexCoord;
1766+
void main() {
1767+
gl_FragColor = texture2D(uSampler, vTexCoord);
1768+
}`);
1769+
1770+
const fbo = p5.createFramebuffer();
1771+
fbo.draw(() => p5.background(255, 0, 0));
1772+
1773+
p5.shader(myShader);
1774+
p5.noStroke();
1775+
myShader.setUniform('uSampler', fbo);
1776+
p5.plane(p5.width, p5.height);
1777+
1778+
screenshot();
1779+
}
1780+
);
17571781
});

test/unit/webgl/p5.RendererGL.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,29 @@ suite('p5.RendererGL', function() {
208208
expect(uSampler.texture.isFramebufferTexture).toBeFalsy();
209209
myp5.pop();
210210
});
211+
212+
test('user-set uSampler on custom shader is not overridden', function() {
213+
myp5.createCanvas(10, 10, myp5.WEBGL);
214+
215+
const myShader = myp5.createFilterShader(`precision highp float;
216+
uniform sampler2D uSampler;
217+
varying vec2 vTexCoord;
218+
void main() {
219+
gl_FragColor = texture2D(uSampler, vTexCoord);
220+
}`);
221+
222+
const fbo = myp5.createFramebuffer();
223+
fbo.draw(() => myp5.background('red'));
224+
225+
myp5.shader(myShader);
226+
myp5.noStroke();
227+
myShader.setUniform('uSampler', fbo);
228+
229+
myp5.plane(myp5.width, myp5.height);
230+
231+
const pixel = myp5.get(5, 5);
232+
assert.deepEqual(pixel, [255, 0, 0, 255]);
233+
});
211234
});
212235

213236
suite('default stroke shader', function() {

0 commit comments

Comments
 (0)