webgl_loader_texture_ktx2.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { KTX2Loader } from './jsm/loaders/KTX2Loader.js';
  28. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  29. const width = window.innerWidth;
  30. const height = window.innerHeight;
  31. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  32. renderer.setSize( width, height );
  33. renderer.outputEncoding = THREE.sRGBEncoding;
  34. document.body.appendChild( renderer.domElement );
  35. const scene = new THREE.Scene();
  36. scene.background = new THREE.Color( 0x202020 );
  37. const camera = new THREE.PerspectiveCamera( 60, width / height, 0.1, 100 );
  38. camera.position.set( 2, 1.5, 1 );
  39. camera.lookAt( scene.position );
  40. scene.add( camera );
  41. const controls = new OrbitControls( camera, renderer.domElement );
  42. controls.autoRotate = true;
  43. // PlaneGeometry UVs assume flipY=true, which compressed textures don't support.
  44. const geometry = flipY( new THREE.PlaneGeometry() );
  45. const material = new THREE.MeshBasicMaterial( {
  46. color: 0xFFFFFF,
  47. side: THREE.DoubleSide
  48. } );
  49. const mesh = new THREE.Mesh( geometry, material );
  50. scene.add( mesh );
  51. const formatStrings = {
  52. [ THREE.RGBAFormat ]: 'RGBA32',
  53. [ THREE.RGBA_BPTC_Format ]: 'RGBA_BPTC',
  54. [ THREE.RGBA_ASTC_4x4_Format ]: 'RGBA_ASTC_4x4',
  55. [ THREE.RGB_S3TC_DXT1_Format ]: 'RGB_S3TC_DXT1',
  56. [ THREE.RGBA_S3TC_DXT5_Format ]: 'RGBA_S3TC_DXT5',
  57. [ THREE.RGB_PVRTC_4BPPV1_Format ]: 'RGB_PVRTC_4BPPV1',
  58. [ THREE.RGBA_PVRTC_4BPPV1_Format ]: 'RGBA_PVRTC_4BPPV1',
  59. [ THREE.RGB_ETC1_Format ]: 'RGB_ETC1',
  60. [ THREE.RGB_ETC2_Format ]: 'RGB_ETC2',
  61. [ THREE.RGBA_ETC2_EAC_Format ]: 'RGB_ETC2_EAC',
  62. };
  63. // Samples: sample_etc1s.ktx2, sample_uastc.ktx2, sample_uastc_zstd.ktx2
  64. const loader = new KTX2Loader()
  65. .setTranscoderPath( 'js/libs/basis/' )
  66. .detectSupport( renderer );
  67. animate();
  68. try {
  69. const texture = await loader.loadAsync( './textures/compressed/sample_uastc_zstd.ktx2' );
  70. console.info( `transcoded to ${formatStrings[ texture.format ]}` );
  71. material.map = texture;
  72. material.transparent = true;
  73. material.needsUpdate = true;
  74. } catch ( e ) {
  75. console.error( e );
  76. } finally {
  77. loader.dispose();
  78. }
  79. function animate() {
  80. requestAnimationFrame( animate );
  81. controls.update();
  82. renderer.render( scene, camera );
  83. }
  84. window.addEventListener( 'resize', onWindowResize );
  85. function onWindowResize() {
  86. const width = window.innerWidth;
  87. const height = window.innerHeight;
  88. camera.aspect = width / height;
  89. camera.updateProjectionMatrix();
  90. renderer.setSize( width, height );
  91. }
  92. /** Correct UVs to be compatible with `flipY=false` textures. */
  93. function flipY( geometry ) {
  94. const uv = geometry.attributes.uv;
  95. for ( let i = 0; i < uv.count; i ++ ) {
  96. uv.setY( i, 1 - uv.getY( i ) );
  97. }
  98. return geometry;
  99. }
  100. </script>
  101. </body>
  102. </html>