Files
bworld/client/rendering/game_renderer.ts
T
2026-11-07 12:13:43 -03:00

108 lines
4.1 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import type { Client } from "@/client/client.ts";
import type { Camera } from "@/client/camera.ts";
import type { ClientLevel } from "@/client/level/client_level.ts";
import {
begin_mode_3d,
canvas,
clear_background,
draw_rect,
end_mode_3d,
set_render_time,
update_lightmap,
} from "@/client/renderer/mod.ts";
import { daylight } from "@/common/time.ts";
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 { render_nameplates } from "./nameplate_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];
const NIGHT_SKY = [0.02, 0.03, 0.08];
// minecraft's darkest sky: moonlight still lights things a little
const NIGHT_SKY_LIGHT = 0.2;
// how much of the screen is the water's color with the camera under water
const UNDERWATER_ALPHA = 0.4;
// draws a frame, like minecraft's GameRenderer: the level from the camera, then the hud and screens on top
export class GameRenderer {
level_renderer = new LevelRenderer();
hud = new Hud();
debug_overlay = new DebugOverlay();
hand = new HandRenderer();
// what the lightmap was last made for, so it's only rebuilt when the light changes
#lightmap_daylight = -1;
// sky color and sky light for the time of day
#setup_sky(level: ClientLevel, partial_tick: number) {
const light = daylight(level.time + partial_tick);
const [r, g, b] = DAY_SKY.map((day, i) => NIGHT_SKY[i] + (day - NIGHT_SKY[i]) * light);
clear_background(r, g, b, 1);
if (Math.abs(light - this.#lightmap_daylight) > 0.001) {
this.#lightmap_daylight = light;
update_lightmap(NIGHT_SKY_LIGHT + (1 - NIGHT_SKY_LIGHT) * light);
}
}
// with the camera in water, the screen and the hand are washed in the biome's water color, like minecraft's
// underwater overlay
#render_underwater(client: Client) {
const { x, y, z } = client.camera;
const [bx, by, bz] = [Math.floor(x), Math.floor(y), Math.floor(z)];
if (!client.level.is_fluid(bx, by, bz)) return;
const color = client.level.water_color(bx, by, bz);
draw_rect(0, 0, canvas.width, canvas.height, [
(color >> 16) / 255,
((color >> 8) & 0xff) / 255,
(color & 0xff) / 255,
UNDERWATER_ALPHA,
]);
}
// partial_tick is how far this frame is between the last tick and the next, entities are drawn in between
render(client: Client, partial_tick: number) {
client.camera.setup(client.player, partial_tick);
this.render_level(client.level, client.camera, partial_tick, client);
profiler.section("hand", () => this.hand.render(client.player, partial_tick));
this.#render_underwater(client);
profiler.section("hud", () => {
this.hud.render(client);
client.screen?.on_render();
});
if (client.debugging) {
profiler.section("debug overlay", () => this.debug_overlay.render(client));
}
}
// the sky and the level from the camera. with a viewer, the camera is in their player, who isn't drawn, and what
// they're breaking and looking at shows. the benchmark draws its level without one
render_level(
level: ClientLevel,
camera: Camera,
partial_tick: number,
viewer?: Pick<Client, "player" | "game_mode" | "hit_result">,
) {
this.#setup_sky(level, partial_tick);
set_render_time(level.ticks + partial_tick);
begin_mode_3d(camera);
const renderer = this.level_renderer;
renderer.render_opaque(level, camera);
profiler.section("entities", () => {
renderer.render_destroy_progress(level, viewer?.game_mode);
renderer.render_block_outline(level, viewer?.hit_result, camera);
renderer.render_entities(level, camera, viewer?.player, partial_tick);
});
profiler.section("visuals", () => renderer.render_visuals(level, camera, partial_tick));
profiler.section("translucent", () => renderer.render_translucent(level, camera));
profiler.section("nameplates", () => render_nameplates(level, camera, viewer?.player, partial_tick));
end_mode_3d();
}
}