From 9d25a463c56ddbf5e874f6a3ba22b397c9dcbe4e Mon Sep 17 00:00:00 2001 From: Deficuet Date: Wed, 17 Jan 2024 21:38:22 -0700 Subject: [PATCH] update --- README.md | 2 ++ .../github/deficuet/unitykt/classes/Sprite.kt | 18 +++++++++++++++++- .../deficuet/unitykt/classes/Texture2D.kt | 2 ++ .../unitykt/internal/impl/SpriteImpl.kt | 9 ++++++--- .../unitykt/internal/impl/Texture2DImpl.kt | 6 +++++- 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index adeec9a8..1d30178d 100644 --- a/README.md +++ b/README.md @@ -54,11 +54,13 @@ So far the objects that can export data includes: - `exportNormals` - The data of lines starts with "vn" in the .obj file, grouped by Vector3. - `exportFaces` - The data of lines starts with "f" in the .obj file, grouped by Vector3. - Texture2D + - `getRawData` - Returns compressed texture data in `ByteArray`. - `getDecompressedData` - Image data as `ByteArray` after decoding. Can be used to create image directly. The color channels are `BGRA`. The size of the array is `mWidth * mHeight * 4`. - `getImage` - A BufferedImage created from the decompressed data. **It is usually up-side-down**. - If the format of the texture is unsupported, both functions will return `null`. - Sprite - `getImage` - An BufferedImage cropped from a `Texture2D` image. Will return `null` if the `Texture2D` object is not found or the format is unsupported. + - Two cropping strategies `SpriteCropStrategy.USE_TEXTURE_RECT` and `USE_RECT` are provided to determine which one of the `SpriteRenderData.textureRect` and `Sprite.mRect` will be used to crop image. - The packing mode `SpritePackingMode.Tight` is not supported yet. - TextAsset - `text(charset)` - This function is used to export content in this object as `String`. A `Charset` can be passed as a parameter, by default it is `Charsets.UTF_8`. diff --git a/src/main/kotlin/io/github/deficuet/unitykt/classes/Sprite.kt b/src/main/kotlin/io/github/deficuet/unitykt/classes/Sprite.kt index b8377604..a735efe2 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/classes/Sprite.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/classes/Sprite.kt @@ -28,7 +28,23 @@ interface Sprite: NamedObject { * * i.e. usually up-side-down. */ - fun getImage(): BufferedImage? + fun getImage(strategy: SpriteCropStrategy = SpriteCropStrategy.USE_TEXTURE_RECT): BufferedImage? +} + +/** + * Has effect only when cropping the image using [mRD][Sprite.mRD]. + * + * i.e. when the `PPtr` [mSpriteAtlas][Sprite.mSpriteAtlas] gets `null`. + */ +enum class SpriteCropStrategy { + /** + * Use [textureRect][SpriteRenderData.textureRect] + */ + USE_TEXTURE_RECT, + /** + * Use [mRect][Sprite.mRect] + */ + USE_RECT } interface SecondarySpriteTexture { diff --git a/src/main/kotlin/io/github/deficuet/unitykt/classes/Texture2D.kt b/src/main/kotlin/io/github/deficuet/unitykt/classes/Texture2D.kt index 680085a6..324616db 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/classes/Texture2D.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/classes/Texture2D.kt @@ -13,6 +13,8 @@ interface Texture2D: Texture { val mTextureSettings: GLTextureSettings val mStreamData: StreamingInfo? + fun getRawData(): ByteArray + fun getDecompressedData(): ByteArray? /** diff --git a/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/SpriteImpl.kt b/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/SpriteImpl.kt index 89f6addf..589cef40 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/SpriteImpl.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/SpriteImpl.kt @@ -120,13 +120,12 @@ internal class SpriteImpl( } } - override fun getImage(): BufferedImage? { + override fun getImage(strategy: SpriteCropStrategy): BufferedImage? { val spriteAtlas = mSpriteAtlas?.safeGetObj() if (spriteAtlas != null) { val spriteAtlasData = spriteAtlas.mRenderDataMap[mRenderDataKey] val tex = spriteAtlasData?.texture?.safeGetObj() if (tex != null) { - println(tex.mPathID) return cutImage( tex, spriteAtlasData.textureRect, //spriteAtlasData.textureRectOffset, spriteAtlasData.downScaleMultiplier, spriteAtlasData.settingsRaw @@ -134,10 +133,14 @@ internal class SpriteImpl( } } else { val tex = mRD.texture.safeGetObj() + val rect = when (strategy) { + SpriteCropStrategy.USE_TEXTURE_RECT -> mRD.textureRect + SpriteCropStrategy.USE_RECT -> mRect + } if (tex != null) { return with(mRD) { cutImage( - tex, textureRect, //textureRectOffset, + tex, rect, //textureRectOffset, downScaleMultiplier, settingsRaw ) } diff --git a/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/Texture2DImpl.kt b/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/Texture2DImpl.kt index c5e28a57..350f0499 100644 --- a/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/Texture2DImpl.kt +++ b/src/main/kotlin/io/github/deficuet/unitykt/internal/impl/Texture2DImpl.kt @@ -90,6 +90,10 @@ internal class Texture2DImpl( }.registerToManager(assetFile.root.manager) } + override fun getRawData(): ByteArray { + return imageData.read() + } + override fun getDecompressedData(): ByteArray? { return decompressTexture(imageData.read()) } @@ -113,8 +117,8 @@ internal class Texture2DImpl( } private fun decompressTexture(data: ByteArray): ByteArray? { - val areaIndices = 0 until mWidth * mHeight val dataSize = mWidth * mHeight * 4 + val areaIndices = 0 until mWidth * mHeight val dataSizeIndices = 0 until dataSize step 4 val out: ByteArray? when (mTextureFormat) {