Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/core/p5.Renderer3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,11 @@ export class Renderer3D extends Renderer {
// the next time a shader is used. However, the texture() function
// works differently and is global p5 state. If the p5 state has
// been cleared, we also need to clear the value in uSampler to match.
fillShader.setUniform("uSampler", this.states._tex || empty);
this._settingFillUniforms = true;
if (this.states._tex || !fillShader._userSetSampler) {
fillShader.setUniform("uSampler", this.states._tex || empty);
}
this._settingFillUniforms = false;
fillShader.setUniform(
"uTint",
this.states.tint?._getRGBA([255, 255, 255, 255]) ?? [255, 255, 255, 255]
Expand Down
4 changes: 4 additions & 0 deletions src/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,10 @@ class Shader {
return;
}

if (uniformName === 'uSampler' && !this._renderer._settingFillUniforms) {
this._userSetSampler = true;
}
Comment thread
BHARATH0153 marked this conversation as resolved.

// In p5.strands-related code, where some of the code may be in
// p5.webgpu.js instead of the main p5.js build, we generally use
// duck typing instead of instanceof to avoid accidentally importing
Expand Down
24 changes: 24 additions & 0 deletions test/unit/visual/cases/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1754,4 +1754,28 @@ visualTest('randomGaussian() in a fragment loop averages to the mean', (p5, scre
screenshot();
});
});

visualTest(
'user-set uSampler on custom shader is not overridden',
function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);

const myShader = p5.createFilterShader(`precision highp float;
uniform sampler2D uSampler;
varying vec2 vTexCoord;
void main() {
gl_FragColor = texture2D(uSampler, vTexCoord);
}`);

const fbo = p5.createFramebuffer();
fbo.draw(() => p5.background(255, 0, 0));

p5.shader(myShader);
p5.noStroke();
myShader.setUniform('uSampler', fbo);
p5.plane(p5.width, p5.height);

screenshot();
}
);
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}
23 changes: 23 additions & 0 deletions test/unit/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,29 @@ suite('p5.RendererGL', function() {
expect(uSampler.texture.isFramebufferTexture).toBeFalsy();
myp5.pop();
});

test('user-set uSampler on custom shader is not overridden', function() {
myp5.createCanvas(10, 10, myp5.WEBGL);

const myShader = myp5.createFilterShader(`precision highp float;
uniform sampler2D uSampler;
varying vec2 vTexCoord;
void main() {
gl_FragColor = texture2D(uSampler, vTexCoord);
}`);

const fbo = myp5.createFramebuffer();
fbo.draw(() => myp5.background('red'));

myp5.shader(myShader);
myp5.noStroke();
myShader.setUniform('uSampler', fbo);

myp5.plane(myp5.width, myp5.height);

const pixel = myp5.get(5, 5);
assert.deepEqual(pixel, [255, 0, 0, 255]);
});
});

suite('default stroke shader', function() {
Expand Down
Loading