DDSLoader.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.DDSLoader = function () {
  5. this._parser = THREE.DDSLoader.parse;
  6. };
  7. THREE.DDSLoader.prototype = Object.create( THREE.CompressedTextureLoader.prototype );
  8. THREE.DDSLoader.parse = function ( buffer, loadMipmaps ) {
  9. var dds = { mipmaps: [], width: 0, height: 0, format: null, mipmapCount: 1 };
  10. // Adapted from @toji's DDS utils
  11. // https://github.com/toji/webgl-texture-utils/blob/master/texture-util/dds.js
  12. // All values and structures referenced from:
  13. // http://msdn.microsoft.com/en-us/library/bb943991.aspx/
  14. var DDS_MAGIC = 0x20534444;
  15. var DDSD_CAPS = 0x1,
  16. DDSD_HEIGHT = 0x2,
  17. DDSD_WIDTH = 0x4,
  18. DDSD_PITCH = 0x8,
  19. DDSD_PIXELFORMAT = 0x1000,
  20. DDSD_MIPMAPCOUNT = 0x20000,
  21. DDSD_LINEARSIZE = 0x80000,
  22. DDSD_DEPTH = 0x800000;
  23. var DDSCAPS_COMPLEX = 0x8,
  24. DDSCAPS_MIPMAP = 0x400000,
  25. DDSCAPS_TEXTURE = 0x1000;
  26. var DDSCAPS2_CUBEMAP = 0x200,
  27. DDSCAPS2_CUBEMAP_POSITIVEX = 0x400,
  28. DDSCAPS2_CUBEMAP_NEGATIVEX = 0x800,
  29. DDSCAPS2_CUBEMAP_POSITIVEY = 0x1000,
  30. DDSCAPS2_CUBEMAP_NEGATIVEY = 0x2000,
  31. DDSCAPS2_CUBEMAP_POSITIVEZ = 0x4000,
  32. DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x8000,
  33. DDSCAPS2_VOLUME = 0x200000;
  34. var DDPF_ALPHAPIXELS = 0x1,
  35. DDPF_ALPHA = 0x2,
  36. DDPF_FOURCC = 0x4,
  37. DDPF_RGB = 0x40,
  38. DDPF_YUV = 0x200,
  39. DDPF_LUMINANCE = 0x20000;
  40. function fourCCToInt32( value ) {
  41. return value.charCodeAt(0) +
  42. (value.charCodeAt(1) << 8) +
  43. (value.charCodeAt(2) << 16) +
  44. (value.charCodeAt(3) << 24);
  45. }
  46. function int32ToFourCC( value ) {
  47. return String.fromCharCode(
  48. value & 0xff,
  49. (value >> 8) & 0xff,
  50. (value >> 16) & 0xff,
  51. (value >> 24) & 0xff
  52. );
  53. }
  54. function loadARGBMip( buffer, dataOffset, width, height ) {
  55. var dataLength = width*height*4;
  56. var srcBuffer = new Uint8Array( buffer, dataOffset, dataLength );
  57. var byteArray = new Uint8Array( dataLength );
  58. var dst = 0;
  59. var src = 0;
  60. for ( var y = 0; y < height; y++ ) {
  61. for ( var x = 0; x < width; x++ ) {
  62. var b = srcBuffer[src]; src++;
  63. var g = srcBuffer[src]; src++;
  64. var r = srcBuffer[src]; src++;
  65. var a = srcBuffer[src]; src++;
  66. byteArray[dst] = r; dst++; //r
  67. byteArray[dst] = g; dst++; //g
  68. byteArray[dst] = b; dst++; //b
  69. byteArray[dst] = a; dst++; //a
  70. }
  71. }
  72. return byteArray;
  73. }
  74. var FOURCC_DXT1 = fourCCToInt32("DXT1");
  75. var FOURCC_DXT3 = fourCCToInt32("DXT3");
  76. var FOURCC_DXT5 = fourCCToInt32("DXT5");
  77. var headerLengthInt = 31; // The header length in 32 bit ints
  78. // Offsets into the header array
  79. var off_magic = 0;
  80. var off_size = 1;
  81. var off_flags = 2;
  82. var off_height = 3;
  83. var off_width = 4;
  84. var off_mipmapCount = 7;
  85. var off_pfFlags = 20;
  86. var off_pfFourCC = 21;
  87. var off_RGBBitCount = 22;
  88. var off_RBitMask = 23;
  89. var off_GBitMask = 24;
  90. var off_BBitMask = 25;
  91. var off_ABitMask = 26;
  92. var off_caps = 27;
  93. var off_caps2 = 28;
  94. var off_caps3 = 29;
  95. var off_caps4 = 30;
  96. // Parse header
  97. var header = new Int32Array( buffer, 0, headerLengthInt );
  98. if ( header[ off_magic ] !== DDS_MAGIC ) {
  99. console.error( 'THREE.DDSLoader.parse: Invalid magic number in DDS header.' );
  100. return dds;
  101. }
  102. if ( ! header[ off_pfFlags ] & DDPF_FOURCC ) {
  103. console.error( 'THREE.DDSLoader.parse: Unsupported format, must contain a FourCC code.' );
  104. return dds;
  105. }
  106. var blockBytes;
  107. var fourCC = header[ off_pfFourCC ];
  108. var isRGBAUncompressed = false;
  109. switch ( fourCC ) {
  110. case FOURCC_DXT1:
  111. blockBytes = 8;
  112. dds.format = THREE.RGB_S3TC_DXT1_Format;
  113. break;
  114. case FOURCC_DXT3:
  115. blockBytes = 16;
  116. dds.format = THREE.RGBA_S3TC_DXT3_Format;
  117. break;
  118. case FOURCC_DXT5:
  119. blockBytes = 16;
  120. dds.format = THREE.RGBA_S3TC_DXT5_Format;
  121. break;
  122. default:
  123. if( header[off_RGBBitCount] ==32
  124. && header[off_RBitMask]&0xff0000
  125. && header[off_GBitMask]&0xff00
  126. && header[off_BBitMask]&0xff
  127. && header[off_ABitMask]&0xff000000 ) {
  128. isRGBAUncompressed = true;
  129. blockBytes = 64;
  130. dds.format = THREE.RGBAFormat;
  131. } else {
  132. console.error( 'THREE.DDSLoader.parse: Unsupported FourCC code ', int32ToFourCC( fourCC ) );
  133. return dds;
  134. }
  135. }
  136. dds.mipmapCount = 1;
  137. if ( header[ off_flags ] & DDSD_MIPMAPCOUNT && loadMipmaps !== false ) {
  138. dds.mipmapCount = Math.max( 1, header[ off_mipmapCount ] );
  139. }
  140. //TODO: Verify that all faces of the cubemap are present with DDSCAPS2_CUBEMAP_POSITIVEX, etc.
  141. dds.isCubemap = header[ off_caps2 ] & DDSCAPS2_CUBEMAP ? true : false;
  142. dds.width = header[ off_width ];
  143. dds.height = header[ off_height ];
  144. var dataOffset = header[ off_size ] + 4;
  145. // Extract mipmaps buffers
  146. var width = dds.width;
  147. var height = dds.height;
  148. var faces = dds.isCubemap ? 6 : 1;
  149. for ( var face = 0; face < faces; face ++ ) {
  150. for ( var i = 0; i < dds.mipmapCount; i ++ ) {
  151. if( isRGBAUncompressed ) {
  152. var byteArray = loadARGBMip( buffer, dataOffset, width, height );
  153. var dataLength = byteArray.length;
  154. } else {
  155. var dataLength = Math.max( 4, width ) / 4 * Math.max( 4, height ) / 4 * blockBytes;
  156. var byteArray = new Uint8Array( buffer, dataOffset, dataLength );
  157. }
  158. var mipmap = { "data": byteArray, "width": width, "height": height };
  159. dds.mipmaps.push( mipmap );
  160. dataOffset += dataLength;
  161. width = Math.max( width * 0.5, 1 );
  162. height = Math.max( height * 0.5, 1 );
  163. }
  164. width = dds.width;
  165. height = dds.height;
  166. }
  167. return dds;
  168. };