webgl_loader_gltf.html 5.9 KB

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