webgl_loader_texture_ktx2.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. ]
  87. },
  88. {
  89. title: 'Compressed',
  90. description: 'Compressed formats (ASTC, BCn, ...) load as THREE.CompressedTexture objects,'
  91. + ' reducing memory cost. Requires native support on the device GPU: no single compressed'
  92. + ' format is supported on every device.',
  93. textures: [
  94. { path: '2d_astc4x4.ktx2' },
  95. { path: '2d_etc1.ktx2' },
  96. { path: '2d_etc2.ktx2' },
  97. { path: '2d_bc1.ktx2' },
  98. { path: '2d_bc3.ktx2' },
  99. // { path: '2d_bc5.ktx2' },
  100. { path: '2d_bc7.ktx2' },
  101. ]
  102. },
  103. {
  104. title: 'Universal',
  105. description: 'Basis Universal textures are specialized intermediate formats supporting fast'
  106. + ' runtime transcoding into other GPU texture compression formats. After transcoding,'
  107. + ' universal textures can be used on any device at reduced memory cost.',
  108. textures: [
  109. { path: '2d_etc1s.ktx2' },
  110. { path: '2d_uastc.ktx2' },
  111. ]
  112. },
  113. ]
  114. init();
  115. async function init() {
  116. canvas = document.getElementById( 'c' );
  117. renderer = new THREE.WebGLRenderer( { canvas, antialias: true } );
  118. renderer.setClearColor( 0xffffff, 1 );
  119. renderer.setPixelRatio( window.devicePixelRatio );
  120. const loader = new KTX2Loader()
  121. .setTranscoderPath( 'jsm/libs/basis/' )
  122. .setPath( 'textures/ktx2/' )
  123. .detectSupport( renderer );
  124. const geometry = flipY( new THREE.PlaneGeometry( 1, 1 ) );
  125. const content = document.getElementById( 'content' );
  126. for ( const section of sections ) {
  127. const sectionElement = document.createElement( 'section' );
  128. const sectionHeader = document.createElement( 'h2' );
  129. sectionHeader.textContent = section.title;
  130. sectionElement.appendChild( sectionHeader );
  131. const sectionDescription = document.createElement( 'p' );
  132. sectionDescription.className = 'description';
  133. sectionDescription.textContent = section.description;
  134. sectionElement.appendChild( sectionDescription );
  135. for ( const { path, supported } of section.textures ) {
  136. const scene = new THREE.Scene();
  137. // make a list item
  138. const element = document.createElement( 'div' );
  139. element.className = 'list-item';
  140. const sceneElement = document.createElement( 'div' );
  141. element.appendChild( sceneElement );
  142. const labelElement = document.createElement( 'div' );
  143. labelElement.innerText = 'file: ' + path;
  144. element.appendChild( labelElement );
  145. // the element that represents the area we want to render the scene
  146. scene.userData.element = sceneElement;
  147. sectionElement.appendChild( element );
  148. const camera = new THREE.PerspectiveCamera( 50, 1, 1, 10 );
  149. camera.position.z = 2;
  150. scene.userData.camera = camera;
  151. try {
  152. const texture = await loader.loadAsync( supported === false ? 'fail_load.ktx2' : path );
  153. const mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial({ map: texture }) );
  154. labelElement.innerText += '\ncolorSpace: ' + texture.colorSpace;
  155. scene.add( mesh );
  156. scenes.push( scene );
  157. } catch (e) {
  158. console.error( `Failed to load ${path}`, e );
  159. }
  160. }
  161. content.appendChild( sectionElement );
  162. }
  163. renderer.setAnimationLoop( animate );
  164. }
  165. function updateSize() {
  166. const width = canvas.clientWidth;
  167. const height = canvas.clientHeight;
  168. if ( canvas.width !== width || canvas.height !== height ) {
  169. renderer.setSize( width, height, false );
  170. }
  171. }
  172. /** Rewrite UVs for `flipY=false` textures. */
  173. function flipY( geometry ) {
  174. const uv = geometry.attributes.uv;
  175. for ( let i = 0; i < uv.count; i ++ ) {
  176. uv.setY( i, 1 - uv.getY( i ) );
  177. }
  178. return geometry;
  179. }
  180. function animate() {
  181. updateSize();
  182. canvas.style.transform = `translateY(${window.scrollY}px)`;
  183. renderer.setClearColor( 0xffffff );
  184. renderer.setScissorTest( false );
  185. renderer.clear();
  186. renderer.setClearColor( 0xe0e0e0 );
  187. renderer.setScissorTest( true );
  188. scenes.forEach( function ( scene ) {
  189. // get the element that is a place holder for where we want to
  190. // draw the scene
  191. const element = scene.userData.element;
  192. // get its position relative to the page's viewport
  193. const rect = element.getBoundingClientRect();
  194. // check if it's offscreen. If so skip it
  195. if ( rect.bottom < 0 || rect.top > renderer.domElement.clientHeight ||
  196. rect.right < 0 || rect.left > renderer.domElement.clientWidth ) {
  197. return; // it's off screen
  198. }
  199. // set the viewport
  200. const width = rect.right - rect.left;
  201. const height = rect.bottom - rect.top;
  202. const left = rect.left;
  203. const bottom = renderer.domElement.clientHeight - rect.bottom;
  204. renderer.setViewport( left, bottom, width, height );
  205. renderer.setScissor( left, bottom, width, height );
  206. const camera = scene.userData.camera;
  207. renderer.render( scene, camera );
  208. } );
  209. }
  210. </script>
  211. </body>
  212. </html>
粤ICP备19079148号