webgl_loader_gltf.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/WebGL.js"></script>
  40. <script src="js/libs/stats.min.js"></script>
  41. <script>
  42. if ( WEBGL.isWebGLAvailable() === false ) {
  43. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  44. }
  45. var container, stats, controls;
  46. var camera, scene, renderer;
  47. init();
  48. animate();
  49. function init() {
  50. container = document.createElement( 'div' );
  51. document.body.appendChild( container );
  52. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  53. camera.position.set( - 1.8, 0.9, 2.7 );
  54. controls = new THREE.OrbitControls( camera );
  55. controls.target.set( 0, - 0.2, - 0.2 );
  56. controls.update();
  57. scene = new THREE.Scene();
  58. var urls = [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ];
  59. var loader = new THREE.CubeTextureLoader().setPath( 'textures/cube/Bridge2/' );
  60. loader.load( urls, function ( texture ) {
  61. texture.encoding = THREE.sRGBEncoding;
  62. // light probe
  63. var lightProbe = new THREE.LightProbe();
  64. // Coefficients computed with setFromCubeTexture( texture ) and sh.toArray()
  65. lightProbe.sh.fromArray(
  66. [ 0.30350402186576847, 0.4695020609740584, 0.6617666153025029,
  67. 0.08320329629560637, 0.17400245533281114, 0.3453152275957874,
  68. 0.12158824672933624, 0.10353622444396401, 0.06530153583524678,
  69. 0.013607857556048842, 0.019188302420841814, 0.01874813525865349,
  70. 0.010822144860690035, 0.01574198289465548, 0.01742654587405097,
  71. 0.06310934215371257, 0.061061933521237545, 0.044428745834389806,
  72. 0.19958014705624538, 0.22020936865062024, 0.19468224569437417,
  73. 0.019647224115989702, 0.032414009820739324, 0.043555315974879015,
  74. 0.13316051440231733, 0.1964793374921572, 0.2189213184804167
  75. ]
  76. );
  77. scene.add( lightProbe );
  78. // model
  79. var loader = new THREE.GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  80. loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
  81. gltf.scene.traverse( function ( child ) {
  82. if ( child.isMesh ) {
  83. child.material.envMap = texture;
  84. }
  85. } );
  86. scene.add( gltf.scene );
  87. } );
  88. scene.background = texture;
  89. } );
  90. renderer = new THREE.WebGLRenderer( { antialias: true } );
  91. renderer.setPixelRatio( window.devicePixelRatio );
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. renderer.gammaOutput = true;
  94. renderer.gammaFactor = 2.2; // approximate sRGB
  95. container.appendChild( renderer.domElement );
  96. window.addEventListener( 'resize', onWindowResize, false );
  97. // stats
  98. stats = new Stats();
  99. container.appendChild( stats.dom );
  100. }
  101. function onWindowResize() {
  102. camera.aspect = window.innerWidth / window.innerHeight;
  103. camera.updateProjectionMatrix();
  104. renderer.setSize( window.innerWidth, window.innerHeight );
  105. }
  106. //
  107. function animate() {
  108. requestAnimationFrame( animate );
  109. renderer.render( scene, camera );
  110. stats.update();
  111. }
  112. </script>
  113. </body>
  114. </html>