Culling (visibility helpers)
Mibo.Elmish.Culling is a helper module that keeps visibility math separate from your renderer and your spatial partitioning.
It operates on geometric primitives:
- A view frustum (built from a camera or light View×Projection matrix)
- A bounding sphere / bounding box to test against it
- 2D rectangle overlap
3D: frustum culling
Build a frustum from a View×Projection matrix and test geometry against it. The frustum type is backend-specific — raylib ships its own Frustum (it has no native one), while MonoGame uses its native BoundingFrustum:
// raylib: Mibo.Elmish.Frustum over System.Numerics.Matrix4x4
let frustum = Frustum(viewProjection)
// MonoGame: Microsoft.Xna.Framework.BoundingFrustum
let frustum = BoundingFrustum(viewProjection)
if Culling.isVisible frustum entitySphere then
// submit draw commands
()
Or for axis-aligned bounding boxes:
if Culling.isVisibleBox frustum nodeBounds then
()
_Where does the View×Projection matrix come from?_ On MonoGame, the camera struct carries it directly —
BoundingFrustum(cam.View * cam.Projection). On raylib, capture it insideBeginMode3D(Rlgl.GetMatrixModelview() * Rlgl.GetMatrixProjection()), or build it fromRaylib.GetCameraMatrix3D(camera)and a perspective matrix.
2D: rectangle overlap
Use Camera2D.viewportBounds with Culling.isVisible2D (the rectangle type is
the backend's native one — float Rectangle on raylib, int Rectangle on
MonoGame):
// raylib — viewportBounds takes the camera by reference (&)
let viewBounds = Camera2D.viewportBounds &camera viewportWidth viewportHeight
// MonoGame — immutable camera, passed by value
let viewBounds = Camera2D.viewportBounds camera viewportWidth viewportHeight
if Culling.isVisible2D viewBounds spriteBounds then
()
What this is not
This module doesn't try to be your spatial index.
- If you have many objects: use a grid / quadtree / BVH / octree.
- Use these helpers at the edge: "is this node/object worth considering for rendering?"
See also: Camera and Rendering overview.
Mibo