Lighting for entities
This commit is contained in:
@@ -32,6 +32,8 @@ let sampler: GPUSampler;
|
||||
// minecraft's lightmap: the color for every block light (x) and sky light (y) pair, see update_lightmap
|
||||
let lightmap: GPUTexture;
|
||||
let lightmap_bind_group: GPUBindGroup;
|
||||
// the same colors on the cpu, r g b from 0 to 1 by sky * 16 + block, for lighting entities, see light_color
|
||||
const lightmap_colors = new Float32Array(16 * 16 * 3);
|
||||
|
||||
const vertex_data = new Float32Array(MAX_SPRITES * VERTS_PER_SPRITE * FLOATS_PER_VERT);
|
||||
let vert_index = 0;
|
||||
@@ -257,6 +259,7 @@ export function update_lightmap(daylight = 1) {
|
||||
pixels[i + 1] = Math.round(color[1] * 255);
|
||||
pixels[i + 2] = Math.round(color[2] * 255);
|
||||
pixels[i + 3] = 255;
|
||||
for (let c = 0; c < 3; c++) lightmap_colors[(sky * 16 + block) * 3 + c] = pixels[i + c] / 255;
|
||||
}
|
||||
}
|
||||
device.queue.writeTexture({ texture: lightmap }, pixels, { bytesPerRow: 16 * 4 }, [16, 16]);
|
||||
@@ -348,6 +351,13 @@ export function begin_view_model(fov: number) {
|
||||
write_mvp();
|
||||
}
|
||||
|
||||
// the lightmap's color for light packed as block light << 4 | sky light (see ClientLevel.get_light), what terrain
|
||||
// lit like that is multiplied by. entities multiply it into their vertex colors, like minecraft's packed light coords
|
||||
export function light_color(packed: number): [number, number, number] {
|
||||
const i = ((packed & 15) * 16 + (packed >> 4)) * 3;
|
||||
return [lightmap_colors[i], lightmap_colors[i + 1], lightmap_colors[i + 2]];
|
||||
}
|
||||
|
||||
export function end_mode_3d() {
|
||||
if (!mode3d) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user