Skip to content

Commit 8d7d96f

Browse files
committed
Add test for shared* usage
1 parent b77cb58 commit 8d7d96f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

test/unit/webgl/p5.Shader.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,39 @@ suite('p5.Shader', function() {
11951195
assert.approximately(centerColor[1], 0, 5); // Green component
11961196
assert.approximately(centerColor[2], 255, 5); // Blue component
11971197
});
1198+
1199+
test('handle passing a value from a vertex hook to a fragment hook using shared*', () => {
1200+
myp5.createCanvas(50, 50, myp5.WEBGL);
1201+
myp5.pixelDensity(1);
1202+
1203+
const testShader = myp5.baseMaterialShader().modify(() => {
1204+
let worldPos = myp5.sharedVec3();
1205+
myp5.getWorldInputs((inputs) => {
1206+
worldPos = inputs.position.xyz;
1207+
return inputs;
1208+
});
1209+
myp5.getFinalColor((c) => {
1210+
return [myp5.abs(worldPos / 25), 1];
1211+
});
1212+
}, { myp5 });
1213+
1214+
myp5.background(0, 0, 255); // Make the background blue to tell it apart
1215+
myp5.noStroke();
1216+
myp5.shader(testShader);
1217+
myp5.plane(myp5.width, myp5.height);
1218+
1219+
// The middle should have position 0,0 which translates to black
1220+
const midColor = myp5.get(25, 25);
1221+
assert.approximately(midColor[0], 0, 5);
1222+
assert.approximately(midColor[1], 0, 5);
1223+
assert.approximately(midColor[2], 0, 5);
1224+
1225+
// The corner should have position 1,1 which translates to yellow
1226+
const cornerColor = myp5.get(0, 0);
1227+
assert.approximately(cornerColor[0], 255, 5);
1228+
assert.approximately(cornerColor[1], 255, 5);
1229+
assert.approximately(cornerColor[2], 0, 5);
1230+
});
11981231
});
11991232

12001233
suite('filter shader hooks', () => {

0 commit comments

Comments
 (0)