webgl_loader_fbx.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <html lang="en">
  2. <head>
  3. <title>three.js webgl - loaders - FBX loader</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <style>
  7. body {
  8. font-family: Monospace;
  9. background-color: #000;
  10. color: #fff;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. color: #fff;
  16. position: absolute;
  17. top: 10px;
  18. width: 100%;
  19. text-align: center;
  20. z-index: 100;
  21. display:block;
  22. }
  23. #info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info">
  28. <a href="http://threejs.org" target="_blank">three.js</a> - FBXLoader test
  29. </div>
  30. <script src="../build/three.min.js"></script>
  31. <script src="js/controls/OrbitControls.js"></script>
  32. <script src="js/loaders/FBXLoader.js"></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/libs/stats.min.js"></script>
  35. <script>
  36. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  37. var container, stats, controls;
  38. var camera, scene, renderer;
  39. var clock = new THREE.Clock();
  40. var mixers = [];
  41. init();
  42. function init() {
  43. container = document.createElement( 'div' );
  44. document.body.appendChild( container );
  45. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  46. scene = new THREE.Scene();
  47. //////////////////////////////////////////////////////////////
  48. // Grid
  49. var size = 14, step = 1;
  50. var geometry = new THREE.Geometry();
  51. var material = new THREE.LineBasicMaterial( { color: 0x303030 } );
  52. for ( var i = - size; i <= size; i += step ) {
  53. geometry.vertices.push( new THREE.Vector3( - size, - 0.04, i ) );
  54. geometry.vertices.push( new THREE.Vector3( size, - 0.04, i ) );
  55. geometry.vertices.push( new THREE.Vector3( i, - 0.04, - size ) );
  56. geometry.vertices.push( new THREE.Vector3( i, - 0.04, size ) );
  57. }
  58. var line = new THREE.LineSegments( geometry, material );
  59. scene.add( line );
  60. //////////////////////////////////////////////////////////////
  61. // Lights
  62. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  63. //////////////////////////////////////////////////////////////
  64. // stats
  65. stats = new Stats();
  66. stats.domElement.style.position = 'absolute';
  67. stats.domElement.style.top = '0px';
  68. container.appendChild( stats.domElement );
  69. //////////////////////////////////////////////////////////////
  70. // model
  71. var manager = new THREE.LoadingManager();
  72. manager.onProgress = function ( item, loaded, total ) {
  73. console.log( item, loaded, total );
  74. };
  75. var onProgress = function ( xhr ) {
  76. if ( xhr.lengthComputable ) {
  77. var percentComplete = xhr.loaded / xhr.total * 100;
  78. console.log( Math.round(percentComplete, 2) + '% downloaded' );
  79. }
  80. };
  81. var onError = function ( xhr ) {
  82. };
  83. var loader = new THREE.FBXLoader( manager );
  84. loader.load( 'models/fbx/xsi_man_skinning.fbx', function ( object ) {
  85. object.traverse( function ( child ) {
  86. if ( child instanceof THREE.Mesh ) {
  87. // pass
  88. }
  89. if ( child instanceof THREE.SkinnedMesh ) {
  90. if ( child.geometry.animations !== undefined || child.geometry.morphAnimations !== undefined ) {
  91. child.mixer = new THREE.AnimationMixer( child );
  92. mixers.push( child.mixer );
  93. var action = child.mixer.clipAction( child.geometry.animations[0] );
  94. action.play();
  95. }
  96. }
  97. } );
  98. scene.add( object );
  99. }, onProgress, onError );
  100. //////////////////////////////////////////////////////////////
  101. renderer = new THREE.WebGLRenderer();
  102. renderer.setPixelRatio( window.devicePixelRatio );
  103. renderer.setSize( window.innerWidth, window.innerHeight );
  104. renderer.setClearColor( 0x000000 );
  105. container.appendChild( renderer.domElement );
  106. //////////////////////////////////////////////////////////////
  107. // controls, camera
  108. controls = new THREE.OrbitControls( camera, renderer.domElement );
  109. controls.target.set( 0, 12, 0 );
  110. camera.position.set( 2, 18, 28 );
  111. camera.lookAt( new THREE.Vector3( 0, 15, 0 ) );
  112. window.addEventListener( 'resize', onWindowResize, false );
  113. animate();
  114. }
  115. function onWindowResize() {
  116. camera.aspect = window.innerWidth / window.innerHeight;
  117. camera.updateProjectionMatrix();
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. }
  120. //
  121. function animate() {
  122. requestAnimationFrame( animate );
  123. if( mixers.length > 0 ){
  124. for ( var i=0; i < mixers.length; i++ ){
  125. mixers[i].update( clock.getDelta() );
  126. }
  127. }
  128. controls.update();
  129. stats.update();
  130. render();
  131. }
  132. function render() {
  133. renderer.render( scene, camera );
  134. }
  135. </script>
  136. </body>
  137. </html>