1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 01:58:49 +00:00

Remove Box struct, use custom Cuboid, rename Box to WireCube

This commit is contained in:
Oliver Booth 2022-06-02 12:10:00 +01:00
parent e501f3b75b
commit 96170eac6f
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
3 changed files with 185 additions and 400 deletions

View File

@ -1,231 +0,0 @@
using UnityEngine;
using X10D.Drawing;
namespace X10D.Unity;
/// <summary>
/// Represents a box that can be drawn using the <see cref="DebugEx" /> class.
/// </summary>
/// <remarks>
/// This structure serves no real purpose except to be used in tandem with <see cref="DebugEx" />. For creating a logical
/// cuboid, consider using the <see cref="Cuboid" /> structure.
/// </remarks>
public readonly struct Box
{
/// <summary>
/// Initializes a new instance of the <see cref="Box" /> struct.
/// </summary>
/// <param name="origin">The origin of the box.</param>
/// <param name="halfExtents">The half extents of the box.</param>
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;
}
/// <summary>
/// Initializes a new instance of the <see cref="Box" /> struct.
/// </summary>
/// <param name="origin">The origin of the box.</param>
/// <param name="halfExtents">The half extents of the box.</param>
/// <param name="orientation">The orientation of the box.</param>
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;
}
/// <summary>
/// Gets the origin of the box.
/// </summary>
/// <value>The origin.</value>
public Vector3 Origin { get; }
/// <summary>
/// Gets the front-top-left corner of the box, in local space.
/// </summary>
/// <value>The front-top-left corner.</value>
public Vector3 LocalFrontTopLeft { get; }
/// <summary>
/// Gets the front-top-right corner of the box, in local space.
/// </summary>
/// <value>The front-top-right corner.</value>
public Vector3 LocalFrontTopRight { get; }
/// <summary>
/// Gets the front-bottom-left corner of the box, in local space.
/// </summary>
/// <value>The front-bottom-left corner.</value>
public Vector3 LocalFrontBottomLeft { get; }
/// <summary>
/// Gets the front-bottom-right corner of the box, in local space.
/// </summary>
/// <value>The front-bottom-right corner.</value>
public Vector3 LocalFrontBottomRight { get; }
/// <summary>
/// Gets the back-top-left corner of the box, in local space.
/// </summary>
/// <value>The back-top-left corner.</value>
public Vector3 LocalBackTopLeft
{
get => -LocalFrontBottomRight;
}
/// <summary>
/// Gets the back-top-right corner of the box, in local space.
/// </summary>
/// <value>The back-top-right corner.</value>
public Vector3 LocalBackTopRight
{
get => -LocalFrontBottomLeft;
}
/// <summary>
/// Gets the back-bottom-left corner of the box, in local space.
/// </summary>
/// <value>The back-bottom-left corner.</value>
public Vector3 LocalBackBottomLeft
{
get => -LocalFrontTopRight;
}
/// <summary>
/// Gets the back-bottom-right corner of the box, in local space.
/// </summary>
/// <value>The back-bottom-right corner.</value>
public Vector3 LocalBackBottomRight
{
get => -LocalFrontTopLeft;
}
/// <summary>
/// Gets the front-top-left corner of the box, in world space.
/// </summary>
/// <value>The front-top-left corner.</value>
public Vector3 FrontTopLeft
{
get => LocalFrontTopLeft + Origin;
}
/// <summary>
/// Gets the front-top-right corner of the box, in world space.
/// </summary>
/// <value>The front-top-right corner.</value>
public Vector3 FrontTopRight
{
get => LocalFrontTopRight + Origin;
}
/// <summary>
/// Gets the front-bottom-left corner of the box, in world space.
/// </summary>
/// <value>The front-bottom-left corner.</value>
public Vector3 FrontBottomLeft
{
get => LocalFrontBottomLeft + Origin;
}
/// <summary>
/// Gets the front-bottom-right corner of the box, in world space.
/// </summary>
/// <value>The front-bottom-right corner.</value>
public Vector3 FrontBottomRight
{
get => LocalFrontBottomRight + Origin;
}
/// <summary>
/// Gets the back-bottom-left corner of the box, in world space.
/// </summary>
/// <value>The back-bottom-left corner.</value>
public Vector3 BackTopLeft
{
get => LocalBackTopLeft + Origin;
}
/// <summary>
/// Gets the back-bottom-right corner of the box, in world space.
/// </summary>
/// <value>The back-bottom-right corner.</value>
public Vector3 BackTopRight
{
get => LocalBackTopRight + Origin;
}
/// <summary>
/// Gets the back-bottom-right corner of the box, in world space.
/// </summary>
/// <value>The back-bottom-right corner.</value>
public Vector3 BackBottomLeft
{
get => LocalBackBottomLeft + Origin;
}
/// <summary>
/// Gets the back-bottom-right corner of the box, in world space.
/// </summary>
/// <value>The back-bottom-right corner.</value>
public Vector3 BackBottomRight
{
get => LocalBackBottomRight + Origin;
}
/// <summary>
/// Implicitly converts an instance of <see cref="Bounds" /> to an instance of <see cref="Box" />.
/// </summary>
/// <param name="bounds">The <see cref="Bounds" /> to convert.</param>
/// <returns>A new instance of <see cref="Box" />.</returns>
public static implicit operator Box(Bounds bounds)
{
return new Box(bounds.center, bounds.extents);
}
/// <summary>
/// Implicitly converts an instance of <see cref="Bounds" /> to an instance of <see cref="Box" />.
/// </summary>
/// <param name="bounds">The <see cref="Bounds" /> to convert.</param>
/// <returns>A new instance of <see cref="Box" />.</returns>
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);
}
}

View File

@ -1,169 +0,0 @@
using UnityEngine;
namespace X10D.Unity;
public static partial class DebugEx
{
/// <summary>
/// Draws a box.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="halfExtents">The extents of the box, halved.</param>
public static void DrawBox(Vector3 center, Vector3 halfExtents)
{
DrawBox(center, halfExtents, Color.white, DefaultDrawDuration, DefaultDepthTest);
}
/// <summary>
/// Draws a box with the specified orientation.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="halfExtents">The extents of the box, halved.</param>
/// <param name="orientation">The orientation of the box.</param>
public static void DrawBox(Vector3 center, Vector3 halfExtents, Quaternion orientation)
{
DrawBox(new Box(center, halfExtents, orientation), Color.white, DefaultDrawDuration, DefaultDepthTest);
}
/// <summary>
/// Draws a box with the specified color.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="halfExtents">The extents of the box, halved.</param>
/// <param name="color">The color of the box.</param>
public static void DrawBox(Vector3 center, Vector3 halfExtents, in Color color)
{
DrawBox(center, halfExtents, color, DefaultDrawDuration, DefaultDepthTest);
}
/// <summary>
/// Draws a box with the specified orientation and color.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="halfExtents">The extents of the box, halved.</param>
/// <param name="orientation">The orientation of the box.</param>
/// <param name="color">The color of the box.</param>
public static void DrawBox(Vector3 center, Vector3 halfExtents, Quaternion orientation, in Color color)
{
DrawBox(new Box(center, halfExtents, orientation), color, DefaultDrawDuration, DefaultDepthTest);
}
/// <summary>
/// Draws a box with the specified color and duration.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="halfExtents">The extents of the box, halved.</param>
/// <param name="color">The color of the box.</param>
/// <param name="duration">
/// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame.
/// </param>
public static void DrawBox(Vector3 center, Vector3 halfExtents, in Color color, float duration)
{
DrawBox(center, halfExtents, color, duration, DefaultDepthTest);
}
/// <summary>
/// Draws a box with the specified orientation, color, and duration.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="halfExtents">The extents of the box, halved.</param>
/// <param name="orientation">The orientation of the box.</param>
/// <param name="color">The color of the box.</param>
/// <param name="duration">
/// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame.
/// </param>
public static void DrawBox(Vector3 center, Vector3 halfExtents, Quaternion orientation, in Color color, float duration)
{
DrawBox(new Box(center, halfExtents, orientation), color, duration, DefaultDepthTest);
}
/// <summary>
/// Draws a box with the specified color and duration.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="halfExtents">The extents of the box, halved.</param>
/// <param name="color">The color of the box.</param>
/// <param name="duration">
/// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame.
/// </param>
/// <param name="depthTest">
/// <see langword="DefaultDepthTest" /> if depth test should be applied; otherwise, <see langword="false" />. Passing
/// <see langword="DefaultDepthTest" /> will have the box be obscured by objects closer to the camera.
/// </param>
public static void DrawBox(Vector3 center, Vector3 halfExtents, in Color color, float duration, bool depthTest)
{
DrawBox(new Box(center, halfExtents), color, duration, depthTest);
}
/// <summary>
/// Draws a box with the specified orientation, color, and duration.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="halfExtents">The extents of the box, halved.</param>
/// <param name="orientation">The orientation of the box.</param>
/// <param name="color">The color of the box.</param>
/// <param name="duration">
/// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame.
/// </param>
/// <param name="depthTest">
/// <see langword="DefaultDepthTest" /> if depth test should be applied; otherwise, <see langword="false" />. Passing
/// <see langword="DefaultDepthTest" /> will have the box be obscured by objects closer to the camera.
/// </param>
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);
}
/// <summary>
/// Draws a box with the specified color.
/// </summary>
/// <param name="box">The box to draw.</param>
/// <param name="color">The color of the box.</param>
public static void DrawBox(Box box, in Color color)
{
DrawBox(box, color, DefaultDrawDuration, DefaultDepthTest);
}
/// <summary>
/// Draws a box with the specified color and duration.
/// </summary>
/// <param name="box">The box to draw.</param>
/// <param name="color">The color of the box.</param>
/// <param name="duration">
/// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame.
/// </param>
public static void DrawBox(Box box, in Color color, float duration)
{
DrawBox(box, color, duration, DefaultDepthTest);
}
/// <summary>
/// Draws a box with the specified color and duration.
/// </summary>
/// <param name="box">The box to draw.</param>
/// <param name="color">The color of the box.</param>
/// <param name="duration">
/// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame.
/// </param>
/// <param name="depthTest">
/// <see langword="DefaultDepthTest" /> if depth test should be applied; otherwise, <see langword="false" />. Passing
/// <see langword="DefaultDepthTest" /> will have the box be obscured by objects closer to the camera.
/// </param>
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);
}
}

View File

@ -0,0 +1,185 @@
using UnityEngine;
using X10D.Drawing;
using X10D.Unity.Numerics;
namespace X10D.Unity;
public static partial class DebugEx
{
/// <summary>
/// Draws a wireframe cube with a center and a size.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="size">The extents of the box.</param>
public static void DrawWireCube(Vector3 center, Vector3 size)
{
DrawWireCube(center, size, Color.white, DefaultDrawDuration, DefaultDepthTest);
}
/// <summary>
/// Draws a box with the specified orientation.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="size">The extents of the box.</param>
/// <param name="orientation">The orientation of the box.</param>
public static void DrawWireCube(Vector3 center, Vector3 size, Quaternion orientation)
{
DrawWireCube(new Cuboid(center.ToSystemVector(), size.ToSystemVector(), orientation.ToSystemQuaternion()), Color.white,
DefaultDrawDuration, DefaultDepthTest);
}
/// <summary>
/// Draws a box with the specified color.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="size">The extents of the box.</param>
/// <param name="color">The color of the box.</param>
public static void DrawWireCube(Vector3 center, Vector3 size, in Color color)
{
DrawWireCube(center, size, color, DefaultDrawDuration, DefaultDepthTest);
}
/// <summary>
/// Draws a box with the specified orientation and color.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="size">The extents of the box.</param>
/// <param name="orientation">The orientation of the box.</param>
/// <param name="color">The color of the box.</param>
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);
}
/// <summary>
/// Draws a box with the specified color and duration.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="size">The extents of the box.</param>
/// <param name="color">The color of the box.</param>
/// <param name="duration">
/// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame.
/// </param>
public static void DrawWireCube(Vector3 center, Vector3 size, in Color color, float duration)
{
DrawWireCube(center, size, color, duration, DefaultDepthTest);
}
/// <summary>
/// Draws a box with the specified orientation, color, and duration.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="size">The extents of the box.</param>
/// <param name="orientation">The orientation of the box.</param>
/// <param name="color">The color of the box.</param>
/// <param name="duration">
/// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame.
/// </param>
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);
}
/// <summary>
/// Draws a box with the specified color and duration.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="size">The extents of the box.</param>
/// <param name="color">The color of the box.</param>
/// <param name="duration">
/// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame.
/// </param>
/// <param name="depthTest">
/// <see langword="DefaultDepthTest" /> if depth test should be applied; otherwise, <see langword="false" />. Passing
/// <see langword="DefaultDepthTest" /> will have the box be obscured by objects closer to the camera.
/// </param>
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);
}
/// <summary>
/// Draws a box with the specified orientation, color, and duration.
/// </summary>
/// <param name="center">The center point.</param>
/// <param name="size">The extents of the box.</param>
/// <param name="orientation">The orientation of the box.</param>
/// <param name="color">The color of the box.</param>
/// <param name="duration">
/// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame.
/// </param>
/// <param name="depthTest">
/// <see langword="DefaultDepthTest" /> if depth test should be applied; otherwise, <see langword="false" />. Passing
/// <see langword="DefaultDepthTest" /> will have the box be obscured by objects closer to the camera.
/// </param>
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);
}
/// <summary>
/// Draws a box with the specified color.
/// </summary>
/// <param name="cuboid">The cuboid to draw.</param>
/// <param name="color">The color of the box.</param>
public static void DrawWireCube(in Cuboid cuboid, in Color color)
{
DrawWireCube(cuboid, color, DefaultDrawDuration, DefaultDepthTest);
}
/// <summary>
/// Draws a box with the specified color and duration.
/// </summary>
/// <param name="cuboid">The cuboid to draw.</param>
/// <param name="color">The color of the box.</param>
/// <param name="duration">
/// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame.
/// </param>
public static void DrawWireCube(in Cuboid cuboid, in Color color, float duration)
{
DrawWireCube(cuboid, color, duration, DefaultDepthTest);
}
/// <summary>
/// Draws a box with the specified color and duration.
/// </summary>
/// <param name="cuboid">The cuboid to draw.</param>
/// <param name="color">The color of the box.</param>
/// <param name="duration">
/// The duration of the box's visibility, in seconds. If 0 is passed, the box is visible for a single frame.
/// </param>
/// <param name="depthTest">
/// <see langword="DefaultDepthTest" /> if depth test should be applied; otherwise, <see langword="false" />. Passing
/// <see langword="DefaultDepthTest" /> will have the box be obscured by objects closer to the camera.
/// </param>
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);
}
}