DDSLoader.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 '../../../build/three.module.js';
  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. // let DDSD_CAPS = 0x1;
  21. // let DDSD_HEIGHT = 0x2;
  22. // let DDSD_WIDTH = 0x4;
  23. // let DDSD_PITCH = 0x8;
  24. // let DDSD_PIXELFORMAT = 0x1000;
  25. const DDSD_MIPMAPCOUNT = 0x20000;
  26. // let DDSD_LINEARSIZE = 0x80000;
  27. // let DDSD_DEPTH = 0x800000;
  28. // let DDSCAPS_COMPLEX = 0x8;
  29. // let DDSCAPS_MIPMAP = 0x400000;
  30. // let 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. // let DDSCAPS2_VOLUME = 0x200000;
  39. // let DDPF_ALPHAPIXELS = 0x1;
  40. // let DDPF_ALPHA = 0x2;
  41. const DDPF_FOURCC = 0x4;
  42. // let DDPF_RGB = 0x40;
  43. // let DDPF_YUV = 0x200;
  44. // let 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. // let off_caps = 27;
  99. const off_caps2 = 28;
  100. // let off_caps3 = 29;
  101. // let 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. if ( ! header[ off_pfFlags ] & DDPF_FOURCC ) {
  109. console.error( 'THREE.DDSLoader.parse: Unsupported format, must contain a FourCC code.' );
  110. return dds;
  111. }
  112. let blockBytes;
  113. const fourCC = header[ off_pfFourCC ];
  114. let isRGBAUncompressed = false;
  115. switch ( fourCC ) {
  116. case FOURCC_DXT1:
  117. blockBytes = 8;
  118. dds.format = RGB_S3TC_DXT1_Format;
  119. break;
  120. case FOURCC_DXT3:
  121. blockBytes = 16;
  122. dds.format = RGBA_S3TC_DXT3_Format;
  123. break;
  124. case FOURCC_DXT5:
  125. blockBytes = 16;
  126. dds.format = RGBA_S3TC_DXT5_Format;
  127. break;
  128. case FOURCC_ETC1:
  129. blockBytes = 8;
  130. dds.format = RGB_ETC1_Format;
  131. break;
  132. default:
  133. if ( header[ off_RGBBitCount ] === 32
  134. && header[ off_RBitMask ] & 0xff0000
  135. && header[ off_GBitMask ] & 0xff00
  136. && header[ off_BBitMask ] & 0xff
  137. && header[ off_ABitMask ] & 0xff000000 ) {
  138. isRGBAUncompressed = true;
  139. blockBytes = 64;
  140. dds.format = RGBAFormat;
  141. } else {
  142. console.error( 'THREE.DDSLoader.parse: Unsupported FourCC code ', int32ToFourCC( fourCC ) );
  143. return dds;
  144. }
  145. }
  146. dds.mipmapCount = 1;
  147. if ( header[ off_flags ] & DDSD_MIPMAPCOUNT && loadMipmaps !== false ) {
  148. dds.mipmapCount = Math.max( 1, header[ off_mipmapCount ] );
  149. }
  150. const caps2 = header[ off_caps2 ];
  151. dds.isCubemap = caps2 & DDSCAPS2_CUBEMAP ? true : false;
  152. if ( dds.isCubemap && (
  153. ! ( caps2 & DDSCAPS2_CUBEMAP_POSITIVEX ) ||
  154. ! ( caps2 & DDSCAPS2_CUBEMAP_NEGATIVEX ) ||
  155. ! ( caps2 & DDSCAPS2_CUBEMAP_POSITIVEY ) ||
  156. ! ( caps2 & DDSCAPS2_CUBEMAP_NEGATIVEY ) ||
  157. ! ( caps2 & DDSCAPS2_CUBEMAP_POSITIVEZ ) ||
  158. ! ( caps2 & DDSCAPS2_CUBEMAP_NEGATIVEZ )
  159. ) ) {
  160. console.error( 'THREE.DDSLoader.parse: Incomplete cubemap faces' );
  161. return dds;
  162. }
  163. dds.width = header[ off_width ];
  164. dds.height = header[ off_height ];
  165. let dataOffset = header[ off_size ] + 4;
  166. // Extract mipmaps buffers
  167. const faces = dds.isCubemap ? 6 : 1;
  168. for ( let face = 0; face < faces; face ++ ) {
  169. let width = dds.width;
  170. let height = dds.height;
  171. for ( let i = 0; i < dds.mipmapCount; i ++ ) {
  172. let byteArray, dataLength;
  173. if ( isRGBAUncompressed ) {
  174. byteArray = loadARGBMip( buffer, dataOffset, width, height );
  175. dataLength = byteArray.length;
  176. } else {
  177. dataLength = Math.max( 4, width ) / 4 * Math.max( 4, height ) / 4 * blockBytes;
  178. byteArray = new Uint8Array( buffer, dataOffset, dataLength );
  179. }
  180. const mipmap = { 'data': byteArray, 'width': width, 'height': height };
  181. dds.mipmaps.push( mipmap );
  182. dataOffset += dataLength;
  183. width = Math.max( width >> 1, 1 );
  184. height = Math.max( height >> 1, 1 );
  185. }
  186. }
  187. return dds;
  188. }
  189. }
  190. export { DDSLoader };