PVRLoader.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import {
  2. CompressedTextureLoader,
  3. RGBA_PVRTC_2BPPV1_Format,
  4. RGBA_PVRTC_4BPPV1_Format,
  5. RGB_PVRTC_2BPPV1_Format,
  6. RGB_PVRTC_4BPPV1_Format
  7. } from 'three';
  8. /**
  9. * A loader for the PVRTC texture compression format.
  10. *
  11. * ```js
  12. * const loader = new PVRLoader();
  13. *
  14. * const map = loader.load( 'textures/compressed/disturb_4bpp_rgb.pvr' );
  15. * map.colorSpace = THREE.SRGBColorSpace; // only for color textures
  16. * ```
  17. *
  18. * @augments CompressedTextureLoader
  19. */
  20. class PVRLoader extends CompressedTextureLoader {
  21. /**
  22. * Constructs a new PVR loader.
  23. *
  24. * @param {LoadingManager} [manager] - The loading manager.
  25. */
  26. constructor( manager ) {
  27. super( manager );
  28. }
  29. /**
  30. * Parses the given PVRTC texture data.
  31. *
  32. * @param {ArrayBuffer} buffer - The raw texture data.
  33. * @param {boolean} loadMipmaps - Whether to load mipmaps or not. This option is not yet supported by the loader.
  34. * @return {CompressedTextureLoader~TexData} An object representing the parsed texture data.
  35. */
  36. parse( buffer, loadMipmaps ) {
  37. const headerLengthInt = 13;
  38. const header = new Uint32Array( buffer, 0, headerLengthInt );
  39. const pvrDatas = {
  40. buffer: buffer,
  41. header: header,
  42. loadMipmaps: loadMipmaps
  43. };
  44. if ( header[ 0 ] === 0x03525650 ) {
  45. // PVR v3
  46. return _parseV3( pvrDatas );
  47. } else if ( header[ 11 ] === 0x21525650 ) {
  48. // PVR v2
  49. return _parseV2( pvrDatas );
  50. } else {
  51. console.error( 'THREE.PVRLoader: Unknown PVR format.' );
  52. }
  53. }
  54. }
  55. function _parseV3( pvrDatas ) {
  56. const header = pvrDatas.header;
  57. let bpp, format;
  58. const metaLen = header[ 12 ],
  59. pixelFormat = header[ 2 ],
  60. height = header[ 6 ],
  61. width = header[ 7 ],
  62. // numSurfs = header[ 9 ],
  63. numFaces = header[ 10 ],
  64. numMipmaps = header[ 11 ];
  65. switch ( pixelFormat ) {
  66. case 0 : // PVRTC 2bpp RGB
  67. bpp = 2;
  68. format = RGB_PVRTC_2BPPV1_Format;
  69. break;
  70. case 1 : // PVRTC 2bpp RGBA
  71. bpp = 2;
  72. format = RGBA_PVRTC_2BPPV1_Format;
  73. break;
  74. case 2 : // PVRTC 4bpp RGB
  75. bpp = 4;
  76. format = RGB_PVRTC_4BPPV1_Format;
  77. break;
  78. case 3 : // PVRTC 4bpp RGBA
  79. bpp = 4;
  80. format = RGBA_PVRTC_4BPPV1_Format;
  81. break;
  82. default :
  83. console.error( 'THREE.PVRLoader: Unsupported PVR format:', pixelFormat );
  84. }
  85. pvrDatas.dataPtr = 52 + metaLen;
  86. pvrDatas.bpp = bpp;
  87. pvrDatas.format = format;
  88. pvrDatas.width = width;
  89. pvrDatas.height = height;
  90. pvrDatas.numSurfaces = numFaces;
  91. pvrDatas.numMipmaps = numMipmaps;
  92. pvrDatas.isCubemap = ( numFaces === 6 );
  93. return _extract( pvrDatas );
  94. }
  95. function _parseV2( pvrDatas ) {
  96. const header = pvrDatas.header;
  97. const headerLength = header[ 0 ],
  98. height = header[ 1 ],
  99. width = header[ 2 ],
  100. numMipmaps = header[ 3 ],
  101. flags = header[ 4 ],
  102. // dataLength = header[ 5 ],
  103. // bpp = header[ 6 ],
  104. // bitmaskRed = header[ 7 ],
  105. // bitmaskGreen = header[ 8 ],
  106. // bitmaskBlue = header[ 9 ],
  107. bitmaskAlpha = header[ 10 ],
  108. // pvrTag = header[ 11 ],
  109. numSurfs = header[ 12 ];
  110. const TYPE_MASK = 0xff;
  111. const PVRTC_2 = 24,
  112. PVRTC_4 = 25;
  113. const formatFlags = flags & TYPE_MASK;
  114. let bpp, format;
  115. const _hasAlpha = bitmaskAlpha > 0;
  116. if ( formatFlags === PVRTC_4 ) {
  117. format = _hasAlpha ? RGBA_PVRTC_4BPPV1_Format : RGB_PVRTC_4BPPV1_Format;
  118. bpp = 4;
  119. } else if ( formatFlags === PVRTC_2 ) {
  120. format = _hasAlpha ? RGBA_PVRTC_2BPPV1_Format : RGB_PVRTC_2BPPV1_Format;
  121. bpp = 2;
  122. } else {
  123. console.error( 'THREE.PVRLoader: Unknown PVR format:', formatFlags );
  124. }
  125. pvrDatas.dataPtr = headerLength;
  126. pvrDatas.bpp = bpp;
  127. pvrDatas.format = format;
  128. pvrDatas.width = width;
  129. pvrDatas.height = height;
  130. pvrDatas.numSurfaces = numSurfs;
  131. pvrDatas.numMipmaps = numMipmaps + 1;
  132. // guess cubemap type seems tricky in v2
  133. // it's just a pvr containing 6 surface (no explicit cubemap type)
  134. pvrDatas.isCubemap = ( numSurfs === 6 );
  135. return _extract( pvrDatas );
  136. }
  137. function _extract( pvrDatas ) {
  138. const pvr = {
  139. mipmaps: [],
  140. width: pvrDatas.width,
  141. height: pvrDatas.height,
  142. format: pvrDatas.format,
  143. mipmapCount: pvrDatas.numMipmaps,
  144. isCubemap: pvrDatas.isCubemap
  145. };
  146. const buffer = pvrDatas.buffer;
  147. let dataOffset = pvrDatas.dataPtr,
  148. dataSize = 0,
  149. blockSize = 0,
  150. blockWidth = 0,
  151. blockHeight = 0,
  152. widthBlocks = 0,
  153. heightBlocks = 0;
  154. const bpp = pvrDatas.bpp,
  155. numSurfs = pvrDatas.numSurfaces;
  156. if ( bpp === 2 ) {
  157. blockWidth = 8;
  158. blockHeight = 4;
  159. } else {
  160. blockWidth = 4;
  161. blockHeight = 4;
  162. }
  163. blockSize = ( blockWidth * blockHeight ) * bpp / 8;
  164. pvr.mipmaps.length = pvrDatas.numMipmaps * numSurfs;
  165. let mipLevel = 0;
  166. while ( mipLevel < pvrDatas.numMipmaps ) {
  167. const sWidth = pvrDatas.width >> mipLevel,
  168. sHeight = pvrDatas.height >> mipLevel;
  169. widthBlocks = sWidth / blockWidth;
  170. heightBlocks = sHeight / blockHeight;
  171. // Clamp to minimum number of blocks
  172. if ( widthBlocks < 2 ) widthBlocks = 2;
  173. if ( heightBlocks < 2 ) heightBlocks = 2;
  174. dataSize = widthBlocks * heightBlocks * blockSize;
  175. for ( let surfIndex = 0; surfIndex < numSurfs; surfIndex ++ ) {
  176. const byteArray = new Uint8Array( buffer, dataOffset, dataSize );
  177. const mipmap = {
  178. data: byteArray,
  179. width: sWidth,
  180. height: sHeight
  181. };
  182. pvr.mipmaps[ surfIndex * pvrDatas.numMipmaps + mipLevel ] = mipmap;
  183. dataOffset += dataSize;
  184. }
  185. mipLevel ++;
  186. }
  187. return pvr;
  188. }
  189. export { PVRLoader };
粤ICP备19079148号