1 line
8.9 KiB
Plaintext
1 line
8.9 KiB
Plaintext
|
|
{"version":3,"file":"KTXLoader.cjs","sources":["../../src/loaders/KTXLoader.js"],"sourcesContent":["import { CompressedTextureLoader } from 'three'\n\n/**\n * for description see https://www.khronos.org/opengles/sdk/tools/KTX/\n * for file layout see https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/\n *\n * ported from https://github.com/BabylonJS/Babylon.js/blob/master/src/Tools/babylon.khronosTextureContainer.ts\n */\n\nclass KTXLoader extends CompressedTextureLoader {\n constructor(manager) {\n super(manager)\n }\n\n parse(buffer, loadMipmaps) {\n const ktx = new KhronosTextureContainer(buffer, 1)\n\n return {\n mipmaps: ktx.mipmaps(loadMipmaps),\n width: ktx.pixelWidth,\n height: ktx.pixelHeight,\n format: ktx.glInternalFormat,\n isCubemap: ktx.numberOfFaces === 6,\n mipmapCount: ktx.numberOfMipmapLevels,\n }\n }\n}\n\nconst HEADER_LEN = 12 + 13 * 4 // identifier + header elements (not including key value meta-data pairs)\n// load types\nconst COMPRESSED_2D = 0 // uses a gl.compressedTexImage2D()\n//const COMPRESSED_3D = 1; // uses a gl.compressedTexImage3D()\n//const TEX_2D = 2; // uses a gl.texImage2D()\n//const TEX_3D = 3; // uses a gl.texImage3D()\n\nclass KhronosTextureContainer {\n /**\n * @param {ArrayBuffer} arrayBuffer- contents of the KTX container file\n * @param {number} facesExpected- should be either 1 or 6, based whether a cube texture or or\n * @param {boolean} threeDExpected- provision for indicating that data should be a 3D texture, not implemented\n * @param {boolean} textureArrayExpected- provision for indicating that data should be a texture array, not implemented\n */\n constructor(arrayBuffer, facesExpected /*, threeDExpected, textureArrayExpected */) {\n this.arrayBuffer = arrayBuffer\n\n // Test that it is a ktx formatted file, based on the first 12 bytes, character representation is:\n // '´', 'K', 'T', 'X', ' ', '1', '1', 'ª', '\\r', '\\n', '\\x1A', '\\n'\n // 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A\n const identifier = new Uint8Array(this.arrayBuffer, 0, 12)\n if (\n identifier[0] !== 0xab ||\n identifier[1] !== 0x4b ||\n identifier[2] !== 0x54 ||\n identifier[3] !== 0x58 ||\n identifier[4] !== 0x20 ||\n identifier[5] !== 0x31 ||\n identifier[6] !== 0x31 ||\n identifier[7] !== 0xbb ||\n identifier[8] !== 0x0d ||\n identifier[9] !== 0x0a ||\n identifier[10] !== 0x1a ||\n identifier[11] !== 0x0a\n ) {\n console.error('texture missing KTX identifier')\n return\n }\n\n // load the reset of the header in native 32 bit uint\n const dataSize = Uint32Array.BYTES_PER_ELEMENT\n const headerDataView = new DataView(this.arrayBuffer, 12, 13 * dataSize)\n const endianness = headerDataView.getUint32(0, true)\n const littleEndian = endianness === 0x04030201\n\n this.glType = headerDataView.getUint32(1 * dataSize, littleEndian) // must be 0 for compressed textures\n this.glTypeSize = headerDataView.getUint32(2 * dataSize, littleEndian) // must be 1 for compressed textures\n this.glFormat = headerDataView.getUint32(3 * dataSize, littleEndian) // must be 0 for compressed textures\n this.glInternalFormat = headerDataView.getUint32(4 * dataSize, littleEndian) // the value of arg passed to gl.compressedTexImage2D(,,x,,,,)\n this.glBaseInternalFormat = headerDataView.getUint32(5 * dataSize, littleEndian) // specify GL_RGB, GL_RGBA, GL_ALPHA, etc (un-compressed only)\n this.pixelWidth = headerDataView.getUint32(6 * dataSize, littleEndian) // level 0 value of arg passed to gl.compressedTexImage2D(,,,x,,,)\n this.pixelHeight = headerDataView.getUint32(7 * dataSize, littleEndian) // level 0 value of arg passed to gl.compressedTexImage2D(,,,,x,,)\n this.pixelDepth = headerDataView.getUint32(8 * dataSize, littleEndian) // level 0 value of arg passed to gl.compressedTexImage3D(,,,,,x,,)\n this.numberOfArrayElements = headerDataView.getUint32(9 * dataSize, littleEndian) // used for
|