webgpu_loader_texture_ktx2.html 7.1 KB

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