webgl_loader_gltf.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. color: #fff;
  17. position: absolute;
  18. top: 10px;
  19. width: 100%;
  20. text-align: center;
  21. z-index: 100;
  22. display:block;
  23. }
  24. #info a {
  25. color: #75ddc1;
  26. font-weight: bold;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info">
  32. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - GLTFLoader<br />
  33. Battle Damaged Sci-fi Helmet by
  34. <a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a><br />
  35. </div>
  36. <script src="../build/three.js"></script>
  37. <script src="js/controls/OrbitControls.js"></script>
  38. <script src="js/loaders/GLTFLoader.js"></script>
  39. <script src="js/Detector.js"></script>
  40. <script src="js/libs/stats.min.js"></script>
  41. <script>
  42. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  43. var container, stats, controls;
  44. var camera, scene, renderer, light;
  45. init();
  46. animate();
  47. function init() {
  48. container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  51. camera.position.set( -1.8, 0.9, 2.7 );
  52. controls = new THREE.OrbitControls( camera );
  53. controls.target.set( 0, -0.2, -0.2 );
  54. controls.update();
  55. // envmap
  56. var path = 'textures/cube/skyboxsun25deg/';
  57. var format = '.jpg';
  58. var envMap = new THREE.CubeTextureLoader().load( [
  59. path + 'px' + format, path + 'nx' + format,
  60. path + 'py' + format, path + 'ny' + format,
  61. path + 'pz' + format, path + 'nz' + format
  62. ] );
  63. scene = new THREE.Scene();
  64. scene.background = envMap;
  65. light = new THREE.HemisphereLight( 0xbbbbff, 0x444422 );
  66. light.position.set( 0, 1, 0 );
  67. scene.add( light );
  68. light = new THREE.DirectionalLight( 0xffffff );
  69. light.position.set( -10, 6, -10 );
  70. scene.add( light );
  71. // model
  72. var loader = new THREE.GLTFLoader();
  73. loader.load( 'models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf', function ( gltf ) {
  74. gltf.scene.traverse( function ( child ) {
  75. if ( child.isMesh ) {
  76. child.material.envMap = envMap;
  77. }
  78. } );
  79. scene.add( gltf.scene );
  80. } );
  81. renderer = new THREE.WebGLRenderer( { antialias: true } );
  82. renderer.setPixelRatio( window.devicePixelRatio );
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. renderer.gammaOutput = true;
  85. container.appendChild( renderer.domElement );
  86. window.addEventListener( 'resize', onWindowResize, false );
  87. // stats
  88. stats = new Stats();
  89. container.appendChild( stats.dom );
  90. }
  91. function onWindowResize() {
  92. camera.aspect = window.innerWidth / window.innerHeight;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. }
  96. //
  97. function animate() {
  98. requestAnimationFrame( animate );
  99. renderer.render( scene, camera );
  100. stats.update();
  101. }
  102. </script>
  103. </body>
  104. </html>