webgl_loader_fbx.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/WebGL.js"></script>
  40. <script src="js/libs/stats.min.js"></script>
  41. <script>
  42. if ( WEBGL.isWebGLAvailable() === false ) {
  43. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  44. }
  45. var container, stats, controls;
  46. var camera, scene, renderer, light;
  47. var clock = new THREE.Clock();
  48. var mixers = [];
  49. init();
  50. animate();
  51. function init() {
  52. container = document.createElement( 'div' );
  53. document.body.appendChild( container );
  54. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  55. camera.position.set( 100, 200, 300 );
  56. controls = new THREE.OrbitControls( camera );
  57. controls.target.set( 0, 100, 0 );
  58. controls.update();
  59. scene = new THREE.Scene();
  60. scene.background = new THREE.Color( 0xa0a0a0 );
  61. scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
  62. light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  63. light.position.set( 0, 200, 0 );
  64. scene.add( light );
  65. light = new THREE.DirectionalLight( 0xffffff );
  66. light.position.set( 0, 200, 100 );
  67. light.castShadow = true;
  68. light.shadow.camera.top = 180;
  69. light.shadow.camera.bottom = - 100;
  70. light.shadow.camera.left = - 120;
  71. light.shadow.camera.right = 120;
  72. scene.add( light );
  73. // scene.add( new THREE.CameraHelper( light.shadow.camera ) );
  74. // ground
  75. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  76. mesh.rotation.x = - Math.PI / 2;
  77. mesh.receiveShadow = true;
  78. scene.add( mesh );
  79. var grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
  80. grid.material.opacity = 0.2;
  81. grid.material.transparent = true;
  82. scene.add( grid );
  83. // model
  84. var loader = new THREE.FBXLoader();
  85. loader.load( 'models/fbx/Samba Dancing.fbx', function ( object ) {
  86. object.mixer = new THREE.AnimationMixer( object );
  87. mixers.push( object.mixer );
  88. var action = object.mixer.clipAction( object.animations[ 0 ] );
  89. action.play();
  90. object.traverse( function ( child ) {
  91. if ( child.isMesh ) {
  92. child.castShadow = true;
  93. child.receiveShadow = true;
  94. }
  95. } );
  96. scene.add( object );
  97. } );
  98. renderer = new THREE.WebGLRenderer( { antialias: true } );
  99. renderer.setPixelRatio( window.devicePixelRatio );
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. renderer.shadowMap.enabled = true;
  102. container.appendChild( renderer.domElement );
  103. window.addEventListener( 'resize', onWindowResize, false );
  104. // stats
  105. stats = new Stats();
  106. container.appendChild( stats.dom );
  107. }
  108. function onWindowResize() {
  109. camera.aspect = window.innerWidth / window.innerHeight;
  110. camera.updateProjectionMatrix();
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. }
  113. //
  114. function animate() {
  115. requestAnimationFrame( animate );
  116. if ( mixers.length > 0 ) {
  117. for ( var i = 0; i < mixers.length; i ++ ) {
  118. mixers[ i ].update( clock.getDelta() );
  119. }
  120. }
  121. renderer.render( scene, camera );
  122. stats.update();
  123. }
  124. </script>
  125. </body>
  126. </html>