UNPKG

cione-comp-lib

Version:

`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`

96 lines (95 loc) 2.85 kB
import Texture from "../Texture.mjs"; import Texture2D from "../Texture2D.mjs"; var DDS_MAGIC = 542327876; var DDSD_MIPMAPCOUNT = 131072; var DDSCAPS2_CUBEMAP = 512; var DDPF_FOURCC = 4; function fourCCToInt32(value) { return value.charCodeAt(0) + (value.charCodeAt(1) << 8) + (value.charCodeAt(2) << 16) + (value.charCodeAt(3) << 24); } var headerLengthInt = 31; var FOURCC_DXT1 = fourCCToInt32("DXT1"); var FOURCC_DXT3 = fourCCToInt32("DXT3"); var FOURCC_DXT5 = fourCCToInt32("DXT5"); var off_magic = 0; var off_size = 1; var off_flags = 2; var off_height = 3; var off_width = 4; var off_mipmapCount = 7; var off_pfFlags = 20; var off_pfFourCC = 21; var off_caps2 = 28; var ret = { parse: function(arrayBuffer, out) { var header = new Int32Array(arrayBuffer, 0, headerLengthInt); if (header[off_magic] !== DDS_MAGIC) { return null; } if (!header(off_pfFlags) & DDPF_FOURCC) { return null; } var fourCC = header(off_pfFourCC); var width = header[off_width]; var height = header[off_height]; var isCubeMap = header[off_caps2] & DDSCAPS2_CUBEMAP; var hasMipmap = header[off_flags] & DDSD_MIPMAPCOUNT; var blockBytes, internalFormat; switch (fourCC) { case FOURCC_DXT1: blockBytes = 8; internalFormat = Texture.COMPRESSED_RGB_S3TC_DXT1_EXT; break; case FOURCC_DXT3: blockBytes = 16; internalFormat = Texture.COMPRESSED_RGBA_S3TC_DXT3_EXT; break; case FOURCC_DXT5: blockBytes = 16; internalFormat = Texture.COMPRESSED_RGBA_S3TC_DXT5_EXT; break; default: return null; } var dataOffset = header[off_size] + 4; var faceNumber = isCubeMap ? 6 : 1; var mipmapCount = 1; if (hasMipmap) { mipmapCount = Math.max(1, header[off_mipmapCount]); } var textures = []; for (var f = 0; f < faceNumber; f++) { var _width = width; var _height = height; textures[f] = new Texture2D({ width: _width, height: _height, format: internalFormat }); var mipmaps = []; for (var i = 0; i < mipmapCount; i++) { var dataLength = Math.max(4, _width) / 4 * Math.max(4, _height) / 4 * blockBytes; var byteArray = new Uint8Array(arrayBuffer, dataOffset, dataLength); dataOffset += dataLength; _width *= 0.5; _height *= 0.5; mipmaps[i] = byteArray; } textures[f].pixels = mipmaps[0]; if (hasMipmap) { textures[f].mipmaps = mipmaps; } } if (out) { out.width = textures[0].width; out.height = textures[0].height; out.format = textures[0].format; out.pixels = textures[0].pixels; out.mipmaps = textures[0].mipmaps; } else { return textures[0]; } } }; var dds = ret; export { dds as default };