Improve a buuuunch of textures and some reworks

This commit is contained in:
2026-09-27 18:05:20 -03:00
parent 9f47c709f1
commit b7ded817d9
186 changed files with 1486 additions and 124 deletions
+4
View File
@@ -46,6 +46,8 @@ export type ToChunkWorker =
version: number;
// copies of the blocks of the 3x3 chunks around it, going +x then +z from -x -z, for lighting
chunks: (Uint32Array | null)[];
// the same chunks' biome colors (see "generated"), blended across their edges
biome_colors: (Uint32Array | null)[];
// where the camera is, to sort the translucent quads
camera: number[];
}
@@ -75,6 +77,8 @@ export type FromChunkWorker =
blocks: Uint32Array;
// blocks that landed in other chunks (from features like a mod's trees), flattened as x, y, z, numeric id
spills: Int32Array;
// each column's biome grass color as 0xRRGGBB, x then z, then each column's foliage color
biome_colors: Uint32Array;
}
| {
type: "meshed";
+94 -10
View File
@@ -26,9 +26,17 @@ import {
UNALIGNED_GROUP,
} from "./chunk_messages.ts";
import { generate_raw_chunk, WorldgenSetup } from "$/common/generation.ts";
import { foliage_color, grass_color } from "$/common/worldgen/biome_colors.ts";
import { load_worldgen } from "$/common/worldgen_loader.ts";
import { default_block_value, get_state_value } from "$/common/utils.ts";
import { bake_model, block_variant, FACE_CORNERS, find_model, type ModelJson } from "$/common/block_models.ts";
import {
bake_model,
block_variant,
FACE_CORNERS,
find_model,
is_tinted,
type ModelJson,
} from "$/common/block_models.ts";
import {
choose_sort_type,
FACE_AXIS,
@@ -105,6 +113,8 @@ interface MeshQuad {
sprite: SpriteRegion;
// 4 weights per corner
light_weights: number[];
// 0xRRGGBB it's drawn in, GRASS or FOLIAGE for the biome's (see "tint"), or white
tint: number;
}
let models: Record<string, ModelJson> = {};
@@ -142,6 +152,7 @@ self.onmessage = async (event: MessageEvent<ToChunkWorker>) => {
case "mesh": {
region.fill(message.chunks);
region.compute(light_tables);
blend_biome_colors(message.biome_colors);
const { solid, cutout, translucent, min_y, max_y } = make_chunk_mesh(
message.chunk_x,
message.chunk_z,
@@ -193,8 +204,66 @@ function post(message: FromChunkWorker, transfer: Transferable[]) {
}
function generate(chunk_x: number, chunk_z: number, seed: string) {
const { blocks, spills } = generate_raw_chunk(chunk_x, chunk_z, seed, block_ids, worldgen, default_values);
post({ type: "generated", chunk_x, chunk_z, blocks, spills }, [blocks.buffer, spills.buffer]);
const { blocks, spills, biomes } = generate_raw_chunk(chunk_x, chunk_z, seed, block_ids, worldgen, default_values);
const biome_colors = new Uint32Array(CHUNK_AREA * 2);
biomes.forEach((biome, column) => {
biome_colors[column] = grass_color(biome);
biome_colors[CHUNK_AREA + column] = foliage_color(biome);
});
post({ type: "generated", chunk_x, chunk_z, blocks, spills, biome_colors }, [
blocks.buffer,
spills.buffer,
biome_colors.buffer,
]);
}
// like minecraft's biome blend at its default radius: each column's grass and foliage color is the average of the 5x5
// columns around it, so biome edges fade instead of being a line. neighbors that aren't there are left out
const BLEND_RADIUS = 2;
// the middle chunk's blended colors, x then z, grass then foliage
const blended = new Uint32Array(CHUNK_AREA * 2);
// what MeshQuad.tint is for the biome's colors, they aren't colors since those only go up to 0xffffff
const GRASS = -1;
const FOLIAGE = -2;
function blend_biome_colors(colors: (Uint32Array | null)[]) {
for (const offset of [0, CHUNK_AREA]) {
for (let z = 0; z < CHUNK_SIZE; z++) {
for (let x = 0; x < CHUNK_SIZE; x++) {
let r = 0;
let g = 0;
let b = 0;
let count = 0;
for (let dz = -BLEND_RADIUS; dz <= BLEND_RADIUS; dz++) {
for (let dx = -BLEND_RADIUS; dx <= BLEND_RADIUS; dx++) {
// which of the 3x3 chunks, and where in it
const sx = x + dx + CHUNK_SIZE;
const sz = z + dz + CHUNK_SIZE;
const chunk = colors[Math.floor(sz / CHUNK_SIZE) * 3 + Math.floor(sx / CHUNK_SIZE)];
if (!chunk) continue;
const color = chunk[offset + (sz % CHUNK_SIZE) * CHUNK_SIZE + (sx % CHUNK_SIZE)];
r += color >> 16;
g += (color >> 8) & 0xff;
b += color & 0xff;
count++;
}
}
const color = count === 0
? 0xffffff
: (Math.round(r / count) << 16) | (Math.round(g / count) << 8) | Math.round(b / count);
blended[offset + z * CHUNK_SIZE + x] = color;
}
}
}
}
// a quad's MeshQuad.tint from its block's "tint"
function quad_tint(block: BlockRegistry, variables: string[]) {
if (!is_tinted(block, variables)) return 0xffffff;
const color = block.tint!.color;
if (color === "grass") return GRASS;
if (color === "foliage") return FOLIAGE;
return parseInt(color.slice(1), 16);
}
function build_block_tables() {
@@ -319,7 +388,16 @@ class QuadBuffer {
let mesh_min_y = Infinity;
let mesh_max_y = -Infinity;
function push_quad(buffer: QuadBuffer, quad: MeshQuad, x: number, y: number, z: number, alpha: number) {
// tint is 0xRRGGBB, multiplied into the quad's color
function push_quad(
buffer: QuadBuffer,
quad: MeshQuad,
x: number,
y: number,
z: number,
alpha: number,
tint = 0xffffff,
) {
buffer.reserve();
const { f32, u16, bytes } = buffer;
const sprite = quad.sprite;
@@ -336,10 +414,10 @@ function push_quad(buffer: QuadBuffer, quad: MeshQuad, x: number, y: number, z:
f32[byte / 4 + 2] = z + quad.positions[corner * 3 + 2];
u16[byte / 2 + 6] = Math.round(atlas_u(sprite, quad.uvs[corner * 2]) * 65535);
u16[byte / 2 + 7] = Math.round(atlas_v(sprite, quad.uvs[corner * 2 + 1]) * 65535);
const brightness = Math.round(quad.shade * corner_ao[corner] * 255);
bytes[byte + 16] = brightness;
bytes[byte + 17] = brightness;
bytes[byte + 18] = brightness;
const brightness = quad.shade * corner_ao[corner];
bytes[byte + 16] = Math.round(brightness * (tint >> 16));
bytes[byte + 17] = Math.round(brightness * ((tint >> 8) & 0xff));
bytes[byte + 18] = Math.round(brightness * (tint & 0xff));
bytes[byte + 19] = alpha_byte;
// where to read the lightmap, block light across and sky light down
bytes[byte + 20] = Math.round((corner_block[corner] + 0.5) / 16 * 255);
@@ -385,6 +463,7 @@ function block_quads(value: number): MeshQuad[] {
shade: quad.shade ? FACE_SHADE[quad.face] : 1,
sprite: textures_info[quad.texture] ?? textures_info["engine:missing"],
light_weights: light_weights(quad.face, quad.positions),
tint: quad_tint(block, quad.variables),
}));
baked.set(key, quads);
return quads;
@@ -490,10 +569,15 @@ 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);
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];
@@ -501,7 +585,7 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, values: Uint32Array,
if (plane > group.max) group.max = plane;
}
} else {
push_quad(translucent_quads, quad, wx, y, wz, alpha);
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);