webgpu_loader_texture_ktx2.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js WebGPU - 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 WebGPU - KTX2 texture loader">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_loader_texture_ktx2.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_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</div>
  65. </div>
  66. <script type="importmap">
  67. {
  68. "imports": {
  69. "three": "../build/three.webgpu.js",
  70. "three/webgpu": "../build/three.webgpu.js",
  71. "three/tsl": "../build/three.tsl.js",
  72. "three/addons/": "./jsm/"
  73. }
  74. }
  75. </script>
  76. <script type="module">
  77. import * as THREE from 'three';
  78. import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
  79. let canvas, renderer;
  80. const scenes = [];
  81. const sections = [
  82. {
  83. title: 'Uncompressed',
  84. description: 'Uncompressed formats (rgba8, rgba16, rgba32) load as THREE.DataTexture objects.'
  85. + ' Lossless, easy to read/write, uncompressed on GPU, optionally compressed over the network.',
  86. textures: [
  87. { path: '2d_rgba8.ktx2' },
  88. { path: '2d_rgba8_linear.ktx2' },
  89. { path: '2d_rgba16_linear.ktx2' },
  90. { path: '2d_rgba32_linear.ktx2' },
  91. { path: '2d_rgb9e5_linear.ktx2' },
  92. { path: '2d_r11g11b10_linear.ktx2' },
  93. ]
  94. },
  95. {
  96. title: 'Compressed',
  97. description: 'Compressed formats (ASTC, BCn, ...) load as THREE.CompressedTexture objects,'
  98. + ' reducing memory cost. Requires native support on the device GPU: no single compressed'
  99. + ' format is supported on every device.',
  100. textures: [
  101. { path: '2d_astc4x4.ktx2' },
  102. { path: '2d_etc1.ktx2' },
  103. { path: '2d_etc2.ktx2' },
  104. { path: '2d_bc1.ktx2' },
  105. { path: '2d_bc3.ktx2' },
  106. { path: '2d_bc4.ktx2' },
  107. { path: '2d_bc5.ktx2' },
  108. { path: '2d_bc7.ktx2' },
  109. ]
  110. },
  111. {
  112. title: 'Universal',
  113. description: 'Basis Universal textures are specialized intermediate formats supporting fast'
  114. + ' runtime transcoding into other GPU texture compression formats. After transcoding,'
  115. + ' universal textures can be used on any device at reduced memory cost.',
  116. textures: [
  117. { path: '2d_etc1s.ktx2' },
  118. { path: '2d_uastc.ktx2' },
  119. ]
  120. },
  121. ];
  122. init();
  123. async function init() {
  124. canvas = document.getElementById( 'c' );
  125. renderer = new THREE.WebGPURenderer( { canvas, antialias: true, forceWebGL: false } );
  126. renderer.setClearColor( 0xffffff, 1 );
  127. renderer.setPixelRatio( window.devicePixelRatio );
  128. await renderer.init();
  129. const loader = new KTX2Loader()
  130. .setPath( 'textures/ktx2/' )
  131. .detectSupport( renderer );
  132. const geometry = flipY( new THREE.PlaneGeometry( 1, 1 ) );
  133. const content = document.getElementById( 'content' );
  134. for ( const section of sections ) {
  135. const sectionElement = document.createElement( 'section' );
  136. const sectionHeader = document.createElement( 'h2' );
  137. sectionHeader.textContent = section.title;
  138. sectionElement.appendChild( sectionHeader );
  139. const sectionDescription = document.createElement( 'p' );
  140. sectionDescription.className = 'description';
  141. sectionDescription.textContent = section.description;
  142. sectionElement.appendChild( sectionDescription );
  143. for ( const { path, supported } of section.textures ) {
  144. const scene = new THREE.Scene();
  145. // make a list item
  146. const element = document.createElement( 'div' );
  147. element.className = 'list-item';
  148. const sceneElement = document.createElement( 'div' );
  149. element.appendChild( sceneElement );
  150. const labelElement = document.createElement( 'div' );
  151. labelElement.innerText = 'file: ' + path;
  152. element.appendChild( labelElement );
  153. // the element that represents the area we want to render the scene
  154. scene.userData.element = sceneElement;
  155. sectionElement.appendChild( element );
  156. const camera = new THREE.PerspectiveCamera( 50, 1, 1, 10 );
  157. camera.position.z = 2;
  158. scene.userData.camera = camera;
  159. try {
  160. const texture = await loader.loadAsync( supported === false ? 'fail_load.ktx2' : path );
  161. const mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map: texture } ) );
  162. labelElement.innerText += '\ncolorSpace: ' + texture.colorSpace;
  163. scene.add( mesh );
  164. scenes.push( scene );
  165. } catch ( e ) {
  166. console.error( `Failed to load ${path}`, e );
  167. }
  168. }
  169. content.appendChild( sectionElement );
  170. }
  171. renderer.setAnimationLoop( animate );
  172. }
  173. function updateSize() {
  174. const width = canvas.clientWidth;
  175. const height = canvas.clientHeight;
  176. if ( canvas.width !== width || canvas.height !== height ) {
  177. renderer.setSize( width, height, false );
  178. }
  179. }
  180. // Rewrite UVs for `flipY=false` textures.
  181. function flipY( geometry ) {
  182. const uv = geometry.attributes.uv;
  183. for ( let i = 0; i < uv.count; i ++ ) {
  184. uv.setY( i, 1 - uv.getY( i ) );
  185. }
  186. return geometry;
  187. }
  188. function animate() {
  189. updateSize();
  190. canvas.style.transform = `translateY(${window.scrollY}px)`;
  191. renderer.setClearColor( 0xffffff );
  192. renderer.setScissorTest( false );
  193. renderer.clear();
  194. renderer.setClearColor( 0xe0e0e0 );
  195. renderer.setScissorTest( true );
  196. scenes.forEach( function ( scene ) {
  197. // get the element that is a place holder for where we want to
  198. // draw the scene
  199. const element = scene.userData.element;
  200. // get its position relative to the page's viewport
  201. const rect = element.getBoundingClientRect();
  202. // check if it's offscreen. If so skip it
  203. if ( rect.top < 0 || rect.bottom > renderer.domElement.clientHeight ||
  204. rect.left < 0 || rect.right > renderer.domElement.clientWidth ) {
  205. return; // it's off screen
  206. }
  207. // set the viewport
  208. const width = rect.right - rect.left;
  209. const height = rect.bottom - rect.top;
  210. const left = rect.left;
  211. const top = rect.top;
  212. renderer.setViewport( left, top, width, height );
  213. renderer.setScissor( left, top, width, height );
  214. const camera = scene.userData.camera;
  215. renderer.render( scene, camera );
  216. } );
  217. }
  218. </script>
  219. </body>
  220. </html>
粤ICP备19079148号