Skip to content

Commit a1277c0

Browse files
committed
test: add regression tests for custom shader with framebuffer depth texture
Adds two tests verifying that custom shaders can correctly sample both color and depth framebuffer textures via setUniform(), which is the pattern used by the Framebuffer Blur example. - custom shader sampling only color texture - custom shader sampling both color and depth textures Both tests confirm the library sets uModelViewMatrix = model x view correctly and that depth textures are readable via uniform sampler2D. Refs: #8940
1 parent 71d062a commit a1277c0

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

‎test/unit/webgl/p5.Framebuffer.js‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,102 @@ suite('p5.Framebuffer', function() {
669669
});
670670
});
671671

672+
suite('custom shader with depth uniform', function() {
673+
test('custom shader sampling only color texture', function() {
674+
myp5.createCanvas(10, 10, myp5.WEBGL);
675+
676+
const fbo = myp5.createFramebuffer();
677+
fbo.draw(() => {
678+
myp5.background(255);
679+
myp5.noStroke();
680+
myp5.fill('red');
681+
myp5.box(5, 5, 5);
682+
});
683+
684+
const vertexShader = `
685+
attribute vec3 aPosition;
686+
attribute vec2 aTexCoord;
687+
uniform mat4 uModelViewMatrix;
688+
uniform mat4 uProjectionMatrix;
689+
varying vec2 vTexCoord;
690+
void main() {
691+
vTexCoord = aTexCoord;
692+
gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aPosition, 1.0);
693+
}
694+
`;
695+
const fragmentShader = `
696+
precision highp float;
697+
varying vec2 vTexCoord;
698+
uniform sampler2D img;
699+
void main() {
700+
gl_FragColor = texture2D(img, vTexCoord);
701+
}
702+
`;
703+
704+
const customShader = myp5.createShader(vertexShader, fragmentShader);
705+
706+
myp5.background(0);
707+
myp5.noStroke();
708+
myp5.shader(customShader);
709+
customShader.setUniform('img', fbo.color);
710+
myp5.plane(myp5.width, -myp5.height);
711+
712+
assert.deepEqual(myp5.get(5, 5), [255, 0, 0, 255]);
713+
});
714+
715+
test('custom shader sampling both color and depth textures', function() {
716+
myp5.createCanvas(10, 10, myp5.WEBGL);
717+
718+
const fbo = myp5.createFramebuffer();
719+
fbo.draw(() => {
720+
myp5.background(255);
721+
myp5.noStroke();
722+
myp5.fill('red');
723+
myp5.box(5, 5, 5);
724+
});
725+
726+
const vertexShader = `
727+
attribute vec3 aPosition;
728+
attribute vec2 aTexCoord;
729+
uniform mat4 uModelViewMatrix;
730+
uniform mat4 uProjectionMatrix;
731+
varying vec2 vTexCoord;
732+
void main() {
733+
vTexCoord = aTexCoord;
734+
gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aPosition, 1.0);
735+
}
736+
`;
737+
const fragmentShader = `
738+
precision highp float;
739+
varying vec2 vTexCoord;
740+
uniform sampler2D img;
741+
uniform sampler2D depth;
742+
void main() {
743+
vec4 color = texture2D(img, vTexCoord);
744+
float d = texture2D(depth, vTexCoord).r;
745+
gl_FragColor = vec4(color.rgb * (1.0 - d), 1.0);
746+
}
747+
`;
748+
749+
const customShader = myp5.createShader(vertexShader, fragmentShader);
750+
751+
myp5.background(0);
752+
myp5.noStroke();
753+
myp5.shader(customShader);
754+
customShader.setUniform('img', fbo.color);
755+
customShader.setUniform('depth', fbo.depth);
756+
myp5.plane(myp5.width, -myp5.height);
757+
758+
// The box is at z=0 with camera at z=800.
759+
// Depth at center should be ~0.91 (232/255).
760+
// Color should be dimmed: 255 * (1 - 0.91) ≈ 23
761+
// Just check it's not full red (not 255) and not zero
762+
const pixel = myp5.get(5, 5);
763+
assert(pixel[0] < 200, 'red should be significantly dimmed by depth');
764+
assert(pixel[0] > 0, 'red should still be visible');
765+
});
766+
});
767+
672768
suite('saveCanvas', function() {
673769
test('should download a png file', async () => {
674770
myp5.createCanvas(100, 100, myp5.WEBGL);

0 commit comments

Comments
 (0)