Repository navigation
Expand file tree
/
Copy pathFixed3x3.Extensions.cs
More file actions
156 lines (139 loc) · 6.3 KB
/
Copy pathFixed3x3.Extensions.cs
File metadata and controls
156 lines (139 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//=======================================================================
// Fixed3x3.Extensions.cs
//=======================================================================
// MIT License, Copyright (c) 2024–present David Oravsky (mrdav30)
// See LICENSE file in the project root for full license information.
//=======================================================================
using System;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace FixedMathSharp
{
/// <summary>
/// Provides raw basis conversion and explicit rotation-only Unity interop for Fixed3x3.
/// </summary>
public static class Fixed3x3UnityExtensions
{
/// <summary>
/// Converts a FixedMathSharp row-vector basis matrix to a Unity column-vector matrix.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix4x4 ToMatrix4x4(this Fixed3x3 matrix)
{
Matrix4x4 unityMatrix = Matrix4x4.identity;
unityMatrix.m00 = (float)matrix.M11;
unityMatrix.m01 = (float)matrix.M21;
unityMatrix.m02 = (float)matrix.M31;
unityMatrix.m10 = (float)matrix.M12;
unityMatrix.m11 = (float)matrix.M22;
unityMatrix.m12 = (float)matrix.M32;
unityMatrix.m20 = (float)matrix.M13;
unityMatrix.m21 = (float)matrix.M23;
unityMatrix.m22 = (float)matrix.M33;
return unityMatrix;
}
/// <summary>
/// Copies the upper-left Unity basis into a FixedMathSharp row-vector matrix without removing scale or shear.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Fixed3x3 ToFixed3x3Basis(this Matrix4x4 matrix)
{
return new Fixed3x3(
(Fixed64)matrix.m00, (Fixed64)matrix.m10, (Fixed64)matrix.m20,
(Fixed64)matrix.m01, (Fixed64)matrix.m11, (Fixed64)matrix.m21,
(Fixed64)matrix.m02, (Fixed64)matrix.m12, (Fixed64)matrix.m22);
}
/// <summary>
/// Extracts the normalized rotation represented by a Unity matrix.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Fixed3x3 ToFixed3x3Rotation(this Matrix4x4 matrix) =>
matrix.rotation.ToFixedQuaternion().ToMatrix3x3();
/// <summary>
/// Gets a Unity transform's world rotation as a FixedMathSharp rotation matrix.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Fixed3x3 ToFixed3x3WorldRotation(this Transform transform)
{
if (transform == null)
throw new ArgumentNullException(nameof(transform));
return transform.rotation.ToFixedQuaternion().ToMatrix3x3();
}
/// <summary>
/// Gets a Unity transform's local rotation as a FixedMathSharp rotation matrix.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Fixed3x3 ToFixed3x3LocalRotation(this Transform transform)
{
if (transform == null)
throw new ArgumentNullException(nameof(transform));
return transform.localRotation.ToFixedQuaternion().ToMatrix3x3();
}
/// <summary>
/// Creates a Unity transform whose local rotation is extracted from the matrix.
/// </summary>
/// <param name="matrix">The rotation matrix to apply.</param>
/// <param name="name">The new GameObject's name.</param>
/// <param name="parent">The optional parent transform.</param>
/// <returns>The created transform.</returns>
public static Transform CreateTransformRotationLocal(
this Fixed3x3 matrix,
string name = "Fixed3x3 Rotation Transform",
Transform parent = null!)
{
Quaternion rotation = ExtractUnityRotation(matrix);
Transform transform = CreateTransform(name, parent);
transform.localRotation = rotation;
return transform;
}
/// <summary>
/// Creates a Unity transform whose world rotation is extracted from the matrix.
/// </summary>
/// <param name="matrix">The rotation matrix to apply.</param>
/// <param name="name">The new GameObject's name.</param>
/// <param name="parent">The optional parent transform.</param>
/// <returns>The created transform.</returns>
public static Transform CreateTransformRotationWorld(
this Fixed3x3 matrix,
string name = "Fixed3x3 Rotation Transform",
Transform parent = null!)
{
Quaternion rotation = ExtractUnityRotation(matrix);
Transform transform = CreateTransform(name, parent);
transform.rotation = rotation;
return transform;
}
/// <summary>
/// Applies the matrix's normalized rotation to a Unity transform in local space.
/// </summary>
public static void ApplyRotationToTransformLocal(this Fixed3x3 matrix, Transform transform)
{
if (transform == null)
throw new ArgumentNullException(nameof(transform));
transform.localRotation = ExtractUnityRotation(matrix);
}
/// <summary>
/// Applies the matrix's normalized rotation to a Unity transform in world space.
/// </summary>
public static void ApplyRotationToTransformWorld(this Fixed3x3 matrix, Transform transform)
{
if (transform == null)
throw new ArgumentNullException(nameof(transform));
transform.rotation = ExtractUnityRotation(matrix);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Quaternion ExtractUnityRotation(Fixed3x3 matrix)
{
return FixedQuaternion.FromMatrix(Fixed3x3.GetNormalized(matrix)).ToQuaternion();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Transform CreateTransform(string name, Transform parent)
{
GameObject gameObject = new(name);
Transform transform = gameObject.transform;
if (parent != null)
transform.SetParent(parent, false);
return transform;
}
}
}