forked from K0lb3/UnityPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3.py
More file actions
159 lines (134 loc) · 2.97 KB
/
Vector3.py
File metadata and controls
159 lines (134 loc) · 2.97 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
157
158
159
class Vector3:
X: float
Y: float
Z: float
def __init__(self, *args):
if len(args) == 3 or len(args) == 1 and isinstance(args[0], (tuple, list)):
self.X, self.Y, self.Z = args
elif len(args) == 1:
# dirty patch for Vector4
self.__dict__ = args[0].__dict__
"""
using System;
using System.Runtime.InteropServices;
namespace AssetStudio
{
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct Vector3 : IEquatable<Vector3>
{
public float X;
public float Y;
public float Z;
public Vector3(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
public float this[int index]
{
get
{
switch (index)
{
case 0: return X;
case 1: return Y;
case 2: return Z;
default: throw new ArgumentOutOfRangeException(nameof(index), "Invalid Vector3 index!");
}
}
set
{
switch (index)
{
case 0: X = value; break;
case 1: Y = value; break;
case 2: Z = value; break;
default: throw new ArgumentOutOfRangeException(nameof(index), "Invalid Vector3 index!");
}
}
}
public override int GetHashCode()
{
return X.GetHashCode() ^ (Y.GetHashCode() << 2) ^ (Z.GetHashCode() >> 2);
}
public override bool Equals(object other)
{
if (!(other is Vector3))
return false;
return Equals((Vector3)other);
}
public bool Equals(Vector3 other)
{
return X.Equals(other.X) && Y.Equals(other.Y) && Z.Equals(other.Z);
}
public void Normalize()
{
var length = Length();
if (length > kEpsilon)
{
var invNorm = 1.0f / length;
X *= invNorm;
Y *= invNorm;
Z *= invNorm;
}
else
{
X = 0;
Y = 0;
Z = 0;
}
}
public float Length()
{
return (float)Math.Sqrt(LengthSquared());
}
public float LengthSquared()
{
return X * X + Y * Y + Z * Z;
}
public static Vector3 Zero => new Vector3();
public static Vector3 operator +(Vector3 a, Vector3 b)
{
return new Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
}
public static Vector3 operator -(Vector3 a, Vector3 b)
{
return new Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
}
public static Vector3 operator -(Vector3 a)
{
return new Vector3(-a.X, -a.Y, -a.Z);
}
public static Vector3 operator *(Vector3 a, float d)
{
return new Vector3(a.X * d, a.Y * d, a.Z * d);
}
public static Vector3 operator *(float d, Vector3 a)
{
return new Vector3(a.X * d, a.Y * d, a.Z * d);
}
public static Vector3 operator /(Vector3 a, float d)
{
return new Vector3(a.X / d, a.Y / d, a.Z / d);
}
public static bool operator ==(Vector3 lhs, Vector3 rhs)
{
return (lhs - rhs).LengthSquared() < kEpsilon * kEpsilon;
}
public static bool operator !=(Vector3 lhs, Vector3 rhs)
{
return !(lhs == rhs);
}
public static implicit operator Vector2(Vector3 v)
{
return new Vector2(v.X, v.Y);
}
public static implicit operator Vector4(Vector3 v)
{
return new Vector4(v.X, v.Y, v.Z, 0.0F);
}
private const float kEpsilon = 0.00001F;
}
}
"""