lil refactor

This commit is contained in:
Radonchnk 2025-04-19 14:48:48 +01:00
parent 4999b300b2
commit 92085b9228

View file

@ -6,6 +6,9 @@ const A7r = std.mem.Allocator;
const comptimePrint = std.fmt.comptimePrint;
const VERTICES_BLOCK_SIZE = 2 * 3 * 3;
const TEXCOORDS_BLOCK_SIZE = 2 * 2 * 3;
const X_DIRECTION = 0;
const Y_DIRECTION = 1;
const Z_DIRECTION = 3;
const RawQuad = struct {
tile: u32,
@ -79,12 +82,12 @@ pub const Chunk = struct {
}
// This cyclically permutes the x, y, z coordinates at compile time. Useful when iterating over x, y, and z axis.
inline fn getTileRawShifted(self: Chunk, x: u5, y: u5, z: u5, comptime d: comptime_int) u32 {
if (d % 3 == 0) {
inline fn getTileRawShifted(self: Chunk, x: u5, y: u5, z: u5, comptime dimention: comptime_int) u32 {
if (dimention % 3 == 0) {
return self.getTileRaw(x, y, z);
} else if (d % 3 == 1) {
} else if (dimention % 3 == 1) {
return self.getTileRaw(y, z, x);
} else if (d % 3 == 2) {
} else if (dimention % 3 == 2) {
return self.getTileRaw(z, x, y);
}
}
@ -95,7 +98,7 @@ pub const Chunk = struct {
y_start: usize, y_end: usize,
z_start: usize, z_end: usize,
sign: comptime_int,
d: comptime_int,
dimention: comptime_int,
surface: u32,
y_minus_obscuring_pattern: u32,
y_plus_obscuring_pattern: u32,
@ -111,8 +114,8 @@ pub const Chunk = struct {
const zleft: f32 = if (sign == 1) zmin else zmax;
const zright: f32 = if (sign == 1) zmax else zmin;
var raw_quad: RawQuad = undefined;
switch (d) {
0 => { // X direction
switch (dimention) {
X_DIRECTION => {
raw_quad = .{
.tile = surface,
.top_left = v3.new(x + 0.5 * sign, ymax, zright),
@ -133,7 +136,7 @@ pub const Chunk = struct {
.bottom_left_obscured = false,
};
},
1 => { // Y direction
Y_DIRECTION => {
raw_quad = .{
.tile = surface,
.bottom_left = v3.new(yleft, zmin, x + 0.5 * sign),
@ -154,7 +157,7 @@ pub const Chunk = struct {
.bottom_left_obscured = false,
};
},
2 => { // Z direction
Z_DIRECTION => {
raw_quad = .{
.tile = surface,
.top_left = v3.new(zleft, x + 0.5 * sign, ymin),
@ -186,7 +189,7 @@ pub const Chunk = struct {
defer raw_quads.deinit();
// Begin scanning the chunk for tile surfaces to make raw quads.
inline for (0..3) |d| { // Iterate over the 3 dimensions, X, Y and Z.
inline for (0..3) |dimention| { // Iterate over the 3 dimensions, X, Y and Z.
for (0..32) |raw_x| {
const x: u5 = @intCast(raw_x);
// Create surface arrays for the +x side of the layer and the -x side.
@ -195,11 +198,11 @@ pub const Chunk = struct {
for (0..32) |raw_y| for (0..32) |raw_z| {
const y: u5 = @intCast(raw_y);
const z: u5 = @intCast(raw_z);
const tile: u32 = chunk.getTileRawShifted(x, y, z, d);
const tile: u32 = chunk.getTileRawShifted(x, y, z, dimention);
if (tile == 0) continue; // If air, there is no surface.
// If either at the edge of the chunk or the tile is exposed, create a tile surface.
if (x == 31 or chunk.getTileRawShifted(x + 1, y, z, d) == 0) positive_tile_surfaces[y][z] = tile;
if (x == 0 or chunk.getTileRawShifted(x - 1, y, z, d) == 0) negative_tile_surfaces[y][z] = tile;
if (x == 31 or chunk.getTileRawShifted(x + 1, y, z, dimention) == 0) positive_tile_surfaces[y][z] = tile;
if (x == 0 or chunk.getTileRawShifted(x - 1, y, z, dimention) == 0) negative_tile_surfaces[y][z] = tile;
};
inline for (.{ -1, 1 }) |sign| {
var tile_surfaces = if (sign == 1) positive_tile_surfaces else negative_tile_surfaces;
@ -226,7 +229,7 @@ pub const Chunk = struct {
for (y_start..y_end) |raw_y| {
const y: u5 = @intCast(raw_y);
z_minus_obscuring_pattern <<= 1;
if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, y, @intCast(z_start - 1), d) != 0) z_minus_obscuring_pattern |= 1;
if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, y, @intCast(z_start - 1), dimention) != 0) z_minus_obscuring_pattern |= 1;
}
}
var z_plus_obscuring_pattern: u32 = 0;
@ -234,7 +237,7 @@ pub const Chunk = struct {
for (y_start..y_end) |raw_y| {
const y: u5 = @intCast(raw_y);
z_plus_obscuring_pattern <<= 1;
if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, y, @intCast(z_end), d) != 0) z_plus_obscuring_pattern |= 1;
if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, y, @intCast(z_end), dimention) != 0) z_plus_obscuring_pattern |= 1;
}
}
var y_minus_obscuring_pattern: u32 = 0;
@ -242,7 +245,7 @@ pub const Chunk = struct {
for (z_start..z_end) |raw_z| {
const z: u5 = @intCast(raw_z);
y_minus_obscuring_pattern <<= 1;
if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, @intCast(y_start - 1), z, d) != 0) y_minus_obscuring_pattern |= 1;
if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, @intCast(y_start - 1), z, dimention) != 0) y_minus_obscuring_pattern |= 1;
}
}
var y_plus_obscuring_pattern: u32 = 0;
@ -250,11 +253,11 @@ pub const Chunk = struct {
for (z_start..z_end) |raw_z| {
const z: u5 = @intCast(raw_z);
y_plus_obscuring_pattern <<= 1;
if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, @intCast(y_end), z, d) != 0) y_plus_obscuring_pattern |= 1;
if (chunk.getTileRawShifted(if (sign == 1) x+1 else x-1, @intCast(y_end), z, dimention) != 0) y_plus_obscuring_pattern |= 1;
}
}
// std.debug.print("{}\n", .{z_minus_obscuring_pattern});
const raw_quad = pack_raw_quad(@floatFromInt(raw_x), y_start, y_end, z_start, z_end, sign, d, surface, y_minus_obscuring_pattern, y_plus_obscuring_pattern, z_minus_obscuring_pattern, z_plus_obscuring_pattern);
const raw_quad = pack_raw_quad(@floatFromInt(raw_x), y_start, y_end, z_start, z_end, sign, dimention, surface, y_minus_obscuring_pattern, y_plus_obscuring_pattern, z_minus_obscuring_pattern, z_plus_obscuring_pattern);
try raw_quads.append(raw_quad);
};
}