BasisTextureLoader.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /**
  2. * Loader for Basis Universal GPU Texture Codec.
  3. *
  4. * Basis Universal is a "supercompressed" GPU texture and texture video
  5. * compression system that outputs a highly compressed intermediate file format
  6. * (.basis) that can be quickly transcoded to a wide variety of GPU texture
  7. * compression formats.
  8. *
  9. * This loader parallelizes the transcoding process across a configurable number
  10. * of web workers, before transferring the transcoded compressed texture back
  11. * to the main thread.
  12. */
  13. THREE.BasisTextureLoader = function ( manager ) {
  14. THREE.Loader.call( this, manager );
  15. this.transcoderPath = '';
  16. this.transcoderBinary = null;
  17. this.transcoderPending = null;
  18. this.workerLimit = 4;
  19. this.workerPool = [];
  20. this.workerNextTaskID = 1;
  21. this.workerSourceURL = '';
  22. this.workerConfig = null;
  23. };
  24. THREE.BasisTextureLoader.taskCache = new WeakMap();
  25. THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
  26. constructor: THREE.BasisTextureLoader,
  27. setTranscoderPath: function ( path ) {
  28. this.transcoderPath = path;
  29. return this;
  30. },
  31. setWorkerLimit: function ( workerLimit ) {
  32. this.workerLimit = workerLimit;
  33. return this;
  34. },
  35. detectSupport: function ( renderer ) {
  36. this.workerConfig = {
  37. astcSupported: renderer.extensions.has( 'WEBGL_compressed_texture_astc' ),
  38. etc1Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc1' ),
  39. etc2Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc' ),
  40. dxtSupported: renderer.extensions.has( 'WEBGL_compressed_texture_s3tc' ),
  41. bptcSupported: renderer.extensions.has( 'EXT_texture_compression_bptc' ),
  42. pvrtcSupported: renderer.extensions.has( 'WEBGL_compressed_texture_pvrtc' )
  43. || renderer.extensions.has( 'WEBKIT_WEBGL_compressed_texture_pvrtc' )
  44. };
  45. return this;
  46. },
  47. load: function ( url, onLoad, onProgress, onError ) {
  48. var loader = new THREE.FileLoader( this.manager );
  49. loader.setResponseType( 'arraybuffer' );
  50. loader.setWithCredentials( this.withCredentials );
  51. var texture = new THREE.CompressedTexture();
  52. loader.load( url, ( buffer ) => {
  53. // Check for an existing task using this buffer. A transferred buffer cannot be transferred
  54. // again from this thread.
  55. if ( THREE.BasisTextureLoader.taskCache.has( buffer ) ) {
  56. var cachedTask = THREE.BasisTextureLoader.taskCache.get( buffer );
  57. return cachedTask.promise.then( onLoad ).catch( onError );
  58. }
  59. this._createTexture( [ buffer ] )
  60. .then( function ( _texture ) {
  61. texture.copy( _texture );
  62. texture.needsUpdate = true;
  63. if ( onLoad ) onLoad( texture );
  64. } )
  65. .catch( onError );
  66. }, onProgress, onError );
  67. return texture;
  68. },
  69. /** Low-level transcoding API, exposed for use by THREE.KTX2Loader. */
  70. parseInternalAsync: function ( options ) {
  71. var { levels } = options;
  72. var buffers = new Set();
  73. for ( var i = 0; i < levels.length; i ++ ) {
  74. buffers.add( levels[ i ].data.buffer );
  75. }
  76. return this._createTexture( Array.from( buffers ), { ...options, lowLevel: true } );
  77. },
  78. /**
  79. * @param {ArrayBuffer[]} buffers
  80. * @param {object?} config
  81. * @return {Promise<THREE.CompressedTexture>}
  82. */
  83. _createTexture: function ( buffers, config ) {
  84. var worker;
  85. var taskID;
  86. var taskConfig = config || {};
  87. var taskCost = 0;
  88. for ( var i = 0; i < buffers.length; i ++ ) {
  89. taskCost += buffers[ i ].byteLength;
  90. }
  91. var texturePending = this._allocateWorker( taskCost )
  92. .then( ( _worker ) => {
  93. worker = _worker;
  94. taskID = this.workerNextTaskID ++;
  95. return new Promise( ( resolve, reject ) => {
  96. worker._callbacks[ taskID ] = { resolve, reject };
  97. worker.postMessage( { type: 'transcode', id: taskID, buffers: buffers, taskConfig: taskConfig }, buffers );
  98. } );
  99. } )
  100. .then( ( message ) => {
  101. var { mipmaps, width, height, format } = message;
  102. var texture = new THREE.CompressedTexture( mipmaps, width, height, format, THREE.UnsignedByteType );
  103. texture.minFilter = mipmaps.length === 1 ? THREE.LinearFilter : THREE.LinearMipmapLinearFilter;
  104. texture.magFilter = THREE.LinearFilter;
  105. texture.generateMipmaps = false;
  106. texture.needsUpdate = true;
  107. return texture;
  108. } );
  109. // Note: replaced '.finally()' with '.catch().then()' block - iOS 11 support (#19416)
  110. texturePending
  111. .catch( () => true )
  112. .then( () => {
  113. if ( worker && taskID ) {
  114. worker._taskLoad -= taskCost;
  115. delete worker._callbacks[ taskID ];
  116. }
  117. } );
  118. // Cache the task result.
  119. THREE.BasisTextureLoader.taskCache.set( buffers[ 0 ], { promise: texturePending } );
  120. return texturePending;
  121. },
  122. _initTranscoder: function () {
  123. if ( ! this.transcoderPending ) {
  124. // Load transcoder wrapper.
  125. var jsLoader = new THREE.FileLoader( this.manager );
  126. jsLoader.setPath( this.transcoderPath );
  127. jsLoader.setWithCredentials( this.withCredentials );
  128. var jsContent = new Promise( ( resolve, reject ) => {
  129. jsLoader.load( 'basis_transcoder.js', resolve, undefined, reject );
  130. } );
  131. // Load transcoder WASM binary.
  132. var binaryLoader = new THREE.FileLoader( this.manager );
  133. binaryLoader.setPath( this.transcoderPath );
  134. binaryLoader.setResponseType( 'arraybuffer' );
  135. binaryLoader.setWithCredentials( this.withCredentials );
  136. var binaryContent = new Promise( ( resolve, reject ) => {
  137. binaryLoader.load( 'basis_transcoder.wasm', resolve, undefined, reject );
  138. } );
  139. this.transcoderPending = Promise.all( [ jsContent, binaryContent ] )
  140. .then( ( [ jsContent, binaryContent ] ) => {
  141. var fn = THREE.BasisTextureLoader.BasisWorker.toString();
  142. var body = [
  143. '/* constants */',
  144. 'var _EngineFormat = ' + JSON.stringify( THREE.BasisTextureLoader.EngineFormat ),
  145. 'var _TranscoderFormat = ' + JSON.stringify( THREE.BasisTextureLoader.TranscoderFormat ),
  146. 'var _BasisFormat = ' + JSON.stringify( THREE.BasisTextureLoader.BasisFormat ),
  147. '/* basis_transcoder.js */',
  148. jsContent,
  149. '/* worker */',
  150. fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) )
  151. ].join( '\n' );
  152. this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) );
  153. this.transcoderBinary = binaryContent;
  154. } );
  155. }
  156. return this.transcoderPending;
  157. },
  158. _allocateWorker: function ( taskCost ) {
  159. return this._initTranscoder().then( () => {
  160. if ( this.workerPool.length < this.workerLimit ) {
  161. var worker = new Worker( this.workerSourceURL );
  162. worker._callbacks = {};
  163. worker._taskLoad = 0;
  164. worker.postMessage( {
  165. type: 'init',
  166. config: this.workerConfig,
  167. transcoderBinary: this.transcoderBinary,
  168. } );
  169. worker.onmessage = function ( e ) {
  170. var message = e.data;
  171. switch ( message.type ) {
  172. case 'transcode':
  173. worker._callbacks[ message.id ].resolve( message );
  174. break;
  175. case 'error':
  176. worker._callbacks[ message.id ].reject( message );
  177. break;
  178. default:
  179. console.error( 'THREE.BasisTextureLoader: Unexpected message, "' + message.type + '"' );
  180. }
  181. };
  182. this.workerPool.push( worker );
  183. } else {
  184. this.workerPool.sort( function ( a, b ) {
  185. return a._taskLoad > b._taskLoad ? - 1 : 1;
  186. } );
  187. }
  188. var worker = this.workerPool[ this.workerPool.length - 1 ];
  189. worker._taskLoad += taskCost;
  190. return worker;
  191. } );
  192. },
  193. dispose: function () {
  194. for ( var i = 0; i < this.workerPool.length; i ++ ) {
  195. this.workerPool[ i ].terminate();
  196. }
  197. this.workerPool.length = 0;
  198. return this;
  199. }
  200. } );
  201. /* CONSTANTS */
  202. THREE.BasisTextureLoader.BasisFormat = {
  203. ETC1S: 0,
  204. UASTC_4x4: 1,
  205. };
  206. THREE.BasisTextureLoader.TranscoderFormat = {
  207. ETC1: 0,
  208. ETC2: 1,
  209. BC1: 2,
  210. BC3: 3,
  211. BC4: 4,
  212. BC5: 5,
  213. BC7_M6_OPAQUE_ONLY: 6,
  214. BC7_M5: 7,
  215. PVRTC1_4_RGB: 8,
  216. PVRTC1_4_RGBA: 9,
  217. ASTC_4x4: 10,
  218. ATC_RGB: 11,
  219. ATC_RGBA_INTERPOLATED_ALPHA: 12,
  220. RGBA32: 13,
  221. RGB565: 14,
  222. BGR565: 15,
  223. RGBA4444: 16,
  224. };
  225. THREE.BasisTextureLoader.EngineFormat = {
  226. RGBAFormat: THREE.RGBAFormat,
  227. RGBA_ASTC_4x4_Format: THREE.RGBA_ASTC_4x4_Format,
  228. RGBA_BPTC_Format: THREE.RGBA_BPTC_Format,
  229. RGBA_ETC2_EAC_Format: THREE.RGBA_ETC2_EAC_Format,
  230. RGBA_PVRTC_4BPPV1_Format: THREE.RGBA_PVRTC_4BPPV1_Format,
  231. RGBA_S3TC_DXT5_Format: THREE.RGBA_S3TC_DXT5_Format,
  232. RGB_ETC1_Format: THREE.RGB_ETC1_Format,
  233. RGB_ETC2_Format: THREE.RGB_ETC2_Format,
  234. RGB_PVRTC_4BPPV1_Format: THREE.RGB_PVRTC_4BPPV1_Format,
  235. RGB_S3TC_DXT1_Format: THREE.RGB_S3TC_DXT1_Format,
  236. };
  237. /* WEB WORKER */
  238. THREE.BasisTextureLoader.BasisWorker = function () {
  239. var config;
  240. var transcoderPending;
  241. var BasisModule;
  242. var EngineFormat = _EngineFormat; // eslint-disable-line no-undef
  243. var TranscoderFormat = _TranscoderFormat; // eslint-disable-line no-undef
  244. var BasisFormat = _BasisFormat; // eslint-disable-line no-undef
  245. onmessage = function ( e ) {
  246. var message = e.data;
  247. switch ( message.type ) {
  248. case 'init':
  249. config = message.config;
  250. init( message.transcoderBinary );
  251. break;
  252. case 'transcode':
  253. transcoderPending.then( () => {
  254. try {
  255. var { width, height, hasAlpha, mipmaps, format } = message.taskConfig.lowLevel
  256. ? transcodeLowLevel( message.taskConfig )
  257. : transcode( message.buffers[ 0 ] );
  258. var buffers = [];
  259. for ( var i = 0; i < mipmaps.length; ++ i ) {
  260. buffers.push( mipmaps[ i ].data.buffer );
  261. }
  262. self.postMessage( { type: 'transcode', id: message.id, width, height, hasAlpha, mipmaps, format }, buffers );
  263. } catch ( error ) {
  264. console.error( error );
  265. self.postMessage( { type: 'error', id: message.id, error: error.message } );
  266. }
  267. } );
  268. break;
  269. }
  270. };
  271. function init( wasmBinary ) {
  272. transcoderPending = new Promise( ( resolve ) => {
  273. BasisModule = { wasmBinary, onRuntimeInitialized: resolve };
  274. BASIS( BasisModule ); // eslint-disable-line no-undef
  275. } ).then( () => {
  276. BasisModule.initializeBasis();
  277. } );
  278. }
  279. function transcodeLowLevel( taskConfig ) {
  280. var { basisFormat, width, height, hasAlpha } = taskConfig;
  281. var { transcoderFormat, engineFormat } = getTranscoderFormat( basisFormat, width, height, hasAlpha );
  282. var blockByteLength = BasisModule.getBytesPerBlockOrPixel( transcoderFormat );
  283. assert( BasisModule.isFormatSupported( transcoderFormat ), 'THREE.BasisTextureLoader: Unsupported format.' );
  284. var mipmaps = [];
  285. if ( basisFormat === BasisFormat.ETC1S ) {
  286. var transcoder = new BasisModule.LowLevelETC1SImageTranscoder();
  287. var { endpointCount, endpointsData, selectorCount, selectorsData, tablesData } = taskConfig.globalData;
  288. try {
  289. var ok;
  290. ok = transcoder.decodePalettes( endpointCount, endpointsData, selectorCount, selectorsData );
  291. assert( ok, 'THREE.BasisTextureLoader: decodePalettes() failed.' );
  292. ok = transcoder.decodeTables( tablesData );
  293. assert( ok, 'THREE.BasisTextureLoader: decodeTables() failed.' );
  294. for ( var i = 0; i < taskConfig.levels.length; i ++ ) {
  295. var level = taskConfig.levels[ i ];
  296. var imageDesc = taskConfig.globalData.imageDescs[ i ];
  297. var dstByteLength = getTranscodedImageByteLength( transcoderFormat, level.width, level.height );
  298. var dst = new Uint8Array( dstByteLength );
  299. ok = transcoder.transcodeImage(
  300. transcoderFormat,
  301. dst, dstByteLength / blockByteLength,
  302. level.data,
  303. getWidthInBlocks( transcoderFormat, level.width ),
  304. getHeightInBlocks( transcoderFormat, level.height ),
  305. level.width, level.height, level.index,
  306. imageDesc.rgbSliceByteOffset, imageDesc.rgbSliceByteLength,
  307. imageDesc.alphaSliceByteOffset, imageDesc.alphaSliceByteLength,
  308. imageDesc.imageFlags,
  309. hasAlpha,
  310. false,
  311. 0, 0
  312. );
  313. assert( ok, 'THREE.BasisTextureLoader: transcodeImage() failed for level ' + level.index + '.' );
  314. mipmaps.push( { data: dst, width: level.width, height: level.height } );
  315. }
  316. } finally {
  317. transcoder.delete();
  318. }
  319. } else {
  320. for ( var i = 0; i < taskConfig.levels.length; i ++ ) {
  321. var level = taskConfig.levels[ i ];
  322. var dstByteLength = getTranscodedImageByteLength( transcoderFormat, level.width, level.height );
  323. var dst = new Uint8Array( dstByteLength );
  324. var ok = BasisModule.transcodeUASTCImage(
  325. transcoderFormat,
  326. dst, dstByteLength / blockByteLength,
  327. level.data,
  328. getWidthInBlocks( transcoderFormat, level.width ),
  329. getHeightInBlocks( transcoderFormat, level.height ),
  330. level.width, level.height, level.index,
  331. 0,
  332. level.data.byteLength,
  333. 0,
  334. hasAlpha,
  335. false,
  336. 0, 0,
  337. - 1, - 1
  338. );
  339. assert( ok, 'THREE.BasisTextureLoader: transcodeUASTCImage() failed for level ' + level.index + '.' );
  340. mipmaps.push( { data: dst, width: level.width, height: level.height } );
  341. }
  342. }
  343. return { width, height, hasAlpha, mipmaps, format: engineFormat };
  344. }
  345. function transcode( buffer ) {
  346. var basisFile = new BasisModule.BasisFile( new Uint8Array( buffer ) );
  347. var basisFormat = basisFile.isUASTC() ? BasisFormat.UASTC_4x4 : BasisFormat.ETC1S;
  348. var width = basisFile.getImageWidth( 0, 0 );
  349. var height = basisFile.getImageHeight( 0, 0 );
  350. var levels = basisFile.getNumLevels( 0 );
  351. var hasAlpha = basisFile.getHasAlpha();
  352. function cleanup() {
  353. basisFile.close();
  354. basisFile.delete();
  355. }
  356. var { transcoderFormat, engineFormat } = getTranscoderFormat( basisFormat, width, height, hasAlpha );
  357. if ( ! width || ! height || ! levels ) {
  358. cleanup();
  359. throw new Error( 'THREE.BasisTextureLoader: Invalid texture' );
  360. }
  361. if ( ! basisFile.startTranscoding() ) {
  362. cleanup();
  363. throw new Error( 'THREE.BasisTextureLoader: .startTranscoding failed' );
  364. }
  365. var mipmaps = [];
  366. for ( var mip = 0; mip < levels; mip ++ ) {
  367. var mipWidth = basisFile.getImageWidth( 0, mip );
  368. var mipHeight = basisFile.getImageHeight( 0, mip );
  369. var dst = new Uint8Array( basisFile.getImageTranscodedSizeInBytes( 0, mip, transcoderFormat ) );
  370. var status = basisFile.transcodeImage(
  371. dst,
  372. 0,
  373. mip,
  374. transcoderFormat,
  375. 0,
  376. hasAlpha
  377. );
  378. if ( ! status ) {
  379. cleanup();
  380. throw new Error( 'THREE.BasisTextureLoader: .transcodeImage failed.' );
  381. }
  382. mipmaps.push( { data: dst, width: mipWidth, height: mipHeight } );
  383. }
  384. cleanup();
  385. return { width, height, hasAlpha, mipmaps, format: engineFormat };
  386. }
  387. //
  388. // Optimal choice of a transcoder target format depends on the Basis format (ETC1S or UASTC),
  389. // device capabilities, and texture dimensions. The list below ranks the formats separately
  390. // for ETC1S and UASTC.
  391. //
  392. // In some cases, transcoding UASTC to RGBA32 might be preferred for higher quality (at
  393. // significant memory cost) compared to ETC1/2, BC1/3, and PVRTC. The transcoder currently
  394. // chooses RGBA32 only as a last resort and does not expose that option to the caller.
  395. var FORMAT_OPTIONS = [
  396. {
  397. if: 'astcSupported',
  398. basisFormat: [ BasisFormat.UASTC_4x4 ],
  399. transcoderFormat: [ TranscoderFormat.ASTC_4x4, TranscoderFormat.ASTC_4x4 ],
  400. engineFormat: [ EngineFormat.RGBA_ASTC_4x4_Format, EngineFormat.RGBA_ASTC_4x4_Format ],
  401. priorityETC1S: Infinity,
  402. priorityUASTC: 1,
  403. needsPowerOfTwo: false,
  404. },
  405. {
  406. if: 'bptcSupported',
  407. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  408. transcoderFormat: [ TranscoderFormat.BC7_M5, TranscoderFormat.BC7_M5 ],
  409. engineFormat: [ EngineFormat.RGBA_BPTC_Format, EngineFormat.RGBA_BPTC_Format ],
  410. priorityETC1S: 3,
  411. priorityUASTC: 2,
  412. needsPowerOfTwo: false,
  413. },
  414. {
  415. if: 'dxtSupported',
  416. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  417. transcoderFormat: [ TranscoderFormat.BC1, TranscoderFormat.BC3 ],
  418. engineFormat: [ EngineFormat.RGB_S3TC_DXT1_Format, EngineFormat.RGBA_S3TC_DXT5_Format ],
  419. priorityETC1S: 4,
  420. priorityUASTC: 5,
  421. needsPowerOfTwo: false,
  422. },
  423. {
  424. if: 'etc2Supported',
  425. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  426. transcoderFormat: [ TranscoderFormat.ETC1, TranscoderFormat.ETC2 ],
  427. engineFormat: [ EngineFormat.RGB_ETC2_Format, EngineFormat.RGBA_ETC2_EAC_Format ],
  428. priorityETC1S: 1,
  429. priorityUASTC: 3,
  430. needsPowerOfTwo: false,
  431. },
  432. {
  433. if: 'etc1Supported',
  434. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  435. transcoderFormat: [ TranscoderFormat.ETC1, TranscoderFormat.ETC1 ],
  436. engineFormat: [ EngineFormat.RGB_ETC1_Format, EngineFormat.RGB_ETC1_Format ],
  437. priorityETC1S: 2,
  438. priorityUASTC: 4,
  439. needsPowerOfTwo: false,
  440. },
  441. {
  442. if: 'pvrtcSupported',
  443. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  444. transcoderFormat: [ TranscoderFormat.PVRTC1_4_RGB, TranscoderFormat.PVRTC1_4_RGBA ],
  445. engineFormat: [ EngineFormat.RGB_PVRTC_4BPPV1_Format, EngineFormat.RGBA_PVRTC_4BPPV1_Format ],
  446. priorityETC1S: 5,
  447. priorityUASTC: 6,
  448. needsPowerOfTwo: true,
  449. },
  450. ];
  451. var ETC1S_OPTIONS = FORMAT_OPTIONS.sort( function ( a, b ) {
  452. return a.priorityETC1S - b.priorityETC1S;
  453. } );
  454. var UASTC_OPTIONS = FORMAT_OPTIONS.sort( function ( a, b ) {
  455. return a.priorityUASTC - b.priorityUASTC;
  456. } );
  457. function getTranscoderFormat( basisFormat, width, height, hasAlpha ) {
  458. var transcoderFormat;
  459. var engineFormat;
  460. var options = basisFormat === BasisFormat.ETC1S ? ETC1S_OPTIONS : UASTC_OPTIONS;
  461. for ( var i = 0; i < options.length; i ++ ) {
  462. var opt = options[ i ];
  463. if ( ! config[ opt.if ] ) continue;
  464. if ( ! opt.basisFormat.includes( basisFormat ) ) continue;
  465. if ( opt.needsPowerOfTwo && ! ( isPowerOfTwo( width ) && isPowerOfTwo( height ) ) ) continue;
  466. transcoderFormat = opt.transcoderFormat[ hasAlpha ? 1 : 0 ];
  467. engineFormat = opt.engineFormat[ hasAlpha ? 1 : 0 ];
  468. return { transcoderFormat, engineFormat };
  469. }
  470. console.warn( 'THREE.BasisTextureLoader: No suitable compressed texture format found. Decoding to RGBA32.' );
  471. transcoderFormat = TranscoderFormat.RGBA32;
  472. engineFormat = EngineFormat.RGBAFormat;
  473. return { transcoderFormat, engineFormat };
  474. }
  475. function assert( ok, message ) {
  476. if ( ! ok ) throw new Error( message );
  477. }
  478. function getWidthInBlocks( transcoderFormat, width ) {
  479. return Math.ceil( width / BasisModule.getFormatBlockWidth( transcoderFormat ) );
  480. }
  481. function getHeightInBlocks( transcoderFormat, height ) {
  482. return Math.ceil( height / BasisModule.getFormatBlockHeight( transcoderFormat ) );
  483. }
  484. function getTranscodedImageByteLength( transcoderFormat, width, height ) {
  485. var blockByteLength = BasisModule.getBytesPerBlockOrPixel( transcoderFormat );
  486. if ( BasisModule.formatIsUncompressed( transcoderFormat ) ) {
  487. return width * height * blockByteLength;
  488. }
  489. if ( transcoderFormat === TranscoderFormat.PVRTC1_4_RGB
  490. || transcoderFormat === TranscoderFormat.PVRTC1_4_RGBA ) {
  491. // GL requires extra padding for very small textures:
  492. // https://www.khronos.org/registry/OpenGL/extensions/IMG/IMG_texture_compression_pvrtc.txt
  493. var paddedWidth = ( width + 3 ) & ~ 3;
  494. var paddedHeight = ( height + 3 ) & ~ 3;
  495. return ( Math.max( 8, paddedWidth ) * Math.max( 8, paddedHeight ) * 4 + 7 ) / 8;
  496. }
  497. return ( getWidthInBlocks( transcoderFormat, width )
  498. * getHeightInBlocks( transcoderFormat, height )
  499. * blockByteLength );
  500. }
  501. function isPowerOfTwo( value ) {
  502. if ( value <= 2 ) return true;
  503. return ( value & ( value - 1 ) ) === 0 && value !== 0;
  504. }
  505. };