Skip to content

Commit 0c24f42

Browse files
committed
fix: Texture2DConverter - DXT swizzle
1 parent e144bcd commit 0c24f42

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

UnityPy/export/Texture2DConverter.py

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -215,39 +215,44 @@ def image_to_texture2d(
215215
pil_mode = "RGB"
216216
# everything else defaulted to RGBA
217217

218+
switch_swizzle = None
218219
if platform == BuildTarget.Switch and platform_blob:
219220
gobs_per_block = TextureSwizzler.get_switch_gobs_per_block(platform_blob)
220221

221-
s_tex_format = tex_format
222222
if tex_format == TextureFormat.RGB24:
223-
s_tex_format = TextureFormat.RGBA32
223+
tex_format = TextureFormat.RGBA32
224224
pil_mode = "RGBA"
225225
elif tex_format == TextureFormat.BGR24:
226-
s_tex_format = TextureFormat.BGRA32
226+
tex_format = TextureFormat.BGRA32
227227
pil_mode = "BGRA"
228228

229-
block_size = TextureSwizzler.TEXTUREFORMAT_BLOCK_SIZE_MAP[s_tex_format]
229+
block_size = TextureSwizzler.TEXTUREFORMAT_BLOCK_SIZE_MAP.get(tex_format)
230+
if not block_size:
231+
raise NotImplementedError(
232+
f"Not implemented swizzle format: {tex_format.name}"
233+
)
234+
230235
width, height = TextureSwizzler.get_padded_texture_size(
231236
img.width, img.height, *block_size, gobs_per_block
232237
)
233-
img = pad_image(img, width, height)
234-
img = Image.frombytes(
235-
pil_mode,
236-
img.size,
237-
TextureSwizzler.swizzle(
238-
img.tobytes("raw", pil_mode), width, height, *block_size, gobs_per_block
239-
),
240-
)
238+
switch_swizzle = (block_size, gobs_per_block)
239+
else:
240+
width, height = get_compressed_image_size(img.width, img.height, tex_format)
241241

242+
img = pad_image(img, width, height)
242243
if compress_func:
243-
width, height = get_compressed_image_size(img.width, img.height, tex_format)
244-
img = pad_image(img, width, height)
245244
enc_img = compress_func(
246-
img.tobytes("raw", "RGBA"), img.width, img.height, tex_format
245+
img.tobytes("raw", pil_mode), img.width, img.height, tex_format
247246
)
248247
else:
249248
enc_img = img.tobytes("raw", pil_mode)
250249

250+
if switch_swizzle is not None:
251+
block_size, gobs_per_block = switch_swizzle
252+
enc_img = bytes(
253+
TextureSwizzler.swizzle(enc_img, width, height, *block_size, gobs_per_block)
254+
)
255+
251256
return enc_img, tex_format
252257

253258

@@ -324,7 +329,12 @@ def parse_image_data(
324329
texture_format = texture_format
325330
pil_mode = "L"
326331

327-
block_size = TextureSwizzler.TEXTUREFORMAT_BLOCK_SIZE_MAP[texture_format]
332+
block_size = TextureSwizzler.TEXTUREFORMAT_BLOCK_SIZE_MAP.get(texture_format)
333+
if not block_size:
334+
raise NotImplementedError(
335+
f"Not implemented swizzle format: {texture_format.name}"
336+
)
337+
328338
width, height = TextureSwizzler.get_padded_texture_size(
329339
width, height, *block_size, gobs_per_block
330340
)
@@ -343,21 +353,20 @@ def parse_image_data(
343353
else:
344354
image_data = texture2ddecoder.unpack_crunch(image_data)
345355

346-
conv_func = CONV_TABLE.get(texture_format)
347-
if not conv_func:
348-
raise NotImplementedError(f"Not implemented texture format: {texture_format}")
349-
350-
img = conv_func(image_data, width, height)
351-
352356
if switch_swizzle is not None:
353357
block_size, gobs_per_block, pil_mode = switch_swizzle
354-
swizzle_data = bytes(
358+
image_data = bytes(
355359
TextureSwizzler.deswizzle(
356-
img.tobytes("raw", pil_mode), width, height, *block_size, gobs_per_block
360+
image_data, width, height, *block_size, gobs_per_block
357361
)
358362
)
359-
img.mode = pil_mode
360-
img = Image.frombytes(img.mode, (width, height), swizzle_data, "raw", pil_mode)
363+
364+
conv_func = CONV_TABLE.get(texture_format)
365+
if not conv_func:
366+
raise NotImplementedError(
367+
f"Not implemented texture format: {texture_format.name}"
368+
)
369+
img = conv_func(image_data, width, height)
361370

362371
if original_width != width or original_height != height:
363372
img = img.crop((0, 0, original_width, original_height))

0 commit comments

Comments
 (0)