DDSLoader.js 7.0 KB

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