DDSLoader.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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; // const DDSD_CAPS = 0x1;
  18. // const DDSD_HEIGHT = 0x2;
  19. // const DDSD_WIDTH = 0x4;
  20. // const DDSD_PITCH = 0x8;
  21. // const DDSD_PIXELFORMAT = 0x1000;
  22. const DDSD_MIPMAPCOUNT = 0x20000; // const DDSD_LINEARSIZE = 0x80000;
  23. // const DDSD_DEPTH = 0x800000;
  24. // const DDSCAPS_COMPLEX = 0x8;
  25. // const DDSCAPS_MIPMAP = 0x400000;
  26. // const 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; // const DDSCAPS2_VOLUME = 0x200000;
  34. // const DDPF_ALPHAPIXELS = 0x1;
  35. // const DDPF_ALPHA = 0x2;
  36. // const DDPF_FOURCC = 0x4;
  37. // const DDPF_RGB = 0x40;
  38. // const DDPF_YUV = 0x200;
  39. // const DDPF_LUMINANCE = 0x20000;
  40. function fourCCToInt32( value ) {
  41. return value.charCodeAt( 0 ) + ( value.charCodeAt( 1 ) << 8 ) + ( value.charCodeAt( 2 ) << 16 ) + ( value.charCodeAt( 3 ) << 24 );
  42. }
  43. function int32ToFourCC( value ) {
  44. return String.fromCharCode( value & 0xff, value >> 8 & 0xff, value >> 16 & 0xff, value >> 24 & 0xff );
  45. }
  46. function loadARGBMip( buffer, dataOffset, width, height ) {
  47. const dataLength = width * height * 4;
  48. const srcBuffer = new Uint8Array( buffer, dataOffset, dataLength );
  49. const byteArray = new Uint8Array( dataLength );
  50. let dst = 0;
  51. let src = 0;
  52. for ( let y = 0; y < height; y ++ ) {
  53. for ( let x = 0; x < width; x ++ ) {
  54. const b = srcBuffer[ src ];
  55. src ++;
  56. const g = srcBuffer[ src ];
  57. src ++;
  58. const r = srcBuffer[ src ];
  59. src ++;
  60. const a = srcBuffer[ src ];
  61. src ++;
  62. byteArray[ dst ] = r;
  63. dst ++; //r
  64. byteArray[ dst ] = g;
  65. dst ++; //g
  66. byteArray[ dst ] = b;
  67. dst ++; //b
  68. byteArray[ dst ] = a;
  69. dst ++; //a
  70. }
  71. }
  72. return byteArray;
  73. }
  74. const FOURCC_DXT1 = fourCCToInt32( 'DXT1' );
  75. const FOURCC_DXT3 = fourCCToInt32( 'DXT3' );
  76. const FOURCC_DXT5 = fourCCToInt32( 'DXT5' );
  77. const FOURCC_ETC1 = fourCCToInt32( 'ETC1' );
  78. const headerLengthInt = 31; // The header length in 32 bit ints
  79. // Offsets into the header array
  80. const off_magic = 0;
  81. const off_size = 1;
  82. const off_flags = 2;
  83. const off_height = 3;
  84. const off_width = 4;
  85. const off_mipmapCount = 7; // 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; // const off_caps = 27;
  92. const off_caps2 = 28; // const off_caps3 = 29;
  93. // const 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. let blockBytes;
  101. const fourCC = header[ off_pfFourCC ];
  102. let isRGBAUncompressed = false;
  103. switch ( fourCC ) {
  104. case FOURCC_DXT1:
  105. blockBytes = 8;
  106. dds.format = THREE.RGB_S3TC_DXT1_Format;
  107. break;
  108. case FOURCC_DXT3:
  109. blockBytes = 16;
  110. dds.format = THREE.RGBA_S3TC_DXT3_Format;
  111. break;
  112. case FOURCC_DXT5:
  113. blockBytes = 16;
  114. dds.format = THREE.RGBA_S3TC_DXT5_Format;
  115. break;
  116. case FOURCC_ETC1:
  117. blockBytes = 8;
  118. dds.format = THREE.RGB_ETC1_Format;
  119. break;
  120. default:
  121. if ( header[ off_RGBBitCount ] === 32 && header[ off_RBitMask ] & 0xff0000 && header[ off_GBitMask ] & 0xff00 && header[ off_BBitMask ] & 0xff && header[ off_ABitMask ] & 0xff000000 ) {
  122. isRGBAUncompressed = true;
  123. blockBytes = 64;
  124. dds.format = THREE.RGBAFormat;
  125. } else {
  126. console.error( 'THREE.DDSLoader.parse: Unsupported FourCC code ', int32ToFourCC( fourCC ) );
  127. return dds;
  128. }
  129. }
  130. dds.mipmapCount = 1;
  131. if ( header[ off_flags ] & DDSD_MIPMAPCOUNT && loadMipmaps !== false ) {
  132. dds.mipmapCount = Math.max( 1, header[ off_mipmapCount ] );
  133. }
  134. const caps2 = header[ off_caps2 ];
  135. dds.isCubemap = caps2 & DDSCAPS2_CUBEMAP ? true : false;
  136. 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 ) ) ) {
  137. console.error( 'THREE.DDSLoader.parse: Incomplete cubemap faces' );
  138. return dds;
  139. }
  140. dds.width = header[ off_width ];
  141. dds.height = header[ off_height ];
  142. let dataOffset = header[ off_size ] + 4; // Extract mipmaps buffers
  143. const faces = dds.isCubemap ? 6 : 1;
  144. for ( let face = 0; face < faces; face ++ ) {
  145. let width = dds.width;
  146. let height = dds.height;
  147. for ( let i = 0; i < dds.mipmapCount; i ++ ) {
  148. let byteArray, dataLength;
  149. if ( isRGBAUncompressed ) {
  150. byteArray = loadARGBMip( buffer, dataOffset, width, height );
  151. dataLength = byteArray.length;
  152. } else {
  153. dataLength = Math.max( 4, width ) / 4 * Math.max( 4, height ) / 4 * blockBytes;
  154. byteArray = new Uint8Array( buffer, dataOffset, dataLength );
  155. }
  156. const mipmap = {
  157. 'data': byteArray,
  158. 'width': width,
  159. 'height': height
  160. };
  161. dds.mipmaps.push( mipmap );
  162. dataOffset += dataLength;
  163. width = Math.max( width >> 1, 1 );
  164. height = Math.max( height >> 1, 1 );
  165. }
  166. }
  167. return dds;
  168. }
  169. }
  170. THREE.DDSLoader = DDSLoader;
  171. } )();