webgl_loader_gltf.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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="http://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=pedestrian_overpass" target="_blank" rel="noopener">Pedestrian Overpass</a> by <a href="https://hdrihaven.com/" target="_blank" rel="noopener">HDRI Haven</a>
  15. </div>
  16. <script src="../build/three.js"></script>
  17. <script src="js/controls/OrbitControls.js"></script>
  18. <script src="js/loaders/GLTFLoader.js"></script>
  19. <script src="js/loaders/EquirectangularToCubeGenerator.js"></script>
  20. <script src="js/loaders/RGBELoader.js"></script>
  21. <script src="js/pmrem/PMREMGenerator.js"></script>
  22. <script src="js/pmrem/PMREMCubeUVPacker.js"></script>
  23. <script src="js/WebGL.js"></script>
  24. <script src="js/libs/stats.min.js"></script>
  25. <script>
  26. if ( WEBGL.isWebGLAvailable() === false ) {
  27. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  28. }
  29. var container, stats, controls;
  30. var camera, scene, renderer;
  31. init();
  32. animate();
  33. function init() {
  34. 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.9, 2.7 );
  38. scene = new THREE.Scene();
  39. new THREE.RGBELoader()
  40. .setType( THREE.UnsignedByteType )
  41. .setPath( 'textures/equirectangular/' )
  42. .load( 'pedestrian_overpass_2k.hdr', function ( texture ) {
  43. var cubeGenerator = new THREE.EquirectangularToCubeGenerator( texture, { resolution: 1024 } );
  44. cubeGenerator.update( renderer );
  45. var pmremGenerator = new THREE.PMREMGenerator( cubeGenerator.renderTarget.texture );
  46. pmremGenerator.update( renderer );
  47. var pmremCubeUVPacker = new THREE.PMREMCubeUVPacker( pmremGenerator.cubeLods );
  48. pmremCubeUVPacker.update( renderer );
  49. var envMap = pmremCubeUVPacker.CubeUVRenderTarget.texture;
  50. // model
  51. var loader = new THREE.GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  52. loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
  53. gltf.scene.traverse( function ( child ) {
  54. if ( child.isMesh ) {
  55. child.material.envMap = envMap;
  56. }
  57. } );
  58. scene.add( gltf.scene );
  59. } );
  60. pmremGenerator.dispose();
  61. pmremCubeUVPacker.dispose();
  62. scene.background = cubeGenerator.renderTarget;
  63. } );
  64. renderer = new THREE.WebGLRenderer( { antialias: true } );
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. renderer.gammaOutput = true;
  68. container.appendChild( renderer.domElement );
  69. controls = new THREE.OrbitControls( camera, renderer.domElement );
  70. controls.target.set( 0, - 0.2, - 0.2 );
  71. controls.update();
  72. window.addEventListener( 'resize', onWindowResize, false );
  73. // stats
  74. stats = new Stats();
  75. container.appendChild( stats.dom );
  76. }
  77. function onWindowResize() {
  78. camera.aspect = window.innerWidth / window.innerHeight;
  79. camera.updateProjectionMatrix();
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. }
  82. //
  83. function animate() {
  84. requestAnimationFrame( animate );
  85. renderer.render( scene, camera );
  86. stats.update();
  87. }
  88. </script>
  89. </body>
  90. </html>