webgl_loader_fbx.html 3.9 KB

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