Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Deficuet authored and Deficuet committed Jan 18, 2024
1 parent 0640d71 commit 9d25a46
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
18 changes: 17 additions & 1 deletion src/main/kotlin/io/github/deficuet/unitykt/classes/Sprite.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ interface Texture2D: Texture {
val mTextureSettings: GLTextureSettings
val mStreamData: StreamingInfo?

fun getRawData(): ByteArray

fun getDecompressedData(): ByteArray?

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,27 @@ 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
)
}
} 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
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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) {
Expand Down

0 comments on commit 9d25a46

Please sign in to comment.