PVRLoader.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * PVRLoader
  3. * Author: pierre lepers
  4. * Date: 17/09/2014 11:09
  5. *
  6. * PVR v2 (legacy) parser
  7. * TODO : Add Support for PVR v3 format
  8. * TODO : implement loadMipmaps option
  9. */
  10. THREE.PVRLoader = function () {
  11. this._parser = THREE.PVRLoader.parse;
  12. };
  13. THREE.PVRLoader.prototype = Object.create( THREE.CompressedTextureLoader.prototype );
  14. THREE.PVRLoader.parse = function ( buffer, loadMipmaps ) {
  15. var headerLengthInt = 13;
  16. var header = new Uint32Array( buffer, 0, headerLengthInt );
  17. var pvrDatas = {
  18. buffer: buffer,
  19. header : header,
  20. loadMipmaps : loadMipmaps
  21. };
  22. if( header[0] === 0x03525650 ) {
  23. // PVR v3
  24. return THREE.PVRLoader._parseV3( pvrDatas );
  25. } else if( header[11] === 0x21525650) {
  26. // PVR v2
  27. return THREE.PVRLoader._parseV2( pvrDatas );
  28. } else {
  29. throw new Error( "[THREE.PVRLoader] Unknown PVR format" );
  30. }
  31. };
  32. THREE.PVRLoader._parseV3 = function ( pvrDatas ) {
  33. var buffer = pvrDatas.buffer;
  34. var header = pvrDatas.header;
  35. };
  36. THREE.PVRLoader._parseV2 = function ( pvrDatas ) {
  37. var buffer = pvrDatas.buffer;
  38. var header = pvrDatas.header;
  39. var headerLength = header[0],
  40. height = header[1],
  41. width = header[2],
  42. numMipmaps = header[3],
  43. flags = header[4],
  44. dataLength = header[5],
  45. bpp = header[6],
  46. bitmaskRed = header[7],
  47. bitmaskGreen = header[8],
  48. bitmaskBlue = header[9],
  49. bitmaskAlpha = header[10],
  50. pvrTag = header[11],
  51. numSurfs = header[12];
  52. var TYPE_MASK = 0xff
  53. var PVRTC_2 = 24,
  54. PVRTC_4 = 25
  55. var formatFlags = flags & TYPE_MASK;
  56. var bpp, format;
  57. var _hasAlpha = bitmaskAlpha > 0;
  58. if (formatFlags == PVRTC_4 ) {
  59. format = _hasAlpha ? THREE.RGBA_PVRTC_4BPPV1_Format : THREE.RGB_PVRTC_4BPPV1_Format;
  60. bpp = 4;
  61. }
  62. else if( formatFlags == PVRTC_2) {
  63. format = _hasAlpha ? THREE.RGBA_PVRTC_4BPPV1_Format : THREE.RGB_PVRTC_4BPPV1_Format;
  64. bpp = 2;
  65. }
  66. else
  67. throw new Error( "pvrtc - unknown format "+formatFlags);
  68. pvrDatas.dataPtr = headerLength;
  69. pvrDatas.bpp = bpp;
  70. pvrDatas.format = format;
  71. pvrDatas.width = width;
  72. pvrDatas.height = height;
  73. pvrDatas.numSurfaces = numSurfs;
  74. pvrDatas.numMipmaps = numMipmaps;
  75. // guess cubemap type seems tricky in v2
  76. // it juste a pvr containing 6 surface (no explicit cubemap type)
  77. pvrDatas.isCubemap = (numSurfs === 6);
  78. return THREE.PVRLoader._extract( pvrDatas );
  79. };
  80. THREE.PVRLoader._extract = function ( pvrDatas ) {
  81. var pvr = { mipmaps: [], width: pvrDatas.width, height: pvrDatas.height, format: pvrDatas.format, mipmapCount: pvrDatas.numMipmaps+1, isCubemap : pvrDatas.isCubemap };
  82. var buffer = pvrDatas.buffer;
  83. // console.log( "--------------------------" );
  84. // console.log( "headerLength ", headerLength);
  85. // console.log( "height ", height );
  86. // console.log( "width ", width );
  87. // console.log( "numMipmaps ", numMipmaps );
  88. // console.log( "flags ", flags );
  89. // console.log( "dataLength ", dataLength );
  90. // console.log( "bpp ", bpp );
  91. // console.log( "bitmaskRed ", bitmaskRed );
  92. // console.log( "bitmaskGreen ", bitmaskGreen);
  93. // console.log( "bitmaskBlue ", bitmaskBlue );
  94. // console.log( "bitmaskAlpha ", bitmaskAlpha);
  95. // console.log( "pvrTag ", pvrTag );
  96. // console.log( "numSurfs ", numSurfs );
  97. var dataOffset = pvrDatas.dataPtr,
  98. bpp = pvrDatas.bpp,
  99. numSurfs = pvrDatas.numSurfaces,
  100. dataSize = 0,
  101. blockSize = 0,
  102. blockWidth = 0,
  103. blockHeight = 0,
  104. widthBlocks = 0,
  105. heightBlocks = 0;
  106. if( bpp === 2 ){
  107. blockWidth = 8;
  108. blockHeight = 4;
  109. } else {
  110. blockWidth = 4;
  111. blockHeight = 4;
  112. }
  113. blockSize = blockWidth * blockHeight;
  114. for ( var surfIndex = 0; surfIndex < numSurfs; surfIndex ++ ) {
  115. var sWidth = pvrDatas.width,
  116. sHeight = pvrDatas.height;
  117. var mipLevel = 0;
  118. while (mipLevel < pvrDatas.numMipmaps + 1 ) {
  119. widthBlocks = sWidth / blockWidth;
  120. heightBlocks = sHeight / blockHeight;
  121. // Clamp to minimum number of blocks
  122. if (widthBlocks < 2)
  123. widthBlocks = 2;
  124. if (heightBlocks < 2)
  125. heightBlocks = 2;
  126. dataSize = widthBlocks * heightBlocks * ((blockSize * bpp) / 8);
  127. var byteArray = new Uint8Array( buffer, dataOffset, dataSize );
  128. var mipmap = { "data": byteArray, "width": sWidth, "height": sHeight };
  129. pvr.mipmaps.push( mipmap );
  130. dataOffset += dataSize;
  131. sWidth = Math.max(sWidth >> 1, 1);
  132. sHeight = Math.max(sHeight >> 1, 1);
  133. mipLevel++;
  134. }
  135. }
  136. return pvr;
  137. }