|
1 | | -# FixedMathSharp-Unity |
| 1 | +# FixedMathSharp for Unity (NoMemoryPack) |
2 | 2 |
|
3 | | - |
| 3 | +Unity package for FixedMathSharp without the `MemoryPack` dependency. |
4 | 4 |
|
5 | | -A **deterministic fixed-point math** library for **Unity** and **.NET**, designed for **lockstep simulation**, **multiplayer determinism**, and **floating-point-free precision**. |
| 5 | +This is the recommended variant for Unity Burst AOT scenarios. `MemoryPack`'s Unity |
| 6 | +support is centered on IL2CPP through its .NET source-generator path, so the |
| 7 | +no-MemoryPack build is the safer choice when Burst AOT compatibility is the priority. |
6 | 8 |
|
7 | | -This package is a Unity-specific implementation of the [FixedMathSharp](https://github.com/mrdav30/FixedMathSharp) library |
| 9 | +## Install |
8 | 10 |
|
9 | | ---- |
| 11 | +Add this package from Git URL in Unity Package Manager: |
10 | 12 |
|
11 | | -## 🛠️ Key Features |
| 13 | +`https://github.com/mrdav30/FixedMathSharp-Unity.git?path=/com.mrdav30.fixedmathsharp.nomemorypack` |
12 | 14 |
|
13 | | -- **Fully Deterministic** – Eliminates floating-point errors for lockstep simulations. |
14 | | -- **Fixed-Point Arithmetic** – Implements `Fixed64` with high precision. |
15 | | -- **Math & Trigonometry** – Optimized `FixedMath` and `FixedTrigonometry` utilities. |
16 | | -- **Vector & Matrix Support** – Includes `Vector2d`, `Vector3d`, `FixedQuaternion`, and `Fixed4x4`. |
17 | | -- **Bounding Volume Utilities** – Use `BoundingBox`, `BoundingSphere`, and `BoundingArea` for collision checks. |
18 | | -- **Unity Conversion Helpers** – Convert between Unity `Transform`, `Matrix4x4`, `Bounds`, and FixedMathSharp matrix and bounds types. |
19 | | -- **Round-Trip Transform Interop** – Capture Unity transform data into `Fixed4x4`/`Fixed3x3`, then apply it back onto preview transforms for validation or tooling. |
20 | | -- **Editor Debugging Support** – Includes inspector support for viewing `Fixed3x3` and `Fixed4x4` data more easily in Unity. |
21 | | -- **Unity Integration** – Compatible with **Unity’s Job System** & **Burst Compiler**. |
22 | | -- **Full Serialization Support:** Out-of-the-box round-trip serialization via `MemoryPack` across all serializable structs. |
| 15 | +## When To Use This Package |
23 | 16 |
|
24 | | ---- |
| 17 | +- You are using Unity Burst AOT. |
| 18 | +- You want the safest default option for Burst-oriented projects. |
| 19 | +- You do not want `MemoryPack` included. |
| 20 | +- You prefer to bring your own serialization solution. |
25 | 21 |
|
26 | | -## 🚀 Installation |
| 22 | +## Choose The Other Variant If |
27 | 23 |
|
28 | | -### **Via Unity Package Manager (UPM)** |
| 24 | +- You want the standard package with built-in `MemoryPack` support. |
| 25 | +- Your project depends on the package's `MemoryPack` serialization path. |
29 | 26 |
|
30 | | -1. Open **Unity Package Manager** (`Window > Package Manager`). |
31 | | -2. Click **Add package from git URL...**. |
32 | | -3. Enter: |
| 27 | +## Included |
33 | 28 |
|
34 | | -<https://github.com/mrdav30/FixedMathSharp-Unity.git> |
| 29 | +- Deterministic fixed-point math via `Fixed64` |
| 30 | +- Vectors, quaternions, matrices, and bounds |
| 31 | +- Unity transform, matrix, and bounds interop helpers |
| 32 | +- Editor debugging helpers |
| 33 | +- Sample demo scene |
| 34 | +- No `MemoryPack` dependency |
35 | 35 |
|
36 | | -### **Manual Import** |
| 36 | +## Related Packages |
37 | 37 |
|
38 | | -1. Download the latest `FixedMathSharp.unitypackage` from the [Releases](https://github.com/mrdav30/FixedMathSharp-Unity/releases). |
39 | | -2. In Unity, go to **Assets > Import Package > Custom Package...**. |
40 | | -3. Select and import `FixedMathSharp.unitypackage`. |
41 | | - |
42 | | ---- |
43 | | - |
44 | | -## 📖 Usage Examples |
45 | | - |
46 | | -### Basic Arithmetic with `Fixed64` |
47 | | - |
48 | | -```csharp |
49 | | -Fixed64 a = new Fixed64(1.5); |
50 | | -Fixed64 b = new Fixed64(2.5); |
51 | | -Fixed64 result = a + b; |
52 | | -Console.WriteLine(result); // Output: 4.0 |
53 | | -``` |
54 | | - |
55 | | -### Vector Operations |
56 | | - |
57 | | -```csharp |
58 | | -Vector3d v1 = new Vector3d(1, 2, 3); |
59 | | -Vector3d v2 = new Vector3d(4, 5, 6); |
60 | | -Fixed64 dotProduct = Vector3d.Dot(v1, v2); |
61 | | -Console.WriteLine(dotProduct); // Output: 32 |
62 | | -``` |
63 | | - |
64 | | -### Quaternion Rotation |
65 | | - |
66 | | -```csharp |
67 | | -FixedQuaternion rotation = FixedQuaternion.FromAxisAngle(Vector3d.Up, FixedMath.PiOver2); // 90 degrees around Y-axis |
68 | | -Vector3d point = new Vector3d(1, 0, 0); |
69 | | -Vector3d rotatedPoint = rotation.Rotate(point); |
70 | | -Console.WriteLine(rotatedPoint); // Output: (0, 0, -1) |
71 | | -``` |
72 | | - |
73 | | -### Matrix Transformations |
74 | | - |
75 | | -```csharp |
76 | | -Fixed4x4 matrix = Fixed4x4.Identity; |
77 | | -Vector3d position = new Vector3d(1, 2, 3); |
78 | | -matrix.SetTransform(position, Vector3d.One, FixedQuaternion.Identity); |
79 | | -Console.WriteLine(matrix); |
80 | | -``` |
81 | | - |
82 | | -### Bounding Shapes and Intersection |
83 | | - |
84 | | -```csharp |
85 | | -BoundingBox box = new BoundingBox(new Vector3d(0, 0, 0), new Vector3d(5, 5, 5)); |
86 | | -BoundingSphere sphere = new BoundingSphere(new Vector3d(3, 3, 3), new Fixed64(1)); |
87 | | -bool intersects = box.Intersects(sphere); |
88 | | -Console.WriteLine(intersects); // Output: True |
89 | | -``` |
90 | | - |
91 | | -### Trigonometry Example |
92 | | - |
93 | | -```csharp |
94 | | -Fixed64 angle = FixedMath.PiOver4; // 45 degrees |
95 | | -Fixed64 sinValue = FixedTrigonometry.Sin(angle); |
96 | | -Console.WriteLine(sinValue); // Output: ~0.707 |
97 | | -``` |
98 | | - |
99 | | -### Unity Transform Interop |
100 | | - |
101 | | -```csharp |
102 | | -Transform source = transform; |
103 | | - |
104 | | -Fixed4x4 worldMatrix = source.ToFixed4x4WorldMatrix(); |
105 | | -Fixed3x3 worldRotation = source.ToFixed3x3WorldRotationMatrix(); |
106 | | -Fixed3x3 worldRotationScale = source.ToFixed3x3WorldRotationScaleMatrix(); |
107 | | -``` |
108 | | - |
109 | | -```csharp |
110 | | -worldMatrix.ApplyToTransformWorld(targetTransform); |
111 | | -worldRotation.ApplyRotationToTransformWorld(rotationOnlyTarget); |
112 | | -worldRotationScale.ApplyRotationScaleToTransformWorld(rotationScaleTarget); |
113 | | -``` |
114 | | - |
115 | | -### Unity Matrix Interop |
116 | | - |
117 | | -```csharp |
118 | | -Matrix4x4 unityMatrix = transform.localToWorldMatrix; |
119 | | - |
120 | | -Fixed4x4 fixedMatrix = unityMatrix.ToFixed4x4(); |
121 | | -Fixed3x3 fixedRotation = unityMatrix.ToFixed3x3RotationMatrix(); |
122 | | -Fixed3x3 fixedRotationScale = unityMatrix.ToFixed3x3RotationScaleMatrix(); |
123 | | - |
124 | | -Matrix4x4 roundTripMatrix = fixedMatrix.ToMatrix4x4(); |
125 | | -``` |
126 | | - |
127 | | -### Unity Bounds Interop |
128 | | - |
129 | | -```csharp |
130 | | -Bounds unityBounds = renderer.bounds; |
131 | | - |
132 | | -BoundingBox fixedBoundingBox = unityBounds.ToBoundingBox(); |
133 | | -BoundingArea fixedBoundingArea = unityBounds.ToBoundingArea(); |
134 | | - |
135 | | -Bounds boxRoundTrip = fixedBoundingBox.ToBounds(); |
136 | | -Bounds areaRoundTrip = fixedBoundingArea.ToBounds(); |
137 | | -``` |
138 | | - |
139 | | ---- |
140 | | - |
141 | | -## 🎬 Example Scene |
142 | | - |
143 | | -The package includes a ready-to-use demo scene and demo components under `Examples/`: |
144 | | - |
145 | | -- `Examples/DemoScene.unity` |
146 | | -- `Examples/FixedTransformInteropDemo.cs` |
147 | | -- `Examples/FixedBoundsInteropDemo.cs` |
148 | | - |
149 | | -`DemoScene.unity` is intended to visually validate the new Unity interop helpers: |
150 | | - |
151 | | -- `Transform -> Fixed4x4` |
152 | | -- `Transform -> Fixed3x3` |
153 | | -- `Fixed4x4/Fixed3x3 -> Transform` |
154 | | -- `Bounds <-> BoundingBox` |
155 | | -- `Bounds <-> BoundingArea` |
156 | | - |
157 | | -The transform demo captures local and world matrix data from a source `Transform`, exposes the fixed values in the inspector, and applies round-tripped results onto preview targets. The bounds demo captures Unity `Bounds`, converts them into FixedMathSharp bounds types, and draws round-tripped gizmos for quick visual comparison. |
158 | | - |
159 | | ---- |
160 | | - |
161 | | -## 🔄 Unity Interop Overview |
162 | | - |
163 | | -The current Unity-facing helpers include: |
164 | | - |
165 | | -- `Transform` to `Fixed4x4` |
166 | | - - `ToFixed4x4LocalMatrix()` |
167 | | - - `ToFixed4x4WorldMatrix()` |
168 | | -- `Transform` to `Fixed3x3` |
169 | | - - `ToFixed3x3LocalRotationMatrix()` |
170 | | - - `ToFixed3x3WorldRotationMatrix()` |
171 | | - - `ToFixed3x3LocalRotationScaleMatrix()` |
172 | | - - `ToFixed3x3WorldRotationScaleMatrix()` |
173 | | -- `Fixed4x4` to Unity |
174 | | - - `ToMatrix4x4()` |
175 | | - - `ApplyToTransformLocal()` |
176 | | - - `ApplyToTransformWorld()` |
177 | | - - `ToTransformLocal()` |
178 | | - - `ToTransformWorld()` |
179 | | -- `Fixed3x3` to Unity |
180 | | - - `ToMatrix4x4()` |
181 | | - - `ApplyRotationToTransformLocal()` |
182 | | - - `ApplyRotationToTransformWorld()` |
183 | | - - `ApplyRotationScaleToTransformLocal()` |
184 | | - - `ApplyRotationScaleToTransformWorld()` |
185 | | - - `ToTransformRotationLocal()` |
186 | | - - `ToTransformRotationWorld()` |
187 | | - - `ToTransformRotationScaleLocal()` |
188 | | - - `ToTransformRotationScaleWorld()` |
189 | | -- `Matrix4x4` to FixedMathSharp |
190 | | - - `ToFixed4x4()` |
191 | | - - `ToFixed3x3RotationMatrix()` |
192 | | - - `ToFixed3x3RotationScaleMatrix()` |
193 | | -- `Bounds` interop |
194 | | - - `ToBoundingBox()` |
195 | | - - `ToBoundingArea()` |
196 | | - - `BoundingBox.ToBounds()` |
197 | | - - `BoundingArea.ToBounds()` |
198 | | - |
199 | | -These helpers are designed to preserve FixedMathSharp’s deterministic math model while still fitting naturally into Unity workflows, editor tooling, and visual debugging. |
200 | | - |
201 | | ---- |
202 | | - |
203 | | -## 🛠️ Compatibility |
204 | | - |
205 | | -- **.NET Standard** 2.1 |
206 | | -- **Unity3D Version:** 2022.3+ |
207 | | -- **Platforms:** Windows, Linux, macOS, WebGL, Mobile |
208 | | - |
209 | | ---- |
210 | | - |
211 | | -## 📄 License |
212 | | - |
213 | | -This project is licensed under the MIT License. |
214 | | - |
215 | | -See the following files for details: |
216 | | - |
217 | | -- LICENSE – standard MIT license |
218 | | -- NOTICE – additional terms regarding project branding and redistribution |
219 | | -- COPYRIGHT – authorship information |
220 | | - |
221 | | ---- |
222 | | - |
223 | | -## 👥 Contributors |
224 | | - |
225 | | -- **mrdav30** - Lead Developer |
226 | | -- Contributions are welcome! Feel free to submit pull requests or report issues. |
227 | | - |
228 | | ---- |
229 | | - |
230 | | -## 💬 Community & Support |
231 | | - |
232 | | -For questions, discussions, or general support, join the official Discord community: |
233 | | - |
234 | | -👉 **[Join the Discord Server](https://discord.gg/mhwK2QFNBA)** |
235 | | - |
236 | | -For bug reports or feature requests, please open an issue in this repository. |
237 | | - |
238 | | -We welcome feedback, contributors, and community discussion across all projects. |
239 | | - |
240 | | ---- |
| 38 | +- Repo overview and variant selection: |
| 39 | + [FixedMathSharp-Unity](https://github.com/mrdav30/FixedMathSharp-Unity) |
| 40 | +- Standard variant: |
| 41 | + `https://github.com/mrdav30/FixedMathSharp-Unity.git?path=/com.mrdav30.fixedmathsharp` |
0 commit comments