webgl_loader_gltf.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/loaders/GLTFLoader.js"></script>
  38. <script src="js/controls/OrbitControls.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, 1, 2000 );
  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. scene = new THREE.Scene();
  56. scene.background = new THREE.Color( 0x443333 );
  57. scene.fog = new THREE.Fog( 0x443333, 2, 10 );
  58. light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  59. light.position.set( 0, 2, 0 );
  60. scene.add( light );
  61. light = new THREE.DirectionalLight( 0xffffff );
  62. light.position.set( 0, 2, 1 );
  63. light.castShadow = true;
  64. light.shadow.camera.top = 1.8;
  65. light.shadow.camera.bottom = -1.8;
  66. light.shadow.camera.left = -1.2;
  67. light.shadow.camera.right = 1.2;
  68. scene.add( light );
  69. // ground
  70. var plane = new THREE.Mesh(
  71. new THREE.PlaneBufferGeometry( 20, 20 ),
  72. new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
  73. );
  74. plane.rotation.x = - Math.PI / 2;
  75. plane.position.y = -1;
  76. plane.receiveShadow = true;
  77. scene.add(plane);
  78. // envmap
  79. var path = 'textures/cube/Park2/';
  80. var format = '.jpg';
  81. var envMap = new THREE.CubeTextureLoader().load( [
  82. path + 'posx' + format, path + 'negx' + format,
  83. path + 'posy' + format, path + 'negy' + format,
  84. path + 'posz' + format, path + 'negz' + format
  85. ] );
  86. // model
  87. var loader = new THREE.GLTFLoader();
  88. loader.load( 'models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf', function ( gltf ) {
  89. gltf.scene.traverse( function ( child ) {
  90. if ( child.isMesh ) {
  91. child.material.envMap = envMap;
  92. child.material.needsUpdate = true;
  93. child.castShadow = true;
  94. }
  95. } );
  96. scene.add.apply( scene, gltf.scene.children );
  97. } );
  98. renderer = new THREE.WebGLRenderer();
  99. renderer.setPixelRatio( window.devicePixelRatio );
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. renderer.shadowMap.enabled = true;
  102. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  103. container.appendChild( renderer.domElement );
  104. window.addEventListener( 'resize', onWindowResize, false );
  105. // stats
  106. stats = new Stats();
  107. container.appendChild( stats.dom );
  108. }
  109. function onWindowResize() {
  110. camera.aspect = window.innerWidth / window.innerHeight;
  111. camera.updateProjectionMatrix();
  112. renderer.setSize( window.innerWidth, window.innerHeight );
  113. }
  114. //
  115. function animate() {
  116. requestAnimationFrame( animate );
  117. renderer.render( scene, camera );
  118. stats.update();
  119. }
  120. </script>
  121. </body>
  122. </html>