BasisTextureLoader.js 19 KB

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