Skip to content

Commit 40487fc

Browse files
committed
Texture2DConverter - extend compression with BC4, BC5, BC7 and LDR ASTC
1 parent 10fa641 commit 40487fc

2 files changed

Lines changed: 47 additions & 15 deletions

File tree

‎UnityPy/export/Texture2DConverter.py‎

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,66 @@ def image_to_texture2d(
3131

3232
# DXT
3333
if target_texture_format in [TF.DXT1, TF.DXT1Crunched]:
34-
raw_img = img.convert("RGBA").tobytes()
34+
raw_img = img.tobytes("raw", "RGBA")
3535
enc_img = etcpak.compress_bc1(raw_img, img.width, img.height)
3636
tex_format = TF.DXT1
3737
elif target_texture_format in [TF.DXT5, TF.DXT5Crunched]:
38-
raw_img = img.convert("RGBA").tobytes()
39-
enc_img = etcpak.compress_to_dxt5(raw_img, img.width, img.height)
38+
raw_img = img.tobytes("raw", "RGBA")
39+
enc_img = etcpak.compress_bc3(raw_img, img.width, img.height)
4040
tex_format = TF.DXT5
41+
elif target_texture_format in [TF.BC4]:
42+
raw_img = img.tobytes("raw", "RGBA")
43+
enc_img = etcpak.compress_bc4(raw_img, img.width, img.height)
44+
tex_format = TF.BC4
45+
elif target_texture_format in [TF.BC5]:
46+
raw_img = img.tobytes("raw", "RGBA")
47+
enc_img = etcpak.compress_bc5(raw_img, img.width, img.height)
48+
tex_format = TF.BC5
49+
elif target_texture_format in [TF.BC7]:
50+
raw_img = img.tobytes("raw", "RGBA")
51+
enc_img = etcpak.compress_bc7(raw_img, img.width, img.height)
52+
tex_format = TF.BC7
4153
# ETC
4254
elif target_texture_format in [TF.ETC_RGB4, TF.ETC_RGB4Crunched, TF.ETC_RGB4_3DS]:
43-
img = assert_rgba(img, target_texture_format)
44-
r, g, b, a = img.split()
45-
raw_img = Image.merge("RGBA", (b, g, r, a)).tobytes()
46-
enc_img = etcpak.compress_to_etc1(raw_img, img.width, img.height)
55+
raw_img = img.tobytes("raw", "RGBA")
56+
enc_img = etcpak.compress_etc1_rgb(raw_img, img.width, img.height)
4757
tex_format = TF.ETC_RGB4
4858
elif target_texture_format == TF.ETC2_RGB:
49-
img = assert_rgba(img, target_texture_format)
50-
r, g, b, a = img.split()
51-
raw_img = Image.merge("RGBA", (b, g, r, a)).tobytes()
52-
enc_img = etcpak.compress_to_etc2_rgb(raw_img, img.width, img.height)
59+
raw_img = img.tobytes("raw", "RGBA")
60+
enc_img = etcpak.compress_etc2_rgb(raw_img, img.width, img.height)
5361
tex_format = TF.ETC2_RGB
5462
elif (
5563
target_texture_format in [TF.ETC2_RGBA8, TF.ETC2_RGBA8Crunched, TF.ETC2_RGBA1]
5664
or "_RGB_" in target_texture_format.name
5765
):
58-
img = assert_rgba(img, target_texture_format)
59-
r, g, b, a = img.split()
60-
raw_img = Image.merge("RGBA", (b, g, r, a)).tobytes()
61-
enc_img = etcpak.compress_to_etc2_rgba(raw_img, img.width, img.height)
66+
raw_img = img.tobytes("raw", "RGBA")
67+
enc_img = etcpak.compress_etc2_rgba(raw_img, img.width, img.height)
6268
tex_format = TF.ETC2_RGBA8
69+
# ASTC
70+
elif target_texture_format.name.startswith("ASTC"):
71+
import astc_encoder
72+
73+
raw_img = img.tobytes("raw", "RGBA")
74+
75+
block_size = tuple(
76+
map(int, target_texture_format.name.rsplit("_", 1)[1].split("x"))
77+
)
78+
79+
config = astc_encoder.ASTCConfig(
80+
astc_encoder.ASTCProfile.LDR, *block_size, 1, 100
81+
)
82+
context = astc_encoder.ASTCContext(config)
83+
raw_img = astc_encoder.ASTCImage(
84+
astc_encoder.ASTCType.U8, img.width, img.height, 1, raw_img
85+
)
86+
if img.mode == "RGB":
87+
tex_format = getattr(TF, f"ASTC_RGB_{block_size[0]}x{block_size[1]}")
88+
else:
89+
tex_format = getattr(TF, f"ASTC_RGBA_{block_size[0]}x{block_size[1]}")
90+
91+
swizzle = astc_encoder.ASTCSwizzle.from_str("RGBA")
92+
enc_img = context.compress(raw_img, swizzle)
93+
tex_format = target_texture_format
6394
# A
6495
elif target_texture_format == TF.Alpha8:
6596
enc_img = img.tobytes("raw", "A")

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ dependencies = [
4444
"Pillow",
4545
"texture2ddecoder", # texture decompression
4646
"etcpak", # ETC & DXT compression
47+
"astc-encoder-py", # ASTC compression
4748
# raw typetree dumping
4849
"tabulate",
4950
# audio extraction

0 commit comments

Comments
 (0)