Block visuals

This commit is contained in:
2026-10-13 23:52:30 -03:00
parent 55ad08266e
commit 6ad476ac18
19 changed files with 854 additions and 14 deletions
+10
View File
@@ -32,6 +32,7 @@ import type { Entity } from "../entity/entity.ts";
import type { ModelJson } from "$/common/block_models.ts";
import { clip_shape, collision_shape_at, FULL_SHAPE, type Shape, shape_at } from "$/common/block_shapes.ts";
import { BlockPredictions } from "./block_predictions.ts";
import { block_visuals, Visuals } from "../rendering/visuals.ts";
export interface Block {
id: string;
@@ -126,6 +127,8 @@ export class ClientLevel {
tick_timer = 0;
// world time in ticks, counted here between the server's updates. see common/time.ts
time = 0;
// ticks since this client joined, which never jumps like time can, for what moves at a steady speed
ticks = 0;
// in chunks, what the server was asked to send
view_distance: number;
// the chunk the server says the player is in
@@ -142,6 +145,7 @@ export class ClientLevel {
entities = new Map<string, Entity>();
// the client data of blocks that have some, see BlockRef.set_client_data, by chunk_key then position_key "x,y,z"
block_data = new Map<number, Map<string, BlockData>>();
visuals = new Visuals((x, y, z) => this.get_block_data(x, y, z));
// server_blocks is the server's block id for each of its numeric ids
constructor(server_blocks: string[], view_distance: number) {
@@ -165,6 +169,7 @@ export class ClientLevel {
models: Object.fromEntries(EverythingRegistry.entries<ModelJson>("models")),
textures_info: AssetManager.instance.get("bworld:textures_info"),
image: { width: this.image.width, height: this.image.height },
visuals: [...block_visuals].map(([nid, visual]) => [nid, visual.replaces_model]),
});
}
@@ -178,6 +183,7 @@ export class ClientLevel {
tick() {
this.time += 1;
this.ticks += 1;
for (const entity of this.entities.values()) {
entity.save_previous_position();
entity.tick();
@@ -253,6 +259,7 @@ export class ClientLevel {
this.workers.terminate();
for (const chunk of this.chunks.values()) {
this.delete_chunk_mesh(chunk);
this.visuals.unload(chunk.x, chunk.z);
}
this.chunks.clear();
this.#dirty.clear();
@@ -525,6 +532,7 @@ export class ClientLevel {
this.chunks.delete(key);
this.#dirty.delete(chunk);
this.block_data.delete(key);
this.visuals.unload(cx, cz);
}
// see block_data in common/protocol.ts
@@ -547,6 +555,7 @@ export class ClientLevel {
} else if (blocks?.delete(position) && blocks.size === 0) {
this.block_data.delete(key);
}
this.visuals.data_changed(x, y, z);
}
get_block_data(x: number, y: number, z: number): Readonly<Record<string, unknown>> | undefined {
@@ -643,6 +652,7 @@ export class ClientLevel {
chunk.light = message.light;
chunk.min_y = message.min_y;
chunk.max_y = message.max_y;
this.visuals.meshed(chunk.x, chunk.z, message.visuals);
for (const layer of RENDER_LAYERS) {
const { vertices, quad_count, groups } = message[layer];