webgl_loader_fbx.html 4.1 KB

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