PVRLoader.js 4.7 KB

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