From 96170eac6fc64534253822f2079a875235b91c9f Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 2 Jun 2022 12:10:00 +0100 Subject: [PATCH] Remove Box struct, use custom Cuboid, rename Box to WireCube --- X10D.Unity/src/Box.cs | 231 ----------------------------- X10D.Unity/src/DebugEx.Box.cs | 169 --------------------- X10D.Unity/src/DebugEx.WireCube.cs | 185 +++++++++++++++++++++++ 3 files changed, 185 insertions(+), 400 deletions(-) delete mode 100644 X10D.Unity/src/Box.cs delete mode 100644 X10D.Unity/src/DebugEx.Box.cs create mode 100644 X10D.Unity/src/DebugEx.WireCube.cs diff --git a/X10D.Unity/src/Box.cs b/X10D.Unity/src/Box.cs deleted file mode 100644 index f2fb8b7..0000000 --- a/X10D.Unity/src/Box.cs +++ /dev/null @@ -1,231 +0,0 @@ -using UnityEngine; -using X10D.Drawing; - -namespace X10D.Unity; - -/// -/// Represents a box that can be drawn using the class. -/// -/// -/// This structure serves no real purpose except to be used in tandem with . For creating a logical -/// cuboid, consider using the structure. -/// -public readonly struct Box -{ - /// - /// Initializes a new instance of the struct. - /// - /// The origin of the box. - /// The half extents of the box. - public Box(Vector3 origin, Vector3 halfExtents) - { - LocalFrontTopLeft = new Vector3(-halfExtents.x, halfExtents.y, -halfExtents.z); - LocalFrontTopRight = new Vector3(halfExtents.x, halfExtents.y, -halfExtents.z); - LocalFrontBottomLeft = new Vector3(-halfExtents.x, -halfExtents.y, -halfExtents.z); - LocalFrontBottomRight = new Vector3(halfExtents.x, -halfExtents.y, -halfExtents.z); - - Origin = origin; - } - - /// - /// Initializes a new instance of the struct. - /// - /// The origin of the box. - /// The half extents of the box. - /// The orientation of the box. - public Box(Vector3 origin, Vector3 halfExtents, Quaternion orientation) - : this(origin, halfExtents) - { - var localFrontTopLeft = new Vector3(-halfExtents.x, halfExtents.y, -halfExtents.z); - var localFrontTopRight = new Vector3(halfExtents.x, halfExtents.y, -halfExtents.z); - var localFrontBottomLeft = new Vector3(-halfExtents.x, -halfExtents.y, -halfExtents.z); - var localFrontBottomRight = new Vector3(halfExtents.x, -halfExtents.y, -halfExtents.z); - - Rotate( - orientation, - ref localFrontTopLeft, - ref localFrontTopRight, - ref localFrontBottomLeft, - ref localFrontBottomRight); - - LocalFrontTopLeft = localFrontTopLeft; - } - - /// - /// Gets the origin of the box. - /// - /// The origin. - public Vector3 Origin { get; } - - /// - /// Gets the front-top-left corner of the box, in local space. - /// - /// The front-top-left corner. - public Vector3 LocalFrontTopLeft { get; } - - /// - /// Gets the front-top-right corner of the box, in local space. - /// - /// The front-top-right corner. - public Vector3 LocalFrontTopRight { get; } - - /// - /// Gets the front-bottom-left corner of the box, in local space. - /// - /// The front-bottom-left corner. - public Vector3 LocalFrontBottomLeft { get; } - - /// - /// Gets the front-bottom-right corner of the box, in local space. - /// - /// The front-bottom-right corner. - public Vector3 LocalFrontBottomRight { get; } - - /// - /// Gets the back-top-left corner of the box, in local space. - /// - /// The back-top-left corner. - public Vector3 LocalBackTopLeft - { - get => -LocalFrontBottomRight; - } - - /// - /// Gets the back-top-right corner of the box, in local space. - /// - /// The back-top-right corner. - public Vector3 LocalBackTopRight - { - get => -LocalFrontBottomLeft; - } - - /// - /// Gets the back-bottom-left corner of the box, in local space. - /// - /// The back-bottom-left corner. - public Vector3 LocalBackBottomLeft - { - get => -LocalFrontTopRight; - } - - /// - /// Gets the back-bottom-right corner of the box, in local space. - /// - /// The back-bottom-right corner. - public Vector3 LocalBackBottomRight - { - get => -LocalFrontTopLeft; - } - - /// - /// Gets the front-top-left corner of the box, in world space. - /// - /// The front-top-left corner. - public Vector3 FrontTopLeft - { - get => LocalFrontTopLeft + Origin; - } - - /// - /// Gets the front-top-right corner of the box, in world space. - /// - /// The front-top-right corner. - public Vector3 FrontTopRight - { - get => LocalFrontTopRight + Origin; - } - - /// - /// Gets the front-bottom-left corner of the box, in world space. - /// - /// The front-bottom-left corner. - public Vector3 FrontBottomLeft - { - get => LocalFrontBottomLeft + Origin; - } - - /// - /// Gets the front-bottom-right corner of the box, in world space. - /// - /// The front-bottom-right corner. - public Vector3 FrontBottomRight - { - get => LocalFrontBottomRight + Origin; - } - - /// - /// Gets the back-bottom-left corner of the box, in world space. - /// - /// The back-bottom-left corner. - public Vector3 BackTopLeft - { - get => LocalBackTopLeft + Origin; - } - - /// - /// Gets the back-bottom-right corner of the box, in world space. - /// - /// The back-bottom-right corner. - public Vector3 BackTopRight - { - get => LocalBackTopRight + Origin; - } - - /// - /// Gets the back-bottom-right corner of the box, in world space. - /// - /// The back-bottom-right corner. - public Vector3 BackBottomLeft - { - get => LocalBackBottomLeft + Origin; - } - - /// - /// Gets the back-bottom-right corner of the box, in world space. - /// - /// The back-bottom-right corner. - public Vector3 BackBottomRight - { - get => LocalBackBottomRight + Origin; - } - - /// - /// Implicitly converts an instance of to an instance of . - /// - /// The to convert. - /// A new instance of . - public static implicit operator Box(Bounds bounds) - { - return new Box(bounds.center, bounds.extents); - } - - /// - /// Implicitly converts an instance of to an instance of . - /// - /// The to convert. - /// A new instance of . - public static implicit operator Box(BoundsInt bounds) - { - return new Box(bounds.center, (Vector3)bounds.size / 2.0f); - } - - private static Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Quaternion rotation) - { - Vector3 direction = point - pivot; - return pivot + (rotation * direction); - } - - private static void Rotate( - Quaternion orientation, - ref Vector3 localFrontTopLeft, - ref Vector3 localFrontTopRight, - ref Vector3 localFrontBottomLeft, - ref Vector3 localFrontBottomRight - ) - { - localFrontTopLeft = RotatePointAroundPivot(localFrontTopLeft, Vector3.zero, orientation); - localFrontTopRight = RotatePointAroundPivot(localFrontTopRight, Vector3.zero, orientation); - localFrontBottomLeft = RotatePointAroundPivot(localFrontBottomLeft, Vector3.zero, orientation); - localFrontBottomRight = RotatePointAroundPivot(localFrontBottomRight, Vector3.zero, orientation); - } -} diff --git a/X10D.Unity/src/DebugEx.Box.cs b/X10D.Unity/src/DebugEx.Box.cs deleted file mode 100644 index 11ab648..0000000 --- a/X10D.Unity/src/DebugEx.Box.cs +++ /dev/null @@ -1,169 +0,0 @@ -using UnityEngine; - -namespace X10D.Unity; - -public static partial class DebugEx -{ - /// - /// Draws a box. - /// - /// The center point. - /// The extents of the box, halved. - public static void DrawBox(Vector3 center, Vector3 halfExtents) - { - DrawBox(center, halfExtents, Color.white, DefaultDrawDuration, DefaultDepthTest); - } - - /// - /// Draws a box with the specified orientation. - /// - /// The center point. - /// The extents of the box, halved. - /// The orientation of the box. - public static void DrawBox(Vector3 center, Vector3 halfExtents, Quaternion orientation) - { - DrawBox(new Box(center, halfExtents, orientation), Color.white, DefaultDrawDuration, DefaultDepthTest); - } - - /// - /// Draws a box with the specified color. - /// - /// The center point. - /// The extents of the box, halved. - /// The color of the box. - public static void DrawBox(Vector3 center, Vector3 halfExtents, in Color color) - { - DrawBox(center, halfExtents, color, DefaultDrawDuration, DefaultDepthTest); - } - - /// - /// Draws a box with the specified orientation and color. - /// - /// The center point. - /// The extents of the box, halved. - /// The orientation of the box. - /// The color of the box. - public static void DrawBox(Vector3 center, Vector3 halfExtents, Quaternion orientation, in Color color) - { - DrawBox(new Box(center, halfExtents, orientation), color, DefaultDrawDuration, DefaultDepthTest); - } - - /// - /// Draws a box with the specified color and duration. - /// - /// The center point. - /// The extents of the box, halved. - /// The color of the box. - /// - /// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame. - /// - public static void DrawBox(Vector3 center, Vector3 halfExtents, in Color color, float duration) - { - DrawBox(center, halfExtents, color, duration, DefaultDepthTest); - } - - /// - /// Draws a box with the specified orientation, color, and duration. - /// - /// The center point. - /// The extents of the box, halved. - /// The orientation of the box. - /// The color of the box. - /// - /// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame. - /// - public static void DrawBox(Vector3 center, Vector3 halfExtents, Quaternion orientation, in Color color, float duration) - { - DrawBox(new Box(center, halfExtents, orientation), color, duration, DefaultDepthTest); - } - - /// - /// Draws a box with the specified color and duration. - /// - /// The center point. - /// The extents of the box, halved. - /// The color of the box. - /// - /// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame. - /// - /// - /// if depth test should be applied; otherwise, . Passing - /// will have the box be obscured by objects closer to the camera. - /// - public static void DrawBox(Vector3 center, Vector3 halfExtents, in Color color, float duration, bool depthTest) - { - DrawBox(new Box(center, halfExtents), color, duration, depthTest); - } - - /// - /// Draws a box with the specified orientation, color, and duration. - /// - /// The center point. - /// The extents of the box, halved. - /// The orientation of the box. - /// The color of the box. - /// - /// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame. - /// - /// - /// if depth test should be applied; otherwise, . Passing - /// will have the box be obscured by objects closer to the camera. - /// - public static void DrawBox(Vector3 center, Vector3 halfExtents, Quaternion orientation, in Color color, float duration, bool depthTest) - { - DrawBox(new Box(center, halfExtents, orientation), color, duration, depthTest); - } - - /// - /// Draws a box with the specified color. - /// - /// The box to draw. - /// The color of the box. - public static void DrawBox(Box box, in Color color) - { - DrawBox(box, color, DefaultDrawDuration, DefaultDepthTest); - } - - /// - /// Draws a box with the specified color and duration. - /// - /// The box to draw. - /// The color of the box. - /// - /// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame. - /// - public static void DrawBox(Box box, in Color color, float duration) - { - DrawBox(box, color, duration, DefaultDepthTest); - } - - /// - /// Draws a box with the specified color and duration. - /// - /// The box to draw. - /// The color of the box. - /// - /// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame. - /// - /// - /// if depth test should be applied; otherwise, . Passing - /// will have the box be obscured by objects closer to the camera. - /// - public static void DrawBox(Box box, in Color color, float duration, bool depthTest) - { - Debug.DrawLine(box.FrontTopLeft, box.FrontTopRight, color, duration, depthTest); - Debug.DrawLine(box.FrontTopRight, box.FrontBottomRight, color, duration, depthTest); - Debug.DrawLine(box.FrontBottomRight, box.FrontBottomLeft, color, duration, depthTest); - Debug.DrawLine(box.FrontBottomLeft, box.FrontTopLeft, color, duration, depthTest); - - Debug.DrawLine(box.BackTopLeft, box.BackTopRight, color, duration, depthTest); - Debug.DrawLine(box.BackTopRight, box.BackBottomRight, color, duration, depthTest); - Debug.DrawLine(box.BackBottomRight, box.BackBottomLeft, color, duration, depthTest); - Debug.DrawLine(box.BackBottomLeft, box.BackTopLeft, color, duration, depthTest); - - Debug.DrawLine(box.FrontTopLeft, box.BackTopLeft, color, duration, depthTest); - Debug.DrawLine(box.FrontTopRight, box.BackTopRight, color, duration, depthTest); - Debug.DrawLine(box.FrontBottomRight, box.BackBottomRight, color, duration, depthTest); - Debug.DrawLine(box.FrontBottomLeft, box.BackBottomLeft, color, duration, depthTest); - } -} diff --git a/X10D.Unity/src/DebugEx.WireCube.cs b/X10D.Unity/src/DebugEx.WireCube.cs new file mode 100644 index 0000000..90f4d77 --- /dev/null +++ b/X10D.Unity/src/DebugEx.WireCube.cs @@ -0,0 +1,185 @@ +using UnityEngine; +using X10D.Drawing; +using X10D.Unity.Numerics; + +namespace X10D.Unity; + +public static partial class DebugEx +{ + /// + /// Draws a wireframe cube with a center and a size. + /// + /// The center point. + /// The extents of the box. + public static void DrawWireCube(Vector3 center, Vector3 size) + { + DrawWireCube(center, size, Color.white, DefaultDrawDuration, DefaultDepthTest); + } + + /// + /// Draws a box with the specified orientation. + /// + /// The center point. + /// The extents of the box. + /// The orientation of the box. + public static void DrawWireCube(Vector3 center, Vector3 size, Quaternion orientation) + { + DrawWireCube(new Cuboid(center.ToSystemVector(), size.ToSystemVector(), orientation.ToSystemQuaternion()), Color.white, + DefaultDrawDuration, DefaultDepthTest); + } + + /// + /// Draws a box with the specified color. + /// + /// The center point. + /// The extents of the box. + /// The color of the box. + public static void DrawWireCube(Vector3 center, Vector3 size, in Color color) + { + DrawWireCube(center, size, color, DefaultDrawDuration, DefaultDepthTest); + } + + /// + /// Draws a box with the specified orientation and color. + /// + /// The center point. + /// The extents of the box. + /// The orientation of the box. + /// The color of the box. + public static void DrawWireCube(Vector3 center, Vector3 size, Quaternion orientation, in Color color) + { + DrawWireCube(new Cuboid(center.ToSystemVector(), size.ToSystemVector(), orientation.ToSystemQuaternion()), color, + DefaultDrawDuration, DefaultDepthTest); + } + + /// + /// Draws a box with the specified color and duration. + /// + /// The center point. + /// The extents of the box. + /// The color of the box. + /// + /// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame. + /// + public static void DrawWireCube(Vector3 center, Vector3 size, in Color color, float duration) + { + DrawWireCube(center, size, color, duration, DefaultDepthTest); + } + + /// + /// Draws a box with the specified orientation, color, and duration. + /// + /// The center point. + /// The extents of the box. + /// The orientation of the box. + /// The color of the box. + /// + /// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame. + /// + public static void DrawWireCube(Vector3 center, Vector3 size, Quaternion orientation, in Color color, float duration) + { + DrawWireCube(new Cuboid(center.ToSystemVector(), size.ToSystemVector(), orientation.ToSystemQuaternion()), color, + duration, DefaultDepthTest); + } + + /// + /// Draws a box with the specified color and duration. + /// + /// The center point. + /// The extents of the box. + /// The color of the box. + /// + /// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame. + /// + /// + /// if depth test should be applied; otherwise, . Passing + /// will have the box be obscured by objects closer to the camera. + /// + public static void DrawWireCube(Vector3 center, Vector3 size, in Color color, float duration, bool depthTest) + { + DrawWireCube(new Cuboid(center.ToSystemVector(), size.ToSystemVector()), color, duration, depthTest); + } + + /// + /// Draws a box with the specified orientation, color, and duration. + /// + /// The center point. + /// The extents of the box. + /// The orientation of the box. + /// The color of the box. + /// + /// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame. + /// + /// + /// if depth test should be applied; otherwise, . Passing + /// will have the box be obscured by objects closer to the camera. + /// + public static void DrawWireCube(Vector3 center, Vector3 size, Quaternion orientation, in Color color, float duration, + bool depthTest) + { + DrawWireCube(new Cuboid(center.ToSystemVector(), size.ToSystemVector(), orientation.ToSystemQuaternion()), color, + duration, depthTest); + } + + /// + /// Draws a box with the specified color. + /// + /// The cuboid to draw. + /// The color of the box. + public static void DrawWireCube(in Cuboid cuboid, in Color color) + { + DrawWireCube(cuboid, color, DefaultDrawDuration, DefaultDepthTest); + } + + /// + /// Draws a box with the specified color and duration. + /// + /// The cuboid to draw. + /// The color of the box. + /// + /// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame. + /// + public static void DrawWireCube(in Cuboid cuboid, in Color color, float duration) + { + DrawWireCube(cuboid, color, duration, DefaultDepthTest); + } + + /// + /// Draws a box with the specified color and duration. + /// + /// The cuboid to draw. + /// The color of the box. + /// + /// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame. + /// + /// + /// if depth test should be applied; otherwise, . Passing + /// will have the box be obscured by objects closer to the camera. + /// + public static void DrawWireCube(in Cuboid cuboid, in Color color, float duration, bool depthTest) + { + Vector3 frontTopLeft = cuboid.FrontTopLeft.ToUnityVector(); + Vector3 frontTopRight = cuboid.FrontTopRight.ToUnityVector(); + Vector3 frontBottomRight = cuboid.FrontBottomRight.ToUnityVector(); + Vector3 frontBottomLeft = cuboid.FrontBottomLeft.ToUnityVector(); + Vector3 backTopLeft = cuboid.BackTopLeft.ToUnityVector(); + Vector3 backTopRight = cuboid.BackTopRight.ToUnityVector(); + Vector3 backBottomRight = cuboid.BackBottomRight.ToUnityVector(); + Vector3 backBottomLeft = cuboid.BackBottomLeft.ToUnityVector(); + + Debug.DrawLine(frontTopLeft, frontTopRight, color, duration, depthTest); + Debug.DrawLine(frontTopRight, frontBottomRight, color, duration, depthTest); + Debug.DrawLine(frontBottomRight, frontBottomLeft, color, duration, depthTest); + Debug.DrawLine(frontBottomLeft, frontTopLeft, color, duration, depthTest); + + Debug.DrawLine(backTopLeft, backTopRight, color, duration, depthTest); + Debug.DrawLine(backTopRight, backBottomRight, color, duration, depthTest); + Debug.DrawLine(backBottomRight, backBottomLeft, color, duration, depthTest); + Debug.DrawLine(backBottomLeft, backTopLeft, color, duration, depthTest); + + Debug.DrawLine(frontTopLeft, backTopLeft, color, duration, depthTest); + Debug.DrawLine(frontTopRight, backTopRight, color, duration, depthTest); + Debug.DrawLine(frontBottomRight, backBottomRight, color, duration, depthTest); + Debug.DrawLine(frontBottomLeft, backBottomLeft, color, duration, depthTest); + } +}