DDSLoader.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. function loadRGBMip( buffer, dataOffset, width, height ) {
  84. const dataLength = width * height * 3;
  85. const srcBuffer = new Uint8Array( buffer, dataOffset, dataLength );
  86. const byteArray = new Uint8Array( width * height * 4 );
  87. let dst = 0;
  88. let src = 0;
  89. for ( let y = 0; y < height; y ++ ) {
  90. for ( let x = 0; x < width; x ++ ) {
  91. const b = srcBuffer[ src ]; src ++;
  92. const g = srcBuffer[ src ]; src ++;
  93. const r = srcBuffer[ src ]; src ++;
  94. byteArray[ dst ] = r; dst ++; //r
  95. byteArray[ dst ] = g; dst ++; //g
  96. byteArray[ dst ] = b; dst ++; //b
  97. byteArray[ dst ] = 1.0; dst ++; //a
  98. }
  99. }
  100. return byteArray;
  101. }
  102. const FOURCC_DXT1 = fourCCToInt32( 'DXT1' );
  103. const FOURCC_DXT3 = fourCCToInt32( 'DXT3' );
  104. const FOURCC_DXT5 = fourCCToInt32( 'DXT5' );
  105. const FOURCC_ETC1 = fourCCToInt32( 'ETC1' );
  106. const FOURCC_DX10 = fourCCToInt32( 'DX10' );
  107. const headerLengthInt = 31; // The header length in 32 bit ints
  108. const extendedHeaderLengthInt = 5; // The extended header length in 32 bit ints
  109. // Offsets into the header array
  110. const off_magic = 0;
  111. const off_size = 1;
  112. const off_flags = 2;
  113. const off_height = 3;
  114. const off_width = 4;
  115. const off_mipmapCount = 7;
  116. // const off_pfFlags = 20;
  117. const off_pfFourCC = 21;
  118. const off_RGBBitCount = 22;
  119. const off_RBitMask = 23;
  120. const off_GBitMask = 24;
  121. const off_BBitMask = 25;
  122. const off_ABitMask = 26;
  123. // const off_caps = 27;
  124. const off_caps2 = 28;
  125. // const off_caps3 = 29;
  126. // const off_caps4 = 30;
  127. // If fourCC = DX10, the extended header starts after 32
  128. const off_dxgiFormat = 0;
  129. // Parse header
  130. const header = new Int32Array( buffer, 0, headerLengthInt );
  131. if ( header[ off_magic ] !== DDS_MAGIC ) {
  132. console.error( 'THREE.DDSLoader.parse: Invalid magic number in DDS header.' );
  133. return dds;
  134. }
  135. let blockBytes;
  136. const fourCC = header[ off_pfFourCC ];
  137. let isRGBAUncompressed = false;
  138. let isRGBUncompressed = false;
  139. let dataOffset = header[ off_size ] + 4;
  140. switch ( fourCC ) {
  141. case FOURCC_DXT1:
  142. blockBytes = 8;
  143. dds.format = RGB_S3TC_DXT1_Format;
  144. break;
  145. case FOURCC_DXT3:
  146. blockBytes = 16;
  147. dds.format = RGBA_S3TC_DXT3_Format;
  148. break;
  149. case FOURCC_DXT5:
  150. blockBytes = 16;
  151. dds.format = RGBA_S3TC_DXT5_Format;
  152. break;
  153. case FOURCC_ETC1:
  154. blockBytes = 8;
  155. dds.format = RGB_ETC1_Format;
  156. break;
  157. case FOURCC_DX10:
  158. dataOffset += extendedHeaderLengthInt * 4;
  159. const extendedHeader = new Int32Array( buffer, ( headerLengthInt + 1 ) * 4, extendedHeaderLengthInt );
  160. const dxgiFormat = extendedHeader[ off_dxgiFormat ];
  161. switch ( dxgiFormat ) {
  162. case DXGI_FORMAT_BC6H_SF16: {
  163. blockBytes = 16;
  164. dds.format = RGB_BPTC_SIGNED_Format;
  165. break;
  166. }
  167. case DXGI_FORMAT_BC6H_UF16: {
  168. blockBytes = 16;
  169. dds.format = RGB_BPTC_UNSIGNED_Format;
  170. break;
  171. }
  172. default: {
  173. console.error( 'THREE.DDSLoader.parse: Unsupported DXGI_FORMAT code ', dxgiFormat );
  174. return dds;
  175. }
  176. }
  177. break;
  178. default:
  179. if ( header[ off_RGBBitCount ] === 32
  180. && header[ off_RBitMask ] & 0xff0000
  181. && header[ off_GBitMask ] & 0xff00
  182. && header[ off_BBitMask ] & 0xff
  183. && header[ off_ABitMask ] & 0xff000000 ) {
  184. isRGBAUncompressed = true;
  185. blockBytes = 64;
  186. dds.format = RGBAFormat;
  187. } else if ( header[ off_RGBBitCount ] === 24
  188. && header[ off_RBitMask ] & 0xff0000
  189. && header[ off_GBitMask ] & 0xff00
  190. && header[ off_BBitMask ] & 0xff ) {
  191. isRGBUncompressed = true;
  192. blockBytes = 64;
  193. dds.format = RGBAFormat;
  194. } else {
  195. console.error( 'THREE.DDSLoader.parse: Unsupported FourCC code ', int32ToFourCC( fourCC ) );
  196. return dds;
  197. }
  198. }
  199. dds.mipmapCount = 1;
  200. if ( header[ off_flags ] & DDSD_MIPMAPCOUNT && loadMipmaps !== false ) {
  201. dds.mipmapCount = Math.max( 1, header[ off_mipmapCount ] );
  202. }
  203. const caps2 = header[ off_caps2 ];
  204. dds.isCubemap = caps2 & DDSCAPS2_CUBEMAP ? true : false;
  205. if ( dds.isCubemap && (
  206. ! ( caps2 & DDSCAPS2_CUBEMAP_POSITIVEX ) ||
  207. ! ( caps2 & DDSCAPS2_CUBEMAP_NEGATIVEX ) ||
  208. ! ( caps2 & DDSCAPS2_CUBEMAP_POSITIVEY ) ||
  209. ! ( caps2 & DDSCAPS2_CUBEMAP_NEGATIVEY ) ||
  210. ! ( caps2 & DDSCAPS2_CUBEMAP_POSITIVEZ ) ||
  211. ! ( caps2 & DDSCAPS2_CUBEMAP_NEGATIVEZ )
  212. ) ) {
  213. console.error( 'THREE.DDSLoader.parse: Incomplete cubemap faces' );
  214. return dds;
  215. }
  216. dds.width = header[ off_width ];
  217. dds.height = header[ off_height ];
  218. // Extract mipmaps buffers
  219. const faces = dds.isCubemap ? 6 : 1;
  220. for ( let face = 0; face < faces; face ++ ) {
  221. let width = dds.width;
  222. let height = dds.height;
  223. for ( let i = 0; i < dds.mipmapCount; i ++ ) {
  224. let byteArray, dataLength;
  225. if ( isRGBAUncompressed ) {
  226. byteArray = loadARGBMip( buffer, dataOffset, width, height );
  227. dataLength = byteArray.length;
  228. } else if ( isRGBUncompressed ) {
  229. byteArray = loadRGBMip( buffer, dataOffset, width, height );
  230. dataLength = width * height * 3;
  231. } else {
  232. dataLength = Math.max( 4, width ) / 4 * Math.max( 4, height ) / 4 * blockBytes;
  233. byteArray = new Uint8Array( buffer, dataOffset, dataLength );
  234. }
  235. const mipmap = { 'data': byteArray, 'width': width, 'height': height };
  236. dds.mipmaps.push( mipmap );
  237. dataOffset += dataLength;
  238. width = Math.max( width >> 1, 1 );
  239. height = Math.max( height >> 1, 1 );
  240. }
  241. }
  242. return dds;
  243. }
  244. }
  245. export { DDSLoader };
粤ICP备19079148号