melonJS
    Preparing search index...

    Class Frustum

    A view frustum — the truncated pyramid that defines a perspective camera's visible volume. Holds the four projection parameters (fov, aspect, near, far) and the matching projection matrix.

    Used by Camera3d as its source of truth for perspective projection. The matrix follows melonJS conventions: Y-down (sprite at higher y appears lower on screen, matching Camera2d) and +Z forward (sprite at higher pos.z is farther from the camera and renders smaller). This differs from the OpenGL default of Y-up and -Z forward, but matches the rest of the engine and lets Camera2d code translate directly to Camera3d.

    Plane-based frustum culling is available via Frustum#planes (rebuilt by Frustum#setFromViewProjection per frame) plus Frustum#containsPoint / Frustum#intersectsSphere for culling tests. Camera3d.update calls setFromViewProjection once per frame so its isVisible(obj) queries against current planes — no per-frame setup needed from user code.

    const frustum = new Frustum({ fov: Math.PI / 3, aspect: 16 / 9 });
    frustum.near = 0.5;
    frustum.update();
    renderer.setProjection(frustum.projectionMatrix);
    Index

    Constructors

    Properties

    aspect: number

    aspect ratio (width / height). Camera3d sets this automatically from its viewport on resize.

    far: number

    distance to the far clipping plane.

    fov: number

    vertical field of view in radians. Mutating this field requires calling Frustum#update to rebuild the projection matrix — or use Frustum#set to change multiple parameters and update in one call.

    near: number

    distance to the near clipping plane (positive — measured along +Z, the camera's forward direction).

    planes: Plane[]

    The six bounding planes of this frustum in world space, in order: left, right, bottom, top, near, far. Each plane is oriented so its (nx, ny, nz) normal points inward — a point with positive signed distance to a plane is on the visible side.

    Populated by Frustum#setFromViewProjection. Callers that use Frustum#intersectsSphere or Frustum#containsPoint must first call setFromViewProjection each frame the camera moves; otherwise the planes describe a stale frustum.

    projectionMatrix: Matrix3d

    the perspective projection matrix derived from fov, aspect, near and far. Rebuilt by Frustum#update.

    Methods

    • Test whether a world-space point is inside this frustum. Always run Frustum#setFromViewProjection first.

      Parameters

      • x: number

        world x

      • y: number

        world y

      • z: number

        world z

      Returns boolean

      true if the point is inside (on the positive side of every plane)

    • Test whether a world-space sphere overlaps this frustum. Conservative — a sphere that touches even one plane's positive side is reported visible. Always run Frustum#setFromViewProjection first so the planes describe the current camera view.

      Parameters

      • x: number

        sphere center x in world coords

      • y: number

        sphere center y in world coords

      • z: number

        sphere center z in world coords

      • radius: number

        sphere radius in world units

      Returns boolean

      true if the sphere is at least partially inside the frustum

    • Atomically set all four parameters and rebuild the projection matrix in one call.

      Parameters

      • fov: number

        vertical field of view in radians

      • aspect: number

        aspect ratio (width / height)

      • near: number

        distance to the near clipping plane

      • far: number

        distance to the far clipping plane

      Returns this

      this Frustum for chaining

    • Rebuild the six bounding Frustum#planes from the world → clip matrix. Standard Gribb–Hartmann extraction: each plane is one of the six combinations of the matrix's row 3 (the "w" row) ± rows 0, 1, 2.

      Call this once per frame after the camera has moved (typically from Camera3d.update); the planes are then valid in world space for that frame and can be tested against world-space bounds via Frustum#intersectsSphere / Frustum#containsPoint.

      Pass projectionMatrix × viewMatrix — column-major / gl-matrix convention: a world-space point becomes a clip-space point via clip = projection × view × world, so the combined matrix is projection × view. The parameter name says viewProjection for that reason — it's the matrix uploaded to uProjectionMatrix in the vertex shader (after Camera3d bakes the per-frame world translate into the view via container.translate).

      Parameters

      • viewProjection: Matrix3d

        the projectionMatrix × viewMatrix matrix

      Returns void

    • Rebuild Frustum#projectionMatrix from the current parameter values. Call this after mutating any of fov, aspect, near, far individually.

      The matrix is the standard OpenGL perspective post-multiplied by scale(1, -1, -1) so that:

      • Y-down matches melonJS screen + Camera2d conventions
      • +Z is forward (positive pos.z = farther from camera)

      Returns void