World shape for dimension

This commit is contained in:
2026-11-06 22:49:43 -03:00
parent 7458a2d42a
commit 818ce22dd6
60 changed files with 749 additions and 349 deletions
+5 -2
View File
@@ -4,6 +4,7 @@
import type { BlockRegistry } from "@/common/everything_registry.ts";
import type { SpriteRegion } from "@/common/constants.ts";
import type { ModelJson } from "@/common/block_models.ts";
import type { WorldShapeJson } from "@/common/world_shape.ts";
import type { SortType } from "./translucent_sort.ts";
// messages between the main thread and the chunk workers
@@ -38,6 +39,8 @@ export type ToChunkWorker =
// the numeric ids of blocks with visuals, and whether each visual replaces the block's model, see
// ctx.render.register_visual
visuals: [number, boolean][];
// the dimension's height and sky light. the chunks meshed hold all of its height
shape: WorldShapeJson;
}
| {
type: "mesh";
@@ -75,7 +78,7 @@ export type FromChunkWorker =
chunk_x: number;
chunk_z: number;
version: number;
// the lowest and highest y of the chunk's quads, for frustum culling
// the lowest and highest y of the chunk's quads in the world, for frustum culling
min_y: number;
max_y: number;
solid: LayerMesh;
@@ -89,7 +92,7 @@ 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
// the chunk's light, one byte per block indexed like its blocks (from the bottom of the world): block light << 4 | sky light, like sodium's
// packed light data. entities are lit from it
light: Uint8Array<ArrayBuffer>;
// the blocks in it with visuals, as pairs of the block's index in the chunk (like light's) and its value
+19 -20
View File
@@ -9,15 +9,7 @@ import {
type BlockRegistry,
type RenderLayer,
} from "@/common/everything_registry.ts";
import {
AIR,
CHUNK_AREA,
CHUNK_HEIGHT,
CHUNK_SIZE,
ID_MASK,
type SpriteRegion,
TEXTURE_SIZE,
} from "@/common/constants.ts";
import { AIR, CHUNK_AREA, CHUNK_SIZE, ID_MASK, type SpriteRegion, TEXTURE_SIZE } from "@/common/constants.ts";
import type { Texture } from "../renderer/types.ts";
import {
FACE_GROUPS,
@@ -104,7 +96,11 @@ const waterlogged_shift = new Uint8Array(TABLE_SIZE);
const block_visual = new Uint8Array(TABLE_SIZE);
const HAS_VISUAL = 1;
const REPLACED_MODEL = 2;
const region = new LightRegion();
// the 3x3 chunks being meshed, made for the dimension's height by "init". its y, like the mesher's, counts from the
// bottom of the world
let region: LightRegion;
// the dimension's min_y, which quads are moved up by to be where they are in the world
let world_min_y = 0;
// a model's quad ready for the mesher: uvs in the atlas, and for each corner how much of each of the face's
// four corner lights it gets, so faces smaller than the block are lit like minecraft's
@@ -145,6 +141,8 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
}
textures_info = message.textures_info;
image = message.image as Texture;
region = new LightRegion(message.shape);
world_min_y = message.shape.min_y;
break;
case "mesh": {
region.fill(message.chunks);
@@ -203,8 +201,8 @@ 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++) {
const light = new Uint8Array(CHUNK_AREA * region.height);
for (let y = 0; y < region.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;
@@ -546,7 +544,7 @@ function hides_fluid(nid: number) {
// a cell's block with its states, from the 3x3 chunks the region was filled from. 0 (air) above the world and for
// chunks that aren't there
function region_value(chunks: (Uint32Array | null)[], index: number, y: number) {
if (y < 0 || y >= CHUNK_HEIGHT) return AIR;
if (y < 0 || y >= region.height) return AIR;
const x = index % REGION_SIZE;
const z = Math.floor(index / REGION_SIZE) % REGION_SIZE;
const chunk = chunks[Math.floor(z / CHUNK_SIZE) * 3 + Math.floor(x / CHUNK_SIZE)];
@@ -663,6 +661,7 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array
// adds a lit quad of the block at x y z in the chunk to its layer
const emit = (quad: MeshQuad, layer_id: number, x: number, y: number, z: number, alpha: number) => {
const wx = chunk_x * CHUNK_SIZE + x;
const wy = world_min_y + y;
const wz = chunk_z * CHUNK_SIZE + z;
const tint = quad.tint === GRASS
? blended[z * CHUNK_SIZE + x]
@@ -674,28 +673,28 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array
if (layer_id !== TRANSLUCENT) {
const group = opaque[layer_id][quad.group];
push_quad(group.buffer, quad, wx, y, wz, alpha, tint);
push_quad(group.buffer, quad, wx, wy, wz, alpha, tint);
if (quad.group !== UNALIGNED_GROUP) {
const axis = FACE_AXIS[quad.face];
const plane = (axis === 0 ? wx : axis === 1 ? y : wz) + quad.positions[axis];
const plane = (axis === 0 ? wx : axis === 1 ? wy : wz) + quad.positions[axis];
if (plane < group.min) group.min = plane;
if (plane > group.max) group.max = plane;
}
} else {
push_quad(translucent_quads, quad, wx, y, wz, alpha, tint);
push_quad(translucent_quads, quad, wx, wy, wz, alpha, tint);
// sorting treats every quad as facing along an axis, rotated ones too
const q = translucent_quads.count - 1;
centers = ensure_capacity(centers, (q + 1) * 3);
faces = ensure_capacity(faces, q + 1);
const p = quad.positions;
centers[q * 3] = wx + (p[0] + p[6]) / 2;
centers[q * 3 + 1] = y + (p[1] + p[7]) / 2;
centers[q * 3 + 1] = wy + (p[1] + p[7]) / 2;
centers[q * 3 + 2] = wz + (p[2] + p[8]) / 2;
faces[q] = quad.face;
}
};
for (let y = 0; y < CHUNK_HEIGHT; y++) {
for (let y = 0; y < region.height; y++) {
for (let z = 0; z < CHUNK_SIZE; z++) {
for (let x = 0; x < CHUNK_SIZE; x++) {
// the middle chunk of the region
@@ -743,8 +742,8 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array
if (block_visual[block_nid]) {
visuals.push(y * CHUNK_AREA + z * CHUNK_SIZE + x, value);
// with the block above and below, since a visual can reach past its block, like a large cog
if (y - 1 < mesh_min_y) mesh_min_y = y - 1;
if (y + 2 > mesh_max_y) mesh_max_y = y + 2;
if (world_min_y + y - 1 < mesh_min_y) mesh_min_y = world_min_y + y - 1;
if (world_min_y + y + 2 > mesh_max_y) mesh_max_y = world_min_y + y + 2;
if (block_visual[block_nid] === REPLACED_MODEL) continue;
}
+31 -14
View File
@@ -9,12 +9,15 @@
// minecraft stores light and updates it as blocks change. here it's worked out from scratch for the
// 3x3 chunks around the chunk being meshed, which gives the same result: light reaches at most 15
// blocks, so nothing outside those chunks can light the middle one or its border
//
// y in a region counts from the bottom of the world, 0 is the dimension's min_y. in a dimension without sky light
// there's only block light
import { AIR, CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
import { AIR, CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
import type { WorldShapeJson } from "@/common/world_shape.ts";
export const REGION_SIZE = CHUNK_SIZE * 3;
export const REGION_LAYER = REGION_SIZE * REGION_SIZE;
export const REGION_VOLUME = REGION_LAYER * CHUNK_HEIGHT;
// what unloaded chunks and the space below the world are made of: opaque, dark, never shown
export const REGION_VOID = ID_MASK;
@@ -25,22 +28,34 @@ export interface LightTables {
}
export class LightRegion {
// the dimension's, in blocks
readonly height: number;
readonly sky_light: boolean;
// block ids without their state bits, indexed y * REGION_LAYER + z * REGION_SIZE + x
blocks = new Uint16Array(REGION_VOLUME);
sky = new Uint8Array(REGION_VOLUME);
block_light = new Uint8Array(REGION_VOLUME);
blocks: Uint16Array;
sky: Uint8Array;
block_light: Uint8Array;
// the lowest y that still sees the sky, per column
#heights = new Int32Array(REGION_LAYER);
#queue = new Int32Array(1 << 18);
#queue_length = 0;
constructor(shape: WorldShapeJson) {
this.height = shape.max_y - shape.min_y;
this.sky_light = shape.sky_light;
const volume = REGION_LAYER * this.height;
this.blocks = new Uint16Array(volume);
this.sky = new Uint8Array(volume);
this.block_light = new Uint8Array(volume);
}
// the 3x3 chunks around the one being meshed, going +x then +z, starting at -x -z. missing ones are void
fill(chunks: (Uint32Array | null)[]) {
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 y = 0; y < CHUNK_HEIGHT; y++) {
for (let y = 0; y < this.height; y++) {
for (let z = 0; z < CHUNK_SIZE; z++) {
const to = y * REGION_LAYER + origin + z * REGION_SIZE;
if (!source) {
@@ -62,13 +77,14 @@ export class LightRegion {
}
#compute_sky({ opacity }: LightTables) {
const { blocks, sky } = this;
const { blocks, sky, height: world_height } = this;
sky.fill(0);
this.#queue_length = 0;
if (!this.sky_light) return;
// straight down from the top, until something isn't fully clear
for (let column = 0; column < REGION_LAYER; column++) {
let y = CHUNK_HEIGHT - 1;
let y = world_height - 1;
while (y >= 0 && opacity[blocks[y * REGION_LAYER + column]] === 0) {
sky[y * REGION_LAYER + column] = 15;
y--;
@@ -89,7 +105,7 @@ export class LightRegion {
if (z < REGION_SIZE - 1) {
highest_neighbor = Math.max(highest_neighbor, this.#heights[column + REGION_SIZE]);
}
const top = Math.min(CHUNK_HEIGHT - 1, Math.max(height, highest_neighbor - 1));
const top = Math.min(world_height - 1, Math.max(height, highest_neighbor - 1));
for (let y = height; y <= top; y++) {
this.#push(y * REGION_LAYER + column);
}
@@ -104,7 +120,7 @@ export class LightRegion {
block_light.fill(0);
this.#queue_length = 0;
for (let i = 0; i < REGION_VOLUME; i++) {
for (let i = 0; i < blocks.length; i++) {
const level = emission[blocks[i]];
if (level > 0) {
block_light[i] = level;
@@ -127,6 +143,7 @@ export class LightRegion {
// 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]];
@@ -150,7 +167,7 @@ export class LightRegion {
const x = rest - z * REGION_SIZE;
if (y > 0) spread(index - REGION_LAYER, level, true);
if (y < CHUNK_HEIGHT - 1) spread(index + REGION_LAYER, level, false);
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);
@@ -162,18 +179,18 @@ export class LightRegion {
// 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 >= CHUNK_HEIGHT) return AIR;
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 >= CHUNK_HEIGHT) return 15;
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 >= CHUNK_HEIGHT || y < 0) return 0;
if (y >= region.height || y < 0) return 0;
return region.block_light[index];
}