webgl_loader_gltf.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - glTF loader</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. Battle Damaged Sci-fi Helmet by
  13. <a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a><br />
  14. <a href="https://hdrihaven.com/hdri/?h=royal_esplanade" target="_blank" rel="noopener">Royal Esplanade</a> by <a href="https://hdrihaven.com/" target="_blank" rel="noopener">HDRI Haven</a>
  15. </div>
  16. <script type="module">
  17. import * as THREE from '../build/three.module.js';
  18. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  19. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  20. import { RGBELoader } from './jsm/loaders/RGBELoader.js';
  21. import { RoughnessMipmapper } from './jsm/utils/RoughnessMipmapper.js';
  22. var container, controls;
  23. var camera, scene, renderer;
  24. init();
  25. render();
  26. function init() {
  27. container = document.createElement( 'div' );
  28. document.body.appendChild( container );
  29. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  30. camera.position.set( - 1.8, 0.6, 2.7 );
  31. scene = new THREE.Scene();
  32. new RGBELoader()
  33. .setDataType( THREE.UnsignedByteType )
  34. .setPath( 'textures/equirectangular/' )
  35. .load( 'royal_esplanade_1k.hdr', function ( texture ) {
  36. var envMap = pmremGenerator.fromEquirectangular( texture ).texture;
  37. scene.background = envMap;
  38. scene.environment = envMap;
  39. texture.dispose();
  40. pmremGenerator.dispose();
  41. render();
  42. // model
  43. // use of RoughnessMipmapper is optional
  44. var roughnessMipmapper = new RoughnessMipmapper( renderer );
  45. var loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  46. loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
  47. gltf.scene.traverse( function ( child ) {
  48. if ( child.isMesh ) {
  49. roughnessMipmapper.generateMipmaps( child.material );
  50. }
  51. } );
  52. scene.add( gltf.scene );
  53. roughnessMipmapper.dispose();
  54. render();
  55. } );
  56. } );
  57. renderer = new THREE.WebGLRenderer( { antialias: true } );
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  61. renderer.toneMappingExposure = 0.8;
  62. renderer.outputEncoding = THREE.sRGBEncoding;
  63. container.appendChild( renderer.domElement );
  64. var pmremGenerator = new THREE.PMREMGenerator( renderer );
  65. pmremGenerator.compileEquirectangularShader();
  66. controls = new OrbitControls( camera, renderer.domElement );
  67. controls.addEventListener( 'change', render ); // use if there is no animation loop
  68. controls.minDistance = 2;
  69. controls.maxDistance = 10
  70. controls.target.set( 0, 0, - 0.2 );
  71. controls.update();
  72. window.addEventListener( 'resize', onWindowResize, false );
  73. }
  74. function onWindowResize() {
  75. camera.aspect = window.innerWidth / window.innerHeight;
  76. camera.updateProjectionMatrix();
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. render();
  79. }
  80. //
  81. function render() {
  82. renderer.render( scene, camera );
  83. }
  84. </script>
  85. </body>
  86. </html>