| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace GDX |
| | 8 | | { |
| | 9 | | public static class Space |
| | 10 | | { |
| | 11 | | public enum Axis |
| | 12 | | { |
| | 13 | | Undefined, |
| | 14 | | X, |
| | 15 | | Y, |
| | 16 | | Z |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public enum Direction |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// An undetermined direction. |
| | 23 | | /// </summary> |
| | 24 | | Undefined, |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Z axis. |
| | 28 | | /// </summary> |
| | 29 | | Forward, |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// -Z axis. |
| | 33 | | /// </summary> |
| | 34 | | Backward, |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// -X axis. |
| | 38 | | /// </summary> |
| | 39 | | Left, |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// X axis. |
| | 43 | | /// </summary> |
| | 44 | | Right, |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Y axis. |
| | 48 | | /// </summary> |
| | 49 | | Up, |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// -Y axis. |
| | 53 | | /// </summary> |
| | 54 | | Down |
| | 55 | | } |
| | 56 | |
|
| | 57 | | #if !UNITY_DOTSRUNTIME |
| 0 | 58 | | static readonly Quaternion k_RotationLeft = Quaternion.Euler(-90, 0, 0); |
| 0 | 59 | | static readonly Quaternion k_RotationRight = Quaternion.Euler(90, 0, 0); |
| 0 | 60 | | static readonly Quaternion k_RotationUp = Quaternion.Euler(0, 90, 0); |
| 0 | 61 | | static readonly Quaternion k_RotationDown = Quaternion.Euler(0, -90, 0); |
| 0 | 62 | | static readonly Quaternion k_RotationForward = Quaternion.Euler(0, 0, 90); |
| 0 | 63 | | static readonly Quaternion k_RotationBackward = Quaternion.Euler(0, 0, -90); |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Get the corresponding <see cref="Direction" /> for a <see cref="Vector3" /> provided direction. |
| | 67 | | /// </summary> |
| | 68 | | /// <remarks>Only works with deterministic directions builtin to Unity.</remarks> |
| | 69 | | /// <param name="direction">The given direction.</param> |
| | 70 | | /// <returns>A qualified <see cref="Direction" /> equivalent to the <paramref name="direction" />.</returns> |
| | 71 | | public static Direction GetDirection(Vector3 direction) |
| 0 | 72 | | { |
| | 73 | | // Left/Right |
| 0 | 74 | | if (direction.y == 0 && direction.z == 0) |
| 0 | 75 | | { |
| 0 | 76 | | if (direction.x > 0) |
| 0 | 77 | | { |
| 0 | 78 | | return Direction.Right; |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | if (direction.x < 0) |
| 0 | 82 | | { |
| 0 | 83 | | return Direction.Left; |
| | 84 | | } |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | // Forward / Backward |
| 0 | 88 | | if (direction.x == 0 && direction.y == 0) |
| 0 | 89 | | { |
| 0 | 90 | | if (direction.z > 0) |
| 0 | 91 | | { |
| 0 | 92 | | return Direction.Forward; |
| | 93 | | } |
| | 94 | |
|
| 0 | 95 | | if (direction.z < 0) |
| 0 | 96 | | { |
| 0 | 97 | | return Direction.Backward; |
| | 98 | | } |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | // Up / Down |
| 0 | 102 | | if (direction.x == 0 && direction.z == 0) |
| 0 | 103 | | { |
| 0 | 104 | | if (direction.y > 0) |
| 0 | 105 | | { |
| 0 | 106 | | return Direction.Up; |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | if (direction.y < 0) |
| 0 | 110 | | { |
| 0 | 111 | | return Direction.Down; |
| | 112 | | } |
| 0 | 113 | | } |
| | 114 | |
|
| 0 | 115 | | return Direction.Undefined; |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | public static Quaternion ToRotation(this Axis axis) |
| 0 | 119 | | { |
| 0 | 120 | | return axis switch |
| | 121 | | { |
| 0 | 122 | | Axis.Undefined => Quaternion.identity, |
| 0 | 123 | | Axis.X => k_RotationRight, |
| 0 | 124 | | Axis.Y => k_RotationUp, |
| 0 | 125 | | Axis.Z => k_RotationForward, |
| 0 | 126 | | _ => Quaternion.identity |
| | 127 | | }; |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | public static Quaternion ToRotation(this Direction direction) |
| 0 | 131 | | { |
| 0 | 132 | | return direction switch |
| | 133 | | { |
| 0 | 134 | | Direction.Undefined => Quaternion.identity, |
| 0 | 135 | | Direction.Left => k_RotationLeft, |
| 0 | 136 | | Direction.Right => k_RotationRight, |
| 0 | 137 | | Direction.Up => k_RotationUp, |
| 0 | 138 | | Direction.Down => k_RotationDown, |
| 0 | 139 | | Direction.Backward => k_RotationBackward, |
| 0 | 140 | | Direction.Forward => k_RotationForward, |
| 0 | 141 | | _ => Quaternion.identity |
| | 142 | | }; |
| 0 | 143 | | } |
| | 144 | | #endif // !UNITY_DOTSRUNTIME |
| | 145 | | } |
| | 146 | | } |