webgl_loader_texture_ktx.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - compressed textures</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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - compressed KTX textures<br />
  12. <a href="https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/">Khronos Texture</a> is a lightweight file format for OpenGL
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { KTXLoader } from './jsm/loaders/KTXLoader.js';
  17. /*
  18. This is how compressed textures are supposed to be used:
  19. best for desktop:
  20. BC1(DXT1) - opaque textures
  21. BC3(DXT5) - transparent textures with full alpha range
  22. best for iOS:
  23. PVR2, PVR4 - opaque textures or alpha
  24. best for Android:
  25. ETC1 - opaque textures
  26. ASTC_4x4, ASTC8x8 - transparent textures with full alpha range
  27. */
  28. var camera, scene, renderer;
  29. var meshes = [];
  30. init();
  31. animate();
  32. function init() {
  33. renderer = new THREE.WebGLRenderer( { antialias: true } );
  34. renderer.setPixelRatio( window.devicePixelRatio );
  35. renderer.setSize( window.innerWidth, window.innerHeight );
  36. document.body.appendChild( renderer.domElement );
  37. var formats = {
  38. astc: renderer.extensions.get( 'WEBGL_compressed_texture_astc' ),
  39. etc1: renderer.extensions.get( 'WEBGL_compressed_texture_etc1' ),
  40. s3tc: renderer.extensions.get( 'WEBGL_compressed_texture_s3tc' ),
  41. pvrtc: renderer.extensions.get( 'WEBGL_compressed_texture_pvrtc' )
  42. };
  43. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
  44. camera.position.z = 1000;
  45. scene = new THREE.Scene();
  46. var geometry = new THREE.BoxBufferGeometry( 200, 200, 200 );
  47. var material1, material2;
  48. // TODO: add cubemap support
  49. var loader = new KTXLoader();
  50. if ( formats.pvrtc ) {
  51. material1 = new THREE.MeshBasicMaterial( {
  52. map: loader.load( 'textures/compressed/disturb_PVR2bpp.ktx' )
  53. } );
  54. material2 = new THREE.MeshBasicMaterial( {
  55. map: loader.load( 'textures/compressed/lensflare_PVR4bpp.ktx' ),
  56. depthTest: false,
  57. transparent: true,
  58. side: THREE.DoubleSide
  59. } );
  60. meshes.push( new THREE.Mesh( geometry, material1 ) );
  61. meshes.push( new THREE.Mesh( geometry, material2 ) );
  62. }
  63. if ( formats.s3tc ) {
  64. material1 = new THREE.MeshBasicMaterial( {
  65. map: loader.load( 'textures/compressed/disturb_BC1.ktx' )
  66. } );
  67. material2 = new THREE.MeshBasicMaterial( {
  68. map: loader.load( 'textures/compressed/lensflare_BC3.ktx' ),
  69. depthTest: false,
  70. transparent: true,
  71. side: THREE.DoubleSide
  72. } );
  73. meshes.push( new THREE.Mesh( geometry, material1 ) );
  74. meshes.push( new THREE.Mesh( geometry, material2 ) );
  75. }
  76. if ( formats.etc1 ) {
  77. material1 = new THREE.MeshBasicMaterial( {
  78. map: loader.load( 'textures/compressed/disturb_ETC1.ktx' )
  79. } );
  80. meshes.push( new THREE.Mesh( geometry, material1 ) );
  81. }
  82. if ( formats.astc ) {
  83. material1 = new THREE.MeshBasicMaterial( {
  84. map: loader.load( 'textures/compressed/disturb_ASTC4x4.ktx' )
  85. } );
  86. material2 = new THREE.MeshBasicMaterial( {
  87. map: loader.load( 'textures/compressed/lensflare_ASTC8x8.ktx' ),
  88. depthTest: false,
  89. transparent: true,
  90. side: THREE.DoubleSide
  91. } );
  92. meshes.push( new THREE.Mesh( geometry, material1 ) );
  93. meshes.push( new THREE.Mesh( geometry, material2 ) );
  94. }
  95. var x = - meshes.length / 2 * 150;
  96. for ( var i = 0; i < meshes.length; ++ i, x += 300 ) {
  97. var mesh = meshes[ i ];
  98. mesh.position.x = x;
  99. mesh.position.y = 0;
  100. scene.add( mesh );
  101. }
  102. window.addEventListener( 'resize', onWindowResize, false );
  103. }
  104. function onWindowResize() {
  105. camera.aspect = window.innerWidth / window.innerHeight;
  106. camera.updateProjectionMatrix();
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. }
  109. function animate() {
  110. requestAnimationFrame( animate );
  111. var time = Date.now() * 0.001;
  112. for ( var i = 0; i < meshes.length; i ++ ) {
  113. var mesh = meshes[ i ];
  114. mesh.rotation.x = time;
  115. mesh.rotation.y = time;
  116. }
  117. renderer.render( scene, camera );
  118. }
  119. </script>
  120. </body>
  121. </html>