123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - KTX2 texture loader</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - KTX2 texture loader<br />
- <a href="http://github.khronos.org/KTX-Specification/" target="_blank" rel="noopener">KTX2</a> with
- <a href="https://github.com/binomialLLC/basis_universal" target="_blank">Basis Universal GPU Texture Codec</a>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { KTX2Loader } from './jsm/loaders/KTX2Loader.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- const width = window.innerWidth;
- const height = window.innerHeight;
- const renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setSize( width, height );
- renderer.outputEncoding = THREE.sRGBEncoding;
- document.body.appendChild( renderer.domElement );
- const scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x202020 );
- const camera = new THREE.PerspectiveCamera( 60, width / height, 0.1, 100 );
- camera.position.set( 2, 1.5, 1 );
- camera.lookAt( scene.position );
- scene.add( camera );
- const controls = new OrbitControls( camera, renderer.domElement );
- controls.autoRotate = true;
- // PlaneGeometry UVs assume flipY=true, which compressed textures don't support.
- const geometry = flipY( new THREE.PlaneGeometry() );
- const material = new THREE.MeshBasicMaterial( {
- color: 0xFFFFFF,
- side: THREE.DoubleSide
- } );
- const mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- const formatStrings = {
- [ THREE.RGBAFormat ]: "RGBA32",
- [ THREE.RGBA_ASTC_4x4_Format ]: "RGBA_ASTC_4x4",
- [ THREE.RGB_S3TC_DXT1_Format ]: "RGB_S3TC_DXT1",
- [ THREE.RGBA_S3TC_DXT5_Format ]: "RGBA_S3TC_DXT5",
- [ THREE.RGB_PVRTC_4BPPV1_Format ]: "RGB_PVRTC_4BPPV1",
- [ THREE.RGBA_PVRTC_4BPPV1_Format ]: "RGBA_PVRTC_4BPPV1",
- [ THREE.RGB_ETC1_Format ]: "RGB_ETC1",
- [ THREE.RGB_ETC2_Format ]: "RGB_ETC2",
- [ THREE.RGBA_ETC2_EAC_Format ]: "RGB_ETC2_EAC",
- };
- // Samples: sample_etc1s.ktx2, sample_uastc.ktx2
- new KTX2Loader()
- .setTranscoderPath( 'js/libs/basis/' )
- .detectSupport( renderer )
- .load( './textures/compressed/sample_uastc.ktx2', ( texture ) => {
- console.info( `transcoded to ${formatStrings[ texture.format ]}` );
- material.map = texture;
- material.transparent = true;
- material.needsUpdate = true;
- }, ( p ) => console.log( `...${p}` ), ( e ) => console.error( e ) );
- animate();
- function animate() {
- requestAnimationFrame( animate );
- controls.update();
- renderer.render( scene, camera );
- }
- window.addEventListener( 'resize', onWindowResize );
- function onWindowResize() {
- const width = window.innerWidth;
- const height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- }
- /** Correct UVs to be compatible with `flipY=false` textures. */
- function flipY( geometry ) {
- const uv = geometry.attributes.uv;
- for ( let i = 0; i < uv.count; i ++ ) {
- uv.setY( i, 1 - uv.getY( i ) );
- }
- return geometry;
- }
- </script>
- </body>
- </html>
|