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
+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