summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 07:59:59 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitfb237529724ae1c01b15f99e3b25bdce7c77cf82 (patch)
tree5402d604d88b6b6fb0f2ef4f1eba673bfb955b12 /examples
parent4350e6840940da0b7af800faf5a1455a1de1908e (diff)
downloadsketchbook-fb237529724ae1c01b15f99e3b25bdce7c77cf82.tar.gz
sketchbook-fb237529724ae1c01b15f99e3b25bdce7c77cf82.zip
examples/live: square units, x and y from the centre in half the height
picture.frag gives the tape x and y in half the target's height, 0 at its centre and y up (`y` -1 to 1, `x` -1.78 to 1.78 on 16:9), so a circle is round on any window, half resolution included. Lang.builtins and the README's table say so; the README's program draws its ring about the centre (`r = sqrt(x * x + y * y)`, the ring's radius and width doubled into the new unit). Test: test/live.script replaces the text (`%`, `c`) with a disc of radius 0.5, `step(x * x + y * y, 0.25)`, and reads its edge 180 pixels from the centre across and up: white at 170, black at 190, each way. zig build test: 85 of 85 steps. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'examples')
-rw-r--r--examples/live/README.md6
-rw-r--r--examples/live/shaders/picture.frag5
-rw-r--r--examples/live/src/Lang.zig4
-rw-r--r--examples/live/test/live.script15
4 files changed, 23 insertions, 7 deletions
diff --git a/examples/live/README.md b/examples/live/README.md
index 3c0c885..e69b583 100644
--- a/examples/live/README.md
+++ b/examples/live/README.md
@@ -14,8 +14,8 @@ beat = t * bpm / 60
kick = exp(-7 * fract(beat))
thump = sin(tau * 48 * t * (1 + 2 * kick)) * kick * 0.5
out = thump
-r = sqrt((x - 0.5) * (x - 0.5) + (y - 0.5) * (y - 0.5))
-color = hsv(fract(beat / 16 + r), 0.6, smoothstep(0.03, 0, abs(r - 0.2 - 0.1 * kick)))
+r = sqrt(x * x + y * y)
+color = hsv(fract(beat / 16 + r), 0.6, smoothstep(0.06, 0, abs(r - 0.4 - 0.2 * kick)))
```
The picture pulses with the kick because both lines say `kick`.
@@ -43,7 +43,7 @@ calls; `#` starts a comment. The builtins (src/Lang.zig's one table):
| | |
| --- | --- |
| `t` | seconds |
-| `x`, `y` | 0..1, left to right and bottom to top; 0 in sound |
+| `x`, `y` | across and up from the centre, in half the window's height (`y` -1 to 1, `x` -1.78 to 1.78 on 16:9: a circle is round); 0 in sound |
| `tau`, `pi` | a turn and half a turn, in radians |
| `sin(a)`, `cos(a)` | of radians |
| `exp(a)`, `log(a)`, `pow(a, b)`, `sqrt(a)` | e to the a, its inverse, a to the b (a not negative), square root |
diff --git a/examples/live/shaders/picture.frag b/examples/live/shaders/picture.frag
index ec1293e..4621b31 100644
--- a/examples/live/shaders/picture.frag
+++ b/examples/live/shaders/picture.frag
@@ -1,6 +1,7 @@
#version 450
#extension GL_GOOGLE_include_directive : require
-// The color tape at every pixel; x and y run 0..1, y up.
+// The color tape at every pixel; x and y in half the target's height, 0 at
+// its centre, y up: a circle is round on any window.
#include "Picture.glsl"
#define TAPE(i) u.tape[i]
#include "vm.glsl"
@@ -8,6 +9,6 @@
layout(location = 0) out vec4 out_color;
void main() {
- vec2 p = vec2(gl_FragCoord.x, u.size.y - gl_FragCoord.y) / u.size;
+ vec2 p = vec2(gl_FragCoord.x - 0.5 * u.size.x, 0.5 * u.size.y - gl_FragCoord.y) / (0.5 * u.size.y);
out_color = vec4(clamp(vm(0u, u.len, u.time, p.x, p.y), 0.0, 1.0), 1.0);
}
diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig
index 6ad1f40..4ce504e 100644
--- a/examples/live/src/Lang.zig
+++ b/examples/live/src/Lang.zig
@@ -92,8 +92,8 @@ pub const Builtin = struct {
/// switch, and only for a program that needs it.
pub const builtins = [_]Builtin{
.{ .call = "t", .op = .t, .doc = "seconds" },
- .{ .call = "x", .op = .x, .doc = "0 at the left to 1 at the right; 0 in sound" },
- .{ .call = "y", .op = .y, .doc = "0 at the bottom to 1 at the top; 0 in sound" },
+ .{ .call = "x", .op = .x, .doc = "across, in half the window's height: 0 at the centre, -1.78 to 1.78 on 16:9; 0 in sound" },
+ .{ .call = "y", .op = .y, .doc = "up, in half the window's height: -1 at the bottom, 0 at the centre, 1 at the top; 0 in sound" },
.{ .call = "tau", .op = .constant, .value = std.math.tau, .doc = "a turn in radians, 6.283..." },
.{ .call = "pi", .op = .constant, .value = std.math.pi, .doc = "half a turn, 3.141..." },
.{ .call = "sin(a)", .op = .sin, .min = 1, .max = 1, .doc = "sine, of radians" },
diff --git a/examples/live/test/live.script b/examples/live/test/live.script
index bedcf83..1ddcec2 100644
--- a/examples/live/test/live.script
+++ b/examples/live/test/live.script
@@ -94,3 +94,18 @@ expect out.code[1].a 1129906176
up 169 27
type u
expect out.code[1].a 1130102784
+# Square units: a disc of radius 0.5 (half the window's height is 1) is
+# 180 pixels across each way from the centre, round on 16:9.
+type %
+type c
+type color = step(x * x + y * y, 0.25)
+key escape
+expect color.len 7
+frames 2
+screenshot .zig-cache/live/disc.png
+expect-pixel .zig-cache/live/disc.png 810 360 255 255 255 255 2
+expect-pixel .zig-cache/live/disc.png 640 190 255 255 255 255 2
+expect-pixel .zig-cache/live/disc.png 470 360 255 255 255 255 2
+expect-pixel .zig-cache/live/disc.png 830 360 0 0 0 255 2
+expect-pixel .zig-cache/live/disc.png 640 170 0 0 0 255 2
+expect-pixel .zig-cache/live/disc.png 640 550 0 0 0 255 2