Seed-based noise and worldgen
This commit is contained in:
36
src/worldgen.zig
Normal file
36
src/worldgen.zig
Normal 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));
|
||||
}
|
||||
Reference in New Issue
Block a user