Skip to content

Commit a090a09

Browse files
authored
1.10.11 - add texture2darray support (K0lb3#225)
* 1.10.11 - add texture2darray support resolves K0lb3#224 * fix get_image_from_texture2d * return images for Texture2DArray instead of image * Update README.md * fix get_image_from_texture2d
1 parent 006883b commit a090a09

File tree

7 files changed

+499
-101
lines changed

7 files changed

+499
-101
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,29 @@ if mesh_renderer.m_GameObject:
368368
mesh_renderer.export(export_dir)
369369
```
370370

371+
### [Texture2DArray](UnityPy/classes/Texture2DArray.py)
372+
373+
WARNING - not well tested
374+
375+
- `.name`
376+
- `.image` converts the texture2darray into a `PIL.Image`
377+
- `.m_Width` - texture width (int)
378+
- `.m_Height` - texture height (int)
379+
380+
**Export**
381+
382+
```python
383+
import os
384+
from PIL import Image
385+
for obj in env.objects:
386+
if obj.type.name == "Texture2DArray":
387+
# export texture
388+
data = obj.read()
389+
for i, image in enumerate(data.images):
390+
image.save(os.path.join(path, f"{data.m_Name}_{i}.png"))
391+
# editing isn't supported yet!
392+
```
393+
371394
## Custom-Filesystem
372395

373396
UnityPy uses [fsspec](https://github.com/fsspec/filesystem_spec) under the hood to manage all filesystem interactions.

UnityPy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "1.10.10"
1+
__version__ = "1.10.11"
22

33
from .environment import Environment
44
from .helpers.ArchiveStorageManager import set_assetbundle_decrypt_key

UnityPy/classes/Texture2DArray.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from typing import Optional, List
2+
3+
from .Texture import Texture
4+
from .Texture2D import StreamingInfo
5+
from ..enums.GraphicsFormat import GraphicsFormat, GRAPHICS_TO_TEXTURE_MAP
6+
from ..export import Texture2DConverter
7+
from ..helpers.ResourceReader import get_resource_data
8+
from PIL import Image
9+
10+
11+
class GLTextureSettings:
12+
m_Aniso: int
13+
m_FilterMode: int
14+
m_MipBias: float
15+
m_WrapMode: Optional[int] = None
16+
m_WrapU: Optional[int] = None
17+
m_WrapV: Optional[int] = None
18+
m_WrapW: Optional[int] = None
19+
20+
def __init__(self, reader):
21+
self.__dict__.update(**self.read_typetree(wrap=True).__dict__)
22+
23+
24+
class Texture2DArray(Texture):
25+
image_data: bytes
26+
m_ColorSpace: int
27+
m_DataSize: int
28+
m_Depth: int
29+
m_Format: GraphicsFormat
30+
m_Height: int
31+
m_IsReadable: bool
32+
m_MipCount: int
33+
m_Name: str
34+
m_TextureSettings: GLTextureSettings
35+
m_Width: int
36+
m_DownscaleFallback: Optional[bool] = None
37+
m_ForcedFallbackFormat: Optional[int] = None
38+
m_IgnoreMipmapLimit: Optional[bool] = None
39+
m_IsAlphaChannelOptional: Optional[bool] = None
40+
m_MipmapLimitGroupName: Optional[str] = None
41+
m_MipsStripped: Optional[int] = None
42+
m_StreamData: Optional[StreamingInfo] = None
43+
m_UsageMode: Optional[int] = None
44+
45+
def __init__(self, reader):
46+
super().__init__(reader=reader)
47+
self.__dict__.update(**reader.read_typetree(wrap=True).__dict__)
48+
self.m_TextureSettings.__class__ = GLTextureSettings
49+
self.m_Format = GraphicsFormat(self.m_Format)
50+
51+
@property
52+
def image_data(self):
53+
data = getattr(self, "image data", None)
54+
if data is None:
55+
data = get_resource_data(
56+
self.m_StreamData.path,
57+
self.assets_file,
58+
self.m_StreamData.offset,
59+
self.m_StreamData.size,
60+
)
61+
return data
62+
63+
@property
64+
def images(self) -> List[Image.Image]:
65+
texture_format = GRAPHICS_TO_TEXTURE_MAP.get(self.m_Format)
66+
if not texture_format:
67+
raise NotImplementedError(
68+
f"GraphicsFormat {self.m_Format} not supported yet"
69+
)
70+
71+
# calculate the number of textures in the array
72+
texture_size = self.m_DataSize // self.m_Depth
73+
return [
74+
Texture2DConverter.parse_image_data(
75+
self.image_data[offset : offset + texture_size],
76+
self.m_Width,
77+
self.m_Height,
78+
texture_format,
79+
self.version,
80+
0,
81+
None,
82+
)
83+
for offset in range(0, self.m_DataSize, texture_size)
84+
]

UnityPy/classes/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
from .TextAsset import TextAsset
3535
from .Texture import Texture
3636
from .Texture2D import Texture2D
37+
from .Texture2DArray import Texture2DArray
3738
from .Transform import Transform
3839
from .VideoClip import VideoClip

0 commit comments

Comments
 (0)