webgl_loader_fbx.html 5.1 KB

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