babylon.tools.dds.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Internals;
  4. (function (Internals) {
  5. // Based on demo done by Brandon Jones - http://media.tojicode.com/webgl-samples/dds.html
  6. // All values and structures referenced from:
  7. // http://msdn.microsoft.com/en-us/library/bb943991.aspx/
  8. var DDS_MAGIC = 0x20534444;
  9. 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;
  10. var DDSCAPS_COMPLEX = 0x8, DDSCAPS_MIPMAP = 0x400000, DDSCAPS_TEXTURE = 0x1000;
  11. 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;
  12. var DDPF_ALPHAPIXELS = 0x1, DDPF_ALPHA = 0x2, DDPF_FOURCC = 0x4, DDPF_RGB = 0x40, DDPF_YUV = 0x200, DDPF_LUMINANCE = 0x20000;
  13. function FourCCToInt32(value) {
  14. return value.charCodeAt(0) +
  15. (value.charCodeAt(1) << 8) +
  16. (value.charCodeAt(2) << 16) +
  17. (value.charCodeAt(3) << 24);
  18. }
  19. function Int32ToFourCC(value) {
  20. return String.fromCharCode(value & 0xff, (value >> 8) & 0xff, (value >> 16) & 0xff, (value >> 24) & 0xff);
  21. }
  22. var FOURCC_DXT1 = FourCCToInt32("DXT1");
  23. var FOURCC_DXT3 = FourCCToInt32("DXT3");
  24. var FOURCC_DXT5 = FourCCToInt32("DXT5");
  25. var headerLengthInt = 31; // The header length in 32 bit ints
  26. // Offsets into the header array
  27. var off_magic = 0;
  28. var off_size = 1;
  29. var off_flags = 2;
  30. var off_height = 3;
  31. var off_width = 4;
  32. var off_mipmapCount = 7;
  33. var off_pfFlags = 20;
  34. var off_pfFourCC = 21;
  35. var off_RGBbpp = 22;
  36. var off_RMask = 23;
  37. var off_GMask = 24;
  38. var off_BMask = 25;
  39. var off_AMask = 26;
  40. var off_caps1 = 27;
  41. var off_caps2 = 28;
  42. ;
  43. var DDSTools = (function () {
  44. function DDSTools() {
  45. }
  46. DDSTools.GetDDSInfo = function (arrayBuffer) {
  47. var header = new Int32Array(arrayBuffer, 0, headerLengthInt);
  48. var mipmapCount = 1;
  49. if (header[off_flags] & DDSD_MIPMAPCOUNT) {
  50. mipmapCount = Math.max(1, header[off_mipmapCount]);
  51. }
  52. return {
  53. width: header[off_width],
  54. height: header[off_height],
  55. mipmapCount: mipmapCount,
  56. isFourCC: (header[off_pfFlags] & DDPF_FOURCC) === DDPF_FOURCC,
  57. isRGB: (header[off_pfFlags] & DDPF_RGB) === DDPF_RGB,
  58. isLuminance: (header[off_pfFlags] & DDPF_LUMINANCE) === DDPF_LUMINANCE,
  59. isCube: (header[off_caps2] & DDSCAPS2_CUBEMAP) === DDSCAPS2_CUBEMAP
  60. };
  61. };
  62. DDSTools.GetRGBAArrayBuffer = function (width, height, dataOffset, dataLength, arrayBuffer) {
  63. var byteArray = new Uint8Array(dataLength);
  64. var srcData = new Uint8Array(arrayBuffer);
  65. var index = 0;
  66. for (var y = height - 1; y >= 0; y--) {
  67. for (var x = 0; x < width; x++) {
  68. var srcPos = dataOffset + (x + y * width) * 4;
  69. byteArray[index + 2] = srcData[srcPos];
  70. byteArray[index + 1] = srcData[srcPos + 1];
  71. byteArray[index] = srcData[srcPos + 2];
  72. byteArray[index + 3] = srcData[srcPos + 3];
  73. index += 4;
  74. }
  75. }
  76. return byteArray;
  77. };
  78. DDSTools.GetRGBArrayBuffer = function (width, height, dataOffset, dataLength, arrayBuffer) {
  79. var byteArray = new Uint8Array(dataLength);
  80. var srcData = new Uint8Array(arrayBuffer);
  81. var index = 0;
  82. for (var y = height - 1; y >= 0; y--) {
  83. for (var x = 0; x < width; x++) {
  84. var srcPos = dataOffset + (x + y * width) * 3;
  85. byteArray[index + 2] = srcData[srcPos];
  86. byteArray[index + 1] = srcData[srcPos + 1];
  87. byteArray[index] = srcData[srcPos + 2];
  88. index += 3;
  89. }
  90. }
  91. return byteArray;
  92. };
  93. DDSTools.GetLuminanceArrayBuffer = function (width, height, dataOffset, dataLength, arrayBuffer) {
  94. var byteArray = new Uint8Array(dataLength);
  95. var srcData = new Uint8Array(arrayBuffer);
  96. var index = 0;
  97. for (var y = height - 1; y >= 0; y--) {
  98. for (var x = 0; x < width; x++) {
  99. var srcPos = dataOffset + (x + y * width);
  100. byteArray[index] = srcData[srcPos];
  101. index++;
  102. }
  103. }
  104. return byteArray;
  105. };
  106. DDSTools.UploadDDSLevels = function (gl, ext, arrayBuffer, info, loadMipmaps, faces) {
  107. var header = new Int32Array(arrayBuffer, 0, headerLengthInt), fourCC, blockBytes, internalFormat, width, height, dataLength, dataOffset, byteArray, mipmapCount, i;
  108. if (header[off_magic] != DDS_MAGIC) {
  109. BABYLON.Tools.Error("Invalid magic number in DDS header");
  110. return;
  111. }
  112. if (!info.isFourCC && !info.isRGB && !info.isLuminance) {
  113. BABYLON.Tools.Error("Unsupported format, must contain a FourCC, RGB or LUMINANCE code");
  114. return;
  115. }
  116. if (info.isFourCC) {
  117. fourCC = header[off_pfFourCC];
  118. switch (fourCC) {
  119. case FOURCC_DXT1:
  120. blockBytes = 8;
  121. internalFormat = ext.COMPRESSED_RGBA_S3TC_DXT1_EXT;
  122. break;
  123. case FOURCC_DXT3:
  124. blockBytes = 16;
  125. internalFormat = ext.COMPRESSED_RGBA_S3TC_DXT3_EXT;
  126. break;
  127. case FOURCC_DXT5:
  128. blockBytes = 16;
  129. internalFormat = ext.COMPRESSED_RGBA_S3TC_DXT5_EXT;
  130. break;
  131. default:
  132. console.error("Unsupported FourCC code:", Int32ToFourCC(fourCC));
  133. return;
  134. }
  135. }
  136. mipmapCount = 1;
  137. if (header[off_flags] & DDSD_MIPMAPCOUNT && loadMipmaps !== false) {
  138. mipmapCount = Math.max(1, header[off_mipmapCount]);
  139. }
  140. var bpp = header[off_RGBbpp];
  141. for (var face = 0; face < faces; face++) {
  142. var sampler = faces == 1 ? gl.TEXTURE_2D : (gl.TEXTURE_CUBE_MAP_POSITIVE_X + face);
  143. width = header[off_width];
  144. height = header[off_height];
  145. dataOffset = header[off_size] + 4;
  146. for (i = 0; i < mipmapCount; ++i) {
  147. if (info.isRGB) {
  148. if (bpp == 24) {
  149. dataLength = width * height * 3;
  150. byteArray = DDSTools.GetRGBArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer);
  151. gl.texImage2D(sampler, i, gl.RGB, width, height, 0, gl.RGB, gl.UNSIGNED_BYTE, byteArray);
  152. }
  153. else {
  154. dataLength = width * height * 4;
  155. byteArray = DDSTools.GetRGBAArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer);
  156. gl.texImage2D(sampler, i, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, byteArray);
  157. }
  158. }
  159. else if (info.isLuminance) {
  160. var unpackAlignment = gl.getParameter(gl.UNPACK_ALIGNMENT);
  161. var unpaddedRowSize = width;
  162. var paddedRowSize = Math.floor((width + unpackAlignment - 1) / unpackAlignment) * unpackAlignment;
  163. dataLength = paddedRowSize * (height - 1) + unpaddedRowSize;
  164. byteArray = DDSTools.GetLuminanceArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer);
  165. gl.texImage2D(sampler, i, gl.LUMINANCE, width, height, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, byteArray);
  166. }
  167. else {
  168. dataLength = Math.max(4, width) / 4 * Math.max(4, height) / 4 * blockBytes;
  169. byteArray = new Uint8Array(arrayBuffer, dataOffset, dataLength);
  170. gl.compressedTexImage2D(sampler, i, internalFormat, width, height, 0, byteArray);
  171. }
  172. dataOffset += dataLength;
  173. width *= 0.5;
  174. height *= 0.5;
  175. width = Math.max(1.0, width);
  176. height = Math.max(1.0, height);
  177. }
  178. }
  179. };
  180. return DDSTools;
  181. })();
  182. Internals.DDSTools = DDSTools;
  183. })(Internals = BABYLON.Internals || (BABYLON.Internals = {}));
  184. })(BABYLON || (BABYLON = {}));
  185. //# sourceMappingURL=babylon.tools.dds.js.map