webgl_loader_gltf_progressive_lod.html 6.2 KB

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