webgl_loader_fbx.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import Stats from './jsm/libs/stats.module.js';
  17. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  18. import { FBXLoader } from './jsm/loaders/FBXLoader.js';
  19. let camera, scene, renderer, stats;
  20. const clock = new THREE.Clock();
  21. let mixer;
  22. init();
  23. animate();
  24. function init() {
  25. const container = document.createElement( 'div' );
  26. document.body.appendChild( container );
  27. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  28. camera.position.set( 100, 200, 300 );
  29. scene = new THREE.Scene();
  30. scene.background = new THREE.Color( 0xa0a0a0 );
  31. scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
  32. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  33. hemiLight.position.set( 0, 200, 0 );
  34. scene.add( hemiLight );
  35. const dirLight = new THREE.DirectionalLight( 0xffffff );
  36. dirLight.position.set( 0, 200, 100 );
  37. dirLight.castShadow = true;
  38. dirLight.shadow.camera.top = 180;
  39. dirLight.shadow.camera.bottom = - 100;
  40. dirLight.shadow.camera.left = - 120;
  41. dirLight.shadow.camera.right = 120;
  42. scene.add( dirLight );
  43. // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
  44. // ground
  45. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  46. mesh.rotation.x = - Math.PI / 2;
  47. mesh.receiveShadow = true;
  48. scene.add( mesh );
  49. const grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
  50. grid.material.opacity = 0.2;
  51. grid.material.transparent = true;
  52. scene.add( grid );
  53. // model
  54. const loader = new FBXLoader();
  55. loader.load( 'models/fbx/Samba Dancing.fbx', function ( object ) {
  56. mixer = new THREE.AnimationMixer( object );
  57. const action = mixer.clipAction( object.animations[ 0 ] );
  58. action.play();
  59. object.traverse( function ( child ) {
  60. if ( child.isMesh ) {
  61. child.castShadow = true;
  62. child.receiveShadow = true;
  63. }
  64. } );
  65. scene.add( object );
  66. } );
  67. renderer = new THREE.WebGLRenderer( { antialias: true } );
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. renderer.shadowMap.enabled = true;
  71. container.appendChild( renderer.domElement );
  72. const controls = new OrbitControls( camera, renderer.domElement );
  73. controls.target.set( 0, 100, 0 );
  74. controls.update();
  75. window.addEventListener( 'resize', onWindowResize );
  76. // stats
  77. stats = new Stats();
  78. container.appendChild( stats.dom );
  79. }
  80. function onWindowResize() {
  81. camera.aspect = window.innerWidth / window.innerHeight;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. }
  85. //
  86. function animate() {
  87. requestAnimationFrame( animate );
  88. const delta = clock.getDelta();
  89. if ( mixer ) mixer.update( delta );
  90. renderer.render( scene, camera );
  91. stats.update();
  92. }
  93. </script>
  94. </body>
  95. </html>