Files

206 lines
7.6 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// minecraft's lighting: every block has a sky light and a block light level from 0 to 15.
// sky light starts at 15 above the world and goes straight down without getting weaker until it hits
// something that isn't fully clear, block light starts at blocks that give off light. both spread to
// neighbors losing max(1, the neighbor's opacity) per step.
//
// minecraft stores light and updates it as blocks change. here it's worked out from scratch around the sections being
// meshed: the 3x3 chunks around theirs, from the section below them to the one above. that gives the same result, since
// light reaches at most 15 blocks, so nothing outside can light them or their border. the only light that comes from
// further is the sky's straight down, which starts where each column's sky height says (see column_heights in
// common/section.ts) instead of being followed down from the top.
//
// y in a region counts from its bottom, which is a section's. in a dimension without sky light there's only block
// light
import { AIR, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
import type { Section } from "@/common/section.ts";
import { SECTION_HEIGHT, type WorldShapeJson } from "@/common/world_shape.ts";
export const REGION_SIZE = CHUNK_SIZE * 3;
export const REGION_LAYER = REGION_SIZE * REGION_SIZE;
// what unloaded chunks and the space below the world are made of: opaque, dark, never shown
export const REGION_VOID = ID_MASK;
// by numeric block id
export interface LightTables {
opacity: Uint8Array;
emission: Uint8Array;
}
export class LightRegion {
// in blocks, of the sections last filled in
height = 0;
readonly sky_light: boolean;
// block ids without their state bits, indexed y * REGION_LAYER + z * REGION_SIZE + x
blocks: Uint16Array;
sky: Uint8Array;
block_light: Uint8Array;
// the lowest y that still sees the sky, per column. the region's height when that's above it
#heights = new Int32Array(REGION_LAYER);
#queue = new Int32Array(1 << 18);
#queue_length = 0;
// room for the dimension's whole height
constructor(shape: WorldShapeJson) {
this.sky_light = shape.sky_light;
const volume = REGION_LAYER * (shape.max_y - shape.min_y);
this.blocks = new Uint16Array(volume);
this.sky = new Uint8Array(volume);
this.block_light = new Uint8Array(volume);
}
// the same sections of the 3x3 chunks around the one being meshed, going +x then +z, starting at -x -z. missing
// chunks are void. sky is each chunk's sky height by column (see column_heights), and bottom the y of the region's
// bottom in the world
fill(chunks: (Section[] | null)[], sky: (ArrayLike<number> | null)[], bottom: number) {
this.height = chunks[4]!.length * SECTION_HEIGHT;
for (let i = 0; i < 9; i++) {
const source = chunks[i];
const origin = Math.floor(i / 3) * CHUNK_SIZE * REGION_SIZE + (i % 3) * CHUNK_SIZE;
for (let z = 0; z < CHUNK_SIZE; z++) {
for (let x = 0; x < CHUNK_SIZE; x++) {
const height = sky[i] ? sky[i]![z * CHUNK_SIZE + x] - bottom : this.height;
this.#heights[origin + z * REGION_SIZE + x] = Math.min(Math.max(height, 0), this.height);
}
}
for (let y = 0; y < this.height; y++) {
const section = source?.[y >> 4];
for (let z = 0; z < CHUNK_SIZE; z++) {
const to = y * REGION_LAYER + origin + z * REGION_SIZE;
if (!section?.blocks) {
this.blocks.fill(section ? section.value & ID_MASK : REGION_VOID, to, to + CHUNK_SIZE);
continue;
}
const from = ((y & 15) << 8) | (z << 4);
for (let x = 0; x < CHUNK_SIZE; x++) {
this.blocks[to + x] = section.blocks[from + x] & ID_MASK;
}
}
}
}
}
compute(tables: LightTables) {
this.#compute_sky(tables);
this.#compute_block_light(tables);
}
#compute_sky({ opacity }: LightTables) {
const { sky, height: region_height } = this;
sky.fill(0, 0, REGION_LAYER * region_height);
this.#queue_length = 0;
if (!this.sky_light) return;
// straight down from the top, to the sky height
for (let column = 0; column < REGION_LAYER; column++) {
for (let y = this.#heights[column]; y < region_height; y++) sky[y * REGION_LAYER + column] = 15;
}
// only the lit cells next to a darker one can spread: the bottom of each column's sunlight,
// and the part of it that's beside a neighbor column's shade
for (let z = 0; z < REGION_SIZE; z++) {
for (let x = 0; x < REGION_SIZE; x++) {
const column = z * REGION_SIZE + x;
const height = this.#heights[column];
let highest_neighbor = height;
if (x > 0) highest_neighbor = Math.max(highest_neighbor, this.#heights[column - 1]);
if (x < REGION_SIZE - 1) highest_neighbor = Math.max(highest_neighbor, this.#heights[column + 1]);
if (z > 0) highest_neighbor = Math.max(highest_neighbor, this.#heights[column - REGION_SIZE]);
if (z < REGION_SIZE - 1) {
highest_neighbor = Math.max(highest_neighbor, this.#heights[column + REGION_SIZE]);
}
const top = Math.min(region_height - 1, Math.max(height, highest_neighbor - 1));
for (let y = height; y <= top; y++) {
this.#push(y * REGION_LAYER + column);
}
}
}
this.#propagate(sky, opacity, true);
}
#compute_block_light({ opacity, emission }: LightTables) {
const { blocks, block_light } = this;
const volume = REGION_LAYER * this.height;
block_light.fill(0, 0, volume);
this.#queue_length = 0;
for (let i = 0; i < volume; i++) {
const level = emission[blocks[i]];
if (level > 0) {
block_light[i] = level;
this.#push(i);
}
}
this.#propagate(block_light, opacity, false);
}
#push(index: number) {
if (this.#queue_length === this.#queue.length) {
const bigger = new Int32Array(this.#queue.length * 2);
bigger.set(this.#queue);
this.#queue = bigger;
}
this.#queue[this.#queue_length++] = index;
}
// breadth first from everything queued. a cell can be queued again when a brighter path reaches it
#propagate(light: Uint8Array, opacity: Uint8Array, is_sky: boolean) {
const blocks = this.blocks;
const top = this.height - 1;
const spread = (to: number, level: number, down: boolean) => {
const block_opacity = opacity[blocks[to]];
const next = is_sky && down && level === 15 && block_opacity === 0
? 15
: level - Math.max(1, block_opacity);
if (next > light[to]) {
light[to] = next;
this.#push(to);
}
};
for (let head = 0; head < this.#queue_length; head++) {
const index = this.#queue[head];
const level = light[index];
if (level <= 1) continue;
const y = Math.floor(index / REGION_LAYER);
const rest = index - y * REGION_LAYER;
const z = Math.floor(rest / REGION_SIZE);
const x = rest - z * REGION_SIZE;
if (y > 0) spread(index - REGION_LAYER, level, true);
if (y < top) spread(index + REGION_LAYER, level, false);
if (x > 0) spread(index - 1, level, false);
if (x < REGION_SIZE - 1) spread(index + 1, level, false);
if (z > 0) spread(index - REGION_SIZE, level, false);
if (z < REGION_SIZE - 1) spread(index + REGION_SIZE, level, false);
}
this.#queue_length = 0;
}
}
// what a cell looks like to the mesher, including above and below the world
export function region_block(region: LightRegion, index: number, y: number) {
if (y >= region.height) return AIR;
if (y < 0) return REGION_VOID;
return region.blocks[index];
}
export function region_sky(region: LightRegion, index: number, y: number) {
if (y >= region.height) return region.sky_light ? 15 : 0;
if (y < 0) return 0;
return region.sky[index];
}
export function region_block_light(region: LightRegion, index: number, y: number) {
if (y >= region.height || y < 0) return 0;
return region.block_light[index];
}