webgl_loader_gltf.html 5.6 KB

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