Seed-based noise and worldgen

This commit is contained in:
2025-12-01 15:36:52 +01:00
parent b3b6b6c30a
commit 46ff0e5729
3 changed files with 78 additions and 64 deletions

36
src/worldgen.zig Normal file
View File

@@ -0,0 +1,36 @@
const std = @import("std");
const math = @import("math.zig");
const Vector2 = math.Vector2;
const Noise = struct {
horizontal_scale: f32,
value_median: f32,
value_amplitude: f32,
pub fn sample(self: Noise, seed: u64, pos: Vector2) f32 {
return math.noise2(seed, pos.divScalar(self.horizontal_scale)) * self.value_amplitude + self.value_median;
}
};
const noise_main: Noise = .{
.horizontal_scale = 80,
.value_median = 7,
.value_amplitude = 5,
};
const noise_secondary: Noise = .{
.horizontal_scale = 15,
.value_median = 0,
.value_amplitude = 0.5,
};
pub fn heightF(seed: u64, pos: Vector2) f32 {
const main = noise_main.sample(seed, pos);
const secondary = noise_secondary.sample(seed, pos);
return @round(main + secondary);
}
pub fn heightI(seed: u64, pos: Vector2) i32 {
return @intFromFloat(heightF(seed, pos));
}