babylon.tools.dds.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 DDSTools = (function () {
  32. function DDSTools() {
  33. }
  34. DDSTools.GetDDSInfo = function (arrayBuffer) {
  35. var header = new Int32Array(arrayBuffer, 0, headerLengthInt);
  36. var mipmapCount = 1;
  37. if (header[off_flags] & DDSD_MIPMAPCOUNT) {
  38. mipmapCount = Math.max(1, header[off_mipmapCount]);
  39. }
  40. return {
  41. width: header[off_width],
  42. height: header[off_height],
  43. mipmapCount: mipmapCount
  44. };
  45. };
  46. DDSTools.UploadDDSLevels = function (gl, ext, arrayBuffer, loadMipmaps) {
  47. var header = new Int32Array(arrayBuffer, 0, headerLengthInt), fourCC, blockBytes, internalFormat, width, height, dataLength, dataOffset, byteArray, mipmapCount, i;
  48. if (header[off_magic] != DDS_MAGIC) {
  49. console.error("Invalid magic number in DDS header");
  50. return;
  51. }
  52. if ((header[off_pfFlags] & DDPF_FOURCC) !== DDPF_FOURCC) {
  53. console.error("Unsupported format, must contain a FourCC code");
  54. return;
  55. }
  56. fourCC = header[off_pfFourCC];
  57. switch (fourCC) {
  58. case FOURCC_DXT1:
  59. blockBytes = 8;
  60. internalFormat = ext.COMPRESSED_RGBA_S3TC_DXT1_EXT;
  61. break;
  62. case FOURCC_DXT3:
  63. blockBytes = 16;
  64. internalFormat = ext.COMPRESSED_RGBA_S3TC_DXT3_EXT;
  65. break;
  66. case FOURCC_DXT5:
  67. blockBytes = 16;
  68. internalFormat = ext.COMPRESSED_RGBA_S3TC_DXT5_EXT;
  69. break;
  70. default:
  71. console.error("Unsupported FourCC code:", Int32ToFourCC(fourCC));
  72. return;
  73. }
  74. mipmapCount = 1;
  75. if (header[off_flags] & DDSD_MIPMAPCOUNT && loadMipmaps !== false) {
  76. mipmapCount = Math.max(1, header[off_mipmapCount]);
  77. }
  78. width = header[off_width];
  79. height = header[off_height];
  80. dataOffset = header[off_size] + 4;
  81. for (i = 0; i < mipmapCount; ++i) {
  82. dataLength = Math.max(4, width) / 4 * Math.max(4, height) / 4 * blockBytes;
  83. byteArray = new Uint8Array(arrayBuffer, dataOffset, dataLength);
  84. gl.compressedTexImage2D(gl.TEXTURE_2D, i, internalFormat, width, height, 0, byteArray);
  85. dataOffset += dataLength;
  86. width *= 0.5;
  87. height *= 0.5;
  88. }
  89. };
  90. return DDSTools;
  91. })();
  92. Internals.DDSTools = DDSTools;
  93. })(BABYLON.Internals || (BABYLON.Internals = {}));
  94. var Internals = BABYLON.Internals;
  95. })(BABYLON || (BABYLON = {}));
  96. //# sourceMappingURL=babylon.tools.dds.js.map