Some optimization

This commit is contained in:
2026-10-25 21:31:32 -03:00
parent c955d8d143
commit f46058f9b5
5 changed files with 92 additions and 25 deletions
+1
View File
@@ -93,6 +93,7 @@ export class DebugOverlay {
text(`chunks: ${level.chunks.size} loaded, ${meshed} meshed, ${level_renderer.visible_chunks} in view`);
text(` ${level.dirty_count} waiting to mesh, ${level.meshes_in_flight} meshing`);
text(` meshes: ${(bytes / 1024 / 1024).toFixed(1)} MB`);
text(`entities: ${level.entities.size} loaded, ${level_renderer.drawn_entities} in view`);
separator();
text("switches");
+11 -7
View File
@@ -367,17 +367,21 @@ function push_sprite(
) {
const region = get_sprite_region(texture_id);
for (const quad of quads) {
for (const i of [0, 1, 2, 0, 2, 3]) {
const [cx, cy, cz] = quad.corners[i];
const [px, py, pz] = transform(cx, cy, cz + Math.sign(cz) * gap);
const [pu, pv] = quad.uvs[i];
const u = (region.x + pu) / atlas.width;
const v = (region.y + pv) / atlas.height;
vertex(px, py, pz, u, v, quad.shade * r, quad.shade * g, quad.shade * b, 1);
// each corner transformed once, though corners 0 and 2 are in both triangles
const points = quad.corners.map(([cx, cy, cz]) => transform(cx, cy, cz + Math.sign(cz) * gap));
for (const i of QUAD_CORNERS) {
const point = points[i];
const uv = quad.uvs[i];
const u = (region.x + uv[0]) / atlas.width;
const v = (region.y + uv[1]) / atlas.height;
vertex(point[0], point[1], point[2], u, v, quad.shade * r, quad.shade * g, quad.shade * b, 1);
}
}
}
// a quad's corners as two triangles
const QUAD_CORNERS = [0, 1, 2, 0, 2, 3];
// the layers drawn over a flat item's texture (see ItemLayer), each in its tint's color, without the ones whose tint
// has no color for this stack
export function item_layers(item: ItemStack): { texture: string; color: Vec3 }[] {
+34 -4
View File
@@ -47,10 +47,17 @@ export class LevelRenderer {
// what to draw, switched off in the f3 performance window to see what each costs
draw = { solid: true, cutout: true, translucent: true, entities: true, visuals: true };
// entities drawn this frame, for the f3 performance window
#drawn_entities = 0;
get visible_chunks() {
return this.#visible.length;
}
get drawn_entities() {
return this.#drawn_entities;
}
// solid and cutout terrain, drawn before entities
render_opaque(level: ClientLevel, camera: Camera) {
profiler.section("chunk upkeep", () => this.#find_visible(level, camera));
@@ -116,15 +123,18 @@ export class LevelRenderer {
return this.frustum.intersects_box(x, chunk.min_y, z, x + CHUNK_SIZE, chunk.max_y, z + CHUNK_SIZE);
}
// every entity but the one the camera is in
// every entity in view but the one the camera is in
render_entities(level: ClientLevel, camera: Camera, camera_entity: Entity, partial_tick: number) {
this.#drawn_entities = 0;
if (!this.draw.entities) return;
flush_batch();
const default_skin = AssetManager.instance.get<Texture>("bworld:skin");
// what they wear is drawn with the atlas, after every skin
const posed: { player: RemotePlayer; pose: PlayerPose; tint: Vec3 }[] = [];
for (const entity of level.entities.values()) {
if (entity !== camera_entity && entity instanceof RemotePlayer) {
if (
entity !== camera_entity && entity instanceof RemotePlayer && this.#entity_in_view(entity, partial_tick)
) {
const skin = entity.skin ?? default_skin;
use_texture(skin.tex);
const tint = entity_tint(entity, partial_tick, entity.hurt_time > 0 ? HURT_TINT : undefined);
@@ -138,9 +148,9 @@ export class LevelRenderer {
set_current_texture(level.image.tex);
for (const { player, pose, tint } of posed) render_worn(player, pose, tint);
for (const entity of level.entities.values()) {
if (entity instanceof ItemEntity) {
if (entity instanceof ItemEntity && this.#entity_in_view(entity, partial_tick)) {
render_item_entity(entity, partial_tick);
} else if (entity instanceof RemoteEntity) {
} else if (entity instanceof RemoteEntity && this.#entity_in_view(entity, partial_tick)) {
render_mod_entity(entity, camera, partial_tick);
}
}
@@ -148,6 +158,23 @@ export class LevelRenderer {
flush_batch();
}
// whether an entity's box, grown by ENTITY_CULL_MARGIN, is in view. entities are drawn on the cpu every frame, so
// the ones out of view are skipped
#entity_in_view(entity: Entity, partial_tick: number) {
const { x, y, z } = entity.render_position(partial_tick);
const half = entity.width / 2 + ENTITY_CULL_MARGIN;
const in_view = this.frustum.intersects_box(
x - half,
y - ENTITY_CULL_MARGIN,
z - half,
x + half,
y + entity.height + ENTITY_CULL_MARGIN,
z + half,
);
if (in_view) this.#drawn_entities += 1;
return in_view;
}
// blocks drawn with code, see client/rendering/visuals.ts. the ones in chunks in view get their on_frame first
render_visuals(level: ClientLevel, camera: Camera, partial_tick: number) {
if (!this.draw.visuals) return;
@@ -224,6 +251,9 @@ export class LevelRenderer {
#baked = new Map<number, BakedQuad[]>();
}
// how far models can stick out of their entity's box, like a swinging arm or a dropped item bobbing
const ENTITY_CULL_MARGIN = 0.5;
// minecraft's outline is black at 40%
const OUTLINE_COLOR = [0, 0, 0, 0.4] as const;
// half the line's width for each block it is away from the camera, about a pixel on a 1080p screen
+15 -9
View File
@@ -63,6 +63,8 @@ const FACES: { rect: (w: number, h: number, d: number) => number[]; corners: num
shade: 0.8,
},
];
// a face's corners as two triangles
const QUAD_CORNERS = [0, 1, 2, 0, 2, 3];
// how a player is posed this frame, for drawing them and what they wear
export function player_pose(player: Player, partial_tick: number): PlayerPose {
@@ -116,20 +118,24 @@ export function push_box(
emit: VertexSink = push_vertex,
) {
const [w, h, d] = size;
// the box's 8 corners, transformed once each rather than once per vertex. indexed by x * 4 + y * 2 + z, 0 for
// the low end and 1 for the high one
const points: Vec3[] = [];
for (let i = 0; i < 8; i++) {
points.push(transform(i & 4 ? high[0] : low[0], i & 2 ? high[1] : low[1], i & 1 ? high[2] : low[2]));
}
for (const face of FACES) {
const [rx, ry, rw, rh] = face.rect(w, h, d);
const [u0, v0] = to_uv(u + rx + UV_INSET, v + ry + UV_INSET);
const [u1, v1] = to_uv(u + rx + rw - UV_INSET, v + ry + rh - UV_INSET);
const [r, g, b] = tint.map((c) => c * face.shade);
const r = tint[0] * face.shade;
const g = tint[1] * face.shade;
const b = tint[2] * face.shade;
for (const i of [0, 1, 2, 0, 2, 3]) {
const [cx, cy, cz, cu, cv] = face.corners[i];
const [px, py, pz] = transform(
cx ? high[0] : low[0],
cy ? high[1] : low[1],
cz ? high[2] : low[2],
);
emit(px, py, pz, cu ? u1 : u0, cv ? v1 : v0, r, g, b, 1);
for (const i of QUAD_CORNERS) {
const corner = face.corners[i];
const point = points[corner[0] * 4 + corner[1] * 2 + corner[2]];
emit(point[0], point[1], point[2], corner[3] ? u1 : u0, corner[4] ? v1 : v0, r, g, b, 1);
}
}
}
+31 -5
View File
@@ -97,7 +97,7 @@ export function draw_item(item: ItemStack, x: number, y: number, show_count = it
const model = item_model_of(item);
const block_info = item_block(item);
if (model) {
draw_item_model(model, icon_matrix(x, y, model.display.gui));
draw_item_model(model, x, y);
} else if (block_info) {
draw_item_block(block_info, x, y);
} else {
@@ -177,10 +177,36 @@ function icon_matrix(x: number, y: number, gui?: DisplayTransform): mat4 {
return mat4.fromValues(size, -size / 2, -1, 0, 0, -size, 1, 0, -size, -size / 2, -1, 0, cx, cy + size * 1.5, 0, 1);
}
// each model's icon in a slot at 0, 0, as x y u v r g b per vertex. a slot's place only moves the icon (see
// icon_matrix), so it's worked out once per model and moved to each slot, rather than every slot every frame
const model_icons = new WeakMap<ItemModel, Float32Array>();
// an item's model in a slot: its faces that face the viewer, back to front
function draw_item_model(model: ItemModel, matrix: mat4) {
function draw_item_model(model: ItemModel, x: number, y: number) {
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
use_texture(atlas.tex);
let icon = model_icons.get(model);
if (!icon) {
icon = model_icon(model, atlas);
model_icons.set(model, icon);
}
for (let i = 0; i < icon.length; i += 7) {
push_vertex(
x + icon[i],
y + icon[i + 1],
0,
icon[i + 2],
icon[i + 3],
icon[i + 4],
icon[i + 5],
icon[i + 6],
1,
);
}
}
function model_icon(model: ItemModel, atlas: Texture): Float32Array {
const matrix = icon_matrix(0, 0, model.display.gui);
// a mirroring matrix (like the y flip to the screen's y down) turns which way a face's corners go round
const flip = Math.sign(mat4.determinant(matrix));
const visible = model.quads
@@ -197,24 +223,24 @@ function draw_item_model(model: ItemModel, matrix: mat4) {
.filter(({ facing }) => facing > 1e-6)
.sort((a, b) => a.depth - b.depth);
const vertices: number[] = [];
for (const { quad, corners } of visible) {
const region = get_sprite_region(quad.texture);
const [r, g, b] = model.block ? item_tint(model.block, quad.variables) : [1, 1, 1];
const shade = quad.shade ? ITEM_FACE_SHADE[quad.face] : 1;
for (const i of [0, 1, 2, 0, 2, 3]) {
push_vertex(
vertices.push(
corners[i][0],
corners[i][1],
0,
sprite_u(atlas, region, quad.uvs[i * 2]),
sprite_v(atlas, region, quad.uvs[i * 2 + 1]),
shade * r,
shade * g,
shade * b,
1,
);
}
}
return new Float32Array(vertices);
}
function draw_item_block(block_info: BlockRegistry, x: number, y: number) {