Struct AABB3D
Axis-aligned 3D bounding box represented as Center + HalfExtents. The canonical Scylla type for shape bounds, frustum culling, spatial queries, and any "box that sits on world axes" use case.
Implements
Inherited Members
Namespace: Scylla.Core.Util.Geom
Assembly: ScyllaCore.dll
Syntax
[Serializable]
public struct AABB3D : IEquatable<AABB3D>
Remarks
Center + HalfExtents is preferred over Min + Max because it keeps Encapsulate(Vector3) / Expand(float) cheap and matches the layout of UnityEngine.Bounds. Implicit conversions to and from UnityEngine.Bounds are provided so callers can use either type freely.
HalfExtents components are expected to be non-negative. Behaviour with negative half-extents is undefined for intersection and containment queries.
Equality comparisons use Approximately(float, float, float) for each component, so two boxes that differ only by floating-point rounding will be considered equal.
For Burst-compatible AABB usage inside spatial data structures such as
ScyllaBVH and ScyllaLooseOctreeCore, use ScyllaAABB3 from the
Scylla.Core.Structures namespace instead.
Constructors
AABB3D(Vector3, Vector3)
Initializes a new AABB3D with the supplied center and half-extents.
Declaration
public AABB3D(Vector3 center, Vector3 halfExtents)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | center | The box center in world (or local) space. |
| Vector3 | halfExtents | Half the box extent on each axis. Each component should be non-negative; negative values produce undefined results in intersection and containment queries. |
Fields
Center
Center of the box expressed in the same coordinate space the box is defined in (typically world space).
Declaration
[SerializeField]
public Vector3 Center
Field Value
| Type | Description |
|---|---|
| Vector3 |
Empty
A zero-size box centered at the world origin. Useful as a default or "uninitialized" sentinel value.
Declaration
public static readonly AABB3D Empty
Field Value
| Type | Description |
|---|---|
| AABB3D |
HalfExtents
Half the box's full extent on each axis. Each component should be non-negative.
The relationship to the full size is Size = HalfExtents * 2.
Declaration
[SerializeField]
public Vector3 HalfExtents
Field Value
| Type | Description |
|---|---|
| Vector3 |
Properties
Max
The maximum (most-positive) corner of the box, computed as
Center + HalfExtents.
Declaration
public readonly Vector3 Max { get; }
Property Value
| Type | Description |
|---|---|
| Vector3 |
Min
The minimum (most-negative) corner of the box, computed as
Center - HalfExtents.
Declaration
public readonly Vector3 Min { get; }
Property Value
| Type | Description |
|---|---|
| Vector3 |
Size
The full size of the box on each axis, equal to HalfExtents * 2. Each
component corresponds to the box's extent along that world axis.
Declaration
public readonly Vector3 Size { get; }
Property Value
| Type | Description |
|---|---|
| Vector3 |
SurfaceArea
The total surface area of the box, in square units. Computed as
2 * (wh + hd + d*w) where w, h, and d are the
box's width, height, and depth respectively.
Declaration
public readonly float SurfaceArea { get; }
Property Value
| Type | Description |
|---|---|
| float |
Volume
The total volume enclosed by the box, in cubic units. Equals
Size.x * Size.y * Size.z. Returns zero for a degenerate (flat or
point) box.
Declaration
public readonly float Volume { get; }
Property Value
| Type | Description |
|---|---|
| float |
Methods
ClosestPoint(Vector3)
Returns the closest point on or inside the box to point. If
point is already inside the box, it is returned unchanged.
Declaration
public readonly Vector3 ClosestPoint(Vector3 point)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | point | The query point in world space. |
Returns
| Type | Description |
|---|---|
| Vector3 | The point within or on the surface of the box that is nearest to
|
Contains(in AABB3D)
Returns true if other is fully contained within this box,
meaning all eight corners of other lie inside or on the boundary
of this box.
Declaration
public bool Contains(in AABB3D other)
Parameters
| Type | Name | Description |
|---|---|---|
| AABB3D | other | The box to test for containment. |
Returns
| Type | Description |
|---|---|
| bool |
|
Contains(Vector3)
Returns true if point is inside or exactly on the
boundary of this box.
Declaration
public bool Contains(Vector3 point)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | point | The world-space point to test. |
Returns
| Type | Description |
|---|---|
| bool |
|
Distance(Vector3)
Returns the Euclidean distance from point to the nearest surface
point of the box. Returns zero if point is inside or on the
boundary of the box.
Declaration
public readonly float Distance(Vector3 point)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | point | The query point in world space. |
Returns
| Type | Description |
|---|---|
| float | A non-negative distance in world units. Zero means the point is inside or touching the box. |
Encapsulate(in AABB3D)
Grows this box minimally to fully include other. Equivalent to
calling Encapsulate(Vector3) for other's
Min and Max corners. This method mutates the box in
place.
Declaration
public void Encapsulate(in AABB3D other)
Parameters
| Type | Name | Description |
|---|---|---|
| AABB3D | other | The box to include in this box. |
Encapsulate(Vector3)
Grows this box minimally to include point. If the point is
already inside, the box is unchanged. This method mutates the box in place.
Declaration
public void Encapsulate(Vector3 point)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | point | The world-space point to include in the box. |
Equals(AABB3D)
Returns true if this box and other are approximately
equal, comparing each component of Center and
HalfExtents using Approximately(float, float, float).
Declaration
public bool Equals(AABB3D other)
Parameters
| Type | Name | Description |
|---|---|---|
| AABB3D | other | The box to compare against. |
Returns
| Type | Description |
|---|---|
| bool |
|
Equals(object)
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj |
Returns
| Type | Description |
|---|---|
| bool |
Overrides
Expand(float)
Expands each half-extent by amount uniformly along all three
axes. Use a negative value to shrink the box. This method mutates the box in place.
Declaration
public void Expand(float amount)
Parameters
| Type | Name | Description |
|---|---|---|
| float | amount | The amount to add to each half-extent component. A positive value grows the box; a negative value shrinks it. Shrinking below zero produces negative half-extents, which are undefined for intersection queries. |
Expand(Vector3)
Expands each half-extent by the corresponding component of amount,
allowing per-axis control. This method mutates the box in place.
Declaration
public void Expand(Vector3 amount)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | amount | Per-axis expansion amounts. A positive component grows the box on that axis; a negative component shrinks it. |
FromCenterSize(Vector3, Vector3)
Builds a new AABB3D from a center and a full-size vector. The
half-extents are set to size * 0.5, matching the UnityEngine.Bounds
constructor convention.
Declaration
public static AABB3D FromCenterSize(Vector3 center, Vector3 size)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | center | The center of the desired box. |
| Vector3 | size | The full size of the box along each axis. |
Returns
| Type | Description |
|---|---|
| AABB3D | A new AABB3D centered at |
FromMinMax(Vector3, Vector3)
Builds a new AABB3D from a minimum and maximum corner pair. The
resulting box's center is the midpoint of min and
max, and the half-extents are half the diagonal.
Declaration
public static AABB3D FromMinMax(Vector3 min, Vector3 max)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | min | The minimum (most-negative) corner of the desired box. |
| Vector3 | max | The maximum (most-positive) corner of the desired box. |
Returns
| Type | Description |
|---|---|
| AABB3D | A new AABB3D that exactly spans the given corners. |
FromPoints(ReadOnlySpan<Vector3>)
Builds the smallest axis-aligned box that contains all points in
points. Returns Empty when the span is empty.
Declaration
public static AABB3D FromPoints(ReadOnlySpan<Vector3> points)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<Vector3> | points | A read-only span of points to enclose. May be empty, in which case Empty is returned. |
Returns
| Type | Description |
|---|---|
| AABB3D | The tightest AABB3D enclosing all supplied points, or
Empty if |
Remarks
The implementation performs a single linear scan, updating per-axis min/max values, so the time complexity is O(n) with no heap allocation.
GetCorner(int)
Returns one of the eight corner points of this box. The index
parameter is bit-packed: bit 0 selects the maximum X face (versus minimum), bit 1
selects the maximum Y face, and bit 2 selects the maximum Z face. Valid indices
are in the range [0, 7].
Declaration
public Vector3 GetCorner(int index)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | A value from 0 to 7 that bit-encodes which corner to retrieve. Bit 0: X axis (0 = min, 1 = max). Bit 1: Y axis (0 = min, 1 = max). Bit 2: Z axis (0 = min, 1 = max). |
Returns
| Type | Description |
|---|---|
| Vector3 | The world-space position of the selected corner. |
Remarks
Iterating from index 0 to 7 visits all eight corners in a consistent, deterministic order. This is useful for computing conservative screen-space bounds or for feeding the eight corners into ToAABB3D().
GetHashCode()
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int |
Overrides
Intersects(in AABB3D)
Returns true if this box and other overlap. Boundary
contact (touching faces, edges, or corners) counts as an intersection.
Declaration
public bool Intersects(in AABB3D other)
Parameters
| Type | Name | Description |
|---|---|---|
| AABB3D | other | The box to test against. |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
The test is a simple per-axis overlap check on the center-distance versus the sum of half-extents, equivalent to a degenerate Separating Axis Theorem test with three axes. It runs in constant time with no allocation.
IntersectsRay(Ray, out float)
Tests whether a ray intersects this box using the slab method. When the ray hits,
distance receives the parametric distance along the ray to the
first intersection point. If the ray origin is already inside the box, the distance
is set to zero.
Declaration
public bool IntersectsRay(Ray ray, out float distance)
Parameters
| Type | Name | Description |
|---|---|---|
| Ray | ray | The ray to test. The direction should be normalized for |
| float | distance | When this method returns |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
Uses the slab method: for each axis the ray is intersected against the two parallel planes that bound the box on that axis. The resulting per-axis entry and exit intervals are intersected to determine overall overlap. Rays parallel to an axis are handled separately to avoid division by near-zero.
This test does not detect back-face intersections from inside the box; the returned distance is always non-negative.
ToString()
Returns a human-readable string representation of this box in the format
AABB3D[Center=(x,y,z), HalfExtents=(x,y,z)].
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | A formatted string describing the box's center and half-extents. |
Overrides
Operators
operator ==(AABB3D, AABB3D)
Approximate equality operator. Delegates to Equals(AABB3D); two boxes that differ only by floating-point rounding are considered equal.
Declaration
public static bool operator ==(AABB3D left, AABB3D right)
Parameters
| Type | Name | Description |
|---|---|---|
| AABB3D | left | The left operand. |
| AABB3D | right | The right operand. |
Returns
| Type | Description |
|---|---|
| bool |
|
implicit operator Bounds(AABB3D)
Implicitly converts an AABB3D to a UnityEngine.Bounds
value. The resulting Bounds has the same center and size.
Declaration
public static implicit operator Bounds(AABB3D box)
Parameters
| Type | Name | Description |
|---|---|---|
| AABB3D | box | The source AABB3D to convert. |
Returns
| Type | Description |
|---|---|
| Bounds | A UnityEngine.Bounds with |
implicit operator AABB3D(Bounds)
Implicitly converts a UnityEngine.Bounds to an AABB3D.
The Bounds.extents field (which equals half the size) is used directly as
HalfExtents.
Declaration
public static implicit operator AABB3D(Bounds bounds)
Parameters
| Type | Name | Description |
|---|---|---|
| Bounds | bounds | The source UnityEngine.Bounds to convert. |
Returns
| Type | Description |
|---|---|
| AABB3D | An AABB3D with Center equal to |
operator !=(AABB3D, AABB3D)
Approximate inequality operator. Returns the negation of operator ==(AABB3D, AABB3D).
Declaration
public static bool operator !=(AABB3D left, AABB3D right)
Parameters
| Type | Name | Description |
|---|---|---|
| AABB3D | left | The left operand. |
| AABB3D | right | The right operand. |
Returns
| Type | Description |
|---|---|
| bool |
|