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"; import type { MultiPlayerGameMode } from "$/client/game_mode.ts"; import type { Entity } from "$/client/entity/entity.ts"; import { RemotePlayer } from "$/client/entity/remote_player.ts"; import { ItemEntity } from "$/client/entity/item_entity.ts"; import { render_block_items, render_item_entity } from "./item_renderer.ts"; import { HURT_TINT, player_pose, render_player } from "./player_renderer.ts"; import { render_worn } from "./worn_renderer.ts"; import type { PlayerPose } from "./player_model.ts"; import type { Vec3 } from "./model_math.ts"; import { render_mod_entity } from "./entity_renderer.ts"; import { entity_tint } from "./entity_light.ts"; import { RemoteEntity } from "$/client/entity/remote_entity.ts"; import { draw_terrain, flush_batch, get_current_texture, push_back_face, push_bottom_face, push_front_face, push_left_face, push_right_face, push_top_face, set_current_texture, Texture, view_projection, } from "$/client/renderer/mod.ts"; import type { RenderLayer } from "$/common/everything_registry.ts"; import { FACE_GROUPS, UNALIGNED_GROUP } from "$/client/workers/chunk_messages.ts"; import { FACE_AXIS, FACE_NORMALS } from "$/client/workers/translucent_sort.ts"; import { Frustum } from "./frustum.ts"; const BREAKING_FACES = [ push_back_face, push_bottom_face, push_front_face, push_left_face, push_right_face, push_top_face, ]; // draws the level, like minecraft's LevelRenderer: terrain in layers, block breaking and entities 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); this.frustum.update(view_projection()); this.#visible.length = 0; for (const chunk of level.chunks.values()) { if (Object.keys(chunk.meshes).length > 0 && this.#in_view(chunk)) { this.#visible.push(chunk); } } // near to far, like sodium, so what's in front fills the depth buffer first and the gpu can skip shading what's // behind it const distance_sq = (chunk: Chunk) => { const dx = (chunk.x + 0.5) * CHUNK_SIZE - camera.x; const dz = (chunk.z + 0.5) * CHUNK_SIZE - camera.z; return dx * dx + dz * dz; }; this.#visible.sort((a, b) => distance_sq(a) - distance_sq(b)); } // 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; return dx * dx + dz * dz; }; const chunks = this.#visible .filter((chunk) => chunk.meshes.translucent) .map((chunk) => ({ mesh: chunk.meshes.translucent!, distance: distance_sq(chunk.x, chunk.z) })) .sort((a, b) => b.distance - a.distance); for (const { mesh } of chunks) { draw_terrain("translucent", mesh.vertex_buffer, 0, mesh.quad_count, mesh.index_buffer); } } #in_view(chunk: Chunk) { const x = chunk.x * CHUNK_SIZE; const z = chunk.z * CHUNK_SIZE; 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 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("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) { const skin = entity.skin ?? default_skin; if (get_current_texture() !== skin.tex) { flush_batch(); set_current_texture(skin.tex); } const tint = entity_tint(entity, partial_tick, entity.hurt_time > 0 ? HURT_TINT : undefined); const pose = player_pose(entity, partial_tick); render_player(skin, pose, tint); posed.push({ player: entity, pose, tint }); } } flush_batch(); 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) { render_item_entity(entity, partial_tick); } else if (entity instanceof RemoteEntity) { render_mod_entity(entity, camera, partial_tick); } } render_block_items(level); flush_batch(); } // the cracks on the block being broken render_destroy_progress(game_mode: MultiPlayerGameMode) { const block = game_mode.destroy_pos; if (!block) { return; } const progress = Math.max(0, Math.min(1, game_mode.destroy_progress / game_mode.destroy_time)); const stage = Math.round(progress * 8); if (Number.isNaN(stage)) { return; } const tex = AssetManager.instance.get("bworld:textures"); const region = get_sprite_region(`engine:break_${stage}`); for (const push_face of BREAKING_FACES) { push_face( tex, block.x, block.y, block.z, region.x, region.y, region.width, region.height, ); } } } // only the face groups that can face the camera: quads facing +x can only be seen from beyond the lowest x plane any // of them lie on. neighboring groups that are both drawn go in one draw call function draw_facing_camera(layer: RenderLayer, mesh: ChunkMesh, camera: Camera) { const groups = mesh.groups; if (!groups) { draw_terrain(layer, mesh.vertex_buffer, 0, mesh.quad_count); return; } let start = 0; let end = 0; for (let g = 0; g < FACE_GROUPS; g++) { const group = groups[g]; if (group.count === 0 || !group_faces_camera(g, group.min, group.max, camera)) { continue; } if (group.first !== end) { draw_terrain(layer, mesh.vertex_buffer, start, end - start); start = group.first; } end = group.first + group.count; } draw_terrain(layer, mesh.vertex_buffer, start, end - start); } export function group_faces_camera( group: number, min: number, max: number, camera: { x: number; y: number; z: number }, ) { if (group === UNALIGNED_GROUP) { return true; } const axis = FACE_AXIS[group]; const position = axis === 0 ? camera.x : axis === 1 ? camera.y : camera.z; return FACE_NORMALS[group][axis] > 0 ? position > min : position < max; }