forked from K0lb3/UnityPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector4.py
More file actions
155 lines (132 loc) · 4.06 KB
/
Vector4.py
File metadata and controls
155 lines (132 loc) · 4.06 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
class Vector4:
X: float
Y: float
Z: float
W: float
def __init__(self, *args):
if len(args) == 4: # float x, float y, float z, float w
self.X = args[0]
self.Y = args[1]
self.Z = args[2]
self.W = args[3]
elif len(args) == 2: # Vector3 value, float w
self.X = args[0].X
self.Y = args[0].Y
self.Z = args[0].Z
self.W = args[1]
"""
public float this[int index]
{
get
{
switch (index)
{
case 0: return X;
case 1: return Y;
case 2: return Z;
case 3: return W;
default: throw new ArgumentOutOfRangeException(nameof(index), "Invalid Vector4 index!");
}
}
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 ArgumentOutOfRangeException(nameof(index), "Invalid Vector4 index!");
}
}
}
public override int GetHashCode()
{
return X.GetHashCode() ^ (Y.GetHashCode() << 2) ^ (Z.GetHashCode() >> 2) ^ (W.GetHashCode() >> 1);
}
public override bool Equals(object other)
{
if (!(other is Vector4))
return false;
return Equals((Vector4)other);
}
public bool Equals(Vector4 other)
{
return X.Equals(other.X) && Y.Equals(other.Y) && Z.Equals(other.Z) && W.Equals(other.W);
}
public void Normalize()
{
var length = Length();
if (length > kEpsilon)
{
var invNorm = 1.0f / length;
X *= invNorm;
Y *= invNorm;
Z *= invNorm;
W *= invNorm;
}
else
{
X = 0;
Y = 0;
Z = 0;
W = 0;
}
}
public float Length()
{
return (float)Math.Sqrt(LengthSquared());
}
public float LengthSquared()
{
return X * X + Y * Y + Z * Z + W * W;
}
public static Vector4 Zero => new Vector4();
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);
}
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);
}
public static Vector4 operator -(Vector4 a)
{
return new Vector4(-a.X, -a.Y, -a.Z, -a.W);
}
public static Vector4 operator *(Vector4 a, float d)
{
return new Vector4(a.X * d, a.Y * d, a.Z * d, a.W * d);
}
public static Vector4 operator *(float d, Vector4 a)
{
return new Vector4(a.X * d, a.Y * d, a.Z * d, a.W * d);
}
public static Vector4 operator /(Vector4 a, float d)
{
return new Vector4(a.X / d, a.Y / d, a.Z / d, a.W / d);
}
public static bool operator ==(Vector4 lhs, Vector4 rhs)
{
return (lhs - rhs).LengthSquared() < kEpsilon * kEpsilon;
}
public static bool operator !=(Vector4 lhs, Vector4 rhs)
{
return !(lhs == rhs);
}
public static implicit operator Vector2(Vector4 v)
{
return new Vector2(v.X, v.Y);
}
public static implicit operator Vector3(Vector4 v)
{
return new Vector3(v.X, v.Y, v.Z);
}
public static implicit operator Color(Vector4 v)
{
return new Color(v.X, v.Y, v.Z, v.W);
}
private const float kEpsilon = 0.00001F;
}
}
"""