2
0

webgl_loader_texture_ktx2.html 5.1 KB

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