Render regions, disabled by default

This commit is contained in:
2026-11-07 19:35:42 -03:00
parent 206cd38f26
commit c639d1231b
8 changed files with 424 additions and 36 deletions
+47 -16
View File
@@ -29,13 +29,14 @@ import {
flush_batch,
get_current_texture,
push_vertex,
render_debug,
set_current_texture,
Texture,
use_texture,
view_projection,
white_tex,
} from "@/client/renderer/mod.ts";
import { type BlockRegistry, EverythingRegistry, type RenderLayer } from "@/common/everything_registry.ts";
import { type BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
import { bake_block, type BakedQuad, FACE_INDEX, looks_by_state, type ModelJson } from "@/common/block_models.ts";
import { BREAK_STAGES, ID_MASK } from "@/common/constants.ts";
import { type Shape, shape_at } from "@/common/block_shapes.ts";
@@ -45,6 +46,7 @@ import { FACE_AXIS, FACE_NORMALS } from "@/client/workers/translucent_sort.ts";
import { Frustum } from "./frustum.ts";
import { draw_visuals } from "./visuals.ts";
import { reachable_sections } from "./occlusion.ts";
import { REGION_LAYERS, type RegionLayer, type RenderRegion } from "@/client/level/render_region.ts";
import { SECTION_HEIGHT } from "@/common/world_shape.ts";
// draws the level, like minecraft's LevelRenderer: terrain in layers, block breaking and entities
@@ -52,8 +54,9 @@ export class LevelRenderer {
frustum = new Frustum();
// sections in view this frame, near to far, worked out once for all the layers
#visible: RenderSection[] = [];
// the chunks they're in
// the chunks they're in, and the regions with the ones in each, the regions near to far too
#visible_chunks = new Set<Chunk>();
#visible_regions = new Map<RenderRegion, RenderSection[]>();
// what the camera could see the last time it was searched for, which only changes when it moves to another section
// or the sections change, see occlusion.ts
#reachable: { sections: RenderSection[] | undefined; level: ClientLevel; version: number; key: string } | undefined;
@@ -75,15 +78,14 @@ export class LevelRenderer {
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) {
for (const layer of REGION_LAYERS) {
if (!this.draw[layer]) continue;
profiler.section(layer, () => {
for (const section of this.#visible) {
const mesh = section.meshes[layer];
if (mesh) {
draw_facing_camera(layer, mesh, camera);
}
if (mesh?.vertex_buffer) draw_facing_camera(layer, mesh.vertex_buffer, mesh, camera);
}
for (const [region, sections] of this.#visible_regions) draw_region(layer, region, sections, camera);
});
}
}
@@ -94,8 +96,11 @@ export class LevelRenderer {
level.request_meshes(camera);
level.update_translucent_sorting(camera);
level.use_render_regions(render_debug.render_regions);
level.update_regions();
this.frustum.update(view_projection());
this.#visible_chunks.clear();
this.#visible_regions.clear();
const reachable = this.#reachable_sections(level, camera);
if (reachable) {
this.#visible = reachable.filter((section) => this.#in_view(section));
@@ -106,7 +111,12 @@ export class LevelRenderer {
.sort((a, b) => a.distance - b.distance)
.map(({ section }) => section);
}
for (const section of this.#visible) this.#visible_chunks.add(section.chunk);
for (const section of this.#visible) {
this.#visible_chunks.add(section.chunk);
const in_region = this.#visible_regions.get(section.region);
if (in_region) in_region.push(section);
else this.#visible_regions.set(section.region, [section]);
}
}
#reachable_sections(level: ClientLevel, camera: Camera) {
@@ -133,7 +143,7 @@ export class LevelRenderer {
.sort((a, b) => b.distance - a.distance);
for (const { mesh } of sections) {
draw_terrain("translucent", mesh.vertex_buffer, 0, mesh.quad_count, mesh.index_buffer);
draw_terrain("translucent", mesh.vertex_buffer!, 0, mesh.quad_count, mesh.index_buffer);
}
}
@@ -412,12 +422,8 @@ function decal_uv(face: number, x: number, y: number, z: number): [number, numbe
// 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;
}
function draw_facing_camera(layer: RegionLayer, buffer: GPUBuffer, mesh: ChunkMesh, camera: Camera) {
const groups = mesh.groups!;
let start = 0;
let end = 0;
for (let g = 0; g < FACE_GROUPS; g++) {
@@ -426,12 +432,37 @@ function draw_facing_camera(layer: RenderLayer, mesh: ChunkMesh, camera: Camera)
continue;
}
if (group.first !== end) {
draw_terrain(layer, mesh.vertex_buffer, start, end - start);
draw_terrain(layer, buffer, start, end - start);
start = group.first;
}
end = group.first + group.count;
}
draw_terrain(layer, mesh.vertex_buffer, start, end - start);
draw_terrain(layer, buffer, start, end - start);
}
// a region's sections in view, a face group at a time (see render_region.ts), only where the group can face the camera:
// quads facing +x can only be seen from beyond the lowest x plane any of them lie on. sections next to each other in the
// buffer that are both drawn go in one draw call
function draw_region(layer: RegionLayer, region: RenderRegion, in_view: RenderSection[], camera: Camera) {
const buffer = region.layers[layer].buffer;
if (!buffer) return;
// in the order they're in the buffer
const sections = in_view.filter((section) => section.meshes[layer]?.slot !== undefined)
.sort((a, b) => a.meshes[layer]!.slot! - b.meshes[layer]!.slot!);
for (let g = 0; g < FACE_GROUPS; g++) {
let start = 0;
let end = 0;
for (const section of sections) {
const group = section.meshes[layer]!.groups![g];
if (group.count === 0 || !group_faces_camera(g, group.min, group.max, camera)) continue;
if (group.first !== end) {
draw_terrain(layer, buffer, start, end - start);
start = group.first;
}
end = group.first + group.count;
}
draw_terrain(layer, buffer, start, end - start);
}
}
export function group_faces_camera(