webgl2_loader_gltf.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - glTF loader (module version)</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 (module version)<br />
  12. Battle Damaged Sci-fi Helmet by
  13. <a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a><br />
  14. </div>
  15. <script type="module">
  16. import {
  17. CubeTextureLoader,
  18. PerspectiveCamera,
  19. Scene,
  20. WebGLRenderer
  21. } from "../build/three.module.js";
  22. import Stats from './jsm/libs/stats.module.js';
  23. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  24. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  25. import { PMREMGenerator } from './jsm/pmrem/PMREMGenerator.js';
  26. import { PMREMCubeUVPacker } from './jsm/pmrem/PMREMCubeUVPacker.js';
  27. var container, stats, controls;
  28. var camera, scene, renderer;
  29. init();
  30. animate();
  31. function init() {
  32. container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. camera = new PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  35. camera.position.set( - 1.8, 0.9, 2.7 );
  36. controls = new OrbitControls( camera );
  37. controls.target.set( 0, - 0.2, - 0.2 );
  38. controls.update();
  39. scene = new Scene();
  40. var urls = [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ];
  41. var loader = new CubeTextureLoader().setPath( 'textures/cube/Bridge2/' );
  42. loader.load( urls, function ( texture ) {
  43. var pmremGenerator = new PMREMGenerator( texture );
  44. pmremGenerator.update( renderer );
  45. var pmremCubeUVPacker = new PMREMCubeUVPacker( pmremGenerator.cubeLods );
  46. pmremCubeUVPacker.update( renderer );
  47. var envMap = pmremCubeUVPacker.CubeUVRenderTarget.texture;
  48. // model
  49. var loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  50. loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
  51. gltf.scene.traverse( function ( child ) {
  52. if ( child.isMesh ) {
  53. child.material.envMap = envMap;
  54. }
  55. } );
  56. scene.add( gltf.scene );
  57. } );
  58. pmremGenerator.dispose();
  59. pmremCubeUVPacker.dispose();
  60. scene.background = texture;
  61. } );
  62. var canvas = document.createElement( 'canvas' );
  63. var context = canvas.getContext( 'webgl2' );
  64. renderer = new WebGLRenderer( { canvas: canvas, context: context } );
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. renderer.gammaOutput = true;
  68. container.appendChild( renderer.domElement );
  69. window.addEventListener( 'resize', onWindowResize, false );
  70. // stats
  71. stats = new Stats();
  72. container.appendChild( stats.dom );
  73. }
  74. function onWindowResize() {
  75. camera.aspect = window.innerWidth / window.innerHeight;
  76. camera.updateProjectionMatrix();
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. }
  79. //
  80. function animate() {
  81. requestAnimationFrame( animate );
  82. renderer.render( scene, camera );
  83. stats.update();
  84. }
  85. </script>
  86. </body>
  87. </html>