Improve a buuuunch of textures and some reworks
This commit is contained in:
@@ -45,6 +45,8 @@ export interface Chunk {
|
||||
// blocks its generation put in neighboring chunks (leaves), as x, y, z, numeric id. kept so a neighbor that
|
||||
// generates later, or unloads and comes back, still gets them
|
||||
spills?: Int32Array;
|
||||
// each column's biome grass then foliage color, once it's generated
|
||||
biome_colors?: Uint32Array;
|
||||
}
|
||||
|
||||
export interface ChunkMesh {
|
||||
@@ -446,9 +448,12 @@ export class ClientLevel {
|
||||
chunk.mesh_version += 1;
|
||||
// copies, the worker lights the whole 3x3 area
|
||||
const chunks: (Uint32Array | null)[] = [];
|
||||
const biome_colors: (Uint32Array | null)[] = [];
|
||||
for (let dz = -1; dz <= 1; dz++) {
|
||||
for (let dx = -1; dx <= 1; dx++) {
|
||||
chunks.push(this.get_chunk(chunk.x + dx, chunk.z + dz)?.blocks.slice() ?? null);
|
||||
const neighbor = this.get_chunk(chunk.x + dx, chunk.z + dz);
|
||||
chunks.push(neighbor?.blocks.slice() ?? null);
|
||||
biome_colors.push(neighbor?.biome_colors ?? null);
|
||||
}
|
||||
}
|
||||
this.workers.post({
|
||||
@@ -457,6 +462,7 @@ export class ClientLevel {
|
||||
chunk_z: chunk.z,
|
||||
version: chunk.mesh_version,
|
||||
chunks,
|
||||
biome_colors,
|
||||
camera: [camera.x, camera.y, camera.z],
|
||||
}, chunks.filter((blocks) => blocks !== null).map((blocks) => blocks.buffer));
|
||||
}
|
||||
@@ -487,7 +493,7 @@ export class ClientLevel {
|
||||
|
||||
#on_worker_message(message: FromChunkWorker) {
|
||||
if (message.type === "generated") {
|
||||
this.#on_generated(message.chunk_x, message.chunk_z, message.blocks, message.spills);
|
||||
this.#on_generated(message.chunk_x, message.chunk_z, message.blocks, message.spills, message.biome_colors);
|
||||
} else if (message.type === "meshed") {
|
||||
this.#on_meshed(message);
|
||||
} else {
|
||||
@@ -495,7 +501,7 @@ export class ClientLevel {
|
||||
}
|
||||
}
|
||||
|
||||
#on_generated(cx: number, cz: number, blocks: Uint32Array, spills: Int32Array) {
|
||||
#on_generated(cx: number, cz: number, blocks: Uint32Array, spills: Int32Array, biome_colors: Uint32Array) {
|
||||
const key = chunk_key(cx, cz);
|
||||
// unloaded or cancelled while the worker was busy
|
||||
if (!this.pending_generation.delete(key)) {
|
||||
@@ -517,6 +523,7 @@ export class ClientLevel {
|
||||
chunk.generated = true;
|
||||
chunk.dirty = true;
|
||||
chunk.spills = spills;
|
||||
chunk.biome_colors = biome_colors;
|
||||
|
||||
// the same rules as the server (server/game/world.ts): a chunk's own blocks, then what its neighbors'
|
||||
// features put in it, only where it has air. both ways, since the neighbors may have generated first
|
||||
|
||||
@@ -5,7 +5,7 @@ import { get_sprite_region } from "$/client/sprites.ts";
|
||||
import type { ItemEntity } from "$/client/entity/item_entity.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import { push_vertex, Texture } from "$/client/renderer/mod.ts";
|
||||
import { block_item_texture, cube_face_texture } from "$/common/block_models.ts";
|
||||
import { block_item_texture, cube_face_texture, cube_face_tint } from "$/common/block_models.ts";
|
||||
import { item_model } from "./item_model.ts";
|
||||
|
||||
// how big items on the ground are drawn, minecraft's ground transform
|
||||
@@ -82,7 +82,8 @@ export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransf
|
||||
for (const face of CUBE_FACES) {
|
||||
const region = get_sprite_region(cube_face_texture(block, face.texture));
|
||||
const corners = face.corners.map(([cx, cy, cz]) => transform(cx - 0.5, cy, cz - 0.5));
|
||||
push_textured_quad(atlas, region, corners, face.shade);
|
||||
const [r, g, b] = cube_face_tint(block, face.texture);
|
||||
push_textured_quad(atlas, region, corners, [face.shade * r, face.shade * g, face.shade * b]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -100,7 +101,7 @@ export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransf
|
||||
}
|
||||
}
|
||||
|
||||
function push_textured_quad(atlas: Texture, region: { x: number; y: number }, corners: number[][], shade: number) {
|
||||
function push_textured_quad(atlas: Texture, region: { x: number; y: number }, corners: number[][], color: number[]) {
|
||||
const u0 = (region.x * TEXTURE_SIZE + UV_PAD) / atlas.width;
|
||||
const v0 = (region.y * TEXTURE_SIZE + UV_PAD) / atlas.height;
|
||||
const u1 = ((region.x + 1) * TEXTURE_SIZE - UV_PAD) / atlas.width;
|
||||
@@ -109,7 +110,7 @@ function push_textured_quad(atlas: Texture, region: { x: number; y: number }, co
|
||||
for (const i of [0, 1, 2, 0, 2, 3]) {
|
||||
const [px, py, pz] = corners[i];
|
||||
const [cu, cv] = CORNER_UVS[i];
|
||||
push_vertex(px, py, pz, cu ? u1 : u0, cv ? v1 : v0, shade, shade, shade, 1);
|
||||
push_vertex(px, py, pz, cu ? u1 : u0, cv ? v1 : v0, color[0], color[1], color[2], 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { get_sprite_region } from "$/client/sprites.ts";
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import { ItemStack } from "$/common/inventory.ts";
|
||||
import { block_item_texture, cube_face_texture } from "$/common/block_models.ts";
|
||||
import { block_item_texture, cube_face_texture, cube_face_tint } from "$/common/block_models.ts";
|
||||
import { draw_text, draw_texture_region, draw_texture_region_skewed, Texture } from "$/client/renderer/mod.ts";
|
||||
|
||||
// the corners keep their size (times scale), the edges stretch along and the middle fills the rest, like minecraft's
|
||||
@@ -146,6 +146,7 @@ function draw_item_block(block_info: BlockRegistry, x: number, y: number) {
|
||||
{ x: cx, y: cy + half_h },
|
||||
{ x: cx - half_w, y: cy },
|
||||
],
|
||||
...cube_face_tint(block_info, "top"),
|
||||
);
|
||||
draw_texture_region_skewed(
|
||||
atlas,
|
||||
@@ -159,6 +160,7 @@ function draw_item_block(block_info: BlockRegistry, x: number, y: number) {
|
||||
{ x: cx, y: cy + half_h + height },
|
||||
{ x: cx - half_w, y: cy + height },
|
||||
],
|
||||
...cube_face_tint(block_info, "side"),
|
||||
);
|
||||
draw_texture_region_skewed(
|
||||
atlas,
|
||||
@@ -172,5 +174,6 @@ function draw_item_block(block_info: BlockRegistry, x: number, y: number) {
|
||||
{ x: cx, y: cy + half_h + height },
|
||||
{ x: cx + half_w, y: cy + height },
|
||||
],
|
||||
...cube_face_tint(block_info, "front"),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user