Skip to content

Commit da3c890

Browse files
committed
feat(examples): add p5.strands translations for shader examples
- rename original GLSL examples to preserve them alongside the new versions - add p5.strands translations for Filter Shader, Adjusting Positions With A Shader, and Shader As A Texture - remix example descriptions to reflect the new p5.strands versions Signed-off-by: MissTipo <dorine.a.tipo@gmail.com>
1 parent 42dd3a2 commit da3c890

File tree

9 files changed

+282
-6
lines changed

9 files changed

+282
-6
lines changed

src/content/examples/en/11_3D/04_Filter_Shader/description.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
featuredImage: "../../../images/featured/11_3D-04_Filter_Shader-thumbnail.png"
33
featuredImageAlt: A screenshot of a video of a city crosswalk, with offset colors.
4-
title: Filter Shader
4+
title: Filter Shader (GLSL)
55
oneLineDescription: Manipulate imagery with a shader.
66

77
remix:
@@ -15,4 +15,4 @@ remix:
1515
---
1616

1717
Filter shaders are a way to apply an effect to anything that
18-
is on the canvas. In this example, the effect is applied to a video.
18+
is on the canvas. In this example, the effect is applied to a video.

src/content/examples/en/11_3D/05_Adjusting_Positions_With_A_Shader/description.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
featuredImage: "../../../images/featured/11_3D-05_Adjusting_Positions_With_A_Shader-thumbnail.png"
33
featuredImageAlt: A red-to-blue waving ribbon on a white background.
4-
title: Adjusting Positions with a Shader
4+
title: Adjusting Positions with a Shader (GLSL)
55
oneLineDescription: Use a shader to adjust shape vertices.
66

77
remix:
@@ -15,4 +15,4 @@ remix:
1515
---
1616

1717
Shaders can adjust the positions of the vertices of shapes.
18-
This lets you distort and animate 3D models.
18+
This lets you distort and animate 3D models.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
let video;
2+
let displaceColors;
3+
4+
function setup() {
5+
createCanvas(700, 400, WEBGL);
6+
video = createVideo(
7+
'https://upload.wikimedia.org/wikipedia/commons/d/d2/DiagonalCrosswalkYongeDundas.webm'
8+
);
9+
video.volume(0);
10+
video.hide();
11+
video.loop();
12+
13+
displaceColors = baseFilterShader().modify(displaceColorsCallback);
14+
15+
describe(
16+
'A video of a city crosswalk, with colors getting more offset the further from the center they are'
17+
);
18+
}
19+
20+
function displaceColorsCallback() {
21+
22+
// Note: arithmetic on coord operates componentwise (vec2), as in GLSL.
23+
function zoom(coord, amount) {
24+
return (coord - 0.5) / amount + 0.5;
25+
}
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+
});
37+
}
38+
39+
function draw() {
40+
background(255);
41+
push();
42+
imageMode(CENTER);
43+
image(
44+
video,
45+
0, 0, width, height,
46+
0, 0, video.width, video.height,
47+
COVER
48+
);
49+
pop();
50+
51+
filter(displaceColors);
52+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
featuredImage: "../../../images/featured/11_3D-04_Filter_Shader-thumbnail.png"
3+
featuredImageAlt: A screenshot of a video of a city crosswalk, with offset colors.
4+
title: Filter Shader (p5.strands)
5+
oneLineDescription: Manipulate imagery with a shader, written in p5.strands.
6+
7+
remix:
8+
- description: Created by
9+
attribution:
10+
- name: Dave Pagurek
11+
URL: https://www.davepagurek.com/
12+
code:
13+
- label: 2023 code
14+
URL: https://github.com/processing/p5.js-example/tree/main/examples/11_3D/04_Filter_Shader
15+
16+
- description: Remixed by
17+
attribution:
18+
- name: Dorine Tipo
19+
URL: https://dorinetipo.vercel.app/
20+
code:
21+
- label: 2026 p5.strands translation
22+
URL: https://github.com/processing/p5.js-website/tree/main/src/content/examples/en/11_3D/07_Filter_Shader_p5strands
23+
24+
- collectivelyAttributedSince: 2024
25+
---
26+
27+
This is a p5.strands version of the Filter Shader example. Filter shaders apply
28+
an effect to anything that is on the canvas. In this example, the effect is applied to
29+
a video.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
let wiggleShader;
2+
let ribbon;
3+
4+
function setup() {
5+
createCanvas(700, 400, WEBGL);
6+
7+
wiggleShader = baseColorShader().modify(wiggleCallback);
8+
9+
let startColor = color('#F55');
10+
let endColor = color('#55F');
11+
ribbon = buildGeometry(() => {
12+
noStroke();
13+
beginShape(QUAD_STRIP);
14+
let numPoints = 50;
15+
for (let currentPoint = 0; currentPoint < numPoints; currentPoint++) {
16+
let x = map(currentPoint, 0, numPoints - 1, -width / 3, width / 3);
17+
let y = map(currentPoint, 0, numPoints - 1, -height / 3, height / 3);
18+
fill(lerpColor(startColor, endColor, currentPoint / (numPoints - 1)));
19+
for (let z of [-50, 50]) {
20+
vertex(x, y, z);
21+
}
22+
}
23+
endShape();
24+
});
25+
26+
describe('A red-to-blue ribbon that waves over time');
27+
}
28+
29+
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+
});
36+
}
37+
38+
function draw() {
39+
background(255);
40+
noStroke();
41+
rotateX(PI * 0.1);
42+
43+
shader(wiggleShader);
44+
45+
model(ribbon);
46+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
featuredImage: "../../../images/featured/11_3D-05_Adjusting_Positions_With_A_Shader-thumbnail.png"
3+
featuredImageAlt: A red-to-blue waving ribbon on a white background.
4+
title: Adjusting Positions with a Shader (p5.strands)
5+
oneLineDescription: Use a shader to adjust shape vertices, written in p5.strands.
6+
7+
remix:
8+
- description: Created by
9+
attribution:
10+
- name: Dave Pagurek
11+
URL: https://www.davepagurek.com/
12+
code:
13+
- label: 2023 code
14+
URL: https://github.com/processing/p5.js-example/tree/main/examples/11_3D/05_Adjusting_Positions_With_A_Shader
15+
16+
- description: Remixed by
17+
attribution:
18+
- name: Dorine Tipo
19+
URL: https://dorinetipo.vercel.app/
20+
code:
21+
- label: 2026 p5.strands translation
22+
URL: https://github.com/processing/p5.js-website/tree/main/src/content/examples/en/11_3D/08_Adjusting_Positions_With_A_Shader_p5strands
23+
24+
- collectivelyAttributedSince: 2024
25+
---
26+
27+
This is a p5.strands version of the Adjusting Positions with a Shader example.
28+
Shaders can adjust the positions of the vertices of shapes.
29+
This lets you distort and animate 3D models.

src/content/examples/en/12_Advanced_Canvas_Rendering/02_Shader_As_A_Texture/description.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
featuredImage: "../../../images/featured/12_Advanced_Canvas_Rendering-02_Shader_As_A_Texture-thumbnail.png"
33
featuredImageAlt: Two spheres broken up into a square grid with a gradient in each grid.
4-
title: Shader as a Texture
4+
title: Shader as a Texture(GLSL)
55
oneLineDescription: Generate a texture for a 3D shape using a shader.
66

77
remix:
@@ -19,4 +19,4 @@ remix:
1919
Shaders can be applied to 2D/3D shapes as textures.
2020

2121
To learn more about using shaders in p5.js:
22-
<a href="https://itp-xstory.github.io/p5js-shaders/">p5.js Shaders</a>
22+
<a href="https://itp-xstory.github.io/p5js-shaders/">p5.js Shaders</a>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
let theShader;
2+
3+
function setup() {
4+
createCanvas(710, 400, WEBGL);
5+
noStroke();
6+
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+
13+
describe('Sphere broken up into a square grid with a gradient in each grid.');
14+
}
15+
16+
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+
22+
let position = sharedVec3();
23+
24+
function rotate2D(st, angle) {
25+
const c = cos(angle);
26+
const s = sin(angle);
27+
const x = st.x - 0.5;
28+
const y = st.y - 0.5;
29+
return [c * x - s * y + 0.5, s * x + c * y + 0.5];
30+
}
31+
32+
function tile(st, zoom) {
33+
return fract(st * zoom);
34+
}
35+
36+
function concentricCircles(st, radius, res, scale) {
37+
return floor(distance(st, radius) * res) / scale;
38+
}
39+
40+
getCameraInputs((inputs) => {
41+
position = inputs.position;
42+
return inputs;
43+
});
44+
45+
getPixelInputs((inputs) => {
46+
const st = inputs.texCoord;
47+
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);
54+
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);
58+
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);
62+
63+
inputs.color = [r, g, b, 1.0];
64+
return inputs;
65+
});
66+
}
67+
68+
function draw() {
69+
background(255);
70+
71+
shader(theShader);
72+
73+
translate(-150, 0, 0);
74+
push();
75+
rotateX(-mouseY);
76+
rotateY(-mouseX);
77+
sphere(125);
78+
pop();
79+
80+
ellipse(260, 0, 200, 200, 100);
81+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
featuredImage: "../../../images/featured/12_Advanced_Canvas_Rendering-02_Shader_As_A_Texture-thumbnail.png"
3+
featuredImageAlt: Two spheres broken up into a square grid with a gradient in each grid.
4+
title: Shader as a Texture (p5.strands)
5+
oneLineDescription: Generate a texture for a 3D shape using a shader, written in p5.strands.
6+
7+
remix:
8+
- description: Based on
9+
attribution:
10+
- name: p5.js Contributors
11+
URL: https://github.com/processing/p5.js-website-legacy/blob/main/src/data/examples/en/20_3D/09_shader_as_a_texture.js
12+
code:
13+
- label: pre-2023 code
14+
URL: https://github.com/processing/p5.js-website-legacy/blob/main/src/data/examples/en/20_3D/09_shader_as_a_texture.js
15+
16+
- description: Revised by
17+
attribution:
18+
- name: Caleb Foss
19+
URL: https://github.com/calebfoss
20+
code:
21+
- label: 2023 code
22+
URL: https://github.com/processing/p5.js-example/tree/main/examples/12_Advanced_Canvas_Rendering/02_Shader_As_A_Texture
23+
24+
- description: Remixed by
25+
attribution:
26+
- name: Dorine Tipo
27+
URL: https://dorinetipo.vercel.app/
28+
code:
29+
- label: 2026 p5.strands translation
30+
URL: https://github.com/processing/p5.js-website/tree/main/src/content/examples/en/12_Advanced_Canvas_Rendering/03_Shader_As_A_Texture_p5strands
31+
32+
- collectivelyAttributedSince: 2024
33+
---
34+
35+
This is a p5.strands version of the Shader as a Texture example.
36+
Shaders can be applied to 2D/3D shapes as textures.
37+
38+
To learn more about using shaders in p5.js:
39+
<a href="https://itp-xstory.github.io/p5js-shaders/">p5.js Shaders</a>

0 commit comments

Comments
 (0)