Optimize renderer
This commit is contained in:
@@ -19,7 +19,7 @@ export class DimensionLogicSystem extends System {
|
||||
}
|
||||
}
|
||||
handle_second(world: ClientWorld, dimension: Dimension) {
|
||||
for (const chunk of dimension.chunks) {
|
||||
for (const chunk of dimension.chunks.values()) {
|
||||
for (const tickable of chunk.blocks_data) {
|
||||
const tile_info = EverythingRegistry.get<BlockRegistry>("blocks", tickable.id);
|
||||
if (tile_info && tile_info.on_second) {
|
||||
@@ -31,7 +31,7 @@ export class DimensionLogicSystem extends System {
|
||||
}
|
||||
|
||||
handle_tick(world: ClientWorld, dimension: Dimension) {
|
||||
for (const chunk of dimension.chunks) {
|
||||
for (const chunk of dimension.chunks.values()) {
|
||||
for (const tickable of chunk.blocks_data) {
|
||||
const tile_info = EverythingRegistry.get<BlockRegistry>("blocks", tickable.id);
|
||||
if (tile_info && tile_info.on_tick) {
|
||||
|
||||
@@ -1,74 +1,17 @@
|
||||
import { EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { Chunk, Dimension } from "$/client/components/dimension.ts";
|
||||
import { Camera } from "$/client/components/camera.ts";
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import { create_vertex_buffer, flush_buffer, set_current_texture } from "$/client/renderer/mod.ts";
|
||||
import { flush_buffer, set_current_texture } from "$/client/renderer/mod.ts";
|
||||
|
||||
let gdimension: Dimension;
|
||||
|
||||
const worker = new Worker(new URL("./workers/chunk_mesh_worker.js", import.meta.url), {
|
||||
type: "module",
|
||||
});
|
||||
|
||||
worker.onmessage = (event) => {
|
||||
const { opaque_vertices, opaque_count, transparent_vertices, transparent_count, chunk_x, chunk_z } = event.data;
|
||||
const chunk = gdimension.get_chunk(chunk_x, chunk_z);
|
||||
if (!chunk) {
|
||||
console.error("bad");
|
||||
return;
|
||||
}
|
||||
gdimension.delete_chunk_mesh(chunk);
|
||||
chunk.dirty = false;
|
||||
|
||||
chunk.opaque_vertex_buffer = create_vertex_buffer(opaque_vertices.subarray(0, opaque_count));
|
||||
chunk.opaque_vertex_count = opaque_count / 9;
|
||||
|
||||
chunk.transparent_vertex_buffer = create_vertex_buffer(transparent_vertices.subarray(0, transparent_count));
|
||||
chunk.transparent_vertex_count = transparent_count / 9;
|
||||
};
|
||||
|
||||
function strip_functions<T extends Record<string, any>>(obj: T) {
|
||||
const out: any = {};
|
||||
|
||||
for (const k in obj) {
|
||||
const v = obj[k];
|
||||
|
||||
if (typeof v === "function") continue;
|
||||
|
||||
if (v && typeof v === "object") {
|
||||
out[k] = strip_functions(v);
|
||||
} else {
|
||||
out[k] = v;
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
export function render_dimension(dimension: Dimension, camera: Camera) {
|
||||
gdimension = dimension;
|
||||
export function render_dimension(dimension: Dimension, _camera: Camera) {
|
||||
dimension.request_meshes();
|
||||
|
||||
set_current_texture(dimension.image.tex);
|
||||
|
||||
for (const chunk of dimension.chunks) {
|
||||
if (chunk.dirty && chunk.generated) {
|
||||
const padded_chunk = dimension.create_padded_chunk(chunk);
|
||||
worker.postMessage({
|
||||
chunk_x: chunk.x,
|
||||
chunk_z: chunk.z,
|
||||
padded_chunk,
|
||||
blocks_registry: strip_functions(EverythingRegistry.get_registry("blocks")),
|
||||
textures_info: AssetManager.instance.get("bworld:textures_info"),
|
||||
image: { width: dimension.image.width, height: dimension.image.height },
|
||||
}, [padded_chunk.buffer]);
|
||||
chunk.dirty = false;
|
||||
}
|
||||
}
|
||||
for (const chunk of dimension.chunks) {
|
||||
for (const chunk of dimension.chunks.values()) {
|
||||
render_chunk_opaque(chunk);
|
||||
}
|
||||
|
||||
for (const chunk of dimension.chunks) {
|
||||
for (const chunk of dimension.chunks.values()) {
|
||||
render_chunk_transparent(chunk);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,27 +18,47 @@ export class WorldGenerationSystem extends System {
|
||||
const player_chunk_x = Math.floor(position.x / CHUNK_SIZE);
|
||||
const player_chunk_z = Math.floor(position.z / CHUNK_SIZE);
|
||||
|
||||
const render_distance = player_component.render_distance;
|
||||
// one extra ring gets generated so the edge of the render distance has neighbors to mesh against
|
||||
const load_distance = player_component.render_distance + 1;
|
||||
const out_of_range = (x: number, z: number) =>
|
||||
Math.max(Math.abs(x - player_chunk_x), Math.abs(z - player_chunk_z)) > load_distance;
|
||||
|
||||
for (const chunk of dimension.chunks) {
|
||||
if (Math.abs(chunk.x - player_chunk_x) > render_distance) {
|
||||
dimension.unload_chunk(chunk.x, chunk.z);
|
||||
// collect first, deleting from the map while iterating it skips entries
|
||||
const to_unload = [];
|
||||
for (const chunk of dimension.chunks.values()) {
|
||||
if (out_of_range(chunk.x, chunk.z)) {
|
||||
to_unload.push(chunk);
|
||||
}
|
||||
if (Math.abs(chunk.z - player_chunk_z) > render_distance) {
|
||||
dimension.unload_chunk(chunk.x, chunk.z);
|
||||
}
|
||||
for (const chunk of to_unload) {
|
||||
dimension.unload_chunk(chunk.x, chunk.z);
|
||||
}
|
||||
for (const pending of [...dimension.pending_generation.values()]) {
|
||||
if (out_of_range(pending.x, pending.z)) {
|
||||
dimension.cancel_chunk_request(pending.x, pending.z);
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = player_chunk_x - render_distance; i <= player_chunk_x + render_distance; i += 1) {
|
||||
for (let j = player_chunk_z - render_distance; j <= player_chunk_z + render_distance; j += 1) {
|
||||
const maybe_chunk = dimension.chunks.find((chunk) => chunk.x === i && chunk.z === j);
|
||||
if (!maybe_chunk || !maybe_chunk.generated) {
|
||||
console.log("loading chunk", i, j);
|
||||
dimension.load_chunk(i, j);
|
||||
// only generate one chunk per frame
|
||||
return;
|
||||
// dont queue up the whole area at once, so walking somewhere new gets the close chunks first
|
||||
const max_in_flight = dimension.workers.size * 2;
|
||||
if (dimension.pending_generation.size >= max_in_flight) {
|
||||
return;
|
||||
}
|
||||
|
||||
const missing: { x: number; z: number; distance: number }[] = [];
|
||||
for (let x = player_chunk_x - load_distance; x <= player_chunk_x + load_distance; x += 1) {
|
||||
for (let z = player_chunk_z - load_distance; z <= player_chunk_z + load_distance; z += 1) {
|
||||
if (!dimension.is_generated(x, z) && !dimension.is_generating(x, z)) {
|
||||
const dx = x - player_chunk_x;
|
||||
const dz = z - player_chunk_z;
|
||||
missing.push({ x, z, distance: dx * dx + dz * dz });
|
||||
}
|
||||
}
|
||||
}
|
||||
missing.sort((a, b) => a.distance - b.distance);
|
||||
|
||||
for (const chunk of missing.slice(0, max_in_flight - dimension.pending_generation.size)) {
|
||||
dimension.request_chunk(chunk.x, chunk.z);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user