@relu-interactives/spatial-ecs
@relu-interactives/spatial-ecs / index / OcclusionBuffer
Class: OcclusionBuffer ​
Defined in: render/softwareOcclusion.ts:23
A small CPU depth buffer for occlusion culling.
Occluders (boxes standing in for walls, buildings, hills) are drawn into it each frame from the camera; an object whose screen rectangle is behind them everywhere isn't drawn. On the CPU because it then works the same on WebGL 2, WebGPU and a headset, with no frame of lag: a GPU query answers a frame or two late, which shows as objects popping in as you turn.
Every choice errs towards drawing:
- depth is the surface's own (1/w, linear on screen), taken at the farthest point of each pixel, so an occluder never seems nearer than it is;
- a triangle crossing the near plane is clipped to it, keeping only the part in front;
- a pixel counts as covered only when its centre is inside a triangle, and an object's rectangle is grown by a pixel before testing;
- callers shrink occluder boxes a little (
inset).
Depth is the view-space distance along the camera's axis (clip w).
Constructors ​
Constructor ​
new OcclusionBuffer(width?, height?): OcclusionBuffer;Defined in: render/softwareOcclusion.ts:33
Parameters ​
| Parameter | Type | Default value |
|---|---|---|
width | number | 256 |
height | number | 128 |
Returns ​
OcclusionBuffer
Properties ​
| Property | Modifier | Type | Default value | Description | Defined in |
|---|---|---|---|---|---|
width | readonly | number | undefined | - | render/softwareOcclusion.ts:24 |
height | readonly | number | undefined | - | render/softwareOcclusion.ts:25 |
depth | readonly | Float32Array | undefined | Nearest occluder depth per pixel; Infinity where nothing is. | render/softwareOcclusion.ts:27 |
triangles | public | number | 0 | Triangles drawn since the last clear. | render/softwareOcclusion.ts:31 |
Methods ​
clear() ​
clear(): void;Defined in: render/softwareOcclusion.ts:40
Returns ​
void
drawBox() ​
drawBox(
box,
matrixWorld,
viewProjection,
near): void;Defined in: render/softwareOcclusion.ts:50
Draw a box (local bounds and its world matrix) seen through viewProjection. near is the camera's near plane: corners nearer than it are behind it.
Parameters ​
| Parameter | Type |
|---|---|
box | Box3 |
matrixWorld | Matrix4 |
viewProjection | Matrix4 |
near | number |
Returns ​
void
finish() ​
finish(): void;Defined in: render/softwareOcclusion.ts:180
Build the max-depth pyramid, once all occluders are in.
Returns ​
void
isBoxOccluded() ​
isBoxOccluded(
box,
matrixWorld,
viewProjection,
near): boolean;Defined in: render/softwareOcclusion.ts:211
Whether a box (local bounds + world matrix) is hidden behind what's drawn. Any corner behind the near plane, or a rectangle off screen, answers no: it might be visible, or it's frustum culling's to decide.
Parameters ​
| Parameter | Type |
|---|---|
box | Box3 |
matrixWorld | Matrix4 |
viewProjection | Matrix4 |
near | number |
Returns ​
boolean
isRectOccluded() ​
isRectOccluded(
minX,
minY,
maxX,
maxY,
nearest): boolean;Defined in: render/softwareOcclusion.ts:240
A screen rectangle (pixels) whose nearest point is nearest deep.
Parameters ​
| Parameter | Type |
|---|---|
minX | number |
minY | number |
maxX | number |
maxY | number |
nearest | number |
Returns ​
boolean

