123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <!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>
- <!-- Import maps polyfill -->
- <!-- Remove this when import maps will be widely supported -->
- <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- let camera, scene, renderer, controls;
- init().then( animate );
- async function init() {
- const width = window.innerWidth;
- const height = window.innerHeight;
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setSize( width, height );
- document.body.appendChild( renderer.domElement );
- window.addEventListener( 'resize', onWindowResize );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x202020 );
- camera = new THREE.PerspectiveCamera( 60, width / height, 0.1, 100 );
- camera.position.set( 2, 1.5, 1 );
- camera.lookAt( scene.position );
- scene.add( camera );
- 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_BPTC_Format ]: 'RGBA_BPTC',
- [ 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, sample_uastc_zstd.ktx2
- const loader = new KTX2Loader()
- .setTranscoderPath( 'jsm/libs/basis/' )
- .detectSupport( renderer );
- try {
- const texture = await loader.loadAsync( './textures/compressed/sample_uastc_zstd.ktx2' );
- console.info( `transcoded to ${formatStrings[ texture.format ]}` );
- material.map = texture;
- material.transparent = true;
- material.needsUpdate = true;
- } catch ( e ) {
- console.error( e );
- } finally {
- loader.dispose();
- }
- }
- function animate() {
- requestAnimationFrame( animate );
- controls.update();
- renderer.render( scene, camera );
- }
- 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>
|