More refactors around assets. Trust me, we need them

This commit is contained in:
2025-12-04 23:31:30 +01:00
parent d885fbea43
commit a372bcb981
13 changed files with 868 additions and 592 deletions

View File

@@ -1,9 +1,10 @@
const Chunk = @This();
const std = @import("std");
const vk = @import("vulkan");
const math = @import("../math.zig");
const shaders = @import("../shaders.zig");
const vk = @import("vulkan");
const voxels = @import("../voxels.zig");
const Blocks = @import("Blocks.zig");
const CommandBuffer = @import("../engine/CommandBuffer.zig");
@@ -11,10 +12,9 @@ const Engine = @import("../engine/Engine.zig");
const Game = @import("../Game.zig");
const GenericBuffer = @import("../engine/GenericBuffer.zig").GenericBuffer;
const Materials = @import("Materials.zig");
const Orientation = @import("../voxel.zig").Orientation;
const Matrix4x4 = math.Matrix4x4;
const ObjectUniformsBuffer = GenericBuffer(void, Game.ObjectUniforms);
const ObjectUniformsBuffer = GenericBuffer(void, shaders.ObjectUniforms);
const Vector3 = math.Vector3;
pub const chunk_size = 16;
@@ -107,7 +107,7 @@ pub fn deinit(self: *Chunk, engine: *Engine, descriptor_pool: vk.DescriptorPool)
}
pub fn refresh(self: *Chunk, engine: *Engine, blocks: *const Blocks, neighbors: Neighbors, temp_allocator: std.mem.Allocator) !void {
var uniforms: std.ArrayList(Game.ObjectUniforms) = try .initCapacity(temp_allocator, initial_capacity);
var uniforms: std.ArrayList(shaders.ObjectUniforms) = try .initCapacity(temp_allocator, initial_capacity);
defer uniforms.deinit(temp_allocator);
for (self.blocks, 0..) |plane, iz| {
@@ -116,81 +116,16 @@ pub fn refresh(self: *Chunk, engine: *Engine, blocks: *const Blocks, neighbors:
const fy: f32 = @floatFromInt(iy);
for (row, 0..) |id, ix| {
const fx: f32 = @floatFromInt(ix);
const block = blocks.getBlock(id).?;
const block: *const Blocks.Definition = &blocks.array.items[id.toInt()];
const center = Vector3.add(self.origin, .init(fx, fy, fz));
const cx, const cy, const cz = center.asArray();
if (block.positive_x != .empty and self.getOpposite(.positive_x, ix, iy, iz, blocks, neighbors) == .empty) {
// zig fmt: off
const matrix: Matrix4x4 = .init(
0, 1, 0, 0,
0, 0, 1, 0,
1, 0, 0, 0,
cx + 0.5, cy, cz, 1,
);
// zig fmt: on
try uniforms.append(temp_allocator, .init(matrix, matrix, block.positive_x));
}
if (block.negative_x != .empty and self.getOpposite(.negative_x, ix, iy, iz, blocks, neighbors) == .empty) {
// zig fmt: off
const matrix: Matrix4x4 = .init(
0, -1, 0, 0,
0, 0, 1, 0,
-1, 0, 0, 0,
cx - 0.5, cy, cz, 1,
);
// zig fmt: on
try uniforms.append(temp_allocator, .init(matrix, matrix, block.negative_x));
}
if (block.positive_y != .empty and self.getOpposite(.positive_y, ix, iy, iz, blocks, neighbors) == .empty) {
// zig fmt: off
const matrix: Matrix4x4 = .init(
-1, 0, 0, 0,
0, 0, 1, 0,
0, 1, 0, 0,
cx, cy + 0.5, cz, 1,
);
// zig fmt: on
try uniforms.append(temp_allocator, .init(matrix, matrix, block.positive_y));
}
if (block.negative_y != .empty and self.getOpposite(.negative_y, ix, iy, iz, blocks, neighbors) == .empty) {
// zig fmt: off
const matrix: Matrix4x4 = .init(
1, 0, 0, 0,
0, 0, 1, 0,
0, -1, 0, 0,
cx, cy - 0.5, cz, 1,
);
// zig fmt: on
try uniforms.append(temp_allocator, .init(matrix, matrix, block.negative_y));
}
if (block.positive_z != .empty and self.getOpposite(.positive_z, ix, iy, iz, blocks, neighbors) == .empty) {
// zig fmt: off
const matrix: Matrix4x4 = .init(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
cx, cy, cz + 0.5, 1,
);
// zig fmt: on
try uniforms.append(temp_allocator, .init(matrix, matrix, block.positive_z));
}
if (block.negative_z != .empty and self.getOpposite(.negative_z, ix, iy, iz, blocks, neighbors) == .empty) {
// zig fmt: off
const matrix: Matrix4x4 = .init(
1, 0, 0, 0,
0, -1, 0, 0,
0, 0, -1, 0,
cx, cy, cz - 0.5, 1,
);
// zig fmt: on
try uniforms.append(temp_allocator, .init(matrix, matrix, block.negative_z));
inline for (@typeInfo(voxels.Orientation).@"enum".fields) |field| {
const side = @field(voxels.Orientation, field.name);
const material = @field(block.walls, field.name).material;
if (material != .empty and self.getOpposite(side, ix, iy, iz, blocks, neighbors) == .empty) {
const matrix = getMatrix(side, center);
try uniforms.append(temp_allocator, .init(matrix, matrix, material));
}
}
}
}
@@ -240,43 +175,86 @@ pub fn draw(self: *const Chunk, layout: vk.PipelineLayout, command_buffer: Comma
command_buffer.drawIndexed(.{ .index_count = 6, .instance_count = self.object_count });
}
fn getOpposite(self: *const Chunk, comptime side: Orientation, x: usize, y: usize, z: usize, blocks: *const Blocks, neighbors: Neighbors) Materials.Id {
fn getOpposite(self: *const Chunk, comptime side: voxels.Orientation, x: usize, y: usize, z: usize, blocks: *const Blocks, neighbors: Neighbors) Materials.Id {
return switch (side) {
.positive_x => if (x + 1 < chunk_size)
blocks.getBlock(self.blocks[z][y][x + 1]).?.negative_x
blocks.array.items[self.blocks[z][y][x + 1].toInt()].walls.negative_x.material
else if (neighbors.positive_x) |neighbor|
blocks.getBlock(neighbor.blocks[z][y][0]).?.negative_x
blocks.array.items[neighbor.blocks[z][y][0].toInt()].walls.negative_x.material
else
.empty,
.negative_x => if (x > 0)
blocks.getBlock(self.blocks[z][y][x - 1]).?.positive_x
blocks.array.items[self.blocks[z][y][x - 1].toInt()].walls.positive_x.material
else if (neighbors.negative_x) |neighbor|
blocks.getBlock(neighbor.blocks[z][y][chunk_size - 1]).?.positive_x
blocks.array.items[neighbor.blocks[z][y][chunk_size - 1].toInt()].walls.positive_x.material
else
.empty,
.positive_y => if (y + 1 < chunk_size)
blocks.getBlock(self.blocks[z][y + 1][x]).?.negative_y
blocks.array.items[self.blocks[z][y + 1][x].toInt()].walls.negative_y.material
else if (neighbors.positive_y) |neighbor|
blocks.getBlock(neighbor.blocks[z][0][x]).?.negative_y
blocks.array.items[neighbor.blocks[z][0][x].toInt()].walls.negative_y.material
else
.empty,
.negative_y => if (y > 0)
blocks.getBlock(self.blocks[z][y - 1][x]).?.positive_y
blocks.array.items[self.blocks[z][y - 1][x].toInt()].walls.positive_y.material
else if (neighbors.negative_y) |neighbor|
blocks.getBlock(neighbor.blocks[z][chunk_size - 1][x]).?.positive_y
blocks.array.items[neighbor.blocks[z][chunk_size - 1][x].toInt()].walls.positive_y.material
else
.empty,
.positive_z => if (z + 1 < chunk_size)
blocks.getBlock(self.blocks[z + 1][y][x]).?.negative_z
blocks.array.items[self.blocks[z + 1][y][x].toInt()].walls.negative_z.material
else if (neighbors.positive_z) |neighbor|
blocks.getBlock(neighbor.blocks[0][y][x]).?.negative_z
blocks.array.items[neighbor.blocks[0][y][x].toInt()].walls.negative_z.material
else
.empty,
.negative_z => if (z > 0)
blocks.getBlock(self.blocks[z - 1][y][x]).?.positive_z
blocks.array.items[self.blocks[z - 1][y][x].toInt()].walls.positive_z.material
else if (neighbors.negative_z) |neighbor|
blocks.getBlock(neighbor.blocks[chunk_size - 1][y][x]).?.positive_z
blocks.array.items[neighbor.blocks[chunk_size - 1][y][x].toInt()].walls.positive_z.material
else
.empty,
};
}
fn getMatrix(comptime side: voxels.Orientation, center: Vector3) Matrix4x4 {
return switch (side) {
// zig fmt: off
.positive_x => .init(
0, 1, 0, 0,
0, 0, 1, 0,
1, 0, 0, 0,
center.getX() + 0.5, center.getY(), center.getZ(), 1,
),
.negative_x => .init(
0, -1, 0, 0,
0, 0, 1, 0,
-1, 0, 0, 0,
center.getX() - 0.5, center.getY(), center.getZ(), 1,
),
.positive_y => .init(
-1, 0, 0, 0,
0, 0, 1, 0,
0, 1, 0, 0,
center.getX(), center.getY() + 0.5, center.getZ(), 1,
),
.negative_y => .init(
1, 0, 0, 0,
0, 0, 1, 0,
0, -1, 0, 0,
center.getX(), center.getY() - 0.5, center.getZ(), 1,
),
.positive_z => .init(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
center.getX(), center.getY(), center.getZ() + 0.5, 1,
),
.negative_z => .init(
1, 0, 0, 0,
0, -1, 0, 0,
0, 0, -1, 0,
center.getX(), center.getY(), center.getZ() - 0.5, 1,
),
// zig fmt: on
};
}