Skip to content

Commit d38b5f6

Browse files
committed
Texture2DConverter - use astc_encoder for astc decoding
1 parent 3071c1b commit d38b5f6

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

UnityPy/export/Texture2DConverter.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import struct
44
from copy import copy
55
from io import BytesIO
6-
from typing import TYPE_CHECKING, Tuple, Union
6+
from typing import TYPE_CHECKING, Dict, Tuple, Union
77

8+
import astc_encoder
89
import texture2ddecoder
910
from PIL import Image
1011

@@ -68,8 +69,6 @@ def image_to_texture2d(
6869
tex_format = TF.ETC2_RGBA8
6970
# ASTC
7071
elif target_texture_format.name.startswith("ASTC"):
71-
import astc_encoder
72-
7372
raw_img = img.tobytes("raw", "RGBA")
7473

7574
block_size = tuple(
@@ -253,9 +252,25 @@ def atc(image_data: bytes, width: int, height: int, alpha: bool) -> Image.Image:
253252
return Image.frombytes("RGBA", (width, height), image_data, "raw", "BGRA")
254253

255254

255+
ASTC_CONTEXTS: Dict[Tuple[int, int], astc_encoder.ASTCContext] = {}
256+
257+
256258
def astc(image_data: bytes, width: int, height: int, block_size: tuple) -> Image.Image:
257-
image_data = texture2ddecoder.decode_astc(image_data, width, height, *block_size)
258-
return Image.frombytes("RGBA", (width, height), image_data, "raw", "BGRA")
259+
context = ASTC_CONTEXTS.get(block_size)
260+
if context is None:
261+
config = astc_encoder.ASTCConfig(
262+
astc_encoder.ASTCProfile.LDR,
263+
*block_size,
264+
1,
265+
100,
266+
astc_encoder.ASTCConfigFlags.USE_DECODE_UNORM8,
267+
)
268+
context = ASTC_CONTEXTS[block_size] = astc_encoder.ASTCContext(config)
269+
270+
image = astc_encoder.ASTCImage(astc_encoder.ASTCType.U8, width, height, 1)
271+
context.decompress(image_data, image, astc_encoder.ASTCSwizzle.from_str("RGBA"))
272+
273+
return Image.frombytes("RGBA", (width, height), image.data, "raw", "RGBA")
259274

260275

261276
def pvrtc(image_data: bytes, width: int, height: int, fmt: bool) -> Image.Image:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ dependencies = [
4444
"Pillow",
4545
"texture2ddecoder", # texture decompression
4646
"etcpak", # ETC & DXT compression
47-
"astc-encoder-py", # ASTC compression
47+
"astc-encoder-py >= 0.1.8", # ASTC compression
4848
# raw typetree dumping
4949
"tabulate",
5050
# audio extraction

0 commit comments

Comments
 (0)