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