Lighting for entities

This commit is contained in:
2026-09-30 16:56:55 -03:00
parent 93382d3c97
commit cbc9edfd60
11 changed files with 163 additions and 18 deletions
+20
View File
@@ -45,6 +45,8 @@ export interface Chunk {
translucent_sort?: TranslucentSort;
// each column's biome grass color, x then z, then each column's foliage color
biome_colors: Uint32Array;
// from its last mesh, see get_light. undefined until it's been meshed
light?: Uint8Array;
}
export interface ChunkMesh {
@@ -85,6 +87,8 @@ const ALL_NEIGHBOR_OFFSETS = [...NEIGHBOR_OFFSETS, [-1, -1], [1, -1], [-1, 1], [
// how far past the view distance chunks are kept, like minecraft's ClientChunkCache. the server tells the client
// which ones to forget, this only catches the ones it didn't
const STORAGE_MARGIN = 3;
// block light 0 and sky light 15, packed like Chunk.light
export const FULL_SKY_LIGHT = 15;
// what minecraft calls the ClientLevel: this client's copy of the world, its chunks and the entities in it. the
// chunks are what the server sent, see common/chunk_streaming.ts
@@ -246,6 +250,21 @@ export class ClientLevel {
this.#mark_neighbors_dirty(block_chunk_x, block_chunk_z, lx, lz, old_nid, nid);
}
// block light << 4 | sky light there, as of its chunk's last mesh. full sky light above the world and where it isn't
// known yet, so entities don't go dark while their chunk loads
get_light(x: number, y: number, z: number): number {
const block_y = Math.floor(y);
if (block_y >= CHUNK_HEIGHT) return FULL_SKY_LIGHT;
if (block_y < 0) return 0;
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
const light = this.get_chunk(chunk_x, chunk_z)?.light;
if (!light) return FULL_SKY_LIGHT;
const lx = Math.floor(x) - chunk_x * CHUNK_SIZE;
const lz = Math.floor(z) - chunk_z * CHUNK_SIZE;
return light[block_y * CHUNK_AREA + lz * CHUNK_SIZE + lx];
}
// the id with its state bits, VOID outside loaded chunks
get_block_value(x: number, y: number, z: number) {
const chunk_x = Math.floor(x / CHUNK_SIZE);
@@ -493,6 +512,7 @@ export class ClientLevel {
}
this.delete_chunk_mesh(chunk);
chunk.light = message.light;
chunk.min_y = message.min_y;
chunk.max_y = message.max_y;