Client side chunk sections
This commit is contained in:
@@ -15,11 +15,14 @@ import {
|
||||
FACE_GROUPS,
|
||||
type FaceGroup,
|
||||
type FromChunkWorker,
|
||||
type SectionLight,
|
||||
TERRAIN_QUAD_BYTES,
|
||||
TERRAIN_VERTEX_BYTES,
|
||||
type ToChunkWorker,
|
||||
UNALIGNED_GROUP,
|
||||
} from "./chunk_messages.ts";
|
||||
import { Section, section_index, SECTION_VOLUME } from "@/common/section.ts";
|
||||
import { SECTION_HEIGHT } from "@/common/world_shape.ts";
|
||||
import {
|
||||
bake_block,
|
||||
FACE_CORNERS,
|
||||
@@ -145,13 +148,14 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
|
||||
world_min_y = message.shape.min_y;
|
||||
break;
|
||||
case "mesh": {
|
||||
region.fill(message.chunks);
|
||||
const chunks = message.chunks.map((data) => data?.map((section) => new Section(section)) ?? null);
|
||||
region.fill(chunks);
|
||||
region.compute(light_tables);
|
||||
blend_biome_colors(message.biome_colors);
|
||||
const { solid, cutout, translucent, min_y, max_y, visuals } = make_chunk_mesh(
|
||||
message.chunk_x,
|
||||
message.chunk_z,
|
||||
message.chunks,
|
||||
chunks,
|
||||
message.camera,
|
||||
);
|
||||
const light = chunk_light();
|
||||
@@ -171,7 +175,7 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
|
||||
visuals,
|
||||
},
|
||||
[
|
||||
light.buffer,
|
||||
...light.filter((section) => typeof section !== "number").map((section) => section.buffer),
|
||||
visuals.buffer,
|
||||
solid.vertices.buffer,
|
||||
cutout.vertices.buffer,
|
||||
@@ -199,19 +203,27 @@ 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 * 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;
|
||||
for (let x = 0; x < CHUNK_SIZE; x++) {
|
||||
light[to + x] = (region.block_light[from + x] << 4) | region.sky[from + x];
|
||||
// the middle chunk of the light region, packed for the main thread by section, see "meshed"
|
||||
const section_light = new Uint8Array(SECTION_VOLUME);
|
||||
|
||||
function chunk_light(): SectionLight[] {
|
||||
const sections: SectionLight[] = [];
|
||||
for (let bottom = 0; bottom < region.height; bottom += SECTION_HEIGHT) {
|
||||
let same = true;
|
||||
for (let y = 0; y < SECTION_HEIGHT; y++) {
|
||||
for (let z = 0; z < CHUNK_SIZE; z++) {
|
||||
const from = (bottom + y) * REGION_LAYER + (z + CHUNK_SIZE) * REGION_SIZE + CHUNK_SIZE;
|
||||
const to = (y << 8) | (z << 4);
|
||||
for (let x = 0; x < CHUNK_SIZE; x++) {
|
||||
const light = (region.block_light[from + x] << 4) | region.sky[from + x];
|
||||
section_light[to + x] = light;
|
||||
if (light !== section_light[0]) same = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
sections.push(same ? section_light[0] : section_light.slice());
|
||||
}
|
||||
return light;
|
||||
return sections;
|
||||
}
|
||||
|
||||
function post(message: FromChunkWorker, transfer: Transferable[]) {
|
||||
@@ -543,12 +555,12 @@ 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) {
|
||||
function region_value(chunks: (Section[] | null)[], index: number, y: number) {
|
||||
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)];
|
||||
return chunk ? chunk[y * CHUNK_AREA + (z % CHUNK_SIZE) * CHUNK_SIZE + (x % CHUNK_SIZE)] : AIR;
|
||||
return chunk ? chunk[y >> 4].get(section_index(x, y, z)) : AIR;
|
||||
}
|
||||
|
||||
// the surface of the fluid with this numeric id in a cell, if the cell is that fluid
|
||||
@@ -563,7 +575,7 @@ const fluid_cells: FluidCell[] = Array.from(
|
||||
);
|
||||
|
||||
// the numeric id of the fluid in a cell: the block, or what it's waterlogged with. 0 for none
|
||||
function cell_fluid(chunks: (Uint32Array | null)[], index: number, y: number) {
|
||||
function cell_fluid(chunks: (Section[] | null)[], index: number, y: number) {
|
||||
const id = region_block(region, index, y);
|
||||
if (fluid_range[id]) return id;
|
||||
const fluid = waterlog_fluid[id];
|
||||
@@ -572,7 +584,7 @@ function cell_fluid(chunks: (Uint32Array | null)[], index: number, y: number) {
|
||||
}
|
||||
|
||||
// the 3x3 cells around the fluid at index and y, see FluidCell. a waterlogged block is a source of its fluid
|
||||
function fill_fluid_cells(chunks: (Uint32Array | null)[], nid: number, index: number, y: number) {
|
||||
function fill_fluid_cells(chunks: (Section[] | null)[], nid: number, index: number, y: number) {
|
||||
for (let dz = -1; dz <= 1; dz++) {
|
||||
for (let dx = -1; dx <= 1; dx++) {
|
||||
const cell = fluid_cells[cell_index(dx, dz)];
|
||||
@@ -640,8 +652,8 @@ function underside(top: MeshQuad): MeshQuad {
|
||||
// region has to be filled and lit first
|
||||
// chunks are the 3x3 chunks' blocks with their states, the middle one's for models that change with them and the
|
||||
// ones around it for fluids, whose surface follows their neighbors'
|
||||
function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array | null)[], camera: number[]) {
|
||||
const values = chunks[4]!;
|
||||
function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Section[] | null)[], camera: number[]) {
|
||||
const middle = chunks[4]!;
|
||||
// solid and cutout get a buffer per face group, translucent one for everything since it's sorted instead
|
||||
const opaque = [SOLID, CUTOUT].map(() =>
|
||||
Array.from({ length: FACE_GROUPS }, () => ({ buffer: new QuadBuffer(), min: Infinity, max: -Infinity }))
|
||||
@@ -695,6 +707,12 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array
|
||||
};
|
||||
|
||||
for (let y = 0; y < region.height; y++) {
|
||||
const section = middle[y >> 4];
|
||||
// nothing to mesh in a section of air
|
||||
if (section.blocks === null && section.value === AIR) {
|
||||
y |= 15;
|
||||
continue;
|
||||
}
|
||||
for (let z = 0; z < CHUNK_SIZE; z++) {
|
||||
for (let x = 0; x < CHUNK_SIZE; x++) {
|
||||
// the middle chunk of the region
|
||||
@@ -738,7 +756,7 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array
|
||||
if (fluid_nid === block_nid) continue;
|
||||
}
|
||||
|
||||
const value = values[y * CHUNK_AREA + z * CHUNK_SIZE + x];
|
||||
const value = section.get(section_index(x, y, z));
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user