webgl_loader_fbx.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - FBX 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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - FBXLoader<br />
  12. Character and animation from <a href="https://www.mixamo.com/" target="_blank" rel="noopener">Mixamo</a>
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
  30. let camera, scene, renderer, stats;
  31. const clock = new THREE.Clock();
  32. let mixer;
  33. init();
  34. animate();
  35. function init() {
  36. const container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  39. camera.position.set( 100, 200, 300 );
  40. scene = new THREE.Scene();
  41. scene.background = new THREE.Color( 0xa0a0a0 );
  42. scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
  43. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  44. hemiLight.position.set( 0, 200, 0 );
  45. scene.add( hemiLight );
  46. const dirLight = new THREE.DirectionalLight( 0xffffff );
  47. dirLight.position.set( 0, 200, 100 );
  48. dirLight.castShadow = true;
  49. dirLight.shadow.camera.top = 180;
  50. dirLight.shadow.camera.bottom = - 100;
  51. dirLight.shadow.camera.left = - 120;
  52. dirLight.shadow.camera.right = 120;
  53. scene.add( dirLight );
  54. // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
  55. // ground
  56. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  57. mesh.rotation.x = - Math.PI / 2;
  58. mesh.receiveShadow = true;
  59. scene.add( mesh );
  60. const grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
  61. grid.material.opacity = 0.2;
  62. grid.material.transparent = true;
  63. scene.add( grid );
  64. // model
  65. const loader = new FBXLoader();
  66. loader.load( 'models/fbx/Samba Dancing.fbx', function ( object ) {
  67. mixer = new THREE.AnimationMixer( object );
  68. const action = mixer.clipAction( object.animations[ 0 ] );
  69. action.play();
  70. object.traverse( function ( child ) {
  71. if ( child.isMesh ) {
  72. child.castShadow = true;
  73. child.receiveShadow = true;
  74. }
  75. } );
  76. scene.add( object );
  77. } );
  78. renderer = new THREE.WebGLRenderer( { antialias: true } );
  79. renderer.setPixelRatio( window.devicePixelRatio );
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. renderer.shadowMap.enabled = true;
  82. container.appendChild( renderer.domElement );
  83. const controls = new OrbitControls( camera, renderer.domElement );
  84. controls.target.set( 0, 100, 0 );
  85. controls.update();
  86. window.addEventListener( 'resize', onWindowResize );
  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. const delta = clock.getDelta();
  100. if ( mixer ) mixer.update( delta );
  101. renderer.render( scene, camera );
  102. stats.update();
  103. }
  104. </script>
  105. </body>
  106. </html>