BasisTextureLoader.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 ), { ...options,
  77. lowLevel: true
  78. } );
  79. }
  80. /**
  81. * @param {ArrayBuffer[]} buffers
  82. * @param {object?} config
  83. * @return {Promise<CompressedTexture>}
  84. */
  85. _createTexture( buffers, config = {} ) {
  86. let worker;
  87. let taskID;
  88. const taskConfig = config;
  89. let taskCost = 0;
  90. for ( let i = 0; i < buffers.length; i ++ ) {
  91. taskCost += buffers[ i ].byteLength;
  92. }
  93. const texturePending = this._allocateWorker( taskCost ).then( _worker => {
  94. worker = _worker;
  95. taskID = this.workerNextTaskID ++;
  96. return new Promise( ( resolve, reject ) => {
  97. worker._callbacks[ taskID ] = {
  98. resolve,
  99. reject
  100. };
  101. worker.postMessage( {
  102. type: 'transcode',
  103. id: taskID,
  104. buffers: buffers,
  105. taskConfig: taskConfig
  106. }, buffers );
  107. } );
  108. } ).then( message => {
  109. const {
  110. mipmaps,
  111. width,
  112. height,
  113. format
  114. } = message;
  115. const texture = new THREE.CompressedTexture( mipmaps, width, height, format, THREE.UnsignedByteType );
  116. texture.minFilter = mipmaps.length === 1 ? THREE.LinearFilter : THREE.LinearMipmapLinearFilter;
  117. texture.magFilter = THREE.LinearFilter;
  118. texture.generateMipmaps = false;
  119. texture.needsUpdate = true;
  120. return texture;
  121. } ); // Note: replaced '.finally()' with '.catch().then()' block - iOS 11 support (#19416)
  122. texturePending.catch( () => true ).then( () => {
  123. if ( worker && taskID ) {
  124. worker._taskLoad -= taskCost;
  125. delete worker._callbacks[ taskID ];
  126. }
  127. } ); // Cache the task result.
  128. _taskCache.set( buffers[ 0 ], {
  129. promise: texturePending
  130. } );
  131. return texturePending;
  132. }
  133. _initTranscoder() {
  134. if ( ! this.transcoderPending ) {
  135. // Load transcoder wrapper.
  136. const jsLoader = new THREE.FileLoader( this.manager );
  137. jsLoader.setPath( this.transcoderPath );
  138. jsLoader.setWithCredentials( this.withCredentials );
  139. const jsContent = new Promise( ( resolve, reject ) => {
  140. jsLoader.load( 'basis_transcoder.js', resolve, undefined, reject );
  141. } ); // Load transcoder WASM binary.
  142. const binaryLoader = new THREE.FileLoader( this.manager );
  143. binaryLoader.setPath( this.transcoderPath );
  144. binaryLoader.setResponseType( 'arraybuffer' );
  145. binaryLoader.setWithCredentials( this.withCredentials );
  146. const binaryContent = new Promise( ( resolve, reject ) => {
  147. binaryLoader.load( 'basis_transcoder.wasm', resolve, undefined, reject );
  148. } );
  149. this.transcoderPending = Promise.all( [ jsContent, binaryContent ] ).then( ( [ jsContent, binaryContent ] ) => {
  150. const fn = BasisTextureLoader.BasisWorker.toString();
  151. 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' );
  152. this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) );
  153. this.transcoderBinary = binaryContent;
  154. } );
  155. }
  156. return this.transcoderPending;
  157. }
  158. _allocateWorker( taskCost ) {
  159. return this._initTranscoder().then( () => {
  160. if ( this.workerPool.length < this.workerLimit ) {
  161. const 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. const 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. const worker = this.workerPool[ this.workerPool.length - 1 ];
  189. worker._taskLoad += taskCost;
  190. return worker;
  191. } );
  192. }
  193. dispose() {
  194. for ( let 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. BasisTextureLoader.BasisFormat = {
  203. ETC1S: 0,
  204. UASTC_4x4: 1
  205. };
  206. 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. 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. BasisTextureLoader.BasisWorker = function () {
  239. let config;
  240. let transcoderPending;
  241. let BasisModule;
  242. const EngineFormat = _EngineFormat; // eslint-disable-line no-undef
  243. const TranscoderFormat = _TranscoderFormat; // eslint-disable-line no-undef
  244. const BasisFormat = _BasisFormat; // eslint-disable-line no-undef
  245. onmessage = function ( e ) {
  246. const 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. const {
  256. width,
  257. height,
  258. hasAlpha,
  259. mipmaps,
  260. format
  261. } = message.taskConfig.lowLevel ? transcodeLowLevel( message.taskConfig ) : transcode( message.buffers[ 0 ] );
  262. const buffers = [];
  263. for ( let i = 0; i < mipmaps.length; ++ i ) {
  264. buffers.push( mipmaps[ i ].data.buffer );
  265. }
  266. self.postMessage( {
  267. type: 'transcode',
  268. id: message.id,
  269. width,
  270. height,
  271. hasAlpha,
  272. mipmaps,
  273. format
  274. }, buffers );
  275. } catch ( error ) {
  276. console.error( error );
  277. self.postMessage( {
  278. type: 'error',
  279. id: message.id,
  280. error: error.message
  281. } );
  282. }
  283. } );
  284. break;
  285. }
  286. };
  287. function init( wasmBinary ) {
  288. transcoderPending = new Promise( resolve => {
  289. BasisModule = {
  290. wasmBinary,
  291. onRuntimeInitialized: resolve
  292. };
  293. BASIS( BasisModule ); // eslint-disable-line no-undef
  294. } ).then( () => {
  295. BasisModule.initializeBasis();
  296. } );
  297. }
  298. function transcodeLowLevel( taskConfig ) {
  299. const {
  300. basisFormat,
  301. width,
  302. height,
  303. hasAlpha
  304. } = taskConfig;
  305. const {
  306. transcoderFormat,
  307. engineFormat
  308. } = getTranscoderFormat( basisFormat, width, height, hasAlpha );
  309. const blockByteLength = BasisModule.getBytesPerBlockOrPixel( transcoderFormat );
  310. assert( BasisModule.isFormatSupported( transcoderFormat ), 'THREE.BasisTextureLoader: Unsupported format.' );
  311. const mipmaps = [];
  312. if ( basisFormat === BasisFormat.ETC1S ) {
  313. const transcoder = new BasisModule.LowLevelETC1SImageTranscoder();
  314. const {
  315. endpointCount,
  316. endpointsData,
  317. selectorCount,
  318. selectorsData,
  319. tablesData
  320. } = taskConfig.globalData;
  321. try {
  322. let ok;
  323. ok = transcoder.decodePalettes( endpointCount, endpointsData, selectorCount, selectorsData );
  324. assert( ok, 'THREE.BasisTextureLoader: decodePalettes() failed.' );
  325. ok = transcoder.decodeTables( tablesData );
  326. assert( ok, 'THREE.BasisTextureLoader: decodeTables() failed.' );
  327. for ( let i = 0; i < taskConfig.levels.length; i ++ ) {
  328. const level = taskConfig.levels[ i ];
  329. const imageDesc = taskConfig.globalData.imageDescs[ i ];
  330. const dstByteLength = getTranscodedImageByteLength( transcoderFormat, level.width, level.height );
  331. const dst = new Uint8Array( dstByteLength );
  332. 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 );
  333. assert( ok, 'THREE.BasisTextureLoader: transcodeImage() failed for level ' + level.index + '.' );
  334. mipmaps.push( {
  335. data: dst,
  336. width: level.width,
  337. height: level.height
  338. } );
  339. }
  340. } finally {
  341. transcoder.delete();
  342. }
  343. } else {
  344. for ( let i = 0; i < taskConfig.levels.length; i ++ ) {
  345. const level = taskConfig.levels[ i ];
  346. const dstByteLength = getTranscodedImageByteLength( transcoderFormat, level.width, level.height );
  347. const dst = new Uint8Array( dstByteLength );
  348. 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 );
  349. assert( ok, 'THREE.BasisTextureLoader: transcodeUASTCImage() failed for level ' + level.index + '.' );
  350. mipmaps.push( {
  351. data: dst,
  352. width: level.width,
  353. height: level.height
  354. } );
  355. }
  356. }
  357. return {
  358. width,
  359. height,
  360. hasAlpha,
  361. mipmaps,
  362. format: engineFormat
  363. };
  364. }
  365. function transcode( buffer ) {
  366. const basisFile = new BasisModule.BasisFile( new Uint8Array( buffer ) );
  367. const basisFormat = basisFile.isUASTC() ? BasisFormat.UASTC_4x4 : BasisFormat.ETC1S;
  368. const width = basisFile.getImageWidth( 0, 0 );
  369. const height = basisFile.getImageHeight( 0, 0 );
  370. const levels = basisFile.getNumLevels( 0 );
  371. const hasAlpha = basisFile.getHasAlpha();
  372. function cleanup() {
  373. basisFile.close();
  374. basisFile.delete();
  375. }
  376. const {
  377. transcoderFormat,
  378. engineFormat
  379. } = getTranscoderFormat( basisFormat, width, height, hasAlpha );
  380. if ( ! width || ! height || ! levels ) {
  381. cleanup();
  382. throw new Error( 'THREE.BasisTextureLoader: Invalid texture' );
  383. }
  384. if ( ! basisFile.startTranscoding() ) {
  385. cleanup();
  386. throw new Error( 'THREE.BasisTextureLoader: .startTranscoding failed' );
  387. }
  388. const mipmaps = [];
  389. for ( let mip = 0; mip < levels; mip ++ ) {
  390. const mipWidth = basisFile.getImageWidth( 0, mip );
  391. const mipHeight = basisFile.getImageHeight( 0, mip );
  392. const dst = new Uint8Array( basisFile.getImageTranscodedSizeInBytes( 0, mip, transcoderFormat ) );
  393. const status = basisFile.transcodeImage( dst, 0, mip, transcoderFormat, 0, hasAlpha );
  394. if ( ! status ) {
  395. cleanup();
  396. throw new Error( 'THREE.BasisTextureLoader: .transcodeImage failed.' );
  397. }
  398. mipmaps.push( {
  399. data: dst,
  400. width: mipWidth,
  401. height: mipHeight
  402. } );
  403. }
  404. cleanup();
  405. return {
  406. width,
  407. height,
  408. hasAlpha,
  409. mipmaps,
  410. format: engineFormat
  411. };
  412. } //
  413. // Optimal choice of a transcoder target format depends on the Basis format (ETC1S or UASTC),
  414. // device capabilities, and texture dimensions. The list below ranks the formats separately
  415. // for ETC1S and UASTC.
  416. //
  417. // In some cases, transcoding UASTC to RGBA32 might be preferred for higher quality (at
  418. // significant memory cost) compared to ETC1/2, BC1/3, and PVRTC. The transcoder currently
  419. // chooses RGBA32 only as a last resort and does not expose that option to the caller.
  420. const FORMAT_OPTIONS = [ {
  421. if: 'astcSupported',
  422. basisFormat: [ BasisFormat.UASTC_4x4 ],
  423. transcoderFormat: [ TranscoderFormat.ASTC_4x4, TranscoderFormat.ASTC_4x4 ],
  424. engineFormat: [ EngineFormat.RGBA_ASTC_4x4_Format, EngineFormat.RGBA_ASTC_4x4_Format ],
  425. priorityETC1S: Infinity,
  426. priorityUASTC: 1,
  427. needsPowerOfTwo: false
  428. }, {
  429. if: 'bptcSupported',
  430. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  431. transcoderFormat: [ TranscoderFormat.BC7_M5, TranscoderFormat.BC7_M5 ],
  432. engineFormat: [ EngineFormat.RGBA_BPTC_Format, EngineFormat.RGBA_BPTC_Format ],
  433. priorityETC1S: 3,
  434. priorityUASTC: 2,
  435. needsPowerOfTwo: false
  436. }, {
  437. if: 'dxtSupported',
  438. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  439. transcoderFormat: [ TranscoderFormat.BC1, TranscoderFormat.BC3 ],
  440. engineFormat: [ EngineFormat.RGB_S3TC_DXT1_Format, EngineFormat.RGBA_S3TC_DXT5_Format ],
  441. priorityETC1S: 4,
  442. priorityUASTC: 5,
  443. needsPowerOfTwo: false
  444. }, {
  445. if: 'etc2Supported',
  446. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  447. transcoderFormat: [ TranscoderFormat.ETC1, TranscoderFormat.ETC2 ],
  448. engineFormat: [ EngineFormat.RGB_ETC2_Format, EngineFormat.RGBA_ETC2_EAC_Format ],
  449. priorityETC1S: 1,
  450. priorityUASTC: 3,
  451. needsPowerOfTwo: false
  452. }, {
  453. if: 'etc1Supported',
  454. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  455. transcoderFormat: [ TranscoderFormat.ETC1, TranscoderFormat.ETC1 ],
  456. engineFormat: [ EngineFormat.RGB_ETC1_Format, EngineFormat.RGB_ETC1_Format ],
  457. priorityETC1S: 2,
  458. priorityUASTC: 4,
  459. needsPowerOfTwo: false
  460. }, {
  461. if: 'pvrtcSupported',
  462. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  463. transcoderFormat: [ TranscoderFormat.PVRTC1_4_RGB, TranscoderFormat.PVRTC1_4_RGBA ],
  464. engineFormat: [ EngineFormat.RGB_PVRTC_4BPPV1_Format, EngineFormat.RGBA_PVRTC_4BPPV1_Format ],
  465. priorityETC1S: 5,
  466. priorityUASTC: 6,
  467. needsPowerOfTwo: true
  468. } ];
  469. const ETC1S_OPTIONS = FORMAT_OPTIONS.sort( function ( a, b ) {
  470. return a.priorityETC1S - b.priorityETC1S;
  471. } );
  472. const UASTC_OPTIONS = FORMAT_OPTIONS.sort( function ( a, b ) {
  473. return a.priorityUASTC - b.priorityUASTC;
  474. } );
  475. function getTranscoderFormat( basisFormat, width, height, hasAlpha ) {
  476. let transcoderFormat;
  477. let engineFormat;
  478. const options = basisFormat === BasisFormat.ETC1S ? ETC1S_OPTIONS : UASTC_OPTIONS;
  479. for ( let i = 0; i < options.length; i ++ ) {
  480. const opt = options[ i ];
  481. if ( ! config[ opt.if ] ) continue;
  482. if ( ! opt.basisFormat.includes( basisFormat ) ) continue;
  483. if ( opt.needsPowerOfTwo && ! ( isPowerOfTwo( width ) && isPowerOfTwo( height ) ) ) continue;
  484. transcoderFormat = opt.transcoderFormat[ hasAlpha ? 1 : 0 ];
  485. engineFormat = opt.engineFormat[ hasAlpha ? 1 : 0 ];
  486. return {
  487. transcoderFormat,
  488. engineFormat
  489. };
  490. }
  491. console.warn( 'THREE.BasisTextureLoader: No suitable compressed texture format found. Decoding to RGBA32.' );
  492. transcoderFormat = TranscoderFormat.RGBA32;
  493. engineFormat = EngineFormat.RGBAFormat;
  494. return {
  495. transcoderFormat,
  496. engineFormat
  497. };
  498. }
  499. function assert( ok, message ) {
  500. if ( ! ok ) throw new Error( message );
  501. }
  502. function getWidthInBlocks( transcoderFormat, width ) {
  503. return Math.ceil( width / BasisModule.getFormatBlockWidth( transcoderFormat ) );
  504. }
  505. function getHeightInBlocks( transcoderFormat, height ) {
  506. return Math.ceil( height / BasisModule.getFormatBlockHeight( transcoderFormat ) );
  507. }
  508. function getTranscodedImageByteLength( transcoderFormat, width, height ) {
  509. const blockByteLength = BasisModule.getBytesPerBlockOrPixel( transcoderFormat );
  510. if ( BasisModule.formatIsUncompressed( transcoderFormat ) ) {
  511. return width * height * blockByteLength;
  512. }
  513. if ( transcoderFormat === TranscoderFormat.PVRTC1_4_RGB || transcoderFormat === TranscoderFormat.PVRTC1_4_RGBA ) {
  514. // GL requires extra padding for very small textures:
  515. // https://www.khronos.org/registry/OpenGL/extensions/IMG/IMG_texture_compression_pvrtc.txt
  516. const paddedWidth = width + 3 & ~ 3;
  517. const paddedHeight = height + 3 & ~ 3;
  518. return ( Math.max( 8, paddedWidth ) * Math.max( 8, paddedHeight ) * 4 + 7 ) / 8;
  519. }
  520. return getWidthInBlocks( transcoderFormat, width ) * getHeightInBlocks( transcoderFormat, height ) * blockByteLength;
  521. }
  522. function isPowerOfTwo( value ) {
  523. if ( value <= 2 ) return true;
  524. return ( value & value - 1 ) === 0 && value !== 0;
  525. }
  526. };
  527. THREE.BasisTextureLoader = BasisTextureLoader;
  528. } )();