webgl_loader_gltf.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, 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. 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. var grid = new THREE.GridHelper( 20, 20, 0x000000, 0x000000 );
  79. grid.position.y = -0.99;
  80. grid.material.opacity = 0.2;
  81. grid.material.transparent = true;
  82. scene.add( grid );
  83. // envmap
  84. var path = 'textures/cube/skybox/';
  85. var format = '.jpg';
  86. var envMap = new THREE.CubeTextureLoader().load( [
  87. path + 'px' + format, path + 'nx' + format,
  88. path + 'py' + format, path + 'ny' + format,
  89. path + 'pz' + format, path + 'nz' + format
  90. ] );
  91. // model
  92. var loader = new THREE.GLTFLoader();
  93. loader.load( 'models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf', function ( gltf ) {
  94. gltf.scene.traverse( function ( child ) {
  95. if ( child.isMesh ) {
  96. child.material.envMap = envMap;
  97. child.material.needsUpdate = true;
  98. child.castShadow = true;
  99. }
  100. } );
  101. scene.add( gltf.scene );
  102. } );
  103. renderer = new THREE.WebGLRenderer( { antialias: true } );
  104. renderer.setPixelRatio( window.devicePixelRatio );
  105. renderer.setSize( window.innerWidth, window.innerHeight );
  106. renderer.gammaOutput = true;
  107. renderer.shadowMap.enabled = true;
  108. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  109. container.appendChild( renderer.domElement );
  110. window.addEventListener( 'resize', onWindowResize, false );
  111. // stats
  112. stats = new Stats();
  113. container.appendChild( stats.dom );
  114. }
  115. function onWindowResize() {
  116. camera.aspect = window.innerWidth / window.innerHeight;
  117. camera.updateProjectionMatrix();
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. }
  120. //
  121. function animate() {
  122. requestAnimationFrame( animate );
  123. renderer.render( scene, camera );
  124. stats.update();
  125. }
  126. </script>
  127. </body>
  128. </html>