webgl_loader_texture_ktx2.html 7.4 KB

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