Rendering performace on F3
This commit is contained in:
@@ -5,6 +5,7 @@ import { Hud } from "$/client/gui/hud.ts";
|
||||
import { DebugOverlay } from "$/client/gui/debug_overlay.ts";
|
||||
import { LevelRenderer } from "./level_renderer.ts";
|
||||
import { HandRenderer } from "./hand_renderer.ts";
|
||||
import { profiler } from "$/client/profiler.ts";
|
||||
|
||||
// the sky at noon and in the middle of the night
|
||||
const DAY_SKY = [0.69, 0.8, 1];
|
||||
@@ -40,17 +41,22 @@ export class GameRenderer {
|
||||
this.#setup_sky(client, partial_tick);
|
||||
|
||||
begin_mode_3d(camera);
|
||||
this.level_renderer.render_opaque(client.level, camera);
|
||||
this.level_renderer.render_destroy_progress(client.game_mode);
|
||||
this.level_renderer.render_entities(client.level, camera, client.player, partial_tick);
|
||||
this.level_renderer.render_translucent(client.level, camera);
|
||||
const level = this.level_renderer;
|
||||
level.render_opaque(client.level, camera);
|
||||
profiler.section("entities", () => {
|
||||
level.render_destroy_progress(client.game_mode);
|
||||
level.render_entities(client.level, camera, client.player, partial_tick);
|
||||
});
|
||||
profiler.section("translucent", () => level.render_translucent(client.level, camera));
|
||||
end_mode_3d();
|
||||
this.hand.render(client.player, partial_tick);
|
||||
profiler.section("hand", () => this.hand.render(client.player, partial_tick));
|
||||
|
||||
this.hud.render(client);
|
||||
client.screen?.on_render();
|
||||
profiler.section("hud", () => {
|
||||
this.hud.render(client);
|
||||
client.screen?.on_render();
|
||||
});
|
||||
if (client.debugging) {
|
||||
this.debug_overlay.render(client);
|
||||
profiler.section("debug overlay", () => this.debug_overlay.render(client));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { type Chunk, CHUNK_SIZE, type ChunkMesh, ClientLevel } from "$/client/level/client_level.ts";
|
||||
import { profiler } from "$/client/profiler.ts";
|
||||
import { Camera } from "$/client/camera.ts";
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import { get_sprite_region } from "$/client/sprites.ts";
|
||||
@@ -47,9 +48,32 @@ export class LevelRenderer {
|
||||
frustum = new Frustum();
|
||||
// chunks in view this frame, worked out once for all the layers
|
||||
#visible: Chunk[] = [];
|
||||
// what to draw, switched off in the f3 performance window to see what each costs
|
||||
draw = { solid: true, cutout: true, translucent: true, entities: true };
|
||||
|
||||
get visible_chunks() {
|
||||
return this.#visible.length;
|
||||
}
|
||||
|
||||
// solid and cutout terrain, drawn before entities
|
||||
render_opaque(level: ClientLevel, camera: Camera) {
|
||||
profiler.section("chunk upkeep", () => this.#find_visible(level, camera));
|
||||
set_current_texture(level.image.tex);
|
||||
for (const layer of ["solid", "cutout"] as const) {
|
||||
if (!this.draw[layer]) continue;
|
||||
profiler.section(layer, () => {
|
||||
for (const chunk of this.#visible) {
|
||||
const mesh = chunk.meshes[layer];
|
||||
if (mesh) {
|
||||
draw_facing_camera(layer, mesh, camera);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// asks for meshes and sorting, then works out which chunks are in view, near to far
|
||||
#find_visible(level: ClientLevel, camera: Camera) {
|
||||
level.request_meshes(camera);
|
||||
level.update_translucent_sorting(camera);
|
||||
|
||||
@@ -68,22 +92,12 @@ export class LevelRenderer {
|
||||
return dx * dx + dz * dz;
|
||||
};
|
||||
this.#visible.sort((a, b) => distance_sq(a) - distance_sq(b));
|
||||
|
||||
set_current_texture(level.image.tex);
|
||||
|
||||
for (const layer of ["solid", "cutout"] as const) {
|
||||
for (const chunk of this.#visible) {
|
||||
const mesh = chunk.meshes[layer];
|
||||
if (mesh) {
|
||||
draw_facing_camera(layer, mesh, camera);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// translucent terrain, drawn after entities so they show through water and glass.
|
||||
// chunks go back to front, and each chunk's quads are already sorted back to front
|
||||
render_translucent(_level: ClientLevel, camera: Camera) {
|
||||
if (!this.draw.translucent) return;
|
||||
const distance_sq = (x: number, z: number) => {
|
||||
const dx = (x + 0.5) * CHUNK_SIZE - camera.x;
|
||||
const dz = (z + 0.5) * CHUNK_SIZE - camera.z;
|
||||
@@ -107,6 +121,7 @@ export class LevelRenderer {
|
||||
|
||||
// every entity but the one the camera is in
|
||||
render_entities(level: ClientLevel, camera: Camera, camera_entity: Entity, partial_tick: number) {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user