webgl_loader_gltf.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - GLTFloader</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 - GLTFloader">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_loader_gltf.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_loader_gltf.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - GLTFLoader<br />
  16. <a href="https://hdrihaven.com/hdri/?h=royal_esplanade" target="_blank" rel="noopener">Royal Esplanade</a> from <a href="https://hdrihaven.com/" target="_blank" rel="noopener">HDRI Haven</a>
  17. </div>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  30. import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js';
  31. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  32. let camera, scene, renderer, controls;
  33. let currentModel, mixer;
  34. let currentLoadId = 0;
  35. const timer = new THREE.Timer();
  36. init();
  37. function init() {
  38. const container = document.createElement( 'div' );
  39. document.body.appendChild( container );
  40. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  41. camera.position.set( - 1.8, 0.6, 2.7 );
  42. scene = new THREE.Scene();
  43. new UltraHDRLoader()
  44. .setPath( 'textures/equirectangular/' )
  45. .load( 'royal_esplanade_2k.hdr.jpg', function ( texture ) {
  46. texture.mapping = THREE.EquirectangularReflectionMapping;
  47. scene.background = texture;
  48. scene.environment = texture;
  49. render();
  50. // model
  51. fetch( 'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/model-index.json' )
  52. .then( response => response.json() )
  53. .then( models => {
  54. const gui = new GUI();
  55. const modelNames = models.map( m => m.name );
  56. const params = { model: 'DamagedHelmet' };
  57. if ( ! modelNames.includes( params.model ) && modelNames.length > 0 ) {
  58. params.model = modelNames[ 0 ];
  59. }
  60. gui.add( params, 'model', modelNames ).onChange( name => {
  61. const modelInfo = models.find( m => m.name === name );
  62. loadModel( modelInfo );
  63. } );
  64. gui.add( scene, 'backgroundBlurriness', 0, 1 );
  65. const initialModel = models.find( m => m.name === params.model );
  66. if ( initialModel ) loadModel( initialModel );
  67. } );
  68. } );
  69. renderer = new THREE.WebGLRenderer( { antialias: true } );
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. renderer.setAnimationLoop( render );
  73. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  74. container.appendChild( renderer.domElement );
  75. controls = new OrbitControls( camera, renderer.domElement );
  76. controls.enableDamping = true;
  77. controls.minDistance = 2;
  78. controls.maxDistance = 10;
  79. controls.target.set( 0, 0, - 0.2 );
  80. controls.update();
  81. window.addEventListener( 'resize', onWindowResize );
  82. }
  83. function loadModel( modelInfo ) {
  84. const variants = modelInfo.variants;
  85. const variant = variants[ 'glTF-Binary' ] || variants[ 'glTF' ];
  86. const url = `https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/${ modelInfo.name }/${ variant.endsWith( '.glb' ) ? 'glTF-Binary' : 'glTF' }/${ variant }`;
  87. if ( currentModel ) {
  88. scene.remove( currentModel );
  89. currentModel = null;
  90. }
  91. if ( mixer ) {
  92. mixer.stopAllAction();
  93. mixer = null;
  94. }
  95. const loadId = ++ currentLoadId;
  96. const loader = new GLTFLoader();
  97. loader.load( url, async function ( gltf ) {
  98. if ( loadId !== currentLoadId ) return;
  99. currentModel = gltf.scene;
  100. // wait until the model can be added to the scene without blocking due to shader compilation
  101. await renderer.compileAsync( currentModel, camera, scene );
  102. if ( loadId !== currentLoadId ) return;
  103. scene.add( currentModel );
  104. fitCameraToSelection( camera, controls, currentModel );
  105. // animations
  106. if ( gltf.animations.length > 0 ) {
  107. mixer = new THREE.AnimationMixer( currentModel );
  108. for ( const animation of gltf.animations ) {
  109. mixer.clipAction( animation ).play();
  110. }
  111. }
  112. } );
  113. }
  114. function fitCameraToSelection( camera, controls, selection, fitOffset = 1.3 ) {
  115. const box = new THREE.Box3();
  116. box.setFromObject( selection );
  117. const size = box.getSize( new THREE.Vector3() );
  118. const center = box.getCenter( new THREE.Vector3() );
  119. const maxSize = Math.max( size.x, size.y, size.z );
  120. const fitHeightDistance = maxSize / ( 2 * Math.atan( Math.PI * camera.fov / 360 ) );
  121. // const fitWidthDistance = fitHeightDistance / camera.aspect;
  122. // const distance = fitOffset * Math.max( fitHeightDistance, fitWidthDistance );
  123. const distance = fitOffset * fitHeightDistance;
  124. const direction = controls.target.clone().sub( camera.position ).normalize().multiplyScalar( distance );
  125. controls.maxDistance = distance * 10;
  126. controls.minDistance = distance / 10;
  127. controls.target.copy( center );
  128. camera.near = distance / 100;
  129. camera.far = distance * 100;
  130. camera.updateProjectionMatrix();
  131. camera.position.copy( controls.target ).sub( direction );
  132. controls.update();
  133. }
  134. function onWindowResize() {
  135. camera.aspect = window.innerWidth / window.innerHeight;
  136. camera.updateProjectionMatrix();
  137. renderer.setSize( window.innerWidth, window.innerHeight );
  138. render();
  139. }
  140. //
  141. function render() {
  142. timer.update();
  143. controls.update();
  144. if ( mixer ) mixer.update( timer.getDelta() );
  145. renderer.render( scene, camera );
  146. }
  147. </script>
  148. </body>
  149. </html>
粤ICP备19079148号