webgl_loader_texture_ktx2.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. <style>
  9. * {
  10. box-sizing: border-box;
  11. -moz-box-sizing: border-box;
  12. }
  13. body {
  14. background-color: #fff;
  15. color: #444;
  16. }
  17. a {
  18. color: #08f;
  19. }
  20. #content {
  21. position: absolute;
  22. top: 0; width: 100%;
  23. z-index: 1;
  24. padding: 3em 0 0 0;
  25. }
  26. section {
  27. padding: 1em;
  28. }
  29. #c {
  30. position: absolute;
  31. left: 0;
  32. width: 100%;
  33. height: 100%;
  34. }
  35. section .description {
  36. max-width: 50em;
  37. text-wrap: pretty;
  38. }
  39. .list-item {
  40. display: inline-block;
  41. margin: 1em;
  42. padding: 1em;
  43. box-shadow: 1px 2px 4px 0px rgba(0,0,0,0.25);
  44. }
  45. .list-item > div:nth-child(1) {
  46. width: 200px;
  47. height: 200px;
  48. }
  49. .list-item > div:nth-child(2) {
  50. color: #888;
  51. font-family: sans-serif;
  52. width: 200px;
  53. margin-top: 0.5em;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <canvas id="c"></canvas>
  59. <div id="content">
  60. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - KTX2 texture loader - webgl</div>
  61. </div>
  62. <script type="importmap">
  63. {
  64. "imports": {
  65. "three": "../build/three.module.js",
  66. "three/addons/": "./jsm/"
  67. }
  68. }
  69. </script>
  70. <script type="module">
  71. import * as THREE from 'three';
  72. import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
  73. let canvas, renderer;
  74. const scenes = [];
  75. const sections = [
  76. {
  77. title: 'Uncompressed',
  78. description: 'Uncompressed formats (rgba8, rgba16, rgba32) load as THREE.DataTexture objects.'
  79. + ' Lossless, easy to read/write, uncompressed on GPU, optionally compressed over the network.',
  80. textures: [
  81. { path: '2d_rgba8.ktx2' },
  82. { path: '2d_rgba8_linear.ktx2' },
  83. { path: '2d_rgba16_linear.ktx2' },
  84. { path: '2d_rgba32_linear.ktx2' },
  85. { path: '2d_rgb9e5_linear.ktx2' },
  86. { path: '2d_r11g11b10_linear.ktx2' },
  87. ]
  88. },
  89. {
  90. title: 'Compressed',
  91. description: 'Compressed formats (ASTC, BCn, ...) load as THREE.CompressedTexture objects,'
  92. + ' reducing memory cost. Requires native support on the device GPU: no single compressed'
  93. + ' format is supported on every device.',
  94. textures: [
  95. { path: '2d_astc4x4.ktx2' },
  96. { path: '2d_etc1.ktx2' },
  97. { path: '2d_etc2.ktx2' },
  98. { path: '2d_bc1.ktx2' },
  99. { path: '2d_bc3.ktx2' },
  100. { path: '2d_bc4.ktx2' },
  101. { path: '2d_bc5.ktx2' },
  102. { path: '2d_bc7.ktx2' },
  103. ]
  104. },
  105. {
  106. title: 'Universal',
  107. description: 'Basis Universal textures are specialized intermediate formats supporting fast'
  108. + ' runtime transcoding into other GPU texture compression formats. After transcoding,'
  109. + ' universal textures can be used on any device at reduced memory cost.',
  110. textures: [
  111. { path: '2d_etc1s.ktx2' },
  112. { path: '2d_uastc.ktx2' },
  113. { path: '2d_uastc_hdr4x4.ktx2' },
  114. ]
  115. },
  116. ];
  117. init();
  118. async function init() {
  119. canvas = document.getElementById( 'c' );
  120. renderer = new THREE.WebGLRenderer( { canvas, antialias: true } );
  121. renderer.setClearColor( 0xffffff, 1 );
  122. renderer.setPixelRatio( window.devicePixelRatio );
  123. const loader = new KTX2Loader()
  124. .setTranscoderPath( 'jsm/libs/basis/' )
  125. .setPath( 'textures/ktx2/' )
  126. .detectSupport( renderer );
  127. const geometry = flipY( new THREE.PlaneGeometry( 1, 1 ) );
  128. const content = document.getElementById( 'content' );
  129. for ( const section of sections ) {
  130. const sectionElement = document.createElement( 'section' );
  131. const sectionHeader = document.createElement( 'h2' );
  132. sectionHeader.textContent = section.title;
  133. sectionElement.appendChild( sectionHeader );
  134. const sectionDescription = document.createElement( 'p' );
  135. sectionDescription.className = 'description';
  136. sectionDescription.textContent = section.description;
  137. sectionElement.appendChild( sectionDescription );
  138. for ( const { path, supported } of section.textures ) {
  139. const scene = new THREE.Scene();
  140. // make a list item
  141. const element = document.createElement( 'div' );
  142. element.className = 'list-item';
  143. const sceneElement = document.createElement( 'div' );
  144. element.appendChild( sceneElement );
  145. const labelElement = document.createElement( 'div' );
  146. labelElement.innerText = 'file: ' + path;
  147. element.appendChild( labelElement );
  148. // the element that represents the area we want to render the scene
  149. scene.userData.element = sceneElement;
  150. sectionElement.appendChild( element );
  151. const camera = new THREE.PerspectiveCamera( 50, 1, 1, 10 );
  152. camera.position.z = 2;
  153. scene.userData.camera = camera;
  154. try {
  155. const texture = await loader.loadAsync( supported === false ? 'fail_load.ktx2' : path );
  156. const mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map: texture } ) );
  157. labelElement.innerText += '\ncolorSpace: ' + texture.colorSpace;
  158. scene.add( mesh );
  159. scenes.push( scene );
  160. } catch ( e ) {
  161. console.error( `Failed to load ${path}`, e );
  162. }
  163. }
  164. content.appendChild( sectionElement );
  165. }
  166. renderer.setAnimationLoop( animate );
  167. }
  168. function updateSize() {
  169. const width = canvas.clientWidth;
  170. const height = canvas.clientHeight;
  171. if ( canvas.width !== width || canvas.height !== height ) {
  172. renderer.setSize( width, height, false );
  173. }
  174. }
  175. /** Rewrite UVs for `flipY=false` textures. */
  176. function flipY( geometry ) {
  177. const uv = geometry.attributes.uv;
  178. for ( let i = 0; i < uv.count; i ++ ) {
  179. uv.setY( i, 1 - uv.getY( i ) );
  180. }
  181. return geometry;
  182. }
  183. function animate() {
  184. updateSize();
  185. canvas.style.transform = `translateY(${window.scrollY}px)`;
  186. renderer.setClearColor( 0xffffff );
  187. renderer.setScissorTest( false );
  188. renderer.clear();
  189. renderer.setClearColor( 0xe0e0e0 );
  190. renderer.setScissorTest( true );
  191. scenes.forEach( function ( scene ) {
  192. // get the element that is a place holder for where we want to
  193. // draw the scene
  194. const element = scene.userData.element;
  195. // get its position relative to the page's viewport
  196. const rect = element.getBoundingClientRect();
  197. // check if it's offscreen. If so skip it
  198. if ( rect.bottom < 0 || rect.top > renderer.domElement.clientHeight ||
  199. rect.right < 0 || rect.left > renderer.domElement.clientWidth ) {
  200. return; // it's off screen
  201. }
  202. // set the viewport
  203. const width = rect.right - rect.left;
  204. const height = rect.bottom - rect.top;
  205. const left = rect.left;
  206. const bottom = renderer.domElement.clientHeight - rect.bottom;
  207. renderer.setViewport( left, bottom, width, height );
  208. renderer.setScissor( left, bottom, width, height );
  209. const camera = scene.userData.camera;
  210. renderer.render( scene, camera );
  211. } );
  212. }
  213. </script>
  214. </body>
  215. </html>
粤ICP备19079148号