webgl_loader_fbx.html 3.6 KB

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