DDSLoader.js 6.2 KB

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