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
+3
View File
@@ -83,6 +83,9 @@ export type FromChunkWorker =
planes: [Float32Array, Float32Array, Float32Array];
};
camera: number[];
// the chunk's light, one byte per block indexed like its blocks: block light << 4 | sky light, like sodium's
// packed light data. entities are lit from it
light: Uint8Array<ArrayBuffer>;
}
| {
type: "sorted";
+18
View File
@@ -141,6 +141,7 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
message.chunks[4]!,
message.camera,
);
const light = chunk_light();
post(
{
type: "meshed",
@@ -153,8 +154,10 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
cutout,
translucent,
camera: message.camera,
light,
},
[
light.buffer,
solid.vertices.buffer,
cutout.vertices.buffer,
translucent.vertices.buffer,
@@ -181,6 +184,21 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
}
};
// the middle chunk of the light region, packed for the main thread, see "meshed"
function chunk_light() {
const light = new Uint8Array(CHUNK_AREA * CHUNK_HEIGHT);
for (let y = 0; y < CHUNK_HEIGHT; y++) {
for (let z = 0; z < CHUNK_SIZE; z++) {
const from = y * REGION_LAYER + (z + CHUNK_SIZE) * REGION_SIZE + CHUNK_SIZE;
const to = y * CHUNK_AREA + z * CHUNK_SIZE;
for (let x = 0; x < CHUNK_SIZE; x++) {
light[to + x] = (region.block_light[from + x] << 4) | region.sky[from + x];
}
}
}
return light;
}
function post(message: FromChunkWorker, transfer: Transferable[]) {
self.postMessage(message, transfer);
}