Seed-based noise and worldgen
This commit is contained in:
25
src/Game.zig
25
src/Game.zig
@@ -5,6 +5,7 @@ const glfw = @import("zglfw");
|
||||
const vk = @import("vulkan");
|
||||
|
||||
const math = @import("math.zig");
|
||||
const worldgen = @import("worldgen.zig");
|
||||
|
||||
const Blocks = @import("assets/Blocks.zig");
|
||||
const Chunk = @import("assets/Chunk.zig");
|
||||
@@ -598,6 +599,8 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
chunks.deinit(allocator);
|
||||
}
|
||||
|
||||
const world_seed = engine.random.int(u64);
|
||||
std.log.info("Using world seed 0x{x:0<16}", .{world_seed});
|
||||
var it = Interator2(i16).init(-10, -10, 9, 9);
|
||||
|
||||
const block_grass = blocks.getFilename("Grass.json").?;
|
||||
@@ -633,7 +636,7 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
@floatFromInt(pos[1]),
|
||||
).add(origin.asVector2());
|
||||
|
||||
const iheight = worldHeightI(fpos);
|
||||
const iheight = worldgen.heightI(world_seed, fpos);
|
||||
|
||||
chunk.blocks[0][y][x] = block_bedrock;
|
||||
var i: i32 = 0;
|
||||
@@ -650,7 +653,7 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine, swapchain: *Swapchain
|
||||
}
|
||||
}
|
||||
|
||||
const player_position = Vector3.init(0, 0, @as(f32, @floatFromInt(worldHeightI(.zero))) + 0.5);
|
||||
const player_position = Vector3.init(0, 0, worldgen.heightF(world_seed, .zero) + 0.5);
|
||||
|
||||
var chunk_it = chunks.iterator();
|
||||
while (chunk_it.next()) |entry| {
|
||||
@@ -758,7 +761,7 @@ pub fn update(self: *Game, dt: f32) void {
|
||||
@floatFromInt(self.swapchain.extent.height),
|
||||
);
|
||||
|
||||
const camera_position = self.player.position.add(.init(0, Player.camera_height, 0));
|
||||
const camera_position = self.player.position.add(.init(0, 0, Player.camera_height));
|
||||
const camera_rotation = Quaternion.mulQuaternion(
|
||||
.initRotationXY(self.player.yaw_rad),
|
||||
.initRotationYZ(self.player.pitch_rad),
|
||||
@@ -926,19 +929,3 @@ fn recreateSwapchain(self: *Game) !void {
|
||||
self.engine.setObjectName(semaphore.*, "S Transfer[{d}]", .{i});
|
||||
}
|
||||
}
|
||||
|
||||
fn worldHeightF(pos: Vector2) f32 {
|
||||
const noise_hscale = 80;
|
||||
|
||||
const noise_zcenter = 8;
|
||||
const noise_zscale = 5;
|
||||
|
||||
const noise = math.noise2(pos.divScalar(noise_hscale));
|
||||
const height = noise_zscale * noise + noise_zcenter;
|
||||
return height;
|
||||
}
|
||||
|
||||
fn worldHeightI(pos: Vector2) i32 {
|
||||
const iheight = worldHeightF(pos);
|
||||
return @intFromFloat(@round(iheight));
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ const Vector2 = @import("Vector2.zig").Vector2;
|
||||
const f32x8 = @Vector(8, f32);
|
||||
const i32x8 = @Vector(8, i32);
|
||||
const u32x8 = @Vector(8, u32);
|
||||
const u64x8 = @Vector(8, u64);
|
||||
|
||||
inline fn ps(value: f32) f32x8 {
|
||||
return @splat(value);
|
||||
@@ -18,50 +19,40 @@ inline fn epu32(value: u32) u32x8 {
|
||||
return @splat(value);
|
||||
}
|
||||
|
||||
const permutation = [256]u8{
|
||||
151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225,
|
||||
140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148,
|
||||
247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32,
|
||||
57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175,
|
||||
74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122,
|
||||
60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54,
|
||||
65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169,
|
||||
200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64,
|
||||
52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212,
|
||||
207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213,
|
||||
119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
|
||||
129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104,
|
||||
218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241,
|
||||
81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157,
|
||||
184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93,
|
||||
222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180,
|
||||
};
|
||||
|
||||
inline fn perm(i: i32) i32 {
|
||||
@setRuntimeSafety(false);
|
||||
const index: u32 = @as(u32, @bitCast(i)) & 0xFF;
|
||||
return permutation[index];
|
||||
inline fn epu64x2(value: u64) u64x8 {
|
||||
return @splat(value);
|
||||
}
|
||||
|
||||
inline fn permv(i: i32x8) i32x8 {
|
||||
@setRuntimeSafety(false);
|
||||
const index: u32x8 = @as(u32x8, @bitCast(i)) & epu32(0xFF);
|
||||
return .{
|
||||
permutation[index[0]],
|
||||
permutation[index[1]],
|
||||
permutation[index[2]],
|
||||
permutation[index[3]],
|
||||
permutation[index[4]],
|
||||
permutation[index[5]],
|
||||
permutation[index[6]],
|
||||
permutation[index[7]],
|
||||
};
|
||||
const prime_x: u64 = 17061574742423305691;
|
||||
const prime_y: u64 = 10555943830568207707;
|
||||
const prime_perm: u64 = 15540206209889782181;
|
||||
|
||||
const prime_xv: u64x8 = epu64x2(prime_x);
|
||||
const prime_yv: u64x8 = epu64x2(prime_y);
|
||||
const prime_permv: u64x8 = epu64x2(prime_perm);
|
||||
|
||||
fn perm(seed: u64, x: i32, y: i32) i32 {
|
||||
var hash: u64 = seed;
|
||||
hash ^= prime_x *% @as(u32, @bitCast(x));
|
||||
hash ^= prime_y *% @as(u32, @bitCast(y));
|
||||
hash = hash *% hash *% hash *% prime_perm;
|
||||
hash = (hash >> 26) ^ hash;
|
||||
return @bitCast(@as(u32, @truncate(hash)));
|
||||
}
|
||||
|
||||
fn permv(seed: u64, x: i32x8, y: i32x8) i32x8 {
|
||||
var hash: u64x8 = epu64x2(seed);
|
||||
hash ^= prime_xv *% @as(u32x8, @bitCast(x));
|
||||
hash ^= prime_yv *% @as(u32x8, @bitCast(y));
|
||||
hash = hash *% hash *% hash *% prime_permv;
|
||||
hash = (hash >> @splat(26)) ^ hash;
|
||||
return @bitCast(@as(u32x8, @truncate(hash)));
|
||||
}
|
||||
|
||||
fn grad2(hash: i32, x: f32, y: f32) f32 {
|
||||
const h: i32 = hash & 0b111;
|
||||
const u: f32 = if (h < 4) x else y;
|
||||
const v: f32 = if (h < 4) x else x;
|
||||
const v: f32 = if (h < 4) y else x;
|
||||
return (if (h & 0b001 != 0) -u else u) + (if (h & 0b010 != 0) -2 * v else 2 * v);
|
||||
}
|
||||
|
||||
@@ -84,7 +75,7 @@ const G2v: f32x8 = ps(G2);
|
||||
const noise2_scale: f32 = 34.11;
|
||||
const noise2v_scale: f32x8 = ps(noise2_scale);
|
||||
|
||||
pub fn noise2(v: Vector2) f32 {
|
||||
pub fn noise2(seed: u64, v: Vector2) f32 {
|
||||
const x: f32, const y: f32 = v.asArray();
|
||||
|
||||
const s: f32 = (x + y) * F2;
|
||||
@@ -111,9 +102,9 @@ pub fn noise2(v: Vector2) f32 {
|
||||
const t1: f32 = 0.5 - x1 * x1 - y1 * y1;
|
||||
const t2: f32 = 0.5 - x2 * x2 - y2 * y2;
|
||||
|
||||
const gi0: i32 = perm(i + perm(j));
|
||||
const gi1: i32 = perm(i + _i1 + perm(j + _j1));
|
||||
const gi2: i32 = perm(i + 1 + perm(j + 1));
|
||||
const gi0: i32 = perm(seed, i, j);
|
||||
const gi1: i32 = perm(seed, i + _i1, j + _j1);
|
||||
const gi2: i32 = perm(seed, i + 1, j + 1);
|
||||
|
||||
const n0: f32 = if (t0 < 0) 0 else (t0 * t0) * (t0 * t0) * grad2(gi0, x0, y0);
|
||||
const n1: f32 = if (t1 < 0) 0 else (t1 * t1) * (t1 * t1) * grad2(gi1, x1, y1);
|
||||
@@ -124,7 +115,7 @@ pub fn noise2(v: Vector2) f32 {
|
||||
return ret;
|
||||
}
|
||||
|
||||
pub fn noise2v(x: f32x8, y: f32x8) f32x8 {
|
||||
pub fn noise2v(seed: u64, x: f32x8, y: f32x8) f32x8 {
|
||||
const s: f32x8 = (x + y) * F2v;
|
||||
const xs: f32x8 = x + s;
|
||||
const ys: f32x8 = y + s;
|
||||
@@ -149,9 +140,9 @@ pub fn noise2v(x: f32x8, y: f32x8) f32x8 {
|
||||
const t1: f32x8 = ps(0.5) - x1 * x1 - y1 * y1;
|
||||
const t2: f32x8 = ps(0.5) - x2 * x2 - y2 * y2;
|
||||
|
||||
const gi0: i32x8 = permv(i + permv(j));
|
||||
const gi1: i32x8 = permv(i + _i1 + permv(j + _j1));
|
||||
const gi2: i32x8 = permv(i + epi32(1) + permv(j + epi32(1)));
|
||||
const gi0: i32x8 = permv(seed, i, j);
|
||||
const gi1: i32x8 = permv(seed, i + _i1, j + _j1);
|
||||
const gi2: i32x8 = permv(seed, i + epi32(1), j + epi32(1));
|
||||
|
||||
const n0: f32x8 = @select(f32, t0 < ps(0), ps(0), (t0 * t0) * (t0 * t0) * grad2(gi0, x0, y0));
|
||||
const n1: f32x8 = @select(f32, t1 < ps(0), ps(0), (t1 * t1) * (t1 * t1) * grad2(gi1, x1, y1));
|
||||
|
||||
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