A little more optimization

This commit is contained in:
2026-10-02 22:25:47 -03:00
parent dcaf878254
commit 4fb392adae
2 changed files with 61 additions and 26 deletions
+36 -16
View File
@@ -96,6 +96,9 @@ export const FULL_SKY_LIGHT = 15;
export class ClientLevel {
image: Texture = AssetManager.instance.get("bworld:textures");
chunks = new Map<number, Chunk>();
// the chunks request_meshes and update_translucent_sorting look at, so they don't go through every chunk each frame
#dirty = new Set<Chunk>();
#sorted = new Set<Chunk>();
second_timer = 0;
tick_timer = 0;
// world time in ticks, counted here between the server's updates. see common/time.ts
@@ -176,9 +179,9 @@ export class ClientLevel {
// its old mesh stays up until the new one is ready
chunk.blocks = blocks;
chunk.biome_colors = biome_colors;
chunk.dirty = true;
this.#mark_dirty(chunk);
} else {
this.chunks.set(chunk_key(x, z), {
const chunk: Chunk = {
x,
z,
blocks,
@@ -188,13 +191,15 @@ export class ClientLevel {
meshes: {},
min_y: 0,
max_y: 0,
});
};
this.chunks.set(chunk_key(x, z), chunk);
this.#dirty.add(chunk);
}
// their border faces and light depend on this one
for (const [dx, dz] of ALL_NEIGHBOR_OFFSETS) {
const neighbor = this.get_chunk(x + dx, z + dz);
if (neighbor) {
neighbor.dirty = true;
this.#mark_dirty(neighbor);
}
}
}
@@ -222,6 +227,7 @@ export class ClientLevel {
this.delete_chunk_mesh(chunk);
}
this.chunks.clear();
this.#dirty.clear();
}
get_chunk(x: number, z: number) {
@@ -247,7 +253,7 @@ export class ClientLevel {
const old_nid = chunk.blocks[index] & ID_MASK;
chunk.blocks[index] = state === undefined ? default_block_value(nid, info) : block_value(nid, state);
chunk.dirty = true;
this.#mark_dirty(chunk);
this.#mark_neighbors_dirty(block_chunk_x, block_chunk_z, lx, lz, old_nid, nid);
}
@@ -325,7 +331,7 @@ export class ClientLevel {
const index = ly * CHUNK_SIZE * CHUNK_SIZE + lz * CHUNK_SIZE + lx;
const old_nid = chunk.blocks[index] & ID_MASK;
chunk.blocks[index] = AIR;
chunk.dirty = true;
this.#mark_dirty(chunk);
this.#mark_neighbors_dirty(block_chunk_x, block_chunk_z, lx, lz, old_nid, AIR);
}
@@ -348,7 +354,7 @@ export class ClientLevel {
for (const [dx, dz] of ALL_NEIGHBOR_OFFSETS) {
const n = this.get_chunk(block_chunk_x + dx, block_chunk_z + dz);
if (n) {
n.dirty = true;
this.#mark_dirty(n);
}
}
return;
@@ -357,23 +363,23 @@ export class ClientLevel {
if (lx === 0) {
const n = this.get_chunk(block_chunk_x - 1, block_chunk_z);
if (n) {
n.dirty = true;
this.#mark_dirty(n);
}
} else if (lx === CHUNK_SIZE - 1) {
const n = this.get_chunk(block_chunk_x + 1, block_chunk_z);
if (n) {
n.dirty = true;
this.#mark_dirty(n);
}
}
if (lz === 0) {
const n = this.get_chunk(block_chunk_x, block_chunk_z - 1);
if (n) {
n.dirty = true;
this.#mark_dirty(n);
}
} else if (lz === CHUNK_SIZE - 1) {
const n = this.get_chunk(block_chunk_x, block_chunk_z + 1);
if (n) {
n.dirty = true;
this.#mark_dirty(n);
}
}
}
@@ -419,6 +425,7 @@ export class ClientLevel {
}
this.delete_chunk_mesh(chunk);
this.chunks.delete(key);
this.#dirty.delete(chunk);
this.block_items.delete(key);
}
@@ -455,11 +462,12 @@ export class ClientLevel {
// sends every dirty chunk that can be meshed to the workers
request_meshes(camera: Camera) {
for (const chunk of this.chunks.values()) {
if (!chunk.dirty || !this.can_mesh(chunk)) {
for (const chunk of this.#dirty) {
if (!this.can_mesh(chunk)) {
continue;
}
chunk.dirty = false;
this.#dirty.delete(chunk);
chunk.mesh_version += 1;
// copies, the worker lights the whole 3x3 area
const chunks: (Uint32Array | null)[] = [];
@@ -488,9 +496,9 @@ export class ClientLevel {
// planes they lie on, so that's the only time they get sorted again
update_translucent_sorting(camera: Camera) {
const position = [camera.x, camera.y, camera.z];
for (const chunk of this.chunks.values()) {
const sort = chunk.translucent_sort;
if (!sort || !crosses_planes(sort.planes, sort.camera, position)) {
for (const chunk of this.#sorted) {
const sort = chunk.translucent_sort!;
if (!crosses_planes(sort.planes, sort.camera, position)) {
continue;
}
sort.camera = position;
@@ -542,6 +550,7 @@ export class ClientLevel {
}
chunk.meshes.translucent!.index_buffer = create_index_buffer(translucent.indices);
if (translucent.sort_type === "dynamic") {
this.#sorted.add(chunk);
chunk.translucent_sort = {
mesh_version: message.version,
centers: translucent.centers,
@@ -577,6 +586,17 @@ export class ClientLevel {
}
chunk.meshes = {};
chunk.translucent_sort = undefined;
this.#sorted.delete(chunk);
}
// chunks that need meshing, some waiting on their neighbors to load
get dirty_count() {
return this.#dirty.size;
}
#mark_dirty(chunk: Chunk) {
chunk.dirty = true;
this.#dirty.add(chunk);
}
// the closest entity whose box is along the view of something at x/y/z looking at yaw/pitch, within max_distance.