2
0

webgl_loader_texture_ktx2.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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="module">
  16. import * as THREE from '../build/three.module.js';
  17. import { KTX2Loader } from './jsm/loaders/KTX2Loader.js';
  18. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  19. const width = window.innerWidth;
  20. const height = window.innerHeight;
  21. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  22. renderer.setSize( width, height );
  23. renderer.outputEncoding = THREE.sRGBEncoding;
  24. document.body.appendChild( renderer.domElement );
  25. const scene = new THREE.Scene();
  26. scene.background = new THREE.Color( 0x202020 );
  27. const camera = new THREE.PerspectiveCamera( 60, width / height, 0.1, 100 );
  28. camera.position.set( 2, 1.5, 1 );
  29. camera.lookAt( scene.position );
  30. scene.add( camera );
  31. const controls = new OrbitControls( camera, renderer.domElement );
  32. controls.autoRotate = true;
  33. // PlaneGeometry UVs assume flipY=true, which compressed textures don't support.
  34. const geometry = flipY( new THREE.PlaneGeometry() );
  35. const material = new THREE.MeshBasicMaterial( {
  36. color: 0xFFFFFF,
  37. side: THREE.DoubleSide
  38. } );
  39. const mesh = new THREE.Mesh( geometry, material );
  40. scene.add( mesh );
  41. const formatStrings = {
  42. [ THREE.RGBAFormat ]: "RGBA32",
  43. [ THREE.RGBA_ASTC_4x4_Format ]: "RGBA_ASTC_4x4",
  44. [ THREE.RGB_S3TC_DXT1_Format ]: "RGB_S3TC_DXT1",
  45. [ THREE.RGBA_S3TC_DXT5_Format ]: "RGBA_S3TC_DXT5",
  46. [ THREE.RGB_PVRTC_4BPPV1_Format ]: "RGB_PVRTC_4BPPV1",
  47. [ THREE.RGBA_PVRTC_4BPPV1_Format ]: "RGBA_PVRTC_4BPPV1",
  48. [ THREE.RGB_ETC1_Format ]: "RGB_ETC1",
  49. [ THREE.RGB_ETC2_Format ]: "RGB_ETC2",
  50. [ THREE.RGBA_ETC2_EAC_Format ]: "RGB_ETC2_EAC",
  51. };
  52. // Samples: sample_etc1s.ktx2, sample_uastc.ktx2
  53. new KTX2Loader()
  54. .setTranscoderPath( 'js/libs/basis/' )
  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', onWindowResize );
  69. function onWindowResize() {
  70. const width = window.innerWidth;
  71. const height = window.innerHeight;
  72. camera.aspect = width / height;
  73. camera.updateProjectionMatrix();
  74. renderer.setSize( width, height );
  75. }
  76. /** Correct UVs to be compatible with `flipY=false` textures. */
  77. function flipY( geometry ) {
  78. const uv = geometry.attributes.uv;
  79. for ( let i = 0; i < uv.count; i ++ ) {
  80. uv.setY( i, 1 - uv.getY( i ) );
  81. }
  82. return geometry;
  83. }
  84. </script>
  85. </body>
  86. </html>