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

View File

@@ -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));
}