BasisTextureLoader.js 19 KB

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