summaryrefslogtreecommitdiff
path: root/src/vector/shaders
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-09-25 21:47:58 -0300
committerGabriel Schneider <[email protected]>2026-09-25 21:48:16 -0300
commit9c33714591a2147f7e5879e77212ca9886d0c967 (patch)
treed250be9d9bc365d7894d49455c0f6376966b58be /src/vector/shaders
downloadsketchbook-9c33714591a2147f7e5879e77212ca9886d0c967.tar.gz
sketchbook-9c33714591a2147f7e5879e77212ca9886d0c967.zip
Put the game first: lib/sketchbook, sketches/vecanim
The root is the game. The platform library moves to lib/sketchbook (with serve_web.py, which serves any sketch's web build, as a tool of it), and vecanim to sketches/vecanim, still its own package on the same library.
Diffstat (limited to 'src/vector/shaders')
-rw-r--r--src/vector/shaders/cover.frag65
-rw-r--r--src/vector/shaders/path.vert9
-rw-r--r--src/vector/shaders/stencil.frag7
3 files changed, 81 insertions, 0 deletions
diff --git a/src/vector/shaders/cover.frag b/src/vector/shaders/cover.frag
new file mode 100644
index 0000000..a57a916
--- /dev/null
+++ b/src/vector/shaders/cover.frag
@@ -0,0 +1,65 @@
+#version 450
+#extension GL_GOOGLE_include_directive : require
+// Paints the covered pixels. Mirrors vector/Paint.zig: `param` gives the
+// gradient parameter, `stop_color` the color at it. The uniform block is
+// generated from vector/layouts.zig.
+layout(location = 0) out vec4 out_color;
+
+#include "Paint.glsl"
+
+float offset_at(int i) {
+ return paint.offsets[i / 4][i % 4];
+}
+
+vec4 premultiply(vec4 c) {
+ return vec4(c.rgb * c.a, c.a);
+}
+
+float param(vec2 q) {
+ vec2 g = vec2(dot(paint.row0.xyz, vec3(q, 1.0)), dot(paint.row1.xyz, vec3(q, 1.0)));
+ vec2 p0 = paint.points.xy;
+ vec2 p1 = paint.points.zw;
+ float t = 0.0;
+ if (paint.row0.w < 1.5) {
+ vec2 dir = p1 - p0;
+ float len2 = dot(dir, dir);
+ t = len2 > 0.0 ? dot(g - p0, dir) / len2 : 0.0;
+ } else {
+ // Where the ray from the focus through g meets the circle.
+ vec2 dq = g - p1;
+ vec2 cf = p1 - p0;
+ float a = dot(dq, dq);
+ if (a > 0.0) {
+ float b = dot(dq, cf);
+ float c = dot(cf, cf) - paint.extra.x * paint.extra.x;
+ float s = (-b + sqrt(max(b * b - a * c, 0.0))) / a;
+ t = s > 0.0 ? 1.0 / s : 1.0;
+ }
+ }
+ if (paint.row1.w < 0.5) return clamp(t, 0.0, 1.0);
+ if (paint.row1.w < 1.5) {
+ float r = mod(t, 2.0);
+ return r > 1.0 ? 2.0 - r : r;
+ }
+ return t - floor(t);
+}
+
+vec4 stop_color(float t) {
+ int count = int(paint.extra.z);
+ if (count == 0) return vec4(0.0);
+ if (t <= offset_at(0)) return premultiply(paint.colors[0]);
+ for (int i = 1; i < count; i++) {
+ float b = offset_at(i);
+ if (t <= b) {
+ float a = offset_at(i - 1);
+ float u = b > a ? (t - a) / (b - a) : 1.0;
+ return premultiply(mix(paint.colors[i - 1], paint.colors[i], u));
+ }
+ }
+ return premultiply(paint.colors[count - 1]);
+}
+
+void main() {
+ vec4 c = paint.row0.w < 0.5 ? paint.color : stop_color(param(gl_FragCoord.xy));
+ out_color = c * paint.extra.y;
+}
diff --git a/src/vector/shaders/path.vert b/src/vector/shaders/path.vert
new file mode 100644
index 0000000..dbca9f8
--- /dev/null
+++ b/src/vector/shaders/path.vert
@@ -0,0 +1,9 @@
+#version 450
+#extension GL_GOOGLE_include_directive : require
+// Device pixels (y down) to normalized device coordinates (y up).
+#include "Vertex.glsl"
+#include "Viewport.glsl"
+
+void main() {
+ gl_Position = vec4(position.x / viewport.size.x * 2.0 - 1.0, 1.0 - position.y / viewport.size.y * 2.0, 0.0, 1.0);
+}
diff --git a/src/vector/shaders/stencil.frag b/src/vector/shaders/stencil.frag
new file mode 100644
index 0000000..caf85f4
--- /dev/null
+++ b/src/vector/shaders/stencil.frag
@@ -0,0 +1,7 @@
+#version 450
+// Stencil passes write no color; the pipeline masks it out anyway.
+layout(location = 0) out vec4 color;
+
+void main() {
+ color = vec4(0.0);
+}