-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfVector3.cs
317 lines (272 loc) · 8.84 KB
/
fVector3.cs
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using System.Runtime.InteropServices;
#if FMATH_ENABLE_NATIVE_PLUGIN
#if UNITY_IPHONE
using fMathStaticLib;
#elif UNITY_ANDROID
using fMathDynamicLib;
#endif
#endif
using UnityEngine;
[StructLayout(LayoutKind.Explicit)]
public struct fVector3
{
[FieldOffset(0)]
public ffloat x;
[FieldOffset(4)]
public ffloat y;
[FieldOffset(8)]
public ffloat z;
#region Defines
static readonly fVector3 zeroVector = new fVector3(0, 0, 0);
static readonly fVector3 oneVector = new fVector3(1, 1, 1);
static readonly fVector3 upVector = new fVector3(0, 1, 0);
static readonly fVector3 downVector = new fVector3(0, -1, 0);
static readonly fVector3 leftVector = new fVector3(-1, 0, 0);
static readonly fVector3 rightVector = new fVector3(1, 0, 0);
static readonly fVector3 forwardVector = new fVector3(0, 0, 1);
static readonly fVector3 backVector = new fVector3(0, 0, -1);
public static fVector3 zero { get { return zeroVector; } }
public static fVector3 one { get { return oneVector; } }
public static fVector3 up { get { return upVector; } }
public static fVector3 down { get { return downVector; } }
public static fVector3 left { get { return leftVector; } }
public static fVector3 right { get { return rightVector; } }
public static fVector3 forward { get { return forwardVector; } }
public static fVector3 back { get { return backVector; } }
#endregion
#region Constructors
public fVector3(ffloat x, ffloat y, ffloat z)
{
this.x = x;
this.y = y;
this.z = z;
}
public fVector3(int x, int y, int z)
{
this.x = (ffloat)x;
this.y = (ffloat)y;
this.z = (ffloat)z;
}
public fVector3(float x, float y, float z)
{
this.x = (ffloat)x;
this.y = (ffloat)y;
this.z = (ffloat)z;
}
public fVector3(Vector3 vector)
{
x = (ffloat)vector.x;
y = (ffloat)vector.y;
z = (ffloat)vector.z;
}
public static fVector3 CreateFromRawValue(int rawX, int rawY, int rawZ)
{
return new fVector3(ffloat.CreateFromRawValue(rawX), ffloat.CreateFromRawValue(rawY), ffloat.CreateFromRawValue(rawZ));
}
#endregion
public fVector3 normalized {
get {
fVector3 result = this;
result.Normalize();
return result;
}
}
public ffloat magnitude => ffloat.Sqrt(x * x + y * y + z * z);
public ffloat sqrMagnitude => x * x + y * y + z * z;
public void Normalize()
{
ffloat mag = magnitude;
if (mag > ffloat.Epsilon)
{
x /= mag;
y /= mag;
z /= mag;
}
}
public void Scale(fVector3 scale)
{
this.x *= scale.x;
this.y *= scale.y;
this.z *= scale.z;
}
public void Set(ffloat newX, ffloat newY, ffloat newZ)
{
this.x = newX;
this.y = newY;
this.z = newZ;
}
public override string ToString()
{
return String.Format("({0:F2}, {1:F2}, {2:F2})", new object[] { x.ToFloat(), y.ToFloat(), z.ToFloat() });
}
#region Arithmetic Operators
public static fVector3 operator +(fVector3 lhs, fVector3 rhs)
{
#if FMATH_ENABLE_NATIVE_PLUGIN && !UNITY_EDITOR
fVector3 result;
fvector3_add(ref lhs, ref rhs, out result);
return result;
#else
return new fVector3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
#endif
}
public static fVector3 operator -(fVector3 vec)
{
return new fVector3(-vec.x, -vec.y, -vec.z);
}
public static fVector3 operator -(fVector3 lhs, fVector3 rhs)
{
#if FMATH_ENABLE_NATIVE_PLUGIN && !UNITY_EDITOR
fVector3 result;
fvector3_sub(ref lhs, ref rhs, out result);
return result;
#else
return new fVector3(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
#endif
}
public static fVector3 operator *(fVector3 lhs, ffloat rhs)
{
#if FMATH_ENABLE_NATIVE_PLUGIN && !UNITY_EDITOR
fVector3 result;
fvector3_mul(ref lhs, rhs, out result);
return result;
#else
return new fVector3(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs);
#endif
}
public static fVector3 operator *(ffloat lhs, fVector3 rhs)
{
#if FMATH_ENABLE_NATIVE_PLUGIN && !UNITY_EDITOR
fVector3 result;
fvector3_mul(ref rhs, lhs, out result);
return result;
#else
return new fVector3(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
#endif
}
public static fVector3 operator *(fVector3 lhs, int rhs)
{
return new fVector3(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs);
}
public static fVector3 operator *(int lhs, fVector3 rhs)
{
return new fVector3(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
}
public static fVector3 operator *(fVector3 lhs, float rhs)
{
return new fVector3(lhs.x * (ffloat)rhs, lhs.y * (ffloat)rhs, lhs.z * (ffloat)rhs);
}
public static fVector3 operator *(float lhs, fVector3 rhs)
{
return new fVector3((ffloat)lhs * rhs.x, (ffloat)lhs * rhs.y, (ffloat)lhs * rhs.z);
}
public static fVector3 operator /(fVector3 lhs, ffloat rhs)
{
#if FMATH_ENABLE_NATIVE_PLUGIN && !UNITY_EDITOR
fVector3 result;
fvector3_div(ref lhs, rhs, out result);
return result;
#else
return new fVector3(lhs.x / rhs, lhs.y / rhs, lhs.z / rhs);
#endif
}
#endregion
#region Comparison Operators
public static bool operator ==(fVector3 lhs, fVector3 rhs)
{
return (lhs - rhs).sqrMagnitude < ffloat.Epsilon;
}
public static bool operator !=(fVector3 lhs, fVector3 rhs)
{
return !(lhs == rhs);
}
public override bool Equals(object obj)
{
return obj is fVector3 &&
(((fVector3)obj).x == x) &&
(((fVector3)obj).y == y) &&
(((fVector3)obj).z == z);
}
public bool Equals(fVector3 other)
{
return (x == other.x) && (y == other.y) && (z == other.z);
}
public override int GetHashCode()
{
return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2;
}
#endregion
#region Conversion Operators
public Vector3 ToUnityVector3()
{
return new Vector3(x.ToFloat(), y.ToFloat(), z.ToFloat());
}
#endregion
#region Math Functions
public static ffloat Distance(fVector3 lhs, fVector3 rhs)
{
return (lhs - rhs).magnitude;
}
public static ffloat Angle(fVector3 from, fVector3 to)
{
return ffloat.Acos(ffloat.Clamp(fVector3.Dot(from.normalized, to.normalized), -ffloat.One, ffloat.One)) * ffloat.Rad2Deg;
}
public static ffloat Dot(fVector3 lhs, fVector3 rhs)
{
#if FMATH_ENABLE_NATIVE_PLUGIN && !UNITY_EDITOR
return fvector3_dot(ref lhs, ref rhs);
#else
return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
#endif
}
public static fVector3 Cross(fVector3 lhs, fVector3 rhs)
{
#if FMATH_ENABLE_NATIVE_PLUGIN && !UNITY_EDITOR
fVector3 result;
fvector3_cross(ref lhs, ref rhs, out result);
return result;
#else
return new fVector3(
lhs.y * rhs.z - lhs.z * rhs.y,
lhs.z * rhs.x - lhs.x * rhs.z,
lhs.x * rhs.y - lhs.y * rhs.x);
#endif
}
public static fVector3 Lerp(fVector3 start, fVector3 end, ffloat t)
{
return new fVector3(
start.x + (end.x - start.x) * t,
start.y + (end.y - start.y) * t,
start.z + (end.z - start.z) * t);
}
public static fVector3 MoveTowards(fVector3 current, fVector3 target, ffloat maxDistance)
{
fVector3 diff = target - current;
ffloat magnitude = diff.magnitude;
if (magnitude <= maxDistance)
return target;
return current + diff / magnitude * maxDistance;
}
public static fVector3 Reflect(fVector3 direction, fVector3 normal)
{
return -2 * fVector3.Dot(normal, direction) * normal + direction;
}
#endregion
#region Native Plugin Interface
#if FMATH_ENABLE_NATIVE_PLUGIN
[DllImport(fMathNativeImport.libName)]
public static extern void fvector3_add(ref fVector3 lhs, ref fVector3 rhs, out fVector3 result);
[DllImport(fMathNativeImport.libName)]
public static extern void fvector3_sub(ref fVector3 lhs, ref fVector3 rhs, out fVector3 result);
[DllImport(fMathNativeImport.libName)]
public static extern void fvector3_mul(ref fVector3 lhs, ffloat rhs, out fVector3 result);
[DllImport(fMathNativeImport.libName)]
public static extern void fvector3_div(ref fVector3 lhs, ffloat rhs, out fVector3 result);
[DllImport(fMathNativeImport.libName)]
public static extern ffloat fvector3_dot(ref fVector3 lhs, ref fVector3 rhs);
[DllImport(fMathNativeImport.libName)]
public static extern void fvector3_cross(ref fVector3 lhs, ref fVector3 rhs, out fVector3 result);
#endif
#endregion
}