Skip to content

Commit

Permalink
examples: add perlin noise
Browse files Browse the repository at this point in the history
  • Loading branch information
arrufat committed Oct 22, 2024
1 parent 18ba479 commit 558f5c0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
3 changes: 2 additions & 1 deletion examples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
_ = buildModule(b, "face_alignment", target, optimize);
_ = buildModule(b, "colorspace", target, optimize);
_ = buildModule(b, "face_alignment", target, optimize);
_ = buildModule(b, "perlin", target, optimize);

const fmt_step = b.step("fmt", "Run zig fmt");
const fmt = b.addFmt(.{
Expand Down
1 change: 1 addition & 0 deletions examples/lib/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ <h2>Examples</h2>
<ul>
<li><a href="colorspace.html">Colorspace</a></li>
<li><a href="face-alignment.html">Face aligment</a></li>
<li><a href="perlin.html">Perlin noise</a></li>
</ul>
</main>
<script src="main.js"></script>
Expand Down
2 changes: 0 additions & 2 deletions examples/lib/main.js

This file was deleted.

10 changes: 4 additions & 6 deletions examples/lib/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
position: relative;
}

.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
grid-gap: 20px;
}

#form {
width: 300px;
margin: 10px;
Expand All @@ -30,6 +24,10 @@
top: 0px;
}

#canvas {
border: 1px solid black;
}

#video {
position: absolute;
}
Expand Down
5 changes: 2 additions & 3 deletions examples/src/js.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");
const assert = std.debug.assert;
const gpa = std.heap.wasm_allocator;

pub const js = struct {
extern "js" fn log(ptr: [*]const u8, len: usize) void;
Expand Down Expand Up @@ -29,10 +28,10 @@ pub fn logFn(
}

export fn alloc(len: usize) [*]u8 {
const slice = gpa.alloc(u8, len) catch @panic("OOM");
const slice = std.heap.wasm_allocator.alloc(u8, len) catch @panic("OOM");
return slice.ptr;
}

export fn free(ptr: [*]const u8, len: usize) void {
gpa.free(ptr[0..len]);
std.heap.wasm_allocator.free(ptr[0..len]);
}
60 changes: 60 additions & 0 deletions examples/src/perlin.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const std = @import("std");
const builtin = @import("builtin");
const perlin = @import("zignal").perlin;
const Rgba = @import("zignal").Rgba;
const Image = @import("zignal").Image;

pub const std_options: std.Options = .{
.logFn = if (builtin.cpu.arch.isWasm()) @import("js.zig").logFn else std.log.defaultLog,
.log_level = .info,
};

var opts = perlin.Options(f32){
.amplitude = 1,
.frequency = 1,
.octaves = 1,
.persistence = 0.5,
.lacunarity = 2,
};

pub export fn set_amplitude(val: f32) void {
std.log.debug("setting amplitude to {d}", .{val});
opts.amplitude = val;
}
pub export fn set_frequency(val: f32) void {
std.log.debug("setting frequency to {d}", .{val});
opts.frequency = val;
}

pub export fn set_octaves(val: usize) void {
std.log.debug("setting octaves to {d}", .{val});
opts.octaves = val;
}

pub export fn set_persistence(val: f32) void {
std.log.debug("setting persistence to {d}", .{val});
opts.persistence = val;
}

pub export fn set_lacunarity(val: f32) void {
std.log.debug("setting lacunarity to {d}", .{val});
opts.lacunarity = val;
}

pub export fn generate(rgba_ptr: [*]Rgba, rows: usize, cols: usize) void {
const size = rows * cols;
const image = Image(Rgba).init(rows, cols, rgba_ptr[0..size]);
for (0..image.rows) |r| {
const y: f32 = @as(f32, @floatFromInt(r)) / @as(f32, @floatFromInt(image.rows));
for (0..image.cols) |c| {
const pos = r * image.cols + c;
const x: f32 = @as(f32, @floatFromInt(c)) / @as(f32, @floatFromInt(image.cols));
const val: u8 = @intFromFloat(
@max(0, @min(255, @round(
255 * (opts.amplitude / 2 * (perlin.generate(f32, x, y, 0, opts) + opts.amplitude)),
))),
);
image.data[pos] = Rgba.fromGray(val, 255);
}
}
}

0 comments on commit 558f5c0

Please sign in to comment.