BasisTextureLoader.js 19 KB

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