PVRLoader.js 4.6 KB

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