-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathpubg-tga-slice-debug.py
241 lines (155 loc) · 8.01 KB
/
pubg-tga-slice-debug.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
import argparse
import math
import numpy
import os
import sys
from PIL import Image
# parse arguments
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--umodel_export_path', help = 'umodel export path')
parser.add_argument('-o', '--output_path', help = 'working directory for extracting and stitching assets', default = '.')
parser.add_argument('-m', '--map', help = 'map identifier, either erangel, miramar, or savage', default = 'erangel')
# parser.add_argument('-l', '--lod', help = 'level-of-detail, either 0, 1, or 2', default = '0')
parser.add_argument('-c', '--compress', help = 'compression level, number between 0 and 10', default = '0')
args = parser.parse_args()
# pakFileNamesByMapNames = {
# 'erangel' : 'TslGame-WindowsNoEditor_erangel_heightmap.pak',
# 'miramar' : 'TslGame-WindowsNoEditor_desert_heightmap.pak' }
tslHeightmapPathsByMap = {
'erangel' : r'Maps\Erangel\Art\Heightmap',
'miramar' : r'Maps\Desert\Art\Heightmap',
'sanhok' : r'Maps\Savage\Art\Heightmap', }
mapIdentifier = args.map.lower()
if mapIdentifier not in {'erangel', 'miramar', 'sanhok' }:
sys.exit('unknown map identifier \'' + mapIdentifier + '\'')
smallMap = mapIdentifier == 'sanhok'
numTiles = 64 if smallMap else 256
assert os.path.isdir(args.umodel_export_path)
umodel_heightmap_path = os.path.abspath(os.path.join(args.umodel_export_path, tslHeightmapPathsByMap[mapIdentifier]))
assert os.path.isdir(umodel_heightmap_path)
output_path = args.output_path
if not os.path.isdir(output_path):
os.makedirs(output_path)
assert os.path.isdir(output_path)
normal_semantic = 'normal_rg8'
height_semantic = 'height_l16'
channel_semantic_8 = 'channel_r8'
channel_semantic_16 = 'channel_r16'
lod = 0 # int(args.lod)
compress = int(args.compress)
# slicing data
tile_width = { 0: 512, 1: 256, 2: 128 }[lod]
tile_height = { 0: 512, 1: 256, 2: 128 }[lod]
# tile_channels = 4
# tile_offset = int({ 0: 0, 1: 512 * 512 * 4 * (1.0), 2: 512 * 512 * 4 * (1.0 + 0.25)}[lod])
tile_scale = 8 if smallMap else 16;
tile_size = int(tile_width * tile_height)
# tile_size_with_mipmaps = int(tile_size * tile_channels * (1.00 + 0.25 + 0.0625))
def extract_tiles(asset_path, offsets, height_target, normal_target):
tile_indices = [[0, 1, 2, 3]] if smallMap else [[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]
num_indices = len(numpy.array(tile_indices).flatten())
offset_scale = 2 if smallMap else 4
sequence_index = 0
for i, indices in enumerate(tile_indices):
for tile_index in indices:
if smallMap:
tile_path = asset_path % (offsets[0], offsets[1], tile_index)
else:
tile_path = asset_path % (offsets[0], offsets[1], i, tile_index)
try:
tile = Image.open(tile_path)
except:
sequence_index += 1
continue
tile_r, tile_g, tile_b, tile_a = tile.split()
# extract normal
channel_r = numpy.asarray(tile_b).flatten()
channel_g = numpy.asarray(tile_a).flatten()
channel_b = numpy.resize(numpy.uint8(), tile_size)
channel_b.fill(255)
rgb = numpy.dstack((channel_r, channel_g, channel_b)).flatten()
# # extract elevation
channel_l = numpy.left_shift(numpy.asarray(tile_r, numpy.uint16()).flatten(), 8)
channel_l = channel_l + numpy.asarray(tile_g).flatten()
# refine stitching sequence
x = offsets[0] * offset_scale + int(i % 2) * 2 + int(tile_index % 2)
y = offsets[1] * offset_scale + int(i / 2) * 2 + int(tile_index / 2)
ordered_index = y * 16 + x
# paste data
target_x = (ordered_index % 16) * tile_width
target_y = math.floor(ordered_index / 16) * tile_height
# write normal tile
normal_tile = Image.frombytes('RGB', (tile_width, tile_height), rgb)
normal_target.paste(normal_tile, (target_x, target_y))
# write height tile
height_tile = Image.frombytes('I;16', (tile_width, tile_height), numpy.asarray(channel_l, order = 'C'))
height_target.paste(height_tile, (target_x, target_y))
# display progress
progress = (offsets[0] * 4 + offsets[1]) * num_indices + sequence_index + 1
print ('processing', str(progress).rjust(2 if smallMap else 3, '0'), 'of', numTiles,
flush = True, end = ('\r' if progress < numTiles else '\n'))
sequence_index += 1
# ubulk.close()
def extract_tiles_channel(asset_path, offsets, channel_target, tile_index, channel):
num_indices = 4 if smallMap else 16
offset_scale = 2 if smallMap else 4
sequence_index = 0
for i in ([0] if smallMap else [0, 1, 2, 3]):
if smallMap:
tile_path = asset_path % (offsets[0], offsets[1], tile_index)
else:
tile_path = asset_path % (offsets[0], offsets[1], i, tile_index)
try:
tile = Image.open(tile_path)
tile_r, tile_g, tile_b, tile_a = tile.split()
channel_l = numpy.asarray([tile_r, tile_g, tile_b, tile_a][channel]).flatten()
# channel_l = numpy.left_shift(numpy.asarray(tile_r, numpy.uint16()).flatten(), 8)
# channel_l = channel_l + numpy.asarray(tile_g).flatten()
except:
channel_l = numpy.random.rand(tile_size)
# refine stitching sequence
x = offsets[0] * offset_scale + int(i % 2) * 2
y = offsets[1] * offset_scale + int(i / 2) * 2
ordered_index = y * 16 + x
# paste data
target_x = (ordered_index % 16) * tile_width
target_y = math.floor(ordered_index / 16) * tile_height
# write normal tile
# normal_tile = Image.frombytes('RGB', (tile_width, tile_height), rgb)
# normal_target.paste(normal_tile, (target_x, target_y))
# write channel tile
# channel_tile = Image.frombytes('I;16', (tile_width, tile_height), numpy.asarray(channel_l, order = 'C'))
channel_tile = Image.frombytes('L', (tile_width, tile_height), channel_l)
channel_target.paste(channel_tile, (target_x, target_y))
# display progress
progress = (offsets[0] * 4 + offsets[1]) * num_indices + sequence_index + 1
print ('processing', str(progress).rjust(2 if smallMap else 3, '0'), 'of', numTiles,
flush = True, end = ('\r' if progress < numTiles else '\n'))
sequence_index += 1
# ubulk.close()
print ('extracting', numTiles, 'tiles (normal and height data) ...')
# normal_composite = Image.new("RGB", (tile_width * tile_scale, tile_height * tile_scale))
# height_composite = Image.new("I", (tile_width * tile_scale, tile_height * tile_scale))
channel_composite = Image.new("L", (tile_width * tile_scale, tile_height * tile_scale))
for p in range(64):
probe_index = int(p / 4)
probe_channel = p % 4
for indices in [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3), (3, 0), (3, 1), (3, 2), (3, 3)]:
# example '.\UmodelExport\Maps\Erangel\Art\Heightmap\Heightmap_x0_y0_00_sharedAssets\Texture2D_0.tga'
path = 'Heightmap_x%d_y%d_'
if not smallMap:
path = path + '0%d_'
path = path + 'sharedAssets\Texture2D_%d.tga'
asset_path = os.path.join(umodel_heightmap_path, path)
# extract_tiles(asset_path, (indices[0], indices[1]), height_composite, normal_composite)
extract_tiles_channel(asset_path, (indices[0], indices[1]), channel_composite, probe_index, probe_channel)
map_size_info = ['8k', '4k', '2k'][(lod + 1) if smallMap else lod]
# normal_stitched_path = os.path.join(output_path, 'pubg_' + mapIdentifier + '_' + normal_semantic + '_lod' + str(lod) + '.png')
# print (normal_stitched_path, 'saving', map_size_info, 'normal map ... hang in there')
# normal_composite.save(normal_stitched_path, 'PNG', compress_level = min(9, compress), optimize = compress == 10)
# height_stitched_path = os.path.join(output_path, 'pubg_' + mapIdentifier + '_' + height_semantic + '_lod' + str(lod) + '.png')
# print (height_stitched_path, 'saving', map_size_info, 'height map ... hang in there')
# height_composite.save(height_stitched_path, 'PNG', compress_level = min(9, compress), optimize = compress == 10)
channel_stitched_path = os.path.join(output_path, 'pubg_' + mapIdentifier + '_' + channel_semantic_8 + '_' + str(probe_index) + '_' + str(probe_channel) + '_lod' + str(lod) + '.png')
print (channel_stitched_path, 'saving', map_size_info, 'channel map ... hang in there')
channel_composite.save(channel_stitched_path, 'PNG', compress_level = min(9, compress), optimize = compress == 10)