webgl_loader_texture_ktx2.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - KTX2 texture loader</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - KTX2 texture loader<br />
  12. <a href="http://github.khronos.org/KTX-Specification/" target="_blank" rel="noopener">KTX2</a> with
  13. <a href="https://github.com/binomialLLC/basis_universal" target="_blank">Basis Universal GPU Texture Codec</a>
  14. </div>
  15. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. let camera, scene, renderer, controls;
  31. init().then( animate );
  32. async function init() {
  33. const width = window.innerWidth;
  34. const height = window.innerHeight;
  35. renderer = new THREE.WebGLRenderer( { antialias: true } );
  36. renderer.setSize( width, height );
  37. document.body.appendChild( renderer.domElement );
  38. window.addEventListener( 'resize', onWindowResize );
  39. scene = new THREE.Scene();
  40. scene.background = new THREE.Color( 0x202020 );
  41. camera = new THREE.PerspectiveCamera( 60, width / height, 0.1, 100 );
  42. camera.position.set( 2, 1.5, 1 );
  43. camera.lookAt( scene.position );
  44. scene.add( camera );
  45. controls = new OrbitControls( camera, renderer.domElement );
  46. controls.autoRotate = true;
  47. // PlaneGeometry UVs assume flipY=true, which compressed textures don't support.
  48. const geometry = flipY( new THREE.PlaneGeometry() );
  49. const material = new THREE.MeshBasicMaterial( {
  50. color: 0xFFFFFF,
  51. side: THREE.DoubleSide
  52. } );
  53. const mesh = new THREE.Mesh( geometry, material );
  54. scene.add( mesh );
  55. const formatStrings = {
  56. [ THREE.RGBAFormat ]: 'RGBA32',
  57. [ THREE.RGBA_BPTC_Format ]: 'RGBA_BPTC',
  58. [ THREE.RGBA_ASTC_4x4_Format ]: 'RGBA_ASTC_4x4',
  59. [ THREE.RGB_S3TC_DXT1_Format ]: 'RGB_S3TC_DXT1',
  60. [ THREE.RGBA_S3TC_DXT5_Format ]: 'RGBA_S3TC_DXT5',
  61. [ THREE.RGB_PVRTC_4BPPV1_Format ]: 'RGB_PVRTC_4BPPV1',
  62. [ THREE.RGBA_PVRTC_4BPPV1_Format ]: 'RGBA_PVRTC_4BPPV1',
  63. [ THREE.RGB_ETC1_Format ]: 'RGB_ETC1',
  64. [ THREE.RGB_ETC2_Format ]: 'RGB_ETC2',
  65. [ THREE.RGBA_ETC2_EAC_Format ]: 'RGB_ETC2_EAC',
  66. };
  67. // Samples: sample_etc1s.ktx2, sample_uastc.ktx2, sample_uastc_zstd.ktx2
  68. const loader = new KTX2Loader()
  69. .setTranscoderPath( 'jsm/libs/basis/' )
  70. .detectSupport( renderer );
  71. try {
  72. const texture = await loader.loadAsync( './textures/compressed/sample_uastc_zstd.ktx2' );
  73. console.info( `transcoded to ${formatStrings[ texture.format ]}` );
  74. material.map = texture;
  75. material.transparent = true;
  76. material.needsUpdate = true;
  77. } catch ( e ) {
  78. console.error( e );
  79. } finally {
  80. loader.dispose();
  81. }
  82. }
  83. function animate() {
  84. requestAnimationFrame( animate );
  85. controls.update();
  86. renderer.render( scene, camera );
  87. }
  88. function onWindowResize() {
  89. const width = window.innerWidth;
  90. const height = window.innerHeight;
  91. camera.aspect = width / height;
  92. camera.updateProjectionMatrix();
  93. renderer.setSize( width, height );
  94. }
  95. /** Correct UVs to be compatible with `flipY=false` textures. */
  96. function flipY( geometry ) {
  97. const uv = geometry.attributes.uv;
  98. for ( let i = 0; i < uv.count; i ++ ) {
  99. uv.setY( i, 1 - uv.getY( i ) );
  100. }
  101. return geometry;
  102. }
  103. </script>
  104. </body>
  105. </html>