11 public const int LBF = 0;
12 public const int LBB = 1;
13 public const int LTF = 2;
14 public const int LTB = 3;
15 public const int RBF = 4;
16 public const int RBB = 5;
17 public const int RTF = 6;
18 public const int RTB = 7;
39 public const int LT = 0;
40 public const int LB = 1;
41 public const int RT = 2;
42 public const int RB = 3;
51 public const int TOP = 0;
52 public const int BOT = 1;
53 public const int LFT = 2;
54 public const int RHT = 3;
55 public const int FWD = 4;
56 public const int BCK = 5;
65 #region Public Static Functions 73 return new Bounds(Vector3.zero, GetInvalidBoundsSize());
80 public static bool IsValid(
this Bounds bounds)
82 return bounds.size != GetInvalidBoundsSize();
97 Vector3 center = bounds.center;
98 Vector3 extents = bounds.extents;
99 float leftEdge = center.x - extents.x;
100 float rightEdge = center.x + extents.x;
101 float bottomEdge = center.y - extents.y;
102 float topEdge = center.y + extents.y;
103 float frontEdge = center.z - extents.z;
104 float backEdge = center.z + extents.z;
107 const int numPoints = 8;
108 if (positions == null || positions.Length != numPoints)
110 positions =
new Vector3[numPoints];
114 positions[
BoundsExtensions.
LBF] = transform.TransformPoint(leftEdge, bottomEdge, frontEdge);
115 positions[
BoundsExtensions.
LBB] = transform.TransformPoint(leftEdge, bottomEdge, backEdge);
118 positions[
BoundsExtensions.
RBF] = transform.TransformPoint(rightEdge, bottomEdge, frontEdge);
119 positions[
BoundsExtensions.
RBB] = transform.TransformPoint(rightEdge, bottomEdge, backEdge);
131 Vector3 center = bounds.center;
132 Vector3 extents = bounds.extents;
133 float leftEdge = center.x - extents.x;
134 float rightEdge = center.x + extents.x;
135 float bottomEdge = center.y - extents.y;
136 float topEdge = center.y + extents.y;
137 float frontEdge = center.z - extents.z;
138 float backEdge = center.z + extents.z;
140 const int numPoints = 8;
141 if (positions == null || positions.Length != numPoints)
143 positions =
new Vector3[numPoints];
158 Vector3 center = bounds.center;
159 Vector3 extents = bounds.extents;
161 const int numPoints = 6;
162 if (positions == null || positions.Length != numPoints)
164 positions =
new Vector3[numPoints];
167 positions[
BoundsExtensions.
TOP] = transform.TransformPoint(center + Vector3.up * extents.y);
168 positions[
BoundsExtensions.
BOT] = transform.TransformPoint(center + Vector3.down * extents.y);
169 positions[
BoundsExtensions.
LFT] = transform.TransformPoint(center + Vector3.left * extents.x);
170 positions[
BoundsExtensions.
RHT] = transform.TransformPoint(center + Vector3.right * extents.x);
171 positions[
BoundsExtensions.
FWD] = transform.TransformPoint(center + Vector3.forward * extents.z);
172 positions[
BoundsExtensions.
BCK] = transform.TransformPoint(center + Vector3.back * extents.z);
183 Vector3 center = bounds.center;
184 Vector3 extents = bounds.extents;
185 float leftEdge = center.x - extents.x;
186 float rightEdge = center.x + extents.x;
187 float bottomEdge = center.y - extents.y;
188 float topEdge = center.y + extents.y;
189 float frontEdge = center.z - extents.z;
190 float backEdge = center.z + extents.z;
194 if (positions == null || positions.Length != numPoints)
196 positions =
new Vector3[numPoints];
200 positions[
BoundsExtensions.
LBF] = transform.TransformPoint(leftEdge, bottomEdge, frontEdge);
201 positions[
BoundsExtensions.
LBB] = transform.TransformPoint(leftEdge, bottomEdge, backEdge);
204 positions[
BoundsExtensions.
RBF] = transform.TransformPoint(rightEdge, bottomEdge, frontEdge);
205 positions[
BoundsExtensions.
RBB] = transform.TransformPoint(rightEdge, bottomEdge, backEdge);
233 Vector3 center = bounds.center;
234 Vector3 extents = bounds.extents;
238 float bottomEdge = 0;
243 if (positions == null || positions.Length != numPoints)
245 positions =
new Vector3[numPoints];
252 leftEdge = center.z - extents.z;
253 rightEdge = center.z + extents.z;
254 bottomEdge = center.y - extents.y;
255 topEdge = center.y + extents.y;
264 leftEdge = center.z - extents.z;
265 rightEdge = center.z + extents.z;
266 bottomEdge = center.x - extents.x;
267 topEdge = center.x + extents.x;
276 leftEdge = center.x - extents.x;
277 rightEdge = center.x + extents.x;
278 bottomEdge = center.y - extents.y;
279 topEdge = center.y + extents.y;
314 public static Bounds
Transform(
this Bounds bounds, Matrix4x4 transformMatrix)
317 Vector3 rightAxis = transformMatrix.GetColumn(0);
318 Vector3 upAxis = transformMatrix.GetColumn(1);
319 Vector3 lookAxis = transformMatrix.GetColumn(2);
324 Vector3 rotatedExtentsRight = rightAxis * bounds.extents.x;
325 Vector3 rotatedExtentsUp = upAxis * bounds.extents.y;
326 Vector3 rotatedExtentsLook = lookAxis * bounds.extents.z;
331 float newSizeX = (Mathf.Abs(rotatedExtentsRight.x) + Mathf.Abs(rotatedExtentsUp.x) + Mathf.Abs(rotatedExtentsLook.x)) * 2.0f;
332 float newSizeY = (Mathf.Abs(rotatedExtentsRight.y) + Mathf.Abs(rotatedExtentsUp.y) + Mathf.Abs(rotatedExtentsLook.y)) * 2.0f;
333 float newSizeZ = (Mathf.Abs(rotatedExtentsRight.z) + Mathf.Abs(rotatedExtentsUp.z) + Mathf.Abs(rotatedExtentsLook.z)) * 2.0f;
336 var transformedBounds =
new Bounds();
337 transformedBounds.center = transformMatrix.MultiplyPoint(bounds.center);
338 transformedBounds.size =
new Vector3(newSizeX, newSizeY, newSizeZ);
341 return transformedBounds;
353 Vector3 aabbCenter = bounds.center;
354 Vector3 aabbExtents = bounds.extents;
359 camera.WorldToScreenPoint(
new Vector3(aabbCenter.x - aabbExtents.x, aabbCenter.y - aabbExtents.y, aabbCenter.z - aabbExtents.z)),
360 camera.WorldToScreenPoint(
new Vector3(aabbCenter.x + aabbExtents.x, aabbCenter.y - aabbExtents.y, aabbCenter.z - aabbExtents.z)),
361 camera.WorldToScreenPoint(
new Vector3(aabbCenter.x + aabbExtents.x, aabbCenter.y + aabbExtents.y, aabbCenter.z - aabbExtents.z)),
362 camera.WorldToScreenPoint(
new Vector3(aabbCenter.x - aabbExtents.x, aabbCenter.y + aabbExtents.y, aabbCenter.z - aabbExtents.z)),
364 camera.WorldToScreenPoint(
new Vector3(aabbCenter.x - aabbExtents.x, aabbCenter.y - aabbExtents.y, aabbCenter.z + aabbExtents.z)),
365 camera.WorldToScreenPoint(
new Vector3(aabbCenter.x + aabbExtents.x, aabbCenter.y - aabbExtents.y, aabbCenter.z + aabbExtents.z)),
366 camera.WorldToScreenPoint(
new Vector3(aabbCenter.x + aabbExtents.x, aabbCenter.y + aabbExtents.y, aabbCenter.z + aabbExtents.z)),
367 camera.WorldToScreenPoint(
new Vector3(aabbCenter.x - aabbExtents.x, aabbCenter.y + aabbExtents.y, aabbCenter.z + aabbExtents.z))
377 Vector2[] screenSpaceCornerPoints = bounds.GetScreenSpaceCornerPoints(camera);
380 Vector3 minScreenPoint = screenSpaceCornerPoints[0], maxScreenPoint = screenSpaceCornerPoints[0];
381 for (
int screenPointIndex = 1; screenPointIndex < screenSpaceCornerPoints.Length; ++screenPointIndex)
383 minScreenPoint = Vector3.Min(minScreenPoint, screenSpaceCornerPoints[screenPointIndex]);
384 maxScreenPoint = Vector3.Max(maxScreenPoint, screenSpaceCornerPoints[screenPointIndex]);
388 return new Rect(minScreenPoint.x, minScreenPoint.y, maxScreenPoint.x - minScreenPoint.x, maxScreenPoint.y - minScreenPoint.y);
396 public static float Volume(
this Bounds bounds)
398 return bounds.size.x * bounds.size.y * bounds.size.z;
409 Bounds tmpBounds = originalBounds;
411 tmpBounds.Encapsulate(otherBounds);
424 return bounds.Contains(otherBounds.min) && bounds.Contains(otherBounds.max);
434 public static bool CloserToPoint(
this Bounds bounds, Vector3 point, Bounds otherBounds)
436 Vector3 distToClosestPoint1 = bounds.ClosestPoint(point) - point;
437 Vector3 distToClosestPoint2 = otherBounds.ClosestPoint(point) - point;
439 if (distToClosestPoint1.magnitude == distToClosestPoint2.magnitude)
441 Vector3 toCenter1 = point - bounds.center;
442 Vector3 toCenter2 = point - otherBounds.center;
443 return (toCenter1.magnitude <= toCenter2.magnitude);
448 return (distToClosestPoint1.magnitude <= distToClosestPoint2.magnitude);
454 #region Private Static Functions 455 private static Vector3 GetInvalidBoundsSize()
460 return new Vector3(
float.MaxValue,
float.MaxValue,
float.MaxValue);
static void GetCornerAndMidPointPositions(this Bounds bounds, Transform transform, ref Vector3[] positions)
Gets all the corner points and mid points from Renderer's Bounds
static Bounds ExpandToContain(this Bounds originalBounds, Bounds otherBounds)
Returns bounds that contain both this bounds and the bounds passed in.
static void GetCornerAndMidPointPositions2D(this Bounds bounds, Transform transform, ref Vector3[] positions, Axis flattenAxis)
Gets all the corner points and mid points from Renderer's Bounds, ignoring the z axis ...
static float Volume(this Bounds bounds)
Returns the volume of the bounds.
static Bounds Transform(this Bounds bounds, Matrix4x4 transformMatrix)
Transforms 'bounds' using the specified transform matrix.
static Bounds GetInvalidBoundsInstance()
Returns an instance of the 'Bounds' class which is invalid. An invalid 'Bounds' instance is one which...
static bool IsValid(this Bounds bounds)
Checks if the specified bounds instance is valid. A valid 'Bounds' instance is one whose size vector ...
static bool ContainsBounds(this Bounds bounds, Bounds otherBounds)
Checks to see if bounds contains the other bounds completely.
static void GetFacePositions(this Bounds bounds, Transform transform, ref Vector3[] positions)
static Rect GetScreenRectangle(this Bounds bounds, Camera camera)
Returns the rectangle which encloses the specifies 'Bounds' instance in screen space.
static bool CloserToPoint(this Bounds bounds, Vector3 point, Bounds otherBounds)
Checks to see whether point is closer to bounds or otherBounds
static Vector2 [] GetScreenSpaceCornerPoints(this Bounds bounds, Camera camera)
Returns the screen space corner points of the specified 'Bounds' instance.
static void GetCornerPositions(this Bounds bounds, Transform transform, ref Vector3[] positions)
Gets all the corner points of the bounds in world space
static void GetCornerPositionsFromRendererBounds(this Bounds bounds, ref Vector3[] positions)
Gets all the corner points from Renderer's Bounds