Skip to content

Commit 83e9ec2

Browse files
committed
refactor(examples): update p5.strands shaders to flat hook API and direct globals
- Switch filter shader to buildFilterShader + filterColor.begin/set/end - Switch wiggle shader to buildColorShader + objectInputs.begin/end; drop time uniform - Switch shader-as-texture to buildMaterialShader + cameraInputs/pixelInputs begin/end - Remove unnecessary uniforms for millis/width/height/mouse and simplify numeric literals Signed-off-by: MissTipo <dorine.a.tipo@gmail.com>
1 parent da3c890 commit 83e9ec2

File tree

3 files changed

+32
-70
lines changed
  • src/content/examples/en
    • 11_3D
      • 07_Filter_Shader_p5strands
      • 08_Adjusting_Positions_With_A_Shader_p5strands
    • 12_Advanced_Canvas_Rendering/03_Shader_As_A_Texture_p5strands

3 files changed

+32
-70
lines changed

src/content/examples/en/11_3D/07_Filter_Shader_p5strands/code.js

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,31 @@ function setup() {
99
video.volume(0);
1010
video.hide();
1111
video.loop();
12-
13-
displaceColors = baseFilterShader().modify(displaceColorsCallback);
14-
12+
displaceColors = buildFilterShader(displaceColorsCallback);
1513
describe(
1614
'A video of a city crosswalk, with colors getting more offset the further from the center they are'
1715
);
1816
}
1917

2018
function displaceColorsCallback() {
21-
22-
// Note: arithmetic on coord operates componentwise (vec2), as in GLSL.
2319
function zoom(coord, amount) {
2420
return (coord - 0.5) / amount + 0.5;
2521
}
26-
27-
getColor((inputs, canvasContent) => {
28-
const uv = inputs.texCoord;
29-
30-
const r = getTexture(canvasContent, uv).r;
31-
const g = getTexture(canvasContent, zoom(uv, 1.05)).g;
32-
const b = getTexture(canvasContent, zoom(uv, 1.1)).b;
33-
const a = getTexture(canvasContent, uv).a;
34-
35-
return [r, g, b, a];
36-
});
22+
filterColor.begin();
23+
const uv = filterColor.texCoord;
24+
const r = getTexture(filterColor.canvasContent, uv).r;
25+
const g = getTexture(filterColor.canvasContent, zoom(uv, 1.05)).g;
26+
const b = getTexture(filterColor.canvasContent, zoom(uv, 1.1)).b;
27+
const a = getTexture(filterColor.canvasContent, uv).a;
28+
filterColor.set([r, g, b, a]);
29+
filterColor.end();
3730
}
3831

3932
function draw() {
4033
background(255);
4134
push();
4235
imageMode(CENTER);
43-
image(
44-
video,
45-
0, 0, width, height,
46-
0, 0, video.width, video.height,
47-
COVER
48-
);
36+
image(video, 0, 0, width, height, 0, 0, video.width, video.height, COVER);
4937
pop();
50-
5138
filter(displaceColors);
5239
}

src/content/examples/en/11_3D/08_Adjusting_Positions_With_A_Shader_p5strands/code.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ let ribbon;
33

44
function setup() {
55
createCanvas(700, 400, WEBGL);
6-
7-
wiggleShader = baseColorShader().modify(wiggleCallback);
6+
wiggleShader = buildColorShader(wiggleCallback);
87

98
let startColor = color('#F55');
109
let endColor = color('#55F');
@@ -22,25 +21,19 @@ function setup() {
2221
}
2322
endShape();
2423
});
25-
2624
describe('A red-to-blue ribbon that waves over time');
2725
}
2826

2927
function wiggleCallback() {
30-
const time = uniformFloat(() => millis());
31-
32-
getObjectInputs((inputs) => {
33-
inputs.position.y += 20 * sin(time * 0.01 + inputs.position.y * 0.1);
34-
return inputs;
35-
});
28+
objectInputs.begin();
29+
objectInputs.position.y += 20 * sin(millis() * 0.01 + objectInputs.position.y * 0.1);
30+
objectInputs.end();
3631
}
3732

3833
function draw() {
3934
background(255);
4035
noStroke();
4136
rotateX(PI * 0.1);
42-
4337
shader(wiggleShader);
44-
4538
model(ribbon);
4639
}

src/content/examples/en/12_Advanced_Canvas_Rendering/03_Shader_As_A_Texture_p5strands/code.js

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,11 @@ function setup() {
44
createCanvas(710, 400, WEBGL);
55
noStroke();
66
angleMode(DEGREES);
7-
8-
// baseMaterialShader is used because it is the only base shader whose
9-
// getPixelInputs hook exposes inputs.texCoord in JS callback form.
10-
// baseColorShader only exposes getFinalColor, which does not include texCoord.
11-
theShader = baseMaterialShader().modify(shaderAsTextureCallback);
12-
7+
theShader = buildMaterialShader(shaderAsTextureCallback);
138
describe('Sphere broken up into a square grid with a gradient in each grid.');
149
}
1510

1611
function shaderAsTextureCallback() {
17-
const time = uniformFloat(() => millis() / 1000.0);
18-
const size = uniformVec2(() => [width, height]);
19-
// Y is flipped to match gl_FragCoord's bottom-up axis.
20-
const mouse = uniformVec2(() => [mouseX, height - mouseY]);
21-
2212
let position = sharedVec3();
2313

2414
function rotate2D(st, angle) {
@@ -37,45 +27,37 @@ function shaderAsTextureCallback() {
3727
return floor(distance(st, radius) * res) / scale;
3828
}
3929

40-
getCameraInputs((inputs) => {
41-
position = inputs.position;
42-
return inputs;
43-
});
44-
45-
getPixelInputs((inputs) => {
46-
const st = inputs.texCoord;
30+
cameraInputs.begin();
31+
position = cameraInputs.position;
32+
cameraInputs.end();
4733

48-
// Shift camera-space position (center origin) to bottom-left origin,
49-
// producing the equivalent of gl_FragCoord.xy
50-
const fragCoord = position.xy + size * 0.5;
51-
const safeMouse = [max(mouse.x, 1.0), max(mouse.y, 1.0)];
52-
const mst = fragCoord / safeMouse;
53-
const mdist = distance([1.0, 1.0], mst);
34+
pixelInputs.begin();
35+
const fragCoord = position.xy + [width, height] * 0.5;
36+
const safeMouse = [max(mouseX, 1), max(height - mouseY, 1)];
37+
const mst = fragCoord / safeMouse;
38+
const mdist = distance([1, 1], mst);
5439

55-
const distToCenter = distance(st, [sin(time / 10.0), cos(time / 10.0)]);
56-
let tiled = tile(st, 10.0);
57-
tiled = rotate2D(tiled, distToCenter / (mdist / 5.0) * PI * 2.0);
40+
const st = pixelInputs.texCoord;
41+
const distToCenter = distance(st, [sin(millis() / 10000), cos(millis() / 10000)]);
42+
let tiled = tile(st, 10);
43+
tiled = rotate2D(tiled, distToCenter / (mdist / 5) * PI * 2);
5844

59-
const r = concentricCircles(tiled, [0.0, 0.0], 5.0, 5.0);
60-
const g = concentricCircles(tiled, [0.0, 0.0], 10.0, 10.0);
61-
const b = concentricCircles(tiled, [0.0, 0.0], 20.0, 10.0);
45+
const r = concentricCircles(tiled, [0, 0], 5, 5);
46+
const g = concentricCircles(tiled, [0, 0], 10, 10);
47+
const b = concentricCircles(tiled, [0, 0], 20, 10);
6248

63-
inputs.color = [r, g, b, 1.0];
64-
return inputs;
65-
});
49+
pixelInputs.color = [r, g, b, 1];
50+
pixelInputs.end();
6651
}
6752

6853
function draw() {
6954
background(255);
70-
7155
shader(theShader);
72-
7356
translate(-150, 0, 0);
7457
push();
7558
rotateX(-mouseY);
7659
rotateY(-mouseX);
7760
sphere(125);
7861
pop();
79-
8062
ellipse(260, 0, 200, 200, 100);
8163
}

0 commit comments

Comments
 (0)