webgl_loader_texture_ktx2.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 src="./js/libs/basis/msc_basis_transcoder.js"></script>
  16. <script type="module">
  17. import * as THREE from '../build/three.module.js';
  18. import { KTX2Loader } from './jsm/loaders/KTX2Loader.js';
  19. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  20. const width = window.innerWidth;
  21. const height = window.innerHeight;
  22. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  23. renderer.setSize( width, height );
  24. renderer.outputEncoding = THREE.sRGBEncoding;
  25. document.body.appendChild( renderer.domElement );
  26. const scene = new THREE.Scene();
  27. scene.background = new THREE.Color( 0x202020 );
  28. const camera = new THREE.PerspectiveCamera( 60, width / height, 0.1, 100 );
  29. camera.position.set( 2, 1.5, 1 );
  30. camera.lookAt( scene.position );
  31. scene.add( camera );
  32. const controls = new OrbitControls( camera, renderer.domElement );
  33. controls.autoRotate = true;
  34. // PlaneGeometry UVs assume flipY=true, which compressed textures don't support.
  35. const geometry = flipY( new THREE.PlaneGeometry() );
  36. const material = new THREE.MeshBasicMaterial( {
  37. color: 0xFFFFFF,
  38. side: THREE.DoubleSide
  39. } );
  40. const mesh = new THREE.Mesh( geometry, material );
  41. scene.add( mesh );
  42. const formatStrings = {
  43. [ THREE.RGBAFormat ]: "RGBA32",
  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. // Samples: sample_etc1s.ktx2, sample_uastc.ktx2
  54. new KTX2Loader()
  55. .detectSupport( renderer )
  56. .load( './textures/compressed/sample_uastc.ktx2', ( texture ) => {
  57. console.info( `transcoded to ${formatStrings[ texture.format ]}` );
  58. material.map = texture;
  59. material.transparent = true;
  60. material.needsUpdate = true;
  61. }, ( p ) => console.log( `...${p}` ), ( e ) => console.error( e ) );
  62. animate();
  63. function animate() {
  64. requestAnimationFrame( animate );
  65. controls.update();
  66. renderer.render( scene, camera );
  67. }
  68. window.addEventListener( 'resize', () => {
  69. const width = window.innerWidth;
  70. const height = window.innerHeight;
  71. camera.aspect = width / height;
  72. camera.updateProjectionMatrix();
  73. renderer.setSize( width, height );
  74. }, false );
  75. /** Correct UVs to be compatible with `flipY=false` textures. */
  76. function flipY( geometry ) {
  77. const uv = geometry.attributes.uv;
  78. for ( let i = 0; i < uv.count; i ++ ) {
  79. uv.setY( i, 1 - uv.getY( i ) );
  80. }
  81. return geometry;
  82. }
  83. </script>
  84. </body>
  85. </html>