webgl_loader_texture_ktx2.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  31. let camera, scene, renderer, controls, loader, material;
  32. const SAMPLES = {
  33. 'BasisU ETC1S': '2d_etc1s.ktx2',
  34. 'BasisU UASTC': '2d_uastc.ktx2',
  35. 'RGBA8 sRGB': '2d_rgba8.ktx2',
  36. 'RGBA8 Linear': '2d_rgba8_linear.ktx2',
  37. 'RGBA16 Linear': '2d_rgba16_linear.ktx2',
  38. 'RGBA32 Linear': '2d_rgba32_linear.ktx2',
  39. 'ASTC 6x6 (mobile)': '2d_astc_6x6.ktx2',
  40. };
  41. const FORMAT_LABELS = {
  42. [ THREE.RGBAFormat ]: 'RGBA',
  43. [ THREE.RGBA_BPTC_Format ]: 'RGBA_BPTC',
  44. [ THREE.RGBA_ASTC_4x4_Format ]: 'RGBA_ASTC_4x4',
  45. [ THREE.RGB_S3TC_DXT1_Format ]: 'RGB_S3TC_DXT1',
  46. [ THREE.RGBA_S3TC_DXT5_Format ]: 'RGBA_S3TC_DXT5',
  47. [ THREE.RGB_PVRTC_4BPPV1_Format ]: 'RGB_PVRTC_4BPPV1',
  48. [ THREE.RGBA_PVRTC_4BPPV1_Format ]: 'RGBA_PVRTC_4BPPV1',
  49. [ THREE.RGB_ETC1_Format ]: 'RGB_ETC1',
  50. [ THREE.RGB_ETC2_Format ]: 'RGB_ETC2',
  51. [ THREE.RGBA_ETC2_EAC_Format ]: 'RGB_ETC2_EAC',
  52. };
  53. const TYPE_LABELS = {
  54. [ THREE.UnsignedByteType ]: 'UnsignedByteType',
  55. [ THREE.ByteType ]: 'ByteType',
  56. [ THREE.ShortType ]: 'ShortType',
  57. [ THREE.UnsignedShortType ]: 'UnsignedShortType',
  58. [ THREE.IntType ]: 'IntType',
  59. [ THREE.UnsignedIntType ]: 'UnsignedIntType',
  60. [ THREE.FloatType ]: 'FloatType',
  61. [ THREE.HalfFloatType ]: 'HalfFloatType',
  62. };
  63. const params = {
  64. sample: Object.values( SAMPLES )[ 0 ],
  65. };
  66. init().then( animate );
  67. async function init() {
  68. const width = window.innerWidth;
  69. const height = window.innerHeight;
  70. renderer = new THREE.WebGLRenderer( { antialias: true } );
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( width, height );
  73. document.body.appendChild( renderer.domElement );
  74. window.addEventListener( 'resize', onWindowResize );
  75. scene = new THREE.Scene();
  76. scene.background = new THREE.Color( 0x202020 );
  77. camera = new THREE.PerspectiveCamera( 60, width / height, 0.1, 100 );
  78. camera.position.set( 0, 0, 2.5 );
  79. camera.lookAt( scene.position );
  80. scene.add( camera );
  81. controls = new OrbitControls( camera, renderer.domElement );
  82. // PlaneGeometry UVs assume flipY=true, which compressed textures don't support.
  83. const geometry = flipY( new THREE.PlaneGeometry() );
  84. material = new THREE.MeshBasicMaterial( {
  85. color: 0xFFFFFF,
  86. side: THREE.DoubleSide,
  87. transparent: true,
  88. } );
  89. const mesh = new THREE.Mesh( geometry, material );
  90. scene.add( mesh );
  91. loader = new KTX2Loader()
  92. .setTranscoderPath( 'jsm/libs/basis/' )
  93. .detectSupport( renderer );
  94. const gui = new GUI();
  95. gui.add( params, 'sample', SAMPLES ).onChange( loadTexture );
  96. await loadTexture( params.sample );
  97. }
  98. function animate() {
  99. requestAnimationFrame( animate );
  100. controls.update();
  101. renderer.render( scene, camera );
  102. }
  103. function onWindowResize() {
  104. const width = window.innerWidth;
  105. const height = window.innerHeight;
  106. camera.aspect = width / height;
  107. camera.updateProjectionMatrix();
  108. renderer.setSize( width, height );
  109. }
  110. async function loadTexture( path ) {
  111. try {
  112. const texture = await loader.loadAsync( `./textures/compressed/${path}` );
  113. texture.minFilter = THREE.NearestMipmapNearestFilter;
  114. material.map = texture;
  115. material.needsUpdate = true;
  116. console.info( `format: ${ FORMAT_LABELS[ texture.format ] }` );
  117. console.info( `type: ${ TYPE_LABELS[ texture.type ] }` );
  118. } catch ( e ) {
  119. console.error( e );
  120. }
  121. // NOTE: Call `loader.dispose()` when finished loading textures.
  122. }
  123. /** Correct UVs to be compatible with `flipY=false` textures. */
  124. function flipY( geometry ) {
  125. const uv = geometry.attributes.uv;
  126. for ( let i = 0; i < uv.count; i ++ ) {
  127. uv.setY( i, 1 - uv.getY( i ) );
  128. }
  129. return geometry;
  130. }
  131. </script>
  132. </body>
  133. </html>