Skip to content

Commit 126c9a5

Browse files
committedNov 11, 2023
Add slow and fast shaders.
1 parent ddcf700 commit 126c9a5

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
 
File renamed without changes.

‎08-checkerboard/slow-shader.frag

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
precision mediump float;
2+
3+
uniform float u_time;
4+
uniform vec2 u_resolution;
5+
6+
// reference: https://iquilezles.org/articles/distfunctions2d/
7+
float sdBox(in vec2 p, in vec2 b) {
8+
vec2 d = abs(p) - b;
9+
return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0);
10+
}
11+
12+
void main() {
13+
vec2 uv = 2.0 * gl_FragCoord.xy / u_resolution - 1.0;
14+
uv.x = uv.x * u_resolution.x / u_resolution.y;
15+
16+
vec3 grey = vec3(0.5);
17+
vec3 black = vec3(0.0);
18+
vec3 white = vec3(1.0);
19+
20+
vec3 color = grey;
21+
22+
const float halfBoardSize = 4.0;
23+
float boxWidth = 1.0 / (halfBoardSize * 4.0);
24+
vec2 boxDimensions = vec2(boxWidth, boxWidth);
25+
float separationSize = boxWidth + 0.01;
26+
27+
for (float i = -halfBoardSize; i <= halfBoardSize; i++) {
28+
for (float j = -halfBoardSize; j <= halfBoardSize; j++) {
29+
vec2 center = vec2(i, j) * (boxWidth + separationSize);
30+
float distToBox = sdBox(uv - center, boxDimensions);
31+
32+
if (mod(i + j, 2.0) <= 0.01) {
33+
color = distToBox < 0.0 ? white : color;
34+
} else {
35+
color = distToBox < 0.0 ? black : color;
36+
}
37+
}
38+
}
39+
40+
gl_FragColor = vec4(color, 1.0);
41+
}

0 commit comments

Comments
 (0)
Please sign in to comment.