forked from K0lb3/UnityPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshHelper.py
More file actions
154 lines (138 loc) · 5.49 KB
/
Copy pathMeshHelper.py
File metadata and controls
154 lines (138 loc) · 5.49 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
from __future__ import annotations
import struct
from enum import IntEnum
from typing import List, Tuple, ByteString
def get_format_size(format: int) -> int:
if format in [
VertexFormat.kVertexFormatFloat,
VertexFormat.kVertexFormatUInt32,
VertexFormat.kVertexFormatSInt32,
]:
return 4
elif format in [
VertexFormat.kVertexFormatFloat16,
VertexFormat.kVertexFormatUNorm16,
VertexFormat.kVertexFormatSNorm16,
VertexFormat.kVertexFormatUInt16,
VertexFormat.kVertexFormatSInt16,
]:
return 2
elif format in [
VertexFormat.kVertexFormatUNorm8,
VertexFormat.kVertexFormatSNorm8,
VertexFormat.kVertexFormatUInt8,
VertexFormat.kVertexFormatSInt8,
]:
return 1
raise ValueError(format)
def is_int_format(version: Tuple[int, int, int, int], format: int) -> bool:
if version[0] < 2017:
return format == 4
elif version[0] < 2019:
return format >= 7
else:
return format >= 6
def parse_float_array(
data: ByteString, component_size: int, format: VertexFormat
) -> List[float]:
if format == VertexFormat.kVertexFormatFloat:
return struct.unpack(f">{'f'*(len(data)//4)}", data)
elif format == VertexFormat.kVertexFormatFloat16:
return struct.unpack(f">{'e'*(len(data)//2)}", data)
elif format == VertexFormat.kVertexFormatUNorm8:
return [byte / 255.0 for byte in data]
elif format == VertexFormat.kVertexFormatSNorm8:
return [max(((byte - 128) / 127.0), -1.0) for byte in data]
elif format == VertexFormat.kVertexFormatUNorm16:
return [x / 65535.0 for x in struct.unpack(f">{'H'*(len(data)//2)}", data)]
elif format == VertexFormat.kVertexFormatSNorm16:
return [
max(((x - 32768) / 32767.0), -1.0)
for x in struct.unpack(f">{'h'*(len(data)//2)}", data)
]
def parse_int_array(data: ByteString, component_size: int):
if component_size == 1:
return [x for x in data]
elif component_size == 2:
return [x for x in struct.unpack(f">{'h'*(len(data)//2)}", data)]
elif component_size == 4:
return [x for x in struct.unpack(f">{'i'*(len(data)//4)}", data)]
def convert_vertex_format(format: int, version: List[int]) -> VertexFormat:
if version[0] < 2017:
if format == VertexChannelFormat.kChannelFormatFloat:
return VertexFormat.kVertexFormatFloat
elif format == VertexChannelFormat.kChannelFormatFloat16:
return VertexFormat.kVertexFormatFloat16
elif format == VertexChannelFormat.kChannelFormatColor: # in 4.x is size 4
return VertexFormat.kVertexFormatUNorm8
elif format == VertexChannelFormat.kChannelFormatByte:
return VertexFormat.kVertexFormatUInt8
elif format == VertexChannelFormat.kChannelFormatUInt32: # in 5.x
return VertexFormat.kVertexFormatUInt32
else:
raise ValueError(f"Failed to convert {format} to VertexFormat")
elif version[0] < 2019:
if format == VertexFormat2017.kVertexFormatFloat:
return VertexFormat.kVertexFormatFloat
elif format == VertexFormat2017.kVertexFormatFloat16:
return VertexFormat.kVertexFormatFloat16
elif (
format == VertexFormat2017.kVertexFormatColor
or format == VertexFormat2017.kVertexFormatUNorm8
):
return VertexFormat.kVertexFormatUNorm8
elif format == VertexFormat2017.kVertexFormatSNorm8:
return VertexFormat.kVertexFormatSNorm8
elif format == VertexFormat2017.kVertexFormatUNorm16:
return VertexFormat.kVertexFormatUNorm16
elif format == VertexFormat2017.kVertexFormatSNorm16:
return VertexFormat.kVertexFormatSNorm16
elif format == VertexFormat2017.kVertexFormatUInt8:
return VertexFormat.kVertexFormatUInt8
elif format == VertexFormat2017.kVertexFormatSInt8:
return VertexFormat.kVertexFormatSInt8
elif format == VertexFormat2017.kVertexFormatUInt16:
return VertexFormat.kVertexFormatUInt16
elif format == VertexFormat2017.kVertexFormatSInt16:
return VertexFormat.kVertexFormatSInt16
elif format == VertexFormat2017.kVertexFormatUInt32:
return VertexFormat.kVertexFormatUInt32
elif format == VertexFormat2017.kVertexFormatSInt32:
return VertexFormat.kVertexFormatSInt32
else:
raise ValueError(f"Failed to convert {format} to VertexFormat")
else:
return VertexFormat(format)
class VertexChannelFormat(IntEnum):
kChannelFormatFloat = 0
kChannelFormatFloat16 = 1
kChannelFormatColor = 2
kChannelFormatByte = 3
kChannelFormatUInt32 = 4
class VertexFormat2017(IntEnum):
kVertexFormatFloat = 0
kVertexFormatFloat16 = 1
kVertexFormatColor = 2
kVertexFormatUNorm8 = 3
kVertexFormatSNorm8 = 4
kVertexFormatUNorm16 = 5
kVertexFormatSNorm16 = 6
kVertexFormatUInt8 = 7
kVertexFormatSInt8 = 8
kVertexFormatUInt16 = 9
kVertexFormatSInt16 = 10
kVertexFormatUInt32 = 11
kVertexFormatSInt32 = 12
class VertexFormat(IntEnum):
kVertexFormatFloat = 0
kVertexFormatFloat16 = 1
kVertexFormatUNorm8 = 2
kVertexFormatSNorm8 = 3
kVertexFormatUNorm16 = 4
kVertexFormatSNorm16 = 5
kVertexFormatUInt8 = 6
kVertexFormatSInt8 = 7
kVertexFormatUInt16 = 8
kVertexFormatSInt16 = 9
kVertexFormatUInt32 = 10
kVertexFormatSInt32 = 11