webgl_loader_fbx.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import Stats from 'three/addons/libs/stats.module.js';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. const manager = new THREE.LoadingManager();
  29. let camera, scene, renderer, stats, object, loader, guiMorphsFolder;
  30. let mixer;
  31. const clock = new THREE.Clock();
  32. const params = {
  33. asset: 'Samba Dancing'
  34. };
  35. const assets = [
  36. 'Samba Dancing',
  37. 'morph_test'
  38. ];
  39. init();
  40. function init() {
  41. const container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  44. camera.position.set( 100, 200, 300 );
  45. scene = new THREE.Scene();
  46. scene.background = new THREE.Color( 0xa0a0a0 );
  47. scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
  48. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444, 5 );
  49. hemiLight.position.set( 0, 200, 0 );
  50. scene.add( hemiLight );
  51. const dirLight = new THREE.DirectionalLight( 0xffffff, 5 );
  52. dirLight.position.set( 0, 200, 100 );
  53. dirLight.castShadow = true;
  54. dirLight.shadow.camera.top = 180;
  55. dirLight.shadow.camera.bottom = - 100;
  56. dirLight.shadow.camera.left = - 120;
  57. dirLight.shadow.camera.right = 120;
  58. scene.add( dirLight );
  59. // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
  60. // ground
  61. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  62. mesh.rotation.x = - Math.PI / 2;
  63. mesh.receiveShadow = true;
  64. scene.add( mesh );
  65. const grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
  66. grid.material.opacity = 0.2;
  67. grid.material.transparent = true;
  68. scene.add( grid );
  69. loader = new FBXLoader( manager );
  70. loadAsset( params.asset );
  71. renderer = new THREE.WebGLRenderer( { antialias: true } );
  72. renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. renderer.setAnimationLoop( animate );
  75. renderer.shadowMap.enabled = true;
  76. container.appendChild( renderer.domElement );
  77. const controls = new OrbitControls( camera, renderer.domElement );
  78. controls.target.set( 0, 100, 0 );
  79. controls.update();
  80. window.addEventListener( 'resize', onWindowResize );
  81. // stats
  82. stats = new Stats();
  83. container.appendChild( stats.dom );
  84. const gui = new GUI();
  85. gui.add( params, 'asset', assets ).onChange( function ( value ) {
  86. loadAsset( value );
  87. } );
  88. guiMorphsFolder = gui.addFolder( 'Morphs' ).hide();
  89. }
  90. function loadAsset( asset ) {
  91. loader.load( 'models/fbx/' + asset + '.fbx', function ( group ) {
  92. if ( object ) {
  93. object.traverse( function ( child ) {
  94. if ( child.material ) {
  95. const materials = Array.isArray( child.material ) ? child.material : [ child.material ];
  96. materials.forEach( material => {
  97. if ( material.map ) material.map.dispose();
  98. material.dispose();
  99. } );
  100. }
  101. if ( child.geometry ) child.geometry.dispose();
  102. } );
  103. scene.remove( object );
  104. }
  105. //
  106. object = group;
  107. if ( object.animations && object.animations.length ) {
  108. mixer = new THREE.AnimationMixer( object );
  109. const action = mixer.clipAction( object.animations[ 0 ] );
  110. action.play();
  111. } else {
  112. mixer = null;
  113. }
  114. guiMorphsFolder.children.forEach( ( child ) => child.destroy() );
  115. guiMorphsFolder.hide();
  116. object.traverse( function ( child ) {
  117. if ( child.isMesh ) {
  118. child.castShadow = true;
  119. child.receiveShadow = true;
  120. if ( child.morphTargetDictionary ) {
  121. guiMorphsFolder.show();
  122. const meshFolder = guiMorphsFolder.addFolder( child.name || child.uuid );
  123. Object.keys( child.morphTargetDictionary ).forEach( ( key ) => {
  124. meshFolder.add( child.morphTargetInfluences, child.morphTargetDictionary[ key ], 0, 1, 0.01 );
  125. } );
  126. }
  127. }
  128. } );
  129. scene.add( object );
  130. } );
  131. }
  132. function onWindowResize() {
  133. camera.aspect = window.innerWidth / window.innerHeight;
  134. camera.updateProjectionMatrix();
  135. renderer.setSize( window.innerWidth, window.innerHeight );
  136. }
  137. //
  138. function animate() {
  139. const delta = clock.getDelta();
  140. if ( mixer ) mixer.update( delta );
  141. renderer.render( scene, camera );
  142. stats.update();
  143. }
  144. </script>
  145. </body>
  146. </html>