webgl_loader_texture_ktx2.html 4.9 KB

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