mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-25 19:49:50 +01:00
feat: normalized alpha
This commit is contained in:
1 parent
fdd8aac017
commit
0f9f15b268
2 files changed
+31
-36
No files matched your search
@@ -6,7 +6,6 @@ export async function readTexture(path: string) {
|
|||||||
const ddsTexturePath = path.replace('.tex', '.dx11.dds.dvpl');
|
const ddsTexturePath = path.replace('.tex', '.dx11.dds.dvpl');
|
||||||
const decompressedDvpl = await readDVPLFile(ddsTexturePath);
|
const decompressedDvpl = await readDVPLFile(ddsTexturePath);
|
||||||
const stream = new DdsStream(decompressedDvpl);
|
const stream = new DdsStream(decompressedDvpl);
|
||||||
console.log('Reading', ddsTexturePath);
|
|
||||||
const ddsRaw = await stream.dds();
|
const ddsRaw = await stream.dds();
|
||||||
|
|
||||||
return await sharp(ddsRaw.data, { raw: ddsRaw }).png().toBuffer();
|
return await sharp(ddsRaw.data, { raw: ddsRaw }).png().toBuffer();
|
||||||
|
|||||||
+31
-35
@@ -175,30 +175,7 @@ export class DdsStream extends WindowsStream {
|
|||||||
async dds() {
|
async dds() {
|
||||||
this.magicNumber();
|
this.magicNumber();
|
||||||
const header = this.header();
|
const header = this.header();
|
||||||
|
const dxgiFormat = this.resolveDxgiFormat(header);
|
||||||
let dxgiFormat: DxgiFormat;
|
|
||||||
|
|
||||||
switch (header.pf.fourCC) {
|
|
||||||
case 'DXT3': {
|
|
||||||
dxgiFormat = DxgiFormat.BC2_UNORM;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'DXT5': {
|
|
||||||
dxgiFormat = DxgiFormat.BC3_UNORM;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'DX10': {
|
|
||||||
dxgiFormat = this.headerDxt10().dxgiFormat;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw new TypeError(`Unsupported FourCC: ${header.pf.fourCC}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(DxgiFormat[dxgiFormat]);
|
|
||||||
|
|
||||||
switch (dxgiFormat) {
|
switch (dxgiFormat) {
|
||||||
case DxgiFormat.BC1_TYPELESS:
|
case DxgiFormat.BC1_TYPELESS:
|
||||||
@@ -218,10 +195,12 @@ export class DdsStream extends WindowsStream {
|
|||||||
const alphaInterpolations = this.bc2AlphaInterpolations();
|
const alphaInterpolations = this.bc2AlphaInterpolations();
|
||||||
const colorInterpolations = this.bc2ColorInterpolations();
|
const colorInterpolations = this.bc2ColorInterpolations();
|
||||||
const colorKeys = this.bc1ColorKeys();
|
const colorKeys = this.bc1ColorKeys();
|
||||||
return (index) => [
|
return (index) => {
|
||||||
...colorInterpolations[colorKeys[index]],
|
return [
|
||||||
alphaInterpolations[index],
|
...colorInterpolations[colorKeys[index]],
|
||||||
];
|
alphaInterpolations[index],
|
||||||
|
];
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,6 +275,22 @@ export class DdsStream extends WindowsStream {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resolveDxgiFormat(header: ReturnType<typeof this.header>) {
|
||||||
|
switch (header.pf.fourCC) {
|
||||||
|
case 'DXT3':
|
||||||
|
return DxgiFormat.BC2_UNORM;
|
||||||
|
|
||||||
|
case 'DXT5':
|
||||||
|
return DxgiFormat.BC3_UNORM;
|
||||||
|
|
||||||
|
case 'DX10':
|
||||||
|
return this.headerDxt10().dxgiFormat;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new TypeError(`Unsupported FourCC: ${header.pf.fourCC}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
assetFactor4(width: number, height: number) {
|
assetFactor4(width: number, height: number) {
|
||||||
if (width % 4 !== 0 || height % 4 !== 0) {
|
if (width % 4 !== 0 || height % 4 !== 0) {
|
||||||
throw new RangeError('Width and height must be divisible by 4');
|
throw new RangeError('Width and height must be divisible by 4');
|
||||||
@@ -337,18 +332,17 @@ export class DdsStream extends WindowsStream {
|
|||||||
}
|
}
|
||||||
R5G6B5() {
|
R5G6B5() {
|
||||||
const buffer = this.consume(2);
|
const buffer = this.consume(2);
|
||||||
const b = (buffer[0] & 0b11111) / 0x1f;
|
const r = (buffer[1] & 0b11111000) >>> 3;
|
||||||
const r = ((buffer[1] & 0b11111000) >>> 3) / 0x1f;
|
const g = ((buffer[1] & 0b111) << 3) | ((buffer[0] & 0b11100000) >>> 5);
|
||||||
const g =
|
const b = buffer[0] & 0b11111;
|
||||||
(((buffer[1] & 0b111) << 3) | ((buffer[0] & 0b11100000) >>> 5)) / 0x3f;
|
|
||||||
|
|
||||||
return [r, g, b] as Vector3Tuple;
|
return [r / 31, g / 63, b / 31] as Vector3Tuple;
|
||||||
}
|
}
|
||||||
R5G6B5A0() {
|
R5G6B5A0() {
|
||||||
return [...this.R5G6B5(), 1] as Vector4Tuple;
|
return [...this.R5G6B5(), 1] as Vector4Tuple;
|
||||||
}
|
}
|
||||||
A8() {
|
A8() {
|
||||||
return this.uint8() / 0xff;
|
return this.uint8() / 255;
|
||||||
}
|
}
|
||||||
|
|
||||||
bc1ColorInterpolations() {
|
bc1ColorInterpolations() {
|
||||||
@@ -417,7 +411,9 @@ export class DdsStream extends WindowsStream {
|
|||||||
const n = (buffer[6] & 0b11110000) >>> 4;
|
const n = (buffer[6] & 0b11110000) >>> 4;
|
||||||
const m = buffer[6] & 0b1111;
|
const m = buffer[6] & 0b1111;
|
||||||
|
|
||||||
return [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p];
|
return [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p].map(
|
||||||
|
(alpha) => alpha / 15,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
bc2ColorInterpolations() {
|
bc2ColorInterpolations() {
|
||||||
const color0 = this.R5G6B5();
|
const color0 = this.R5G6B5();
|
||||||
|
|||||||
Reference in new issue
Block a user