PVRLoader.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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;
  111. // guess cubemap type seems tricky in v2
  112. // it juste a pvr containing 6 surface (no explicit cubemap type)
  113. pvrDatas.isCubemap = numSurfs === 6;
  114. return _extract( pvrDatas );
  115. }
  116. function _extract( pvrDatas ) {
  117. const pvr = {
  118. mipmaps: [],
  119. width: pvrDatas.width,
  120. height: pvrDatas.height,
  121. format: pvrDatas.format,
  122. mipmapCount: pvrDatas.numMipmaps,
  123. isCubemap: pvrDatas.isCubemap
  124. };
  125. const buffer = pvrDatas.buffer;
  126. let dataOffset = pvrDatas.dataPtr,
  127. dataSize = 0,
  128. blockSize = 0,
  129. blockWidth = 0,
  130. blockHeight = 0,
  131. widthBlocks = 0,
  132. heightBlocks = 0;
  133. const bpp = pvrDatas.bpp,
  134. numSurfs = pvrDatas.numSurfaces;
  135. if ( bpp === 2 ) {
  136. blockWidth = 8;
  137. blockHeight = 4;
  138. } else {
  139. blockWidth = 4;
  140. blockHeight = 4;
  141. }
  142. blockSize = blockWidth * blockHeight * bpp / 8;
  143. pvr.mipmaps.length = pvrDatas.numMipmaps * numSurfs;
  144. let mipLevel = 0;
  145. while ( mipLevel < pvrDatas.numMipmaps ) {
  146. const sWidth = pvrDatas.width >> mipLevel,
  147. sHeight = pvrDatas.height >> mipLevel;
  148. widthBlocks = sWidth / blockWidth;
  149. heightBlocks = sHeight / blockHeight;
  150. // Clamp to minimum number of blocks
  151. if ( widthBlocks < 2 ) widthBlocks = 2;
  152. if ( heightBlocks < 2 ) heightBlocks = 2;
  153. dataSize = widthBlocks * heightBlocks * blockSize;
  154. for ( let surfIndex = 0; surfIndex < numSurfs; surfIndex ++ ) {
  155. const byteArray = new Uint8Array( buffer, dataOffset, dataSize );
  156. const mipmap = {
  157. data: byteArray,
  158. width: sWidth,
  159. height: sHeight
  160. };
  161. pvr.mipmaps[ surfIndex * pvrDatas.numMipmaps + mipLevel ] = mipmap;
  162. dataOffset += dataSize;
  163. }
  164. mipLevel ++;
  165. }
  166. return pvr;
  167. }
  168. THREE.PVRLoader = PVRLoader;
  169. } )();