|
| 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 | + ] |
0 commit comments