webgl_loader_texture_ktx2.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. var width = window.innerWidth;
  21. var height = window.innerHeight;
  22. var renderer = new THREE.WebGLRenderer( { antialias: true } );
  23. renderer.setSize( width, height );
  24. renderer.outputEncoding = THREE.sRGBEncoding;
  25. document.body.appendChild( renderer.domElement );
  26. var scene = new THREE.Scene();
  27. scene.background = new THREE.Color( 0xF0F0F0 );
  28. var 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. var controls = new OrbitControls( camera, renderer.domElement );
  33. controls.autoRotate = true;
  34. // PlaneBufferGeometry UVs assume flipY=true, which compressed textures don't support.
  35. var geometry = flipY( new THREE.PlaneBufferGeometry() );
  36. var material = new THREE.MeshBasicMaterial( {
  37. color: 0xFFFFFF,
  38. side: THREE.DoubleSide
  39. } );
  40. var mesh = new THREE.Mesh(geometry, material);
  41. scene.add(mesh);
  42. var formatStrings = {
  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. .detectSupport( renderer )
  55. .load( './textures/compressed/sample_uastc.ktx2', ( texture ) => {
  56. console.info( `transcoded to ${formatStrings[texture.format]}` );
  57. material.map = texture;
  58. material.needsUpdate = true;
  59. }, ( p ) => console.log( `...${p}` ), ( e ) => console.error( e ) );
  60. animate();
  61. function animate() {
  62. requestAnimationFrame( animate );
  63. controls.update();
  64. renderer.render( scene, camera );
  65. }
  66. window.addEventListener( 'resize', () => {
  67. var width = window.innerWidth;
  68. var height = window.innerHeight;
  69. camera.aspect = width / height;
  70. camera.updateProjectionMatrix();
  71. renderer.setSize( width, height );
  72. }, false );
  73. /** Correct UVs to be compatible with `flipY=false` textures. */
  74. function flipY ( geometry ) {
  75. var uv = geometry.attributes.uv;
  76. for ( var i = 0; i < uv.count; i ++ ) {
  77. uv.setY( i, 1 - uv.getY( i ) );
  78. }
  79. return geometry;
  80. }
  81. </script>
  82. </body>
  83. </html>