-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Vector4.cs
357 lines (314 loc) · 14.8 KB
/
Vector4.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Runtime.InteropServices;
using UnityEngine.Scripting;
using UnityEngine.Bindings;
using scm = System.ComponentModel;
using uei = UnityEngine.Internal;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace UnityEngine
{
[NativeHeader("Runtime/Math/Vector4.h")]
[NativeClass("Vector4f")]
[RequiredByNativeCode(Optional = true, GenerateProxy = true)]
[Unity.IL2CPP.CompilerServices.Il2CppEagerStaticClassConstruction]
// Representation of four-dimensional vectors.
public partial struct Vector4 : IEquatable<Vector4>, IFormattable
{
// *undocumented*
public const float kEpsilon = 0.00001F;
// X component of the vector.
public float x;
// Y component of the vector.
public float y;
// Z component of the vector.
public float z;
// W component of the vector.
public float w;
// Access the x, y, z, w components using [0], [1], [2], [3] respectively.
public float this[int index]
{
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
get
{
switch (index)
{
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
default:
throw new IndexOutOfRangeException("Invalid Vector4 index!");
}
}
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
set
{
switch (index)
{
case 0: x = value; break;
case 1: y = value; break;
case 2: z = value; break;
case 3: w = value; break;
default:
throw new IndexOutOfRangeException("Invalid Vector4 index!");
}
}
}
// Creates a new vector with given x, y, z, w components.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public Vector4(float x, float y, float z, float w) { this.x = x; this.y = y; this.z = z; this.w = w; }
// Creates a new vector with given x, y, z components and sets /w/ to zero.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public Vector4(float x, float y, float z) { this.x = x; this.y = y; this.z = z; this.w = 0F; }
// Creates a new vector with given x, y components and sets /z/ and /w/ to zero.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public Vector4(float x, float y) { this.x = x; this.y = y; this.z = 0F; this.w = 0F; }
// Set x, y, z and w components of an existing Vector4.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public void Set(float newX, float newY, float newZ, float newW) { x = newX; y = newY; z = newZ; w = newW; }
// Linearly interpolates between two vectors.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static Vector4 Lerp(Vector4 a, Vector4 b, float t)
{
t = Mathf.Clamp01(t);
return new Vector4(
a.x + (b.x - a.x) * t,
a.y + (b.y - a.y) * t,
a.z + (b.z - a.z) * t,
a.w + (b.w - a.w) * t
);
}
// Linearly interpolates between two vectors without clamping the interpolant
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static Vector4 LerpUnclamped(Vector4 a, Vector4 b, float t)
{
return new Vector4(
a.x + (b.x - a.x) * t,
a.y + (b.y - a.y) * t,
a.z + (b.z - a.z) * t,
a.w + (b.w - a.w) * t
);
}
// Moves a point /current/ towards /target/.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static Vector4 MoveTowards(Vector4 current, Vector4 target, float maxDistanceDelta)
{
float toVector_x = target.x - current.x;
float toVector_y = target.y - current.y;
float toVector_z = target.z - current.z;
float toVector_w = target.w - current.w;
float sqdist = (toVector_x * toVector_x +
toVector_y * toVector_y +
toVector_z * toVector_z +
toVector_w * toVector_w);
if (sqdist == 0 || (maxDistanceDelta >= 0 && sqdist <= maxDistanceDelta * maxDistanceDelta))
return target;
var dist = (float)Math.Sqrt(sqdist);
return new Vector4(current.x + toVector_x / dist * maxDistanceDelta,
current.y + toVector_y / dist * maxDistanceDelta,
current.z + toVector_z / dist * maxDistanceDelta,
current.w + toVector_w / dist * maxDistanceDelta);
}
// Multiplies two vectors component-wise.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static Vector4 Scale(Vector4 a, Vector4 b)
{
return new Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
}
// Multiplies every component of this vector by the same component of /scale/.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public void Scale(Vector4 scale)
{
x *= scale.x;
y *= scale.y;
z *= scale.z;
w *= scale.w;
}
// used to allow Vector4s to be used as keys in hash tables
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public override int GetHashCode()
{
return x.GetHashCode() ^ (y.GetHashCode() << 2) ^ (z.GetHashCode() >> 2) ^ (w.GetHashCode() >> 1);
}
// also required for being able to use Vector4s as keys in hash tables
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public override bool Equals(object other)
{
if (other is Vector4 v)
return Equals(v);
return false;
}
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public bool Equals(Vector4 other)
{
return x == other.x && y == other.y && z == other.z && w == other.w;
}
// *undoc* --- we have normalized property now
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static Vector4 Normalize(Vector4 a)
{
float mag = Magnitude(a);
if (mag > kEpsilon)
return a / mag;
else
return zero;
}
// Makes this vector have a ::ref::magnitude of 1.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public void Normalize()
{
float mag = Magnitude(this);
if (mag > kEpsilon)
this = this / mag;
else
this = zero;
}
// Returns this vector with a ::ref::magnitude of 1 (RO).
public Vector4 normalized
{
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
get
{
return Vector4.Normalize(this);
}
}
// Dot Product of two vectors.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static float Dot(Vector4 a, Vector4 b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; }
// Projects a vector onto another vector.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static Vector4 Project(Vector4 a, Vector4 b) { return b * (Dot(a, b) / Dot(b, b)); }
// Returns the distance between /a/ and /b/.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static float Distance(Vector4 a, Vector4 b) { return Magnitude(a - b); }
// *undoc* --- there's a property now
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static float Magnitude(Vector4 a) { return (float)Math.Sqrt(Dot(a, a)); }
// Returns the length of this vector (RO).
public float magnitude
{
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
get { return (float)Math.Sqrt(Dot(this, this)); }
}
// Returns the squared length of this vector (RO).
public float sqrMagnitude
{
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
get { return Dot(this, this); }
}
// Returns a vector that is made from the smallest components of two vectors.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static Vector4 Min(Vector4 lhs, Vector4 rhs)
{
return new Vector4(Mathf.Min(lhs.x, rhs.x), Mathf.Min(lhs.y, rhs.y), Mathf.Min(lhs.z, rhs.z), Mathf.Min(lhs.w, rhs.w));
}
// Returns a vector that is made from the largest components of two vectors.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static Vector4 Max(Vector4 lhs, Vector4 rhs)
{
return new Vector4(Mathf.Max(lhs.x, rhs.x), Mathf.Max(lhs.y, rhs.y), Mathf.Max(lhs.z, rhs.z), Mathf.Max(lhs.w, rhs.w));
}
static readonly Vector4 zeroVector = new Vector4(0F, 0F, 0F, 0F);
static readonly Vector4 oneVector = new Vector4(1F, 1F, 1F, 1F);
static readonly Vector4 positiveInfinityVector = new Vector4(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
static readonly Vector4 negativeInfinityVector = new Vector4(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity);
// Shorthand for writing @@Vector4(0,0,0,0)@@
public static Vector4 zero { [MethodImpl(MethodImplOptionsEx.AggressiveInlining)] get { return zeroVector; } }
// Shorthand for writing @@Vector4(1,1,1,1)@@
public static Vector4 one { [MethodImpl(MethodImplOptionsEx.AggressiveInlining)] get { return oneVector; } }
// Shorthand for writing @@Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity)@@
public static Vector4 positiveInfinity { [MethodImpl(MethodImplOptionsEx.AggressiveInlining)] get { return positiveInfinityVector; } }
// Shorthand for writing @@Vector3(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity)@@
public static Vector4 negativeInfinity { [MethodImpl(MethodImplOptionsEx.AggressiveInlining)] get { return negativeInfinityVector; } }
// Adds two vectors.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static Vector4 operator+(Vector4 a, Vector4 b) { return new Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); }
// Subtracts one vector from another.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static Vector4 operator-(Vector4 a, Vector4 b) { return new Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); }
// Negates a vector.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static Vector4 operator-(Vector4 a) { return new Vector4(-a.x, -a.y, -a.z, -a.w); }
// Multiplies a vector by a number.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static Vector4 operator*(Vector4 a, float d) { return new Vector4(a.x * d, a.y * d, a.z * d, a.w * d); }
// Multiplies a vector by a number.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static Vector4 operator*(float d, Vector4 a) { return new Vector4(a.x * d, a.y * d, a.z * d, a.w * d); }
// Divides a vector by a number.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static Vector4 operator/(Vector4 a, float d) { return new Vector4(a.x / d, a.y / d, a.z / d, a.w / d); }
// Returns true if the vectors are equal.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static bool operator==(Vector4 lhs, Vector4 rhs)
{
// Returns false in the presence of NaN values.
float diffx = lhs.x - rhs.x;
float diffy = lhs.y - rhs.y;
float diffz = lhs.z - rhs.z;
float diffw = lhs.w - rhs.w;
float sqrmag = diffx * diffx + diffy * diffy + diffz * diffz + diffw * diffw;
return sqrmag < kEpsilon * kEpsilon;
}
// Returns true if vectors are different.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static bool operator!=(Vector4 lhs, Vector4 rhs)
{
// Returns true in the presence of NaN values.
return !(lhs == rhs);
}
// Converts a [[Vector3]] to a Vector4.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static implicit operator Vector4(Vector3 v)
{
return new Vector4(v.x, v.y, v.z, 0.0F);
}
// Converts a Vector4 to a [[Vector3]].
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static implicit operator Vector3(Vector4 v)
{
return new Vector3(v.x, v.y, v.z);
}
// Converts a [[Vector2]] to a Vector4.
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static implicit operator Vector4(Vector2 v)
{
return new Vector4(v.x, v.y, 0.0F, 0.0F);
}
// Converts a Vector4 to a [[Vector2]].
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static implicit operator Vector2(Vector4 v)
{
return new Vector2(v.x, v.y);
}
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public override string ToString()
{
return ToString(null, null);
}
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public string ToString(string format)
{
return ToString(format, null);
}
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public string ToString(string format, IFormatProvider formatProvider)
{
if (string.IsNullOrEmpty(format))
format = "F2";
if (formatProvider == null)
formatProvider = CultureInfo.InvariantCulture.NumberFormat;
return UnityString.Format("({0}, {1}, {2}, {3})", x.ToString(format, formatProvider), y.ToString(format, formatProvider), z.ToString(format, formatProvider), w.ToString(format, formatProvider));
}
// *undoc* --- there's a property now
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public static float SqrMagnitude(Vector4 a) { return Vector4.Dot(a, a); }
// *undoc* --- there's a property now
[MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
public float SqrMagnitude() { return Dot(this, this); }
}
} // namespace