Skip to content

@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 ​

ts
new OcclusionBuffer(width?, height?): OcclusionBuffer;

Defined in: render/softwareOcclusion.ts:33

Parameters ​

ParameterTypeDefault value
widthnumber256
heightnumber128

Returns ​

OcclusionBuffer

Properties ​

PropertyModifierTypeDefault valueDescriptionDefined in
widthreadonlynumberundefined-render/softwareOcclusion.ts:24
heightreadonlynumberundefined-render/softwareOcclusion.ts:25
depthreadonlyFloat32ArrayundefinedNearest occluder depth per pixel; Infinity where nothing is.render/softwareOcclusion.ts:27
trianglespublicnumber0Triangles drawn since the last clear.render/softwareOcclusion.ts:31

Methods ​

clear() ​

ts
clear(): void;

Defined in: render/softwareOcclusion.ts:40

Returns ​

void


drawBox() ​

ts
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 ​

ParameterType
boxBox3
matrixWorldMatrix4
viewProjectionMatrix4
nearnumber

Returns ​

void


finish() ​

ts
finish(): void;

Defined in: render/softwareOcclusion.ts:180

Build the max-depth pyramid, once all occluders are in.

Returns ​

void


isBoxOccluded() ​

ts
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 ​

ParameterType
boxBox3
matrixWorldMatrix4
viewProjectionMatrix4
nearnumber

Returns ​

boolean


isRectOccluded() ​

ts
isRectOccluded(
   minX, 
   minY, 
   maxX, 
   maxY, 
   nearest): boolean;

Defined in: render/softwareOcclusion.ts:240

A screen rectangle (pixels) whose nearest point is nearest deep.

Parameters ​

ParameterType
minXnumber
minYnumber
maxXnumber
maxYnumber
nearestnumber

Returns ​

boolean