Skip to content

Commit

Permalink
feat: support more texture formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsuk1ko committed Aug 8, 2023
1 parent ad657a3 commit 709bd2b
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 52 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
"postversion": "tpv"
},
"dependencies": {
"@arkntools/unity-js-tools": "^1.0.1",
"@arkntools/unity-js-tools": "^1.1.0",
"buffer-reader": "^0.1.0",
"jimp": "^0.22.10",
"jszip": "^3.10.1",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"nested-error-stacks": "^2.1.1"
},
"devDependencies": {
"@tsuk1ko/postversion": "^1.0.2",
"@types/buffer-reader": "^0.1.0",
"@types/lodash": "^4.14.196",
"@types/nested-error-stacks": "^2.1.0",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"eslint": "^8.46.0",
Expand Down
20 changes: 9 additions & 11 deletions src/classes/texture2d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import BufferReader from 'buffer-reader';
import Jimp from 'jimp';
import { last, omit, once } from 'lodash';
import { decodeAstc, isSupportedAstcFormat } from '../utils/astc';
import { decodeEtc1 } from '../utils/etc';
import NestedError from 'nested-error-stacks';
import { decodeTexture } from '../utils/decodeTexture';
import type { BufferReaderExtended } from '../utils/reader';
import { AssetBase } from './base';
import { AssetType, TextureFormat } from './types';
import { AssetType } from './types';

export interface Texture2DResult {
name: string;
Expand Down Expand Up @@ -76,7 +76,7 @@ export class Texture2D extends AssetBase<Texture2DResult> {
? this.readStreamInfo(r)
: undefined;
const data = streamInfo?.path ? this.readData(streamInfo) : r.nextBuffer(size);
const decodedData = this.decodeImage(data, width, height, format, name);
const decodedData = this.decodeTexture(data, width, height, format, name);
const image = new Jimp({ data: decodedData, width, height });
return {
name,
Expand Down Expand Up @@ -112,13 +112,11 @@ export class Texture2D extends AssetBase<Texture2DResult> {
return r.nextBuffer(streamInfo.size);
}

private decodeImage(data: Buffer, width: number, height: number, format: number, name: string) {
switch (format) {
case TextureFormat.ETC_RGB4:
return decodeEtc1(data, width, height);
default:
if (isSupportedAstcFormat(format)) return decodeAstc(data, width, height, format);
throw new Error(`Texture2d image "${name}" format "${format}" is not implemented.`);
private decodeTexture(data: Buffer, width: number, height: number, format: number, name: string) {
try {
return decodeTexture(data, width, height, format);
} catch (error: any) {
throw new NestedError(`Decode texture for "${name}" failed.`, error);
}
}

Expand Down
35 changes: 0 additions & 35 deletions src/utils/astc.ts

This file was deleted.

77 changes: 77 additions & 0 deletions src/utils/decodeTexture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {
decodeAstc,
decodeAtcRgb4,
decodeAtcRgba8,
decodeBc1,
decodeBc3,
decodeBc4,
decodeBc5,
decodeBc6Unsigned,
decodeBc7,
decodeEacr,
decodeEacrSigned,
decodeEacrg,
decodeEacrgSigned,
decodeEtc1,
decodeEtc2Rgb,
decodeEtc2Rgba1,
decodeEtc2Rgba8,
decodePvrtc2bpp,
decodePvrtc4bpp,
} from '@arkntools/unity-js-tools';
import { TextureFormat as TF } from '../classes/types';

type DecodeFunction = (data: Uint8Array, width: number, height: number) => Uint8Array;

const getAstcDecodeFunc =
(blockSize: number): DecodeFunction =>
(...args) =>
decodeAstc(...args, blockSize, blockSize);

const funcMap: Partial<Record<TF, DecodeFunction>> = {
[TF.ATC_RGB4]: decodeAtcRgb4,
[TF.ATC_RGBA8]: decodeAtcRgba8,
[TF.ASTC_RGB_4x4]: getAstcDecodeFunc(4),
[TF.ASTC_RGB_5x5]: getAstcDecodeFunc(5),
[TF.ASTC_RGB_6x6]: getAstcDecodeFunc(6),
[TF.ASTC_RGB_8x8]: getAstcDecodeFunc(8),
[TF.ASTC_RGB_10x10]: getAstcDecodeFunc(10),
[TF.ASTC_RGB_12x12]: getAstcDecodeFunc(12),
[TF.ASTC_RGBA_4x4]: getAstcDecodeFunc(4),
[TF.ASTC_RGBA_5x5]: getAstcDecodeFunc(5),
[TF.ASTC_RGBA_6x6]: getAstcDecodeFunc(6),
[TF.ASTC_RGBA_8x8]: getAstcDecodeFunc(8),
[TF.ASTC_RGBA_10x10]: getAstcDecodeFunc(10),
[TF.ASTC_RGBA_12x12]: getAstcDecodeFunc(12),
[TF.ASTC_HDR_4x4]: getAstcDecodeFunc(4),
[TF.ASTC_HDR_5x5]: getAstcDecodeFunc(5),
[TF.ASTC_HDR_6x6]: getAstcDecodeFunc(6),
[TF.ASTC_HDR_8x8]: getAstcDecodeFunc(8),
[TF.ASTC_HDR_10x10]: getAstcDecodeFunc(10),
[TF.ASTC_HDR_12x12]: getAstcDecodeFunc(12),
[TF.DXT1]: decodeBc1,
[TF.DXT5]: decodeBc3,
[TF.BC4]: decodeBc4,
[TF.BC5]: decodeBc5,
[TF.BC6H]: decodeBc6Unsigned, // not sure
[TF.BC7]: decodeBc7,
[TF.ETC_RGB4]: decodeEtc1,
[TF.ETC_RGB4_3DS]: decodeEtc1,
[TF.ETC2_RGB]: decodeEtc2Rgb,
[TF.ETC2_RGBA1]: decodeEtc2Rgba1,
[TF.ETC2_RGBA8]: decodeEtc2Rgba8,
[TF.EAC_R]: decodeEacr,
[TF.EAC_R_SIGNED]: decodeEacrSigned,
[TF.EAC_RG]: decodeEacrg,
[TF.EAC_RG_SIGNED]: decodeEacrgSigned,
[TF.PVRTC_RGB2]: decodePvrtc2bpp,
[TF.PVRTC_RGBA2]: decodePvrtc2bpp,
[TF.PVRTC_RGB4]: decodePvrtc4bpp,
[TF.PVRTC_RGBA4]: decodePvrtc4bpp,
};

export const decodeTexture = (data: Uint8Array, width: number, height: number, format: TF) => {
const decodeFunc = funcMap[format];
if (!decodeFunc) throw new Error(`Texture2d format "${format}" decoder is not implemented.`);
return decodeFunc(data, width, height);
};
1 change: 1 addition & 0 deletions src/utils/etc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const ETC1_MODIFIER_TABLE = [
[47, 183],
];

/** @deprecated use @arkntools/unity-js-tools */
export const decodeEtc1 = (data: Buffer, w: number, h: number) => {
const image = new Uint8Array(w * h * 4);
const numBX = Math.ceil(w / 4);
Expand Down
18 changes: 14 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
resolved "https://registry.yarnpkg.com/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz#bd9154aec9983f77b3a034ecaa015c2e4201f6cf"
integrity sha512-1Yjs2SvM8TflER/OD3cOjhWWOZb58A2t7wpE2S9XfBYTiIl+XFhQG2bjy4Pu1I+EAlCNUzRDYDdFwFYUKvXcIA==

"@arkntools/unity-js-tools@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@arkntools/unity-js-tools/-/unity-js-tools-1.0.1.tgz#db0ee3255c9264d56d88a47d80bbdb336db31615"
integrity sha512-n8U6Lvr4nSnCTETYN5rPYdAWiFbRJDOrod+dxrDxzlCQTnsIHGVdqsjPX4/qTfK18DmOnzrWA7NBzkptn7F3lg==
"@arkntools/unity-js-tools@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@arkntools/unity-js-tools/-/unity-js-tools-1.1.0.tgz#3df25594fd86f383c9bf86cbd070bb1935c6e2ad"
integrity sha512-+oJxVRV214GpRWkCktROTX+k29EM96pgyC7jx9AtGqc17jRZvwpU0peM4Rp9imQ8wTVWCkPllkaJvAVN9x4fww==

"@cspotcode/source-map-support@^0.8.0":
version "0.8.1"
Expand Down Expand Up @@ -449,6 +449,11 @@
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.196.tgz#a7c3d6fc52d8d71328b764e28e080b4169ec7a95"
integrity sha512-22y3o88f4a94mKljsZcanlNWPzO0uBsBdzLAngf2tp533LzZcQzb6+eZPJ+vCTt+bqF2XnvT9gejTLsAcJAJyQ==

"@types/nested-error-stacks@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@types/nested-error-stacks/-/nested-error-stacks-2.1.0.tgz#0e4caf703b874c3c4e6ba74dbc0c6acf88b8ce05"
integrity sha512-7+la7jn6iA603lBgyASoaW5Nk/R5G3+hkJ4Y0gtc9VYHlcixvm/YBV2KV92dTBpeCQJYjpN2owb5jVuKCqxOaA==

"@types/node@*":
version "20.3.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.3.1.tgz#e8a83f1aa8b649377bb1fb5d7bac5cb90e784dfe"
Expand Down Expand Up @@ -2216,6 +2221,11 @@ natural-compare@^1.4.0:
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==

nested-error-stacks@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/nested-error-stacks/-/nested-error-stacks-2.1.1.tgz#26c8a3cee6cc05fbcf1e333cd2fc3e003326c0b5"
integrity sha512-9iN1ka/9zmX1ZvLV9ewJYEk9h7RyRRtqdK0woXcqohu8EWIerfPUjYJPg0ULy0UqP7cslmdGc8xKDJcojlKiaw==

node-fetch@^2.6.1:
version "2.6.11"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.11.tgz#cde7fc71deef3131ef80a738919f999e6edfff25"
Expand Down

0 comments on commit 709bd2b

Please sign in to comment.