DDSLoader.js 8.8 KB

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