PVRLoader.js 5.3 KB

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