Unity.Mathematics.AABB
Assembly:
Unity.Mathematics (Unity.Mathematics.dll)
Namespace:
Unity.Mathematics
Type:
public struct AABB
Base:
System.ValueType (struct)
Summary:
AABB represents an axis-aligned bounding box using a Center and Extents (half-size). It provides convenience properties (Size, Min, Max), containment tests, transforms (to world space using a 4x4 transform), and a squared-distance query. This is a value type (struct) intended for high-performance math operations (uses Unity.Mathematics float3/float4x4 and math utility functions). Note: ToString returns "AABB(Center:{Center}, Extents:{Extents}" — the string is missing a closing parenthesis (likely a minor formatting bug).
Fields
-
public float3 Center
Represents the center point of the bounding box in local (or world) space. -
public float3 Extents
Half-size of the bounding box along each axis. Size = Extents * 2.
Properties
-
public float3 Size => Extents * 2f
Computed full size of the box (width, height, depth). Read-only. -
public float3 Min => Center - Extents
Minimum corner (lowest x,y,z) of the box. -
public float3 Max => Center + Extents
Maximum corner (highest x,y,z) of the box.
Constructors
public AABB()
(implicit default)
As a struct, AABB has an implicit parameterless constructor that initializes Center and Extents to default(float3) (zero). Typical usage is to assign Center and Extents explicitly, or create and set via an object initializer:var b = new AABB { Center = new float3(...), Extents = new float3(...) };
Methods
-
public override string ToString()
: System.String
Returns a simple textual representation: "AABB(Center:{Center}, Extents:{Extents}". (Note the missing closing parenthesis in the implementation.) -
public bool Contains(float3 point)
: System.Boolean
Checks whether the given point is inside (or on the boundary of) this AABB. Implementation compares each axis: returns false if point.x/y/z is outside Center +/- Extents; otherwise true. -
public bool Contains(AABB b)
: System.Boolean
Checks whether this AABB fully contains another AABB b. Implementation tests all eight corners of b by creating corner positions from b.Center +/- b.Extents and testing them with Contains(float3). Returns true only if every corner is contained. -
private static float3 RotateExtents(float3 extents, float3 m0, float3 m1, float3 m2)
: System.Single[] (float3)
Helper that computes the new extents after rotating the box by the 3x3 part of a transform. It multiplies each column vector (m0, m1, m2) by the corresponding extent component and sums the absolute values: abs(m0 * extents.x) + abs(m1 * extents.y) + abs(m2 * extents.z). This yields the axis-aligned extents that enclose the rotated box. -
public static AABB Transform(float4x4 transform, AABB localBounds)
: AABB
Transforms an axis-aligned local-space AABB into world-space (still axis-aligned) by: - computing the rotated extents using the first three columns of the transform (transform.c0.xyz, transform.c1.xyz, transform.c2.xyz)
-
transforming the center via math.transform(transform, localBounds.Center) Returns a new AABB that is axis-aligned in the target space and fully contains the rotated box.
-
public float DistanceSq(float3 point)
: System.Single
Returns the squared distance from a point to the box. If the point is inside the box the result is 0. Implementation: compute delta = max(abs(point - Center), Extents) - Extents; return length squared of delta. This is useful for distance comparisons without taking a square root.
Usage Example
using Unity.Mathematics;
using static Unity.Mathematics.math;
// Create an AABB with center at (0,0,0) and extents (1,2,1)
AABB box = new AABB { Center = new float3(0f, 0f, 0f), Extents = new float3(1f, 2f, 1f) };
// Query properties
float3 size = box.Size; // (2,4,2)
float3 min = box.Min; // (-1,-2,-1)
float3 max = box.Max; // (1,2,1)
// Point containment
float3 point = new float3(0.5f, 0f, 0f);
bool containsPoint = box.Contains(point); // true
// AABB containment
AABB small = new AABB { Center = new float3(0f, 0f, 0f), Extents = new float3(0.5f, 0.5f, 0.5f) };
bool containsAABB = box.Contains(small); // true
// Transform bounds by a transform matrix (e.g., translation + rotation + scale)
float4x4 worldTransform = float4x4.Translate(new float3(10f, 0f, 0f)) * float4x4.RotateY(radians(45));
AABB worldBox = AABB.Transform(worldTransform, box);
// Squared distance from a point to the box (0 if inside)
float distSq = box.DistanceSq(new float3(5f, 0f, 0f));
Additional notes for modding (Cities: Skylines 2): - Use this struct for lightweight bounds calculations (visibility tests, collision checks, spatial indexing). - Being a struct, AABB copies by value — be mindful of copying costs if used in large arrays; it's efficient compared to reference types and incurs no GC. - Transform(...) produces an axis-aligned bounding box that encloses the rotated local bounds — this can be larger than the original due to axis alignment after rotation. - Contains(AABB) checks all eight corners; for tight-fitting containment with floating point imprecision, consider a small epsilon if needed.