webgl_loader_texture_basis.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - basis 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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - basis texture loader<br />
  12. <a href="https://github.com/binomialLLC/basis_universal" target="_blank">Basis Universal GPU Texture Codec</a>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { BasisTextureLoader } from './jsm/loaders/BasisTextureLoader.js';
  17. let camera, scene, renderer;
  18. let mesh;
  19. const clock = new THREE.Clock();
  20. init();
  21. animate();
  22. function init() {
  23. renderer = new THREE.WebGLRenderer( { antialias: true } );
  24. renderer.setPixelRatio( window.devicePixelRatio );
  25. renderer.setSize( window.innerWidth, window.innerHeight );
  26. renderer.outputEncoding = THREE.sRGBEncoding;
  27. document.body.appendChild( renderer.domElement );
  28. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
  29. camera.position.z = 500;
  30. scene = new THREE.Scene();
  31. const geometry = flipY( new THREE.BoxGeometry( 200, 200, 200 ) );
  32. const material = new THREE.MeshBasicMaterial();
  33. mesh = new THREE.Mesh( geometry, material );
  34. scene.add( mesh );
  35. const loader = new BasisTextureLoader();
  36. loader.setTranscoderPath( 'js/libs/basis/' );
  37. loader.detectSupport( renderer );
  38. loader.load( 'textures/compressed/PavingStones.basis', function ( texture ) {
  39. texture.encoding = THREE.sRGBEncoding;
  40. material.map = texture;
  41. material.needsUpdate = true;
  42. }, undefined, function ( error ) {
  43. console.error( error );
  44. } );
  45. window.addEventListener( 'resize', onWindowResize, false );
  46. }
  47. function onWindowResize() {
  48. camera.aspect = window.innerWidth / window.innerHeight;
  49. camera.updateProjectionMatrix();
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. }
  52. function animate() {
  53. requestAnimationFrame( animate );
  54. const delta = clock.getDelta() * 0.5;
  55. mesh.rotation.x += delta;
  56. mesh.rotation.y += delta;
  57. renderer.render( scene, camera );
  58. }
  59. /** Correct UVs to be compatible with `flipY=false` textures. */
  60. function flipY( geometry ) {
  61. const uv = geometry.attributes.uv;
  62. for ( let i = 0; i < uv.count; i ++ ) {
  63. uv.setY( i, 1 - uv.getY( i ) );
  64. }
  65. return geometry;
  66. }
  67. </script>
  68. </body>
  69. </html>