webgl_loader_fbx.html 3.7 KB

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