webgl_loader_gltf_progressive_lod.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - GLTF with progressive loading</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. <!--
  10. Documentation:
  11. https://www.npmjs.com/package/@needle-tools/gltf-progressive
  12. Example:
  13. Initial download reduced by 98.8% with progressive loading.
  14. Mesh and texture LODs are loaded on demand depending on the mesh size on screen.
  15. → 237.9 MB original asset sizes
  16. → 2.9 MB initial download (base data)
  17. → 46.3 MB progressive download on demand (all LODs, in reality only individual levels are actually loaded)
  18. -->
  19. <body>
  20. <div id="info">
  21. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - GLTF progressive loading: 82x faster - <a href="https://www.npmjs.com/package/@needle-tools/gltf-progressive" target="_blank" rel="noopener">@needle-tools/gltf-progressive</a><br />
  22. Mobile Home & Peachy Balloon by
  23. <a href="https://sketchfab.com/3d-models/mobile-home-5240b1dbc29c4ea28be7f91b3638951a" target="_blank" rel="noopener">ConradJustin</a><br/>
  24. The Forgotten Knight by
  25. <a href="https://sketchfab.com/3d-models/the-forgotten-knight-d14eb14d83bd4e7ba7cbe443d76a10fd" target="_blank" rel="noopener">Dark Igorek</a><br />
  26. <a href="https://hdrihaven.com/hdri/?h=quarry_01" target="_blank" rel="noopener">Quarry 01</a> from <a href="https://hdrihaven.com/" target="_blank" rel="noopener">HDRI Haven</a>
  27. </div>
  28. <script type="importmap">
  29. {
  30. "imports": {
  31. "three": "../build/three.module.js",
  32. "three/addons/": "./jsm/",
  33. "three/examples/": "./",
  34. "@needle-tools/gltf-progressive": "https://cdn.jsdelivr.net/npm/@needle-tools/gltf-progressive@3.2.0/gltf-progressive.min.js"
  35. }
  36. }
  37. </script>
  38. <script type="module">
  39. import * as THREE from 'three';
  40. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  41. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  42. import { HDRLoader } from 'three/addons/loaders/HDRLoader.js';
  43. import { useNeedleProgressive } from '@needle-tools/gltf-progressive';
  44. let camera, scene, renderer, mixer;
  45. let airshipModel;
  46. init();
  47. function init() {
  48. const container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. scene = new THREE.Scene();
  51. renderer = new THREE.WebGLRenderer( { antialias: true } );
  52. renderer.setPixelRatio( window.devicePixelRatio );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  55. renderer.toneMappingExposure = 1;
  56. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 40 );
  57. camera.position.set( - 9, 2, - 13 );
  58. const fog = new THREE.Fog( '#131055', 15, 50 );
  59. scene.fog = fog;
  60. const controls = new OrbitControls( camera, renderer.domElement );
  61. controls.minDistance = .1;
  62. controls.maxDistance = 20;
  63. controls.target.set( - 1, 2.1, 0 );
  64. controls.update();
  65. new HDRLoader()
  66. .setPath( 'textures/equirectangular/' )
  67. .load( 'quarry_01_1k.hdr', function ( texture ) {
  68. texture.mapping = THREE.EquirectangularReflectionMapping;
  69. scene.background = new THREE.Color( '#192022' );
  70. scene.backgroundBlurriness = .5;
  71. scene.environment = texture;
  72. scene.environmentRotation = new THREE.Euler( 0, Math.PI / - 2, 0, 'XYZ' );
  73. } );
  74. mixer = new THREE.AnimationMixer( scene );
  75. const loader = new GLTFLoader();
  76. useNeedleProgressive( loader, renderer );
  77. loader.load( 'https://cloud.needle.tools/-/assets/Z23hmXBZ2sPRdk-world/file', function ( gltf ) {
  78. const model = gltf.scene;
  79. model.scale.multiplyScalar( 0.1 );
  80. scene.add( model );
  81. const animations = gltf.animations;
  82. if ( animations && animations.length ) {
  83. for ( const animation of animations ) {
  84. mixer.clipAction( animation ).play();
  85. }
  86. }
  87. } );
  88. loader.load( 'https://cloud.needle.tools/-/assets/Z23hmXBZnlceI-ZnlceI-world/file', function ( gltf ) {
  89. const model = gltf.scene;
  90. model.scale.multiplyScalar( 0.0005 );
  91. model.position.set( 1.6, 6, 7 );
  92. model.rotation.set( 0, Math.PI * 1.4, 0 );
  93. scene.add( model );
  94. airshipModel = model;
  95. const animations = gltf.animations;
  96. if ( animations && animations.length ) {
  97. for ( const animation of animations ) {
  98. mixer.clipAction( animation ).play();
  99. }
  100. }
  101. } );
  102. loader.load( 'https://cloud.needle.tools/-/assets/Z23hmXBZ21QnG-Z21QnG-product/file', function ( gltf ) {
  103. const model = gltf.scene;
  104. model.scale.multiplyScalar( 0.5 );
  105. model.position.set( 2, 5.15, 2.3 );
  106. model.rotation.set( 0, Math.PI * 1, 0 );
  107. scene.add( model );
  108. const animations = gltf.animations;
  109. if ( animations && animations.length ) {
  110. for ( const animation of animations ) {
  111. mixer.clipAction( animation ).play();
  112. }
  113. }
  114. } );
  115. container.appendChild( renderer.domElement );
  116. window.addEventListener( 'resize', onWindowResize );
  117. }
  118. function onWindowResize() {
  119. camera.aspect = window.innerWidth / window.innerHeight;
  120. camera.updateProjectionMatrix();
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. }
  123. //
  124. const clock = new THREE.Clock();
  125. let time = 0;
  126. function animate() {
  127. const dt = clock.getDelta();
  128. time += dt;
  129. mixer.update( dt );
  130. if ( airshipModel ) {
  131. airshipModel.position.y += Math.sin( time ) * 0.002;
  132. }
  133. renderer.render( scene, camera );
  134. window.requestAnimationFrame( animate );
  135. }
  136. animate();
  137. </script>
  138. </body>
  139. </html>
粤ICP备19079148号