Occlusion culling
This commit is contained in:
@@ -52,6 +52,7 @@ import {
|
||||
REGION_VOID,
|
||||
} from "./lighting.ts";
|
||||
import { cell_index, fluid_quads, type FluidCell } from "./fluid_mesh.ts";
|
||||
import { ALL_VISIBLE, section_visibility } from "./visibility.ts";
|
||||
import { fluid_height, fluid_textures } from "@/common/everything_registry.ts";
|
||||
|
||||
type TexturesInfo = Record<string, SpriteRegion>;
|
||||
@@ -87,6 +88,8 @@ const block_layers = new Uint8Array(TABLE_SIZE);
|
||||
const block_cull_same = new Uint8Array(TABLE_SIZE);
|
||||
// darkens the corners it touches (minecraft's ambient occlusion), full cubes with collision do
|
||||
const block_occludes = new Uint8Array(TABLE_SIZE);
|
||||
// blocks nothing can be seen through, drawn as whole opaque cubes, see visibility.ts
|
||||
const block_hides_view = new Uint8Array(TABLE_SIZE);
|
||||
// light can come around a corner past it
|
||||
const block_lets_light_by = new Uint8Array(TABLE_SIZE);
|
||||
const light_tables: LightTables = { opacity: new Uint8Array(TABLE_SIZE), emission: new Uint8Array(TABLE_SIZE) };
|
||||
@@ -141,6 +144,7 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
|
||||
block_visual.fill(0);
|
||||
for (const [nid, replaces_model] of message.visuals) {
|
||||
block_visual[nid] = replaces_model ? REPLACED_MODEL : HAS_VISUAL;
|
||||
if (replaces_model) block_hides_view[nid] = 0;
|
||||
}
|
||||
textures_info = message.textures_info;
|
||||
image = message.image as Texture;
|
||||
@@ -164,6 +168,7 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
|
||||
version,
|
||||
...make_section_mesh(message.chunk_x, message.chunk_z, chunks, from, bottom, message.camera),
|
||||
light: section_light(from),
|
||||
visibility: visibility(chunks[4]![from >> 4], from),
|
||||
};
|
||||
});
|
||||
post(
|
||||
@@ -223,6 +228,25 @@ function section_light(from: number): SectionLight {
|
||||
return same ? packed_light[0] : packed_light.slice();
|
||||
}
|
||||
|
||||
const open_rows = new Uint16Array(256);
|
||||
|
||||
// which faces of the section see each other, see visibility.ts. from is its bottom in the region
|
||||
function visibility(section: Section, from: number) {
|
||||
if (!section.blocks) return block_hides_view[section.value & ID_MASK] ? 0 : ALL_VISIBLE;
|
||||
for (let y = 0; y < SECTION_HEIGHT; y++) {
|
||||
for (let z = 0; z < CHUNK_SIZE; z++) {
|
||||
// the middle chunk of the region
|
||||
const row = (from + y) * REGION_LAYER + (z + CHUNK_SIZE) * REGION_SIZE + CHUNK_SIZE;
|
||||
let open = 0;
|
||||
for (let x = 0; x < CHUNK_SIZE; x++) {
|
||||
if (!block_hides_view[region.blocks[row + x]]) open |= 1 << x;
|
||||
}
|
||||
open_rows[y << 4 | z] = open;
|
||||
}
|
||||
}
|
||||
return section_visibility(open_rows);
|
||||
}
|
||||
|
||||
function post(message: FromChunkWorker, transfer: Transferable[]) {
|
||||
self.postMessage(message, transfer);
|
||||
}
|
||||
@@ -316,6 +340,7 @@ function build_block_tables() {
|
||||
block_layers[nid] = layer;
|
||||
block_cull_same[nid] = (block.cull_same ?? layer === TRANSLUCENT) ? 1 : 0;
|
||||
block_occludes[nid] = block.has_collision && layer !== TRANSLUCENT && is_full_cube(block) ? 1 : 0;
|
||||
block_hides_view[nid] = layer === SOLID && is_full_cube(block) ? 1 : 0;
|
||||
block_lets_light_by[nid] = layer !== SOLID || opacity === 0 ? 1 : 0;
|
||||
light_tables.opacity[nid] = opacity;
|
||||
light_tables.emission[nid] = block_light_emission(block);
|
||||
|
||||
Reference in New Issue
Block a user