DDSLoader.js 6.1 KB

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