This commit is contained in:
2025-01-04 00:34:03 +01:00
parent 41829408dc
commit 0ca14bbc19
18111 changed files with 1871397 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
"use strict";
var parseDDS = require("./parseDDS.js"), parseKTX = require("./parseKTX.js");
exports.parseDDS = parseDDS.parseDDS;
exports.FORMATS_TO_COMPONENTS = parseKTX.FORMATS_TO_COMPONENTS;
exports.TYPES_TO_BYTES_PER_COMPONENT = parseKTX.TYPES_TO_BYTES_PER_COMPONENT;
exports.TYPES_TO_BYTES_PER_PIXEL = parseKTX.TYPES_TO_BYTES_PER_PIXEL;
exports.parseKTX = parseKTX.parseKTX;
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;"}

View File

@@ -0,0 +1,10 @@
import { parseDDS } from "./parseDDS.mjs";
import { FORMATS_TO_COMPONENTS, TYPES_TO_BYTES_PER_COMPONENT, TYPES_TO_BYTES_PER_PIXEL, parseKTX } from "./parseKTX.mjs";
export {
FORMATS_TO_COMPONENTS,
TYPES_TO_BYTES_PER_COMPONENT,
TYPES_TO_BYTES_PER_PIXEL,
parseDDS,
parseKTX
};
//# sourceMappingURL=index.mjs.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}

View File

@@ -0,0 +1,107 @@
"use strict";
var _const = require("../const.js");
require("../resources/index.js");
var CompressedTextureResource = require("../resources/CompressedTextureResource.js");
const DDS_MAGIC_SIZE = 4, DDS_HEADER_SIZE = 124, DDS_HEADER_PF_SIZE = 32, DDS_HEADER_DX10_SIZE = 20, DDS_MAGIC = 542327876, DDS_FIELDS = {
SIZE: 1,
FLAGS: 2,
HEIGHT: 3,
WIDTH: 4,
MIPMAP_COUNT: 7,
PIXEL_FORMAT: 19
}, DDS_PF_FIELDS = {
SIZE: 0,
FLAGS: 1,
FOURCC: 2,
RGB_BITCOUNT: 3,
R_BIT_MASK: 4,
G_BIT_MASK: 5,
B_BIT_MASK: 6,
A_BIT_MASK: 7
}, DDS_DX10_FIELDS = {
DXGI_FORMAT: 0,
RESOURCE_DIMENSION: 1,
MISC_FLAG: 2,
ARRAY_SIZE: 3,
MISC_FLAGS2: 4
}, PF_FLAGS = 1, DDPF_ALPHA = 2, DDPF_FOURCC = 4, DDPF_RGB = 64, DDPF_YUV = 512, DDPF_LUMINANCE = 131072, FOURCC_DXT1 = 827611204, FOURCC_DXT3 = 861165636, FOURCC_DXT5 = 894720068, FOURCC_DX10 = 808540228, DDS_RESOURCE_MISC_TEXTURECUBE = 4, FOURCC_TO_FORMAT = {
[FOURCC_DXT1]: _const.INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT1_EXT,
[FOURCC_DXT3]: _const.INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT3_EXT,
[FOURCC_DXT5]: _const.INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT5_EXT
}, DXGI_TO_FORMAT = {
// WEBGL_compressed_texture_s3tc
70: _const.INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT1_EXT,
71: _const.INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT1_EXT,
73: _const.INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT3_EXT,
74: _const.INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT3_EXT,
76: _const.INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT5_EXT,
77: _const.INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT5_EXT,
// WEBGL_compressed_texture_s3tc_srgb
72: _const.INTERNAL_FORMATS.COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT,
75: _const.INTERNAL_FORMATS.COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT,
78: _const.INTERNAL_FORMATS.COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT,
// EXT_texture_compression_bptc
// BC6H
96: _const.INTERNAL_FORMATS.COMPRESSED_RGB_BPTC_SIGNED_FLOAT_EXT,
95: _const.INTERNAL_FORMATS.COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_EXT,
// BC7
98: _const.INTERNAL_FORMATS.COMPRESSED_RGBA_BPTC_UNORM_EXT,
99: _const.INTERNAL_FORMATS.COMPRESSED_SRGB_ALPHA_BPTC_UNORM_EXT
};
function parseDDS(arrayBuffer) {
const data = new Uint32Array(arrayBuffer);
if (data[0] !== DDS_MAGIC)
throw new Error("Invalid DDS file magic word");
const header = new Uint32Array(arrayBuffer, 0, DDS_HEADER_SIZE / Uint32Array.BYTES_PER_ELEMENT), height = header[DDS_FIELDS.HEIGHT], width = header[DDS_FIELDS.WIDTH], mipmapCount = header[DDS_FIELDS.MIPMAP_COUNT], pixelFormat = new Uint32Array(
arrayBuffer,
DDS_FIELDS.PIXEL_FORMAT * Uint32Array.BYTES_PER_ELEMENT,
DDS_HEADER_PF_SIZE / Uint32Array.BYTES_PER_ELEMENT
), formatFlags = pixelFormat[PF_FLAGS];
if (formatFlags & DDPF_FOURCC) {
const fourCC = pixelFormat[DDS_PF_FIELDS.FOURCC];
if (fourCC !== FOURCC_DX10) {
const internalFormat2 = FOURCC_TO_FORMAT[fourCC], dataOffset2 = DDS_MAGIC_SIZE + DDS_HEADER_SIZE, texData = new Uint8Array(arrayBuffer, dataOffset2);
return [new CompressedTextureResource.CompressedTextureResource(texData, {
format: internalFormat2,
width,
height,
levels: mipmapCount
// CompressedTextureResource will separate the levelBuffers for us!
})];
}
const dx10Offset = DDS_MAGIC_SIZE + DDS_HEADER_SIZE, dx10Header = new Uint32Array(
data.buffer,
dx10Offset,
DDS_HEADER_DX10_SIZE / Uint32Array.BYTES_PER_ELEMENT
), dxgiFormat = dx10Header[DDS_DX10_FIELDS.DXGI_FORMAT], resourceDimension = dx10Header[DDS_DX10_FIELDS.RESOURCE_DIMENSION], miscFlag = dx10Header[DDS_DX10_FIELDS.MISC_FLAG], arraySize = dx10Header[DDS_DX10_FIELDS.ARRAY_SIZE], internalFormat = DXGI_TO_FORMAT[dxgiFormat];
if (internalFormat === void 0)
throw new Error(`DDSParser cannot parse texture data with DXGI format ${dxgiFormat}`);
if (miscFlag === DDS_RESOURCE_MISC_TEXTURECUBE)
throw new Error("DDSParser does not support cubemap textures");
if (resourceDimension === 6)
throw new Error("DDSParser does not supported 3D texture data");
const imageBuffers = new Array(), dataOffset = DDS_MAGIC_SIZE + DDS_HEADER_SIZE + DDS_HEADER_DX10_SIZE;
if (arraySize === 1)
imageBuffers.push(new Uint8Array(arrayBuffer, dataOffset));
else {
const pixelSize = _const.INTERNAL_FORMAT_TO_BYTES_PER_PIXEL[internalFormat];
let imageSize = 0, levelWidth = width, levelHeight = height;
for (let i = 0; i < mipmapCount; i++) {
const alignedLevelWidth = Math.max(1, levelWidth + 3 & -4), alignedLevelHeight = Math.max(1, levelHeight + 3 & -4), levelSize = alignedLevelWidth * alignedLevelHeight * pixelSize;
imageSize += levelSize, levelWidth = levelWidth >>> 1, levelHeight = levelHeight >>> 1;
}
let imageOffset = dataOffset;
for (let i = 0; i < arraySize; i++)
imageBuffers.push(new Uint8Array(arrayBuffer, imageOffset, imageSize)), imageOffset += imageSize;
}
return imageBuffers.map((buffer) => new CompressedTextureResource.CompressedTextureResource(buffer, {
format: internalFormat,
width,
height,
levels: mipmapCount
}));
}
throw formatFlags & DDPF_RGB ? new Error("DDSParser does not support uncompressed texture data.") : formatFlags & DDPF_YUV ? new Error("DDSParser does not supported YUV uncompressed texture data.") : formatFlags & DDPF_LUMINANCE ? new Error("DDSParser does not support single-channel (lumninance) texture data!") : formatFlags & DDPF_ALPHA ? new Error("DDSParser does not support single-channel (alpha) texture data!") : new Error("DDSParser failed to load a texture file due to an unknown reason!");
}
exports.parseDDS = parseDDS;
//# sourceMappingURL=parseDDS.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,108 @@
import { INTERNAL_FORMATS, INTERNAL_FORMAT_TO_BYTES_PER_PIXEL } from "../const.mjs";
import "../resources/index.mjs";
import { CompressedTextureResource } from "../resources/CompressedTextureResource.mjs";
const DDS_MAGIC_SIZE = 4, DDS_HEADER_SIZE = 124, DDS_HEADER_PF_SIZE = 32, DDS_HEADER_DX10_SIZE = 20, DDS_MAGIC = 542327876, DDS_FIELDS = {
SIZE: 1,
FLAGS: 2,
HEIGHT: 3,
WIDTH: 4,
MIPMAP_COUNT: 7,
PIXEL_FORMAT: 19
}, DDS_PF_FIELDS = {
SIZE: 0,
FLAGS: 1,
FOURCC: 2,
RGB_BITCOUNT: 3,
R_BIT_MASK: 4,
G_BIT_MASK: 5,
B_BIT_MASK: 6,
A_BIT_MASK: 7
}, DDS_DX10_FIELDS = {
DXGI_FORMAT: 0,
RESOURCE_DIMENSION: 1,
MISC_FLAG: 2,
ARRAY_SIZE: 3,
MISC_FLAGS2: 4
}, PF_FLAGS = 1, DDPF_ALPHA = 2, DDPF_FOURCC = 4, DDPF_RGB = 64, DDPF_YUV = 512, DDPF_LUMINANCE = 131072, FOURCC_DXT1 = 827611204, FOURCC_DXT3 = 861165636, FOURCC_DXT5 = 894720068, FOURCC_DX10 = 808540228, DDS_RESOURCE_MISC_TEXTURECUBE = 4, FOURCC_TO_FORMAT = {
[FOURCC_DXT1]: INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT1_EXT,
[FOURCC_DXT3]: INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT3_EXT,
[FOURCC_DXT5]: INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT5_EXT
}, DXGI_TO_FORMAT = {
// WEBGL_compressed_texture_s3tc
70: INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT1_EXT,
71: INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT1_EXT,
73: INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT3_EXT,
74: INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT3_EXT,
76: INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT5_EXT,
77: INTERNAL_FORMATS.COMPRESSED_RGBA_S3TC_DXT5_EXT,
// WEBGL_compressed_texture_s3tc_srgb
72: INTERNAL_FORMATS.COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT,
75: INTERNAL_FORMATS.COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT,
78: INTERNAL_FORMATS.COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT,
// EXT_texture_compression_bptc
// BC6H
96: INTERNAL_FORMATS.COMPRESSED_RGB_BPTC_SIGNED_FLOAT_EXT,
95: INTERNAL_FORMATS.COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_EXT,
// BC7
98: INTERNAL_FORMATS.COMPRESSED_RGBA_BPTC_UNORM_EXT,
99: INTERNAL_FORMATS.COMPRESSED_SRGB_ALPHA_BPTC_UNORM_EXT
};
function parseDDS(arrayBuffer) {
const data = new Uint32Array(arrayBuffer);
if (data[0] !== DDS_MAGIC)
throw new Error("Invalid DDS file magic word");
const header = new Uint32Array(arrayBuffer, 0, DDS_HEADER_SIZE / Uint32Array.BYTES_PER_ELEMENT), height = header[DDS_FIELDS.HEIGHT], width = header[DDS_FIELDS.WIDTH], mipmapCount = header[DDS_FIELDS.MIPMAP_COUNT], pixelFormat = new Uint32Array(
arrayBuffer,
DDS_FIELDS.PIXEL_FORMAT * Uint32Array.BYTES_PER_ELEMENT,
DDS_HEADER_PF_SIZE / Uint32Array.BYTES_PER_ELEMENT
), formatFlags = pixelFormat[PF_FLAGS];
if (formatFlags & DDPF_FOURCC) {
const fourCC = pixelFormat[DDS_PF_FIELDS.FOURCC];
if (fourCC !== FOURCC_DX10) {
const internalFormat2 = FOURCC_TO_FORMAT[fourCC], dataOffset2 = DDS_MAGIC_SIZE + DDS_HEADER_SIZE, texData = new Uint8Array(arrayBuffer, dataOffset2);
return [new CompressedTextureResource(texData, {
format: internalFormat2,
width,
height,
levels: mipmapCount
// CompressedTextureResource will separate the levelBuffers for us!
})];
}
const dx10Offset = DDS_MAGIC_SIZE + DDS_HEADER_SIZE, dx10Header = new Uint32Array(
data.buffer,
dx10Offset,
DDS_HEADER_DX10_SIZE / Uint32Array.BYTES_PER_ELEMENT
), dxgiFormat = dx10Header[DDS_DX10_FIELDS.DXGI_FORMAT], resourceDimension = dx10Header[DDS_DX10_FIELDS.RESOURCE_DIMENSION], miscFlag = dx10Header[DDS_DX10_FIELDS.MISC_FLAG], arraySize = dx10Header[DDS_DX10_FIELDS.ARRAY_SIZE], internalFormat = DXGI_TO_FORMAT[dxgiFormat];
if (internalFormat === void 0)
throw new Error(`DDSParser cannot parse texture data with DXGI format ${dxgiFormat}`);
if (miscFlag === DDS_RESOURCE_MISC_TEXTURECUBE)
throw new Error("DDSParser does not support cubemap textures");
if (resourceDimension === 6)
throw new Error("DDSParser does not supported 3D texture data");
const imageBuffers = new Array(), dataOffset = DDS_MAGIC_SIZE + DDS_HEADER_SIZE + DDS_HEADER_DX10_SIZE;
if (arraySize === 1)
imageBuffers.push(new Uint8Array(arrayBuffer, dataOffset));
else {
const pixelSize = INTERNAL_FORMAT_TO_BYTES_PER_PIXEL[internalFormat];
let imageSize = 0, levelWidth = width, levelHeight = height;
for (let i = 0; i < mipmapCount; i++) {
const alignedLevelWidth = Math.max(1, levelWidth + 3 & -4), alignedLevelHeight = Math.max(1, levelHeight + 3 & -4), levelSize = alignedLevelWidth * alignedLevelHeight * pixelSize;
imageSize += levelSize, levelWidth = levelWidth >>> 1, levelHeight = levelHeight >>> 1;
}
let imageOffset = dataOffset;
for (let i = 0; i < arraySize; i++)
imageBuffers.push(new Uint8Array(arrayBuffer, imageOffset, imageSize)), imageOffset += imageSize;
}
return imageBuffers.map((buffer) => new CompressedTextureResource(buffer, {
format: internalFormat,
width,
height,
levels: mipmapCount
}));
}
throw formatFlags & DDPF_RGB ? new Error("DDSParser does not support uncompressed texture data.") : formatFlags & DDPF_YUV ? new Error("DDSParser does not supported YUV uncompressed texture data.") : formatFlags & DDPF_LUMINANCE ? new Error("DDSParser does not support single-channel (lumninance) texture data!") : formatFlags & DDPF_ALPHA ? new Error("DDSParser does not support single-channel (alpha) texture data!") : new Error("DDSParser failed to load a texture file due to an unknown reason!");
}
export {
parseDDS
};
//# sourceMappingURL=parseDDS.mjs.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,164 @@
"use strict";
var core = require("@pixi/core"), _const = require("../const.js");
require("../resources/index.js");
var CompressedTextureResource = require("../resources/CompressedTextureResource.js");
const FILE_IDENTIFIER = [171, 75, 84, 88, 32, 49, 49, 187, 13, 10, 26, 10], ENDIANNESS = 67305985, KTX_FIELDS = {
FILE_IDENTIFIER: 0,
ENDIANNESS: 12,
GL_TYPE: 16,
GL_TYPE_SIZE: 20,
GL_FORMAT: 24,
GL_INTERNAL_FORMAT: 28,
GL_BASE_INTERNAL_FORMAT: 32,
PIXEL_WIDTH: 36,
PIXEL_HEIGHT: 40,
PIXEL_DEPTH: 44,
NUMBER_OF_ARRAY_ELEMENTS: 48,
NUMBER_OF_FACES: 52,
NUMBER_OF_MIPMAP_LEVELS: 56,
BYTES_OF_KEY_VALUE_DATA: 60
}, FILE_HEADER_SIZE = 64, TYPES_TO_BYTES_PER_COMPONENT = {
[core.TYPES.UNSIGNED_BYTE]: 1,
[core.TYPES.UNSIGNED_SHORT]: 2,
[core.TYPES.INT]: 4,
[core.TYPES.UNSIGNED_INT]: 4,
[core.TYPES.FLOAT]: 4,
[core.TYPES.HALF_FLOAT]: 8
}, FORMATS_TO_COMPONENTS = {
[core.FORMATS.RGBA]: 4,
[core.FORMATS.RGB]: 3,
[core.FORMATS.RG]: 2,
[core.FORMATS.RED]: 1,
[core.FORMATS.LUMINANCE]: 1,
[core.FORMATS.LUMINANCE_ALPHA]: 2,
[core.FORMATS.ALPHA]: 1
}, TYPES_TO_BYTES_PER_PIXEL = {
[core.TYPES.UNSIGNED_SHORT_4_4_4_4]: 2,
[core.TYPES.UNSIGNED_SHORT_5_5_5_1]: 2,
[core.TYPES.UNSIGNED_SHORT_5_6_5]: 2
};
function parseKTX(url, arrayBuffer, loadKeyValueData = !1) {
const dataView = new DataView(arrayBuffer);
if (!validate(url, dataView))
return null;
const littleEndian = dataView.getUint32(KTX_FIELDS.ENDIANNESS, !0) === ENDIANNESS, glType = dataView.getUint32(KTX_FIELDS.GL_TYPE, littleEndian), glFormat = dataView.getUint32(KTX_FIELDS.GL_FORMAT, littleEndian), glInternalFormat = dataView.getUint32(KTX_FIELDS.GL_INTERNAL_FORMAT, littleEndian), pixelWidth = dataView.getUint32(KTX_FIELDS.PIXEL_WIDTH, littleEndian), pixelHeight = dataView.getUint32(KTX_FIELDS.PIXEL_HEIGHT, littleEndian) || 1, pixelDepth = dataView.getUint32(KTX_FIELDS.PIXEL_DEPTH, littleEndian) || 1, numberOfArrayElements = dataView.getUint32(KTX_FIELDS.NUMBER_OF_ARRAY_ELEMENTS, littleEndian) || 1, numberOfFaces = dataView.getUint32(KTX_FIELDS.NUMBER_OF_FACES, littleEndian), numberOfMipmapLevels = dataView.getUint32(KTX_FIELDS.NUMBER_OF_MIPMAP_LEVELS, littleEndian), bytesOfKeyValueData = dataView.getUint32(KTX_FIELDS.BYTES_OF_KEY_VALUE_DATA, littleEndian);
if (pixelHeight === 0 || pixelDepth !== 1)
throw new Error("Only 2D textures are supported");
if (numberOfFaces !== 1)
throw new Error("CubeTextures are not supported by KTXLoader yet!");
if (numberOfArrayElements !== 1)
throw new Error("WebGL does not support array textures");
const blockWidth = 4, blockHeight = 4, alignedWidth = pixelWidth + 3 & -4, alignedHeight = pixelHeight + 3 & -4, imageBuffers = new Array(numberOfArrayElements);
let imagePixels = pixelWidth * pixelHeight;
glType === 0 && (imagePixels = alignedWidth * alignedHeight);
let imagePixelByteSize;
if (glType !== 0 ? TYPES_TO_BYTES_PER_COMPONENT[glType] ? imagePixelByteSize = TYPES_TO_BYTES_PER_COMPONENT[glType] * FORMATS_TO_COMPONENTS[glFormat] : imagePixelByteSize = TYPES_TO_BYTES_PER_PIXEL[glType] : imagePixelByteSize = _const.INTERNAL_FORMAT_TO_BYTES_PER_PIXEL[glInternalFormat], imagePixelByteSize === void 0)
throw new Error("Unable to resolve the pixel format stored in the *.ktx file!");
const kvData = loadKeyValueData ? parseKvData(dataView, bytesOfKeyValueData, littleEndian) : null;
let mipByteSize = imagePixels * imagePixelByteSize, mipWidth = pixelWidth, mipHeight = pixelHeight, alignedMipWidth = alignedWidth, alignedMipHeight = alignedHeight, imageOffset = FILE_HEADER_SIZE + bytesOfKeyValueData;
for (let mipmapLevel = 0; mipmapLevel < numberOfMipmapLevels; mipmapLevel++) {
const imageSize = dataView.getUint32(imageOffset, littleEndian);
let elementOffset = imageOffset + 4;
for (let arrayElement = 0; arrayElement < numberOfArrayElements; arrayElement++) {
let mips = imageBuffers[arrayElement];
mips || (mips = imageBuffers[arrayElement] = new Array(numberOfMipmapLevels)), mips[mipmapLevel] = {
levelID: mipmapLevel,
// don't align mipWidth when texture not compressed! (glType not zero)
levelWidth: numberOfMipmapLevels > 1 || glType !== 0 ? mipWidth : alignedMipWidth,
levelHeight: numberOfMipmapLevels > 1 || glType !== 0 ? mipHeight : alignedMipHeight,
levelBuffer: new Uint8Array(arrayBuffer, elementOffset, mipByteSize)
}, elementOffset += mipByteSize;
}
imageOffset += imageSize + 4, imageOffset = imageOffset % 4 !== 0 ? imageOffset + 4 - imageOffset % 4 : imageOffset, mipWidth = mipWidth >> 1 || 1, mipHeight = mipHeight >> 1 || 1, alignedMipWidth = mipWidth + blockWidth - 1 & ~(blockWidth - 1), alignedMipHeight = mipHeight + blockHeight - 1 & ~(blockHeight - 1), mipByteSize = alignedMipWidth * alignedMipHeight * imagePixelByteSize;
}
return glType !== 0 ? {
uncompressed: imageBuffers.map((levelBuffers) => {
let buffer = levelBuffers[0].levelBuffer, convertToInt = !1;
return glType === core.TYPES.FLOAT ? buffer = new Float32Array(
levelBuffers[0].levelBuffer.buffer,
levelBuffers[0].levelBuffer.byteOffset,
levelBuffers[0].levelBuffer.byteLength / 4
) : glType === core.TYPES.UNSIGNED_INT ? (convertToInt = !0, buffer = new Uint32Array(
levelBuffers[0].levelBuffer.buffer,
levelBuffers[0].levelBuffer.byteOffset,
levelBuffers[0].levelBuffer.byteLength / 4
)) : glType === core.TYPES.INT && (convertToInt = !0, buffer = new Int32Array(
levelBuffers[0].levelBuffer.buffer,
levelBuffers[0].levelBuffer.byteOffset,
levelBuffers[0].levelBuffer.byteLength / 4
)), {
resource: new core.BufferResource(
buffer,
{
width: levelBuffers[0].levelWidth,
height: levelBuffers[0].levelHeight
}
),
type: glType,
format: convertToInt ? convertFormatToInteger(glFormat) : glFormat
};
}),
kvData
} : {
compressed: imageBuffers.map((levelBuffers) => new CompressedTextureResource.CompressedTextureResource(null, {
format: glInternalFormat,
width: pixelWidth,
height: pixelHeight,
levels: numberOfMipmapLevels,
levelBuffers
})),
kvData
};
}
function validate(url, dataView) {
for (let i = 0; i < FILE_IDENTIFIER.length; i++)
if (dataView.getUint8(i) !== FILE_IDENTIFIER[i])
return console.error(`${url} is not a valid *.ktx file!`), !1;
return !0;
}
function convertFormatToInteger(format) {
switch (format) {
case core.FORMATS.RGBA:
return core.FORMATS.RGBA_INTEGER;
case core.FORMATS.RGB:
return core.FORMATS.RGB_INTEGER;
case core.FORMATS.RG:
return core.FORMATS.RG_INTEGER;
case core.FORMATS.RED:
return core.FORMATS.RED_INTEGER;
default:
return format;
}
}
function parseKvData(dataView, bytesOfKeyValueData, littleEndian) {
const kvData = /* @__PURE__ */ new Map();
let bytesIntoKeyValueData = 0;
for (; bytesIntoKeyValueData < bytesOfKeyValueData; ) {
const keyAndValueByteSize = dataView.getUint32(FILE_HEADER_SIZE + bytesIntoKeyValueData, littleEndian), keyAndValueByteOffset = FILE_HEADER_SIZE + bytesIntoKeyValueData + 4, valuePadding = 3 - (keyAndValueByteSize + 3) % 4;
if (keyAndValueByteSize === 0 || keyAndValueByteSize > bytesOfKeyValueData - bytesIntoKeyValueData) {
console.error("KTXLoader: keyAndValueByteSize out of bounds");
break;
}
let keyNulByte = 0;
for (; keyNulByte < keyAndValueByteSize && dataView.getUint8(keyAndValueByteOffset + keyNulByte) !== 0; keyNulByte++)
;
if (keyNulByte === -1) {
console.error("KTXLoader: Failed to find null byte terminating kvData key");
break;
}
const key = new TextDecoder().decode(
new Uint8Array(dataView.buffer, keyAndValueByteOffset, keyNulByte)
), value = new DataView(
dataView.buffer,
keyAndValueByteOffset + keyNulByte + 1,
keyAndValueByteSize - keyNulByte - 1
);
kvData.set(key, value), bytesIntoKeyValueData += 4 + keyAndValueByteSize + valuePadding;
}
return kvData;
}
exports.FORMATS_TO_COMPONENTS = FORMATS_TO_COMPONENTS;
exports.TYPES_TO_BYTES_PER_COMPONENT = TYPES_TO_BYTES_PER_COMPONENT;
exports.TYPES_TO_BYTES_PER_PIXEL = TYPES_TO_BYTES_PER_PIXEL;
exports.parseKTX = parseKTX;
//# sourceMappingURL=parseKTX.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,166 @@
import { TYPES, FORMATS, BufferResource } from "@pixi/core";
import { INTERNAL_FORMAT_TO_BYTES_PER_PIXEL } from "../const.mjs";
import "../resources/index.mjs";
import { CompressedTextureResource } from "../resources/CompressedTextureResource.mjs";
const FILE_IDENTIFIER = [171, 75, 84, 88, 32, 49, 49, 187, 13, 10, 26, 10], ENDIANNESS = 67305985, KTX_FIELDS = {
FILE_IDENTIFIER: 0,
ENDIANNESS: 12,
GL_TYPE: 16,
GL_TYPE_SIZE: 20,
GL_FORMAT: 24,
GL_INTERNAL_FORMAT: 28,
GL_BASE_INTERNAL_FORMAT: 32,
PIXEL_WIDTH: 36,
PIXEL_HEIGHT: 40,
PIXEL_DEPTH: 44,
NUMBER_OF_ARRAY_ELEMENTS: 48,
NUMBER_OF_FACES: 52,
NUMBER_OF_MIPMAP_LEVELS: 56,
BYTES_OF_KEY_VALUE_DATA: 60
}, FILE_HEADER_SIZE = 64, TYPES_TO_BYTES_PER_COMPONENT = {
[TYPES.UNSIGNED_BYTE]: 1,
[TYPES.UNSIGNED_SHORT]: 2,
[TYPES.INT]: 4,
[TYPES.UNSIGNED_INT]: 4,
[TYPES.FLOAT]: 4,
[TYPES.HALF_FLOAT]: 8
}, FORMATS_TO_COMPONENTS = {
[FORMATS.RGBA]: 4,
[FORMATS.RGB]: 3,
[FORMATS.RG]: 2,
[FORMATS.RED]: 1,
[FORMATS.LUMINANCE]: 1,
[FORMATS.LUMINANCE_ALPHA]: 2,
[FORMATS.ALPHA]: 1
}, TYPES_TO_BYTES_PER_PIXEL = {
[TYPES.UNSIGNED_SHORT_4_4_4_4]: 2,
[TYPES.UNSIGNED_SHORT_5_5_5_1]: 2,
[TYPES.UNSIGNED_SHORT_5_6_5]: 2
};
function parseKTX(url, arrayBuffer, loadKeyValueData = !1) {
const dataView = new DataView(arrayBuffer);
if (!validate(url, dataView))
return null;
const littleEndian = dataView.getUint32(KTX_FIELDS.ENDIANNESS, !0) === ENDIANNESS, glType = dataView.getUint32(KTX_FIELDS.GL_TYPE, littleEndian), glFormat = dataView.getUint32(KTX_FIELDS.GL_FORMAT, littleEndian), glInternalFormat = dataView.getUint32(KTX_FIELDS.GL_INTERNAL_FORMAT, littleEndian), pixelWidth = dataView.getUint32(KTX_FIELDS.PIXEL_WIDTH, littleEndian), pixelHeight = dataView.getUint32(KTX_FIELDS.PIXEL_HEIGHT, littleEndian) || 1, pixelDepth = dataView.getUint32(KTX_FIELDS.PIXEL_DEPTH, littleEndian) || 1, numberOfArrayElements = dataView.getUint32(KTX_FIELDS.NUMBER_OF_ARRAY_ELEMENTS, littleEndian) || 1, numberOfFaces = dataView.getUint32(KTX_FIELDS.NUMBER_OF_FACES, littleEndian), numberOfMipmapLevels = dataView.getUint32(KTX_FIELDS.NUMBER_OF_MIPMAP_LEVELS, littleEndian), bytesOfKeyValueData = dataView.getUint32(KTX_FIELDS.BYTES_OF_KEY_VALUE_DATA, littleEndian);
if (pixelHeight === 0 || pixelDepth !== 1)
throw new Error("Only 2D textures are supported");
if (numberOfFaces !== 1)
throw new Error("CubeTextures are not supported by KTXLoader yet!");
if (numberOfArrayElements !== 1)
throw new Error("WebGL does not support array textures");
const blockWidth = 4, blockHeight = 4, alignedWidth = pixelWidth + 3 & -4, alignedHeight = pixelHeight + 3 & -4, imageBuffers = new Array(numberOfArrayElements);
let imagePixels = pixelWidth * pixelHeight;
glType === 0 && (imagePixels = alignedWidth * alignedHeight);
let imagePixelByteSize;
if (glType !== 0 ? TYPES_TO_BYTES_PER_COMPONENT[glType] ? imagePixelByteSize = TYPES_TO_BYTES_PER_COMPONENT[glType] * FORMATS_TO_COMPONENTS[glFormat] : imagePixelByteSize = TYPES_TO_BYTES_PER_PIXEL[glType] : imagePixelByteSize = INTERNAL_FORMAT_TO_BYTES_PER_PIXEL[glInternalFormat], imagePixelByteSize === void 0)
throw new Error("Unable to resolve the pixel format stored in the *.ktx file!");
const kvData = loadKeyValueData ? parseKvData(dataView, bytesOfKeyValueData, littleEndian) : null;
let mipByteSize = imagePixels * imagePixelByteSize, mipWidth = pixelWidth, mipHeight = pixelHeight, alignedMipWidth = alignedWidth, alignedMipHeight = alignedHeight, imageOffset = FILE_HEADER_SIZE + bytesOfKeyValueData;
for (let mipmapLevel = 0; mipmapLevel < numberOfMipmapLevels; mipmapLevel++) {
const imageSize = dataView.getUint32(imageOffset, littleEndian);
let elementOffset = imageOffset + 4;
for (let arrayElement = 0; arrayElement < numberOfArrayElements; arrayElement++) {
let mips = imageBuffers[arrayElement];
mips || (mips = imageBuffers[arrayElement] = new Array(numberOfMipmapLevels)), mips[mipmapLevel] = {
levelID: mipmapLevel,
// don't align mipWidth when texture not compressed! (glType not zero)
levelWidth: numberOfMipmapLevels > 1 || glType !== 0 ? mipWidth : alignedMipWidth,
levelHeight: numberOfMipmapLevels > 1 || glType !== 0 ? mipHeight : alignedMipHeight,
levelBuffer: new Uint8Array(arrayBuffer, elementOffset, mipByteSize)
}, elementOffset += mipByteSize;
}
imageOffset += imageSize + 4, imageOffset = imageOffset % 4 !== 0 ? imageOffset + 4 - imageOffset % 4 : imageOffset, mipWidth = mipWidth >> 1 || 1, mipHeight = mipHeight >> 1 || 1, alignedMipWidth = mipWidth + blockWidth - 1 & ~(blockWidth - 1), alignedMipHeight = mipHeight + blockHeight - 1 & ~(blockHeight - 1), mipByteSize = alignedMipWidth * alignedMipHeight * imagePixelByteSize;
}
return glType !== 0 ? {
uncompressed: imageBuffers.map((levelBuffers) => {
let buffer = levelBuffers[0].levelBuffer, convertToInt = !1;
return glType === TYPES.FLOAT ? buffer = new Float32Array(
levelBuffers[0].levelBuffer.buffer,
levelBuffers[0].levelBuffer.byteOffset,
levelBuffers[0].levelBuffer.byteLength / 4
) : glType === TYPES.UNSIGNED_INT ? (convertToInt = !0, buffer = new Uint32Array(
levelBuffers[0].levelBuffer.buffer,
levelBuffers[0].levelBuffer.byteOffset,
levelBuffers[0].levelBuffer.byteLength / 4
)) : glType === TYPES.INT && (convertToInt = !0, buffer = new Int32Array(
levelBuffers[0].levelBuffer.buffer,
levelBuffers[0].levelBuffer.byteOffset,
levelBuffers[0].levelBuffer.byteLength / 4
)), {
resource: new BufferResource(
buffer,
{
width: levelBuffers[0].levelWidth,
height: levelBuffers[0].levelHeight
}
),
type: glType,
format: convertToInt ? convertFormatToInteger(glFormat) : glFormat
};
}),
kvData
} : {
compressed: imageBuffers.map((levelBuffers) => new CompressedTextureResource(null, {
format: glInternalFormat,
width: pixelWidth,
height: pixelHeight,
levels: numberOfMipmapLevels,
levelBuffers
})),
kvData
};
}
function validate(url, dataView) {
for (let i = 0; i < FILE_IDENTIFIER.length; i++)
if (dataView.getUint8(i) !== FILE_IDENTIFIER[i])
return console.error(`${url} is not a valid *.ktx file!`), !1;
return !0;
}
function convertFormatToInteger(format) {
switch (format) {
case FORMATS.RGBA:
return FORMATS.RGBA_INTEGER;
case FORMATS.RGB:
return FORMATS.RGB_INTEGER;
case FORMATS.RG:
return FORMATS.RG_INTEGER;
case FORMATS.RED:
return FORMATS.RED_INTEGER;
default:
return format;
}
}
function parseKvData(dataView, bytesOfKeyValueData, littleEndian) {
const kvData = /* @__PURE__ */ new Map();
let bytesIntoKeyValueData = 0;
for (; bytesIntoKeyValueData < bytesOfKeyValueData; ) {
const keyAndValueByteSize = dataView.getUint32(FILE_HEADER_SIZE + bytesIntoKeyValueData, littleEndian), keyAndValueByteOffset = FILE_HEADER_SIZE + bytesIntoKeyValueData + 4, valuePadding = 3 - (keyAndValueByteSize + 3) % 4;
if (keyAndValueByteSize === 0 || keyAndValueByteSize > bytesOfKeyValueData - bytesIntoKeyValueData) {
console.error("KTXLoader: keyAndValueByteSize out of bounds");
break;
}
let keyNulByte = 0;
for (; keyNulByte < keyAndValueByteSize && dataView.getUint8(keyAndValueByteOffset + keyNulByte) !== 0; keyNulByte++)
;
if (keyNulByte === -1) {
console.error("KTXLoader: Failed to find null byte terminating kvData key");
break;
}
const key = new TextDecoder().decode(
new Uint8Array(dataView.buffer, keyAndValueByteOffset, keyNulByte)
), value = new DataView(
dataView.buffer,
keyAndValueByteOffset + keyNulByte + 1,
keyAndValueByteSize - keyNulByte - 1
);
kvData.set(key, value), bytesIntoKeyValueData += 4 + keyAndValueByteSize + valuePadding;
}
return kvData;
}
export {
FORMATS_TO_COMPONENTS,
TYPES_TO_BYTES_PER_COMPONENT,
TYPES_TO_BYTES_PER_PIXEL,
parseKTX
};
//# sourceMappingURL=parseKTX.mjs.map

File diff suppressed because one or more lines are too long