DDSLoader.js 6.0 KB

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