This repository has been archived by the owner on Apr 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathGLOBALS.py
255 lines (199 loc) · 7.09 KB
/
GLOBALS.py
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
import math
import random
import struct
try:
from .ByteIO import ByteIO
except Exception:
from ByteIO import ByteIO
class SourceVector:
def __init__(self, init_vec=None):
if init_vec:
self.x, self.y, self.z = init_vec
else:
self.x = 0.0
self.y = 0.0
self.z = 0.0
def read(self, reader: ByteIO):
self.x, self.y, self.z = reader.read_fmt('fff')
return self
def __eq__(self, other):
return self.x == other.x and self.y == other.y and self.z == other.z
def __add__(self, other):
return SourceVector([self.x + other.x, self.y + other.y, self.z + other.z])
def __sub__(self, other):
return SourceVector([self.x - other.x, self.y - other.y, self.z - other.z])
def to_degrees(self):
return SourceVector(init_vec=[math.degrees(self.x), math.degrees(self.y), math.degrees(self.z)])
@property
def as_list(self):
return [self.x, self.y, self.z]
@property
def as_string_smd(self):
return "{:.6f} {:.6f} {:.6f}".format(self.x, self.y, self.z)
def as_rounded(self, n):
return "{} {} {}".format(round(self.x, n), round(self.y, n), round(self.z, n))
@property
def as_string(self):
return " X:{} Y:{} Z:{}".format(self.x, self.y, self.z)
def magnitude(self):
magn = math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)
return magn
def normalize(self):
magn = self.magnitude()
if magn == 0:
return self
self.x = self.x / magn
self.y = self.y / magn
self.z = self.z / magn
return self
@property
def as_normalized(self):
magn = self.magnitude()
if magn == 0:
return self
x = self.x / magn
y = self.y / magn
z = self.z / magn
return SourceVector(init_vec=[x, y, z])
def __str__(self):
return "<Vector 3D X:{} Y:{} Z:{}".format(self.x, self.y, self.z)
def __repr__(self):
return "<Vector 3D X:{} Y:{} Z:{}".format(self.x, self.y, self.z)
class SourceQuaternion:
def __init__(self):
self.x = 0.0
self.y = 0.0
self.z = 0.0
self.w = 0.0
def read(self, reader: ByteIO):
self.x, self.y, self.z, self.w = reader.read_fmt('ffff')
return self
def __str__(self):
return "<Quaternion X:{} Y:{} Z:{} W:{}".format(self.x, self.y, self.z, self.w)
def __repr__(self):
return "<Quaternion X:{} Y:{} Z:{} W:{}".format(self.x, self.y, self.z, self.w)
class SourceFloat16bits:
float32bias = 127
float16bias = 15
max_float_16bits = 65504.0
half_denorm = (1.0 / 16384.0)
def __init__(self):
self.value_16bit = 0
def read(self, reader: ByteIO):
self.value_16bit = reader.read_uint16()
return self
@property
def float_value(self):
sign = self.get_sign(self.value_16bit)
if sign == 1:
float_sign = -1
else:
float_sign = 0
if self.is_infinity(self.value_16bit):
return self.max_float_16bits * float_sign
if self.is_nan(self.value_16bit):
return 0
mantissa = self.get_mantissa(self.value_16bit)
biased_exponent = self.get_biased_exponent(self.value_16bit)
if biased_exponent == 0 and mantissa != 0:
float_mantissa = mantissa / 1024.0
result = float_sign * float_mantissa * self.half_denorm
else:
result = self.get_single()
return result
@staticmethod
def get_mantissa(value):
return value & 0x3FF
@staticmethod
def get_biased_exponent(value):
return (value & 0x7C00) >> 10
@staticmethod
def get_sign(value):
return (value & 0x8000) >> 15
def get_single(self):
bits_result = IntegerAndSingleUnion()
bits_result.i = 0
mantissa = self.get_mantissa(self.value_16bit)
biased_exponent = self.get_biased_exponent(self.value_16bit)
sign = self.get_sign(self.value_16bit)
result_mantissa = mantissa << (23 - 10)
if biased_exponent == 0:
result_biased_exponent = 0
else:
result_biased_exponent = (biased_exponent - self.float16bias + self.float32bias) << 23
result_sign = sign << 31
bits_result.i = result_sign | result_biased_exponent | result_mantissa
return bits_result.s
def is_infinity(self, value):
mantissa = self.get_mantissa(value)
biased_exponent = self.get_biased_exponent(value)
return (biased_exponent == 31) and (mantissa == 0)
def is_nan(self, value):
mantissa = self.get_mantissa(value)
biased_exponent = self.get_biased_exponent(value)
return (biased_exponent == 31) and (mantissa != 0)
def __str__(self):
return self.float_value
def __repr__(self):
return self.float_value
class IntegerAndSingleUnion:
def __init__(self):
self.i = 0
@property
def s(self):
a = struct.pack('!I', self.i)
return struct.unpack('!f', a)[0]
class SourceVertex:
def __init__(self):
self.boneWeight = SourceBoneWeight()
self.position = SourceVector()
self.normal = SourceVector()
self.texCoordX = 0
self.texCoordY = 0
def read(self, reader: ByteIO):
self.boneWeight.read(reader)
self.position.read(reader)
self.normal.read(reader)
self.texCoordX = reader.read_float()
self.texCoordY = reader.read_float()
return self
def __str__(self):
return "<Vertex pos:{}>".format(self.position.as_string)
def __repr__(self):
return self.__str__()
class SourceMdlTexture:
def __init__(self):
self.nameOffset = 0
self.flags = 0
self.used = 0
self.unused1 = 0
self.materialP = 0
self.clientMaterialP = 0
self.unused = [] # len 10
self.thePathFileName = 'texture' + str(random.randint(0, 256))
def read(self, reader: ByteIO):
entry = reader.tell()
self.nameOffset = reader.read_uint32()
self.thePathFileName = reader.read_from_offset(entry + self.nameOffset, reader.read_ascii_string)
self.flags = reader.read_uint32()
self.used = reader.read_uint32()
self.unused1 = reader.read_uint32()
self.materialP = reader.read_uint32()
self.clientMaterialP = reader.read_uint32()
self.unused = [reader.read_uint32() for _ in range(5)]
def __repr__(self):
return "<MDL texture name:{}>".format(self.thePathFileName)
class SourceBoneWeight:
def __init__(self):
self.weight = []
self.bone = []
self.boneCount = b"\x00"
def read(self, reader: ByteIO):
self.weight = [reader.read_float() for _ in range(3)]
self.bone = [reader.read_uint8() for _ in range(3)]
self.boneCount = reader.read_uint8()
return self
def __str__(self):
return '<BoneWeight Weight: {} Bone: {} BoneCount: {}>'.format(self.weight, self.bone, self.boneCount)
def __repr__(self):
return self.__str__()