babylon.tools.dds.js 9.3 KB

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