babylon.tools.dds.js 9.6 KB

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