CullingVolume3D

Package: Libraries.Game.Graphics.Trees · Inherits from: Libraries.Language.Object

CullingVolume3D is the region a camera or a light can see, as a set of planes facing inward. A box is inside the volume if it is on the inner side of every plane. The planes are taken from a view-projection matrix, the matrix that turns a world position into a position on the screen. A point is visible when, after the matrix, its x and y are between -w and w and its z is no more than w. Each of those five limits is one plane. Building the planes this way works for any camera or light, left-handed or right-handed, perspective or orthographic, because it uses only what the matrix does. There is no near plane. OpenGL and Vulkan put it in different places (z of -w or 0), and leaving it out means the volume does not need to know which graphics system is running. For a camera, the four side planes already reject everything behind it, so the only extra items are ones between the camera and its near plane, a very small space. For a directional light's shadows, the near plane must be left out anyway: items between the light and its shadow area still cast shadows into it. The library's Frustum class is not used. Its box test, BoundsInFrustum, is commented out, and it builds its planes from corners that assume OpenGL's depth range in a fixed order, which may no longer face inward in the left-handed world.

Example Code

use Libraries.Game.Graphics.Trees.CullingVolume3D
use Libraries.Compute.Matrix4
use Libraries.Game.BoundingBox
use Libraries.Compute.Vector3

// A new Matrix4 does nothing to a point, so this volume holds x and y
// from -1 to 1, and z up to 1.
Matrix4 matrix
CullingVolume3D volume
volume:Set(matrix)

Vector3 minimum
Vector3 maximum
minimum:Set(-0.5, -0.5, 0)
maximum:Set(0.5, 0.5, 0.5)
BoundingBox box
box:Set(minimum, maximum)
output volume:Test(box) = volume:INSIDE

Variables

  • integer OUTSIDE
  • integer INTERSECTING
  • integer INSIDE: 32 bits is the precision the graphics card works in anyway.

Actions

GetPlaneCount()

This action returns how many planes the volume has: 5, or fewer if the matrix gave a plane with no direction.

Returns

integer: The number of planes.

Set(Libraries.Compute.Matrix4 matrix)

This action sets the volume from a view-projection matrix, such as a camera's combined matrix or a light's shadow matrix.

Parameters

SetShadowColumn(Libraries.Compute.Vector3 direction, number x, number y, number z, number distance)

This action sets the volume to the column of space that can cast a directional light's shadow within a distance of a point, such as the camera. The column is a square, twice the distance wide, centered on the point and running along the light's direction. Toward the light it has no end, so a tall object far toward the light is still inside it. Away from the light, it ends the distance past the point, since anything farther is behind whatever its shadow would fall on. Its five planes face inward, as those read from a matrix do: two at the distance on either side of the point, along a direction across the light; two the same, along the other direction across the light; and one the distance past the point, along the light.

Parameters

  • direction Libraries.Compute.Vector3: The direction the light shines in. It does not need to be unit length.
  • x number: The x coordinate of the column's center.
  • y number: The y coordinate of the column's center.
  • z number: The z coordinate of the column's center.
  • distance number: How far the column reaches to each side, and past the center.

Returns

boolean: False if the direction has no length. The volume is then left with no planes, and since a box is only outside when a plane rules it out, Test says every box is inside. Do not search with the volume when this returns false.

Test(Libraries.Game.BoundingBox box)

This action tests a box against the volume and returns OUTSIDE, INTERSECTING or INSIDE. For each plane, the box's corner farthest along the plane's direction is tested first. If even that corner is on the outer side, the whole box is outside. Otherwise the opposite corner is tested. If it is on the outer side, the box crosses the plane. A box is inside only if it crosses no plane.

Parameters

Returns

integer: OUTSIDE, INTERSECTING or INSIDE.

Inherited Actions

3 actions from Libraries.Language.Object