webgl_loader_fbx.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. color: #fff;
  17. position: absolute;
  18. top: 10px;
  19. width: 100%;
  20. text-align: center;
  21. z-index: 100;
  22. display:block;
  23. }
  24. #info a {
  25. color: #046;
  26. font-weight: bold;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info">
  32. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - FBXLoader<br />
  33. Character and animation from <a href="https://www.mixamo.com/" target="_blank" rel="noopener">Mixamo</a>
  34. </div>
  35. <script src="../build/three.js"></script>
  36. <script src="js/libs/inflate.min.js"></script>
  37. <script src="js/loaders/FBXLoader.js"></script>
  38. <script src="js/controls/OrbitControls.js"></script>
  39. <script src="js/Detector.js"></script>
  40. <script src="js/libs/stats.min.js"></script>
  41. <script>
  42. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  43. var container, stats, controls;
  44. var camera, scene, renderer, light;
  45. var clock = new THREE.Clock();
  46. var mixers = [];
  47. init();
  48. animate();
  49. function init() {
  50. container = document.createElement( 'div' );
  51. document.body.appendChild( container );
  52. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  53. camera.position.set( 100, 200, 300 );
  54. controls = new THREE.OrbitControls( camera );
  55. controls.target.set( 0, 100, 0 );
  56. controls.update();
  57. scene = new THREE.Scene();
  58. scene.background = new THREE.Color( 0xa0a0a0 );
  59. scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
  60. light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  61. light.position.set( 0, 200, 0 );
  62. scene.add( light );
  63. light = new THREE.DirectionalLight( 0xffffff );
  64. light.position.set( 0, 200, 100 );
  65. light.castShadow = true;
  66. light.shadow.camera.top = 180;
  67. light.shadow.camera.bottom = -100;
  68. light.shadow.camera.left = -120;
  69. light.shadow.camera.right = 120;
  70. scene.add( light );
  71. // scene.add( new THREE.CameraHelper( light.shadow.camera ) );
  72. // ground
  73. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  74. mesh.rotation.x = - Math.PI / 2;
  75. mesh.receiveShadow = true;
  76. scene.add( mesh );
  77. var grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
  78. grid.material.opacity = 0.2;
  79. grid.material.transparent = true;
  80. scene.add( grid );
  81. // model
  82. var loader = new THREE.FBXLoader();
  83. loader.load( 'models/fbx/Samba Dancing.fbx', function ( object ) {
  84. object.mixer = new THREE.AnimationMixer( object );
  85. mixers.push( object.mixer );
  86. var action = object.mixer.clipAction( object.animations[ 0 ] );
  87. action.play();
  88. object.traverse( function ( child ) {
  89. if ( child.isMesh ) {
  90. child.castShadow = true;
  91. child.receiveShadow = true;
  92. }
  93. } );
  94. scene.add( object );
  95. } );
  96. renderer = new THREE.WebGLRenderer( { antialias: true } );
  97. renderer.setPixelRatio( window.devicePixelRatio );
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. renderer.shadowMap.enabled = true;
  100. container.appendChild( renderer.domElement );
  101. window.addEventListener( 'resize', onWindowResize, false );
  102. // stats
  103. stats = new Stats();
  104. container.appendChild( stats.dom );
  105. }
  106. function onWindowResize() {
  107. camera.aspect = window.innerWidth / window.innerHeight;
  108. camera.updateProjectionMatrix();
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. }
  111. //
  112. function animate() {
  113. requestAnimationFrame( animate );
  114. if ( mixers.length > 0 ) {
  115. for ( var i = 0; i < mixers.length; i ++ ) {
  116. mixers[ i ].update( clock.getDelta() );
  117. }
  118. }
  119. renderer.render( scene, camera );
  120. stats.update();
  121. }
  122. </script>
  123. </body>
  124. </html>