webgl_loader_fbx.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. <meta property="og:title" content="three.js webgl - FBX loader">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_loader_fbx.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_loader_fbx.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - FBXLoader<br />
  16. Character and animation from <a href="https://www.mixamo.com/" target="_blank" rel="noopener">Mixamo</a>
  17. </div>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
  31. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  32. const manager = new THREE.LoadingManager();
  33. let camera, scene, renderer, stats, object, loader, guiMorphsFolder;
  34. let mixer;
  35. const timer = new THREE.Timer();
  36. timer.connect( document );
  37. const params = {
  38. asset: 'Samba Dancing'
  39. };
  40. const assets = [
  41. 'Samba Dancing',
  42. 'morph_test',
  43. 'monkey',
  44. 'monkey_embedded_texture',
  45. 'vCube',
  46. 'archer/ArcherRi01',
  47. 'warrior/Warrior',
  48. 'stanford-bunny',
  49. 'mixamo',
  50. 'RotationTest',
  51. 'exampleWindow',
  52. 'Head_69',
  53. 'morph-translation',
  54. ];
  55. const scales = new Map();
  56. scales.set( 'warrior/Warrior', 100 );
  57. scales.set( 'archer/ArcherRi01', 100 );
  58. scales.set( 'stanford-bunny', 0.001 );
  59. scales.set( 'Head_69', 100 );
  60. init();
  61. function init() {
  62. const container = document.createElement( 'div' );
  63. document.body.appendChild( container );
  64. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  65. camera.position.set( 100, 200, 300 );
  66. scene = new THREE.Scene();
  67. scene.background = new THREE.Color( 0xa0a0a0 );
  68. scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
  69. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444, 5 );
  70. hemiLight.position.set( 0, 200, 0 );
  71. scene.add( hemiLight );
  72. const dirLight = new THREE.DirectionalLight( 0xffffff, 5 );
  73. dirLight.position.set( 0, 200, 100 );
  74. dirLight.castShadow = true;
  75. dirLight.shadow.camera.top = 180;
  76. dirLight.shadow.camera.bottom = - 100;
  77. dirLight.shadow.camera.left = - 120;
  78. dirLight.shadow.camera.right = 120;
  79. scene.add( dirLight );
  80. // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
  81. // ground
  82. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  83. mesh.rotation.x = - Math.PI / 2;
  84. mesh.receiveShadow = true;
  85. scene.add( mesh );
  86. const grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
  87. grid.material.opacity = 0.2;
  88. grid.material.transparent = true;
  89. scene.add( grid );
  90. loader = new FBXLoader( manager );
  91. loadAsset( params.asset );
  92. renderer = new THREE.WebGLRenderer( { antialias: true } );
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. renderer.setAnimationLoop( animate );
  96. renderer.shadowMap.enabled = true;
  97. container.appendChild( renderer.domElement );
  98. const controls = new OrbitControls( camera, renderer.domElement );
  99. controls.target.set( 0, 100, 0 );
  100. controls.update();
  101. window.addEventListener( 'resize', onWindowResize );
  102. // stats
  103. stats = new Stats();
  104. container.appendChild( stats.dom );
  105. const gui = new GUI();
  106. gui.add( params, 'asset', assets ).onChange( function ( value ) {
  107. loadAsset( value );
  108. } );
  109. guiMorphsFolder = gui.addFolder( 'Morphs' ).hide();
  110. }
  111. function loadAsset( asset ) {
  112. loader.load( 'models/fbx/' + asset + '.fbx', function ( group ) {
  113. if ( object ) {
  114. object.traverse( function ( child ) {
  115. if ( child.isSkinnedMesh ) {
  116. child.skeleton.dispose();
  117. }
  118. if ( child.material ) {
  119. const materials = Array.isArray( child.material ) ? child.material : [ child.material ];
  120. materials.forEach( material => {
  121. if ( material.map ) material.map.dispose();
  122. material.dispose();
  123. } );
  124. }
  125. if ( child.geometry ) child.geometry.dispose();
  126. } );
  127. scene.remove( object );
  128. }
  129. //
  130. object = group;
  131. const scale = scales.get( asset );
  132. object.scale.setScalar( scale || 1 );
  133. if ( object.animations && object.animations.length ) {
  134. mixer = new THREE.AnimationMixer( object );
  135. const action = mixer.clipAction( object.animations[ 0 ] );
  136. action.play();
  137. } else {
  138. mixer = null;
  139. }
  140. guiMorphsFolder.children.forEach( ( child ) => child.destroy() );
  141. guiMorphsFolder.hide();
  142. object.traverse( function ( child ) {
  143. if ( child.isMesh ) {
  144. child.castShadow = true;
  145. child.receiveShadow = true;
  146. if ( child.morphTargetDictionary ) {
  147. guiMorphsFolder.show();
  148. const meshFolder = guiMorphsFolder.addFolder( child.name || child.uuid );
  149. Object.keys( child.morphTargetDictionary ).forEach( ( key ) => {
  150. meshFolder.add( child.morphTargetInfluences, child.morphTargetDictionary[ key ], 0, 1, 0.01 );
  151. } );
  152. }
  153. }
  154. } );
  155. scene.add( object );
  156. } );
  157. }
  158. function onWindowResize() {
  159. camera.aspect = window.innerWidth / window.innerHeight;
  160. camera.updateProjectionMatrix();
  161. renderer.setSize( window.innerWidth, window.innerHeight );
  162. }
  163. //
  164. function animate() {
  165. timer.update();
  166. const delta = timer.getDelta();
  167. if ( mixer ) mixer.update( delta );
  168. renderer.render( scene, camera );
  169. stats.update();
  170. }
  171. </script>
  172. </body>
  173. </html>
粤ICP备19079148号