diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-16 21:28:20 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-16 21:28:20 -0300 |
| commit | 6933f99ddb2d7103515d26838496309a807bba26 (patch) | |
| tree | 991c2fe7d4a57957222876e190fa4d16a0fb889c /examples/noc/src/processing_noise.zig | |
| parent | 82fca5c9f29124f37f6dcd08e73e6999ce44a69d (diff) | |
| download | sketchbook-6933f99ddb2d7103515d26838496309a807bba26.tar.gz sketchbook-6933f99ddb2d7103515d26838496309a807bba26.zip | |
Move standalone sketches into sketchbook examples
Diffstat (limited to 'examples/noc/src/processing_noise.zig')
| -rw-r--r-- | examples/noc/src/processing_noise.zig | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/examples/noc/src/processing_noise.zig b/examples/noc/src/processing_noise.zig new file mode 100644 index 0000000..e5606e6 --- /dev/null +++ b/examples/noc/src/processing_noise.zig @@ -0,0 +1,86 @@ +//! Processing's own `noise()` (PApplet.noise), for ports that must match the +//! book's pictures: its 4096-entry table filled by java.util.Random seeded +//! with `seed`, as `noiseSeed(0)` would, and its half-degree cosine fade. +//! Four octaves, falloff 0.5, like Processing's defaults. Otherwise prefer +//! `sk.math.noise` (table-free, any seed, same on every host). +const std = @import("std"); + +const seed: u64 = 0; +const perlin_size = 4095; +const perlin: [perlin_size + 1]f32 = blk: { + @setEvalBranchQuota(100_000); + const mask: u64 = (1 << 48) - 1; + var s: u64 = (seed ^ 0x5DEECE66D) & mask; + var table: [perlin_size + 1]f32 = undefined; + for (&table) |*v| { + s = (s *% 0x5DEECE66D +% 0xB) & mask; + v.* = @as(f32, @floatFromInt(s >> 24)) / (1 << 24); + } + break :blk table; +}; +/// Processing's cosine fade, through its half-degree cos table. +fn fade(i: f32) f32 { + const k: f32 = @floor(i * 360); + return 0.5 * (1 - @cos(@mod(k, 720) * std.math.pi / 360)); +} + +pub fn noise(x_in: f32, y_in: f32, z_in: f32) f32 { + const ywrapb = 4; + const ywrap = 1 << ywrapb; + const zwrapb = 8; + const zwrap = 1 << zwrapb; + const x = @abs(x_in); + const y = @abs(y_in); + const z = @abs(z_in); + var xi: i32 = @intFromFloat(x); + var yi: i32 = @intFromFloat(y); + var zi: i32 = @intFromFloat(z); + var xf = x - @as(f32, @floatFromInt(xi)); + var yf = y - @as(f32, @floatFromInt(yi)); + var zf = z - @as(f32, @floatFromInt(zi)); + var r: f32 = 0; + var ampl: f32 = 0.5; + const p = struct { + fn at(i: i32) f32 { + return perlin[@intCast(i & perlin_size)]; + } + }.at; + for (0..4) |_| { + var of = xi + (yi << ywrapb) + (zi << zwrapb); + const rxf = fade(xf); + const ryf = fade(yf); + var n1 = p(of); + n1 += rxf * (p(of + 1) - n1); + var n2 = p(of + ywrap); + n2 += rxf * (p(of + ywrap + 1) - n2); + n1 += ryf * (n2 - n1); + of += zwrap; + n2 = p(of); + n2 += rxf * (p(of + 1) - n2); + var n3 = p(of + ywrap); + n3 += rxf * (p(of + ywrap + 1) - n3); + n2 += ryf * (n3 - n2); + n1 += fade(zf) * (n2 - n1); + r += n1 * ampl; + ampl *= 0.5; + xi <<= 1; + xf *= 2; + yi <<= 1; + yf *= 2; + zi <<= 1; + zf *= 2; + if (xf >= 1) { + xi += 1; + xf -= 1; + } + if (yf >= 1) { + yi += 1; + yf -= 1; + } + if (zf >= 1) { + zi += 1; + zf -= 1; + } + } + return r; +} |
