DDSLoader.js 8.9 KB

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