Water flowing
This commit is contained in:
+135
-32
@@ -52,6 +52,8 @@ import {
|
||||
region_sky,
|
||||
REGION_VOID,
|
||||
} from "./lighting.ts";
|
||||
import { cell_index, liquid_quads, type LiquidCell } from "./liquid_mesh.ts";
|
||||
import { liquid_height, liquid_textures } from "$/common/everything_registry.ts";
|
||||
|
||||
type TexturesInfo = Record<string, SpriteRegion>;
|
||||
|
||||
@@ -92,6 +94,9 @@ const block_occludes = new Uint8Array(TABLE_SIZE);
|
||||
// light can come around a corner past it
|
||||
const block_lets_light_by = new Uint8Array(TABLE_SIZE);
|
||||
const light_tables: LightTables = { opacity: new Uint8Array(TABLE_SIZE), emission: new Uint8Array(TABLE_SIZE) };
|
||||
// liquids: their range, 0 for blocks that aren't one, and where their level state is in the state bits
|
||||
const liquid_range = new Uint8Array(TABLE_SIZE);
|
||||
const liquid_level_shift = new Uint8Array(TABLE_SIZE);
|
||||
const region = new LightRegion();
|
||||
|
||||
// a model's quad ready for the mesher: uvs in the atlas, and for each corner how much of each of the face's
|
||||
@@ -137,7 +142,7 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
|
||||
const { solid, cutout, translucent, min_y, max_y } = make_chunk_mesh(
|
||||
message.chunk_x,
|
||||
message.chunk_z,
|
||||
message.chunks[4]!,
|
||||
message.chunks,
|
||||
message.camera,
|
||||
);
|
||||
const light = chunk_light();
|
||||
@@ -262,6 +267,11 @@ function build_block_tables() {
|
||||
block_lets_light_by[nid] = layer !== SOLID || opacity === 0 ? 1 : 0;
|
||||
light_tables.opacity[nid] = opacity;
|
||||
light_tables.emission[nid] = block_light_emission(block);
|
||||
liquid_range[nid] = block.liquid?.range ?? 0;
|
||||
if (block.liquid) {
|
||||
// the level state is the last one, see block_from_json
|
||||
liquid_level_shift[nid] = (block.states ?? []).slice(0, -1).reduce((bits, state) => bits + state.bits, 0);
|
||||
}
|
||||
});
|
||||
block_lets_light_by[AIR] = 1;
|
||||
block_layers[REGION_VOID] = SOLID;
|
||||
@@ -505,9 +515,79 @@ function light_quad(quad: MeshQuad, index: number, y: number, face_offsets: numb
|
||||
corner_ao.fill(1);
|
||||
}
|
||||
|
||||
// whether a block hides the face of a liquid against it, like show_face. air's layer isn't set, so it's checked first
|
||||
function hides_liquid(nid: number) {
|
||||
return nid !== AIR && block_layers[nid] === SOLID;
|
||||
}
|
||||
|
||||
// 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;
|
||||
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;
|
||||
}
|
||||
|
||||
// the surface of the liquid with this numeric id in a cell, if the cell is that liquid
|
||||
function liquid_cell_height(nid: number, value: number) {
|
||||
const level = ((value >>> 16) >>> liquid_level_shift[nid]) & 7;
|
||||
return liquid_height(level, liquid_range[nid]);
|
||||
}
|
||||
|
||||
const liquid_cells: LiquidCell[] = Array.from(
|
||||
{ length: 9 },
|
||||
() => ({ same: false, height: 0, air: false, solid: false, top_same: false }),
|
||||
);
|
||||
|
||||
// the 3x3 cells around the liquid at index and y, see LiquidCell
|
||||
function fill_liquid_cells(chunks: (Uint32Array | null)[], nid: number, index: number, y: number) {
|
||||
for (let dz = -1; dz <= 1; dz++) {
|
||||
for (let dx = -1; dx <= 1; dx++) {
|
||||
const cell = liquid_cells[cell_index(dx, dz)];
|
||||
const at = index + dx + dz * REGION_SIZE;
|
||||
const id = region_block(region, at, y);
|
||||
cell.same = id === nid;
|
||||
cell.height = cell.same ? liquid_cell_height(nid, region_value(chunks, at, y)) : 0;
|
||||
cell.air = id === AIR;
|
||||
cell.solid = hides_liquid(id);
|
||||
cell.top_same = region_block(region, at + REGION_LAYER, y + 1) === nid;
|
||||
}
|
||||
}
|
||||
return liquid_cells;
|
||||
}
|
||||
|
||||
// a liquid's quad for the mesher, see liquid_mesh.ts. it's lit like a full block's face on that side
|
||||
function liquid_mesh_quad(
|
||||
block: BlockRegistry,
|
||||
face: number,
|
||||
positions: number[],
|
||||
uvs: number[],
|
||||
still: boolean,
|
||||
): MeshQuad {
|
||||
const textures = liquid_textures(block.textures);
|
||||
const flat = FACE_AXIS[face] !== 1 || (positions[1] === positions[4] && positions[4] === positions[7] &&
|
||||
positions[7] === positions[10]);
|
||||
return {
|
||||
positions,
|
||||
uvs,
|
||||
face,
|
||||
cull: -1,
|
||||
flush: true,
|
||||
group: flat ? face : UNALIGNED_GROUP,
|
||||
shade: FACE_SHADE[face],
|
||||
sprite: textures_info[still ? textures.still : textures.flowing] ?? textures_info["engine:missing"],
|
||||
light_weights: light_weights(face, positions),
|
||||
tint: quad_tint(block, [still ? "still" : "flowing"]),
|
||||
};
|
||||
}
|
||||
|
||||
// region has to be filled and lit first
|
||||
// values is the middle chunk's blocks with their states, for models that change with them
|
||||
function make_chunk_mesh(chunk_x: number, chunk_z: number, values: Uint32Array, camera: number[]) {
|
||||
// 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 liquids, whose surface follows their neighbors'
|
||||
function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array | null)[], camera: number[]) {
|
||||
const values = 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 }))
|
||||
@@ -522,6 +602,39 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, values: Uint32Array,
|
||||
// where the neighbor on each face is, same order as FACE_NORMALS
|
||||
const face_offsets = FACE_NORMALS.map(([nx, ny, nz]) => nx + ny * REGION_LAYER + nz * REGION_SIZE);
|
||||
|
||||
// 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 wz = chunk_z * CHUNK_SIZE + z;
|
||||
const tint = quad.tint === GRASS
|
||||
? blended[z * CHUNK_SIZE + x]
|
||||
: quad.tint === FOLIAGE
|
||||
? blended[CHUNK_AREA + z * CHUNK_SIZE + x]
|
||||
: quad.tint;
|
||||
|
||||
if (layer_id !== TRANSLUCENT) {
|
||||
const group = opaque[layer_id][quad.group];
|
||||
push_quad(group.buffer, quad, wx, y, 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];
|
||||
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);
|
||||
// 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 + 2] = wz + (p[2] + p[8]) / 2;
|
||||
faces[q] = quad.face;
|
||||
}
|
||||
};
|
||||
|
||||
for (let y = 0; y < CHUNK_HEIGHT; y++) {
|
||||
for (let z = 0; z < CHUNK_SIZE; z++) {
|
||||
for (let x = 0; x < CHUNK_SIZE; x++) {
|
||||
@@ -534,8 +647,24 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, values: Uint32Array,
|
||||
const layer_id = block_layers[block_nid];
|
||||
const alpha = layer_id === TRANSLUCENT ? block_info.alpha ?? 1 : 1;
|
||||
|
||||
const wx = chunk_x * CHUNK_SIZE + x;
|
||||
const wz = chunk_z * CHUNK_SIZE + z;
|
||||
if (liquid_range[block_nid]) {
|
||||
const cells = fill_liquid_cells(chunks, block_nid, index, y);
|
||||
const above = region_block(region, index + REGION_LAYER, y + 1);
|
||||
const below = region_block(region, index - REGION_LAYER, y - 1);
|
||||
const quads = liquid_quads(
|
||||
cells,
|
||||
{ same: above === block_nid, solid: hides_liquid(above) },
|
||||
{ same: below === block_nid, solid: hides_liquid(below) },
|
||||
);
|
||||
for (const { face, positions, uvs, still } of quads) {
|
||||
const quad = liquid_mesh_quad(block_info, face, positions, uvs, still);
|
||||
light_quad(quad, index, y, face_offsets);
|
||||
// like minecraft's liquids, no ambient occlusion
|
||||
corner_ao.fill(1);
|
||||
emit(quad, layer_id, x, y, z, alpha);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const quad of block_quads(values[y * CHUNK_AREA + z * CHUNK_SIZE + x])) {
|
||||
if (quad.cull >= 0) {
|
||||
@@ -548,33 +677,7 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, values: Uint32Array,
|
||||
}
|
||||
|
||||
light_quad(quad, index, y, face_offsets);
|
||||
const tint = quad.tint === GRASS
|
||||
? blended[z * CHUNK_SIZE + x]
|
||||
: quad.tint === FOLIAGE
|
||||
? blended[CHUNK_AREA + z * CHUNK_SIZE + x]
|
||||
: quad.tint;
|
||||
|
||||
if (layer_id !== TRANSLUCENT) {
|
||||
const group = opaque[layer_id][quad.group];
|
||||
push_quad(group.buffer, quad, wx, y, 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];
|
||||
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);
|
||||
// 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 + 2] = wz + (p[2] + p[8]) / 2;
|
||||
faces[q] = quad.face;
|
||||
}
|
||||
emit(quad, layer_id, x, y, z, alpha);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user