123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- var BABYLON = BABYLON || {};
- (function () {
- BABYLON.Tools = BABYLON.Tools || {};
- // Based on https://github.com/toji/webgl-texture-utils/blob/master/texture-util/dds.js
- // All values and structures referenced from:
- // http://msdn.microsoft.com/en-us/library/bb943991.aspx/
- var DDS_MAGIC = 0x20534444;
- var DDSD_CAPS = 0x1,
- DDSD_HEIGHT = 0x2,
- DDSD_WIDTH = 0x4,
- DDSD_PITCH = 0x8,
- DDSD_PIXELFORMAT = 0x1000,
- DDSD_MIPMAPCOUNT = 0x20000,
- DDSD_LINEARSIZE = 0x80000,
- DDSD_DEPTH = 0x800000;
- var DDSCAPS_COMPLEX = 0x8,
- DDSCAPS_MIPMAP = 0x400000,
- DDSCAPS_TEXTURE = 0x1000;
- var DDSCAPS2_CUBEMAP = 0x200,
- DDSCAPS2_CUBEMAP_POSITIVEX = 0x400,
- DDSCAPS2_CUBEMAP_NEGATIVEX = 0x800,
- DDSCAPS2_CUBEMAP_POSITIVEY = 0x1000,
- DDSCAPS2_CUBEMAP_NEGATIVEY = 0x2000,
- DDSCAPS2_CUBEMAP_POSITIVEZ = 0x4000,
- DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x8000,
- DDSCAPS2_VOLUME = 0x200000;
- var DDPF_ALPHAPIXELS = 0x1,
- DDPF_ALPHA = 0x2,
- DDPF_FOURCC = 0x4,
- DDPF_RGB = 0x40,
- DDPF_YUV = 0x200,
- DDPF_LUMINANCE = 0x20000;
- function fourCCToInt32(value) {
- return value.charCodeAt(0) +
- (value.charCodeAt(1) << 8) +
- (value.charCodeAt(2) << 16) +
- (value.charCodeAt(3) << 24);
- }
- function int32ToFourCC(value) {
- return String.fromCharCode(
- value & 0xff,
- (value >> 8) & 0xff,
- (value >> 16) & 0xff,
- (value >> 24) & 0xff
- );
- }
- var FOURCC_DXT1 = fourCCToInt32("DXT1");
- var FOURCC_DXT3 = fourCCToInt32("DXT3");
- var FOURCC_DXT5 = fourCCToInt32("DXT5");
- var headerLengthInt = 31; // The header length in 32 bit ints
- // Offsets into the header array
- 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;
- function dxtToRgb565(src, src16Offset, width, height) {
- var c = new Uint16Array(4);
- var dst = new Uint16Array(width * height);
- var nWords = (width * height) / 4;
- var m = 0;
- var dstI = 0;
- var i = 0;
- var r0 = 0, g0 = 0, b0 = 0, r1 = 0, g1 = 0, b1 = 0;
- var blockWidth = width / 4;
- var blockHeight = height / 4;
- for (var blockY = 0; blockY < blockHeight; blockY++) {
- for (var blockX = 0; blockX < blockWidth; blockX++) {
- i = src16Offset + 4 * (blockY * blockWidth + blockX);
- c[0] = src[i];
- c[1] = src[i + 1];
- r0 = c[0] & 0x1f;
- g0 = c[0] & 0x7e0;
- b0 = c[0] & 0xf800;
- r1 = c[1] & 0x1f;
- g1 = c[1] & 0x7e0;
- b1 = c[1] & 0xf800;
- // Interpolate between c0 and c1 to get c2 and c3.
- // Note that we approximate 1/3 as 3/8 and 2/3 as 5/8 for
- // speed. This also appears to be what the hardware DXT
- // decoder in many GPUs does :)
- c[2] = ((5 * r0 + 3 * r1) >> 3)
- | (((5 * g0 + 3 * g1) >> 3) & 0x7e0)
- | (((5 * b0 + 3 * b1) >> 3) & 0xf800);
- c[3] = ((5 * r1 + 3 * r0) >> 3)
- | (((5 * g1 + 3 * g0) >> 3) & 0x7e0)
- | (((5 * b1 + 3 * b0) >> 3) & 0xf800);
- m = src[i + 2];
- dstI = (blockY * 4) * width + blockX * 4;
- dst[dstI] = c[m & 0x3];
- dst[dstI + 1] = c[(m >> 2) & 0x3];
- dst[dstI + 2] = c[(m >> 4) & 0x3];
- dst[dstI + 3] = c[(m >> 6) & 0x3];
- dstI += width;
- dst[dstI] = c[(m >> 8) & 0x3];
- dst[dstI + 1] = c[(m >> 10) & 0x3];
- dst[dstI + 2] = c[(m >> 12) & 0x3];
- dst[dstI + 3] = c[(m >> 14)];
- m = src[i + 3];
- dstI += width;
- dst[dstI] = c[m & 0x3];
- dst[dstI + 1] = c[(m >> 2) & 0x3];
- dst[dstI + 2] = c[(m >> 4) & 0x3];
- dst[dstI + 3] = c[(m >> 6) & 0x3];
- dstI += width;
- dst[dstI] = c[(m >> 8) & 0x3];
- dst[dstI + 1] = c[(m >> 10) & 0x3];
- dst[dstI + 2] = c[(m >> 12) & 0x3];
- dst[dstI + 3] = c[(m >> 14)];
- }
- }
- return dst;
- }
- function uploadDDSLevels(gl, ext, arrayBuffer) {
- var header = new Int32Array(arrayBuffer, 0, headerLengthInt),
- fourCC, blockBytes, internalFormat,
- width, height, dataLength, dataOffset,
- rgb565Data, byteArray, mipmapCount, i;
- if (header[off_magic] != DDS_MAGIC) {
- console.error("Invalid magic number in DDS header");
- return 0;
- }
- if (!header[off_pfFlags] & DDPF_FOURCC) {
- console.error("Unsupported format, must contain a FourCC code");
- return 0;
- }
- fourCC = header[off_pfFourCC];
- switch (fourCC) {
- case FOURCC_DXT1:
- blockBytes = 8;
- internalFormat = ext ? ext.COMPRESSED_RGB_S3TC_DXT1_EXT : null;
- break;
- case FOURCC_DXT3:
- blockBytes = 16;
- internalFormat = ext ? ext.COMPRESSED_RGBA_S3TC_DXT3_EXT : null;
- break;
- case FOURCC_DXT5:
- blockBytes = 16;
- internalFormat = ext ? ext.COMPRESSED_RGBA_S3TC_DXT5_EXT : null;
- break;
- default:
- console.error("Unsupported FourCC code:", int32ToFourCC(fourCC));
- return null;
- }
- mipmapCount = 1;
- if (header[off_flags] & DDSD_MIPMAPCOUNT && loadMipmaps !== false) {
- mipmapCount = Math.max(1, header[off_mipmapCount]);
- }
- width = header[off_width];
- height = header[off_height];
- dataOffset = header[off_size] + 4;
- if (ext) {
- for (i = 0; i < mipmapCount; ++i) {
- dataLength = Math.max(4, width) / 4 * Math.max(4, height) / 4 * blockBytes;
- byteArray = new Uint8Array(arrayBuffer, dataOffset, dataLength);
- gl.compressedTexImage2D(gl.TEXTURE_2D, i, internalFormat, width, height, 0, byteArray);
- dataOffset += dataLength;
- width *= 0.5;
- height *= 0.5;
- }
- } else {
- if (fourCC == FOURCC_DXT1) {
- dataLength = Math.max(4, width) / 4 * Math.max(4, height) / 4 * blockBytes;
- byteArray = new Uint16Array(arrayBuffer);
- rgb565Data = dxtToRgb565(byteArray, dataOffset / 2, width, height);
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, width, height, 0, gl.RGB, gl.UNSIGNED_SHORT_5_6_5, rgb565Data);
- if (loadMipmaps) {
- gl.generateMipmap(gl.TEXTURE_2D);
- }
- } else {
- console.error("No manual decoder for", int32ToFourCC(fourCC), "and no native support");
- return 0;
- }
- }
- return mipmapCount;
- }
- BABYLON.Tools.LoadDDSTexture = function(gl, ext, data) {
- return uploadDDSLevels(gl, ext, data);
- };
- })();
|