webgl_loader_fbx.html 5.5 KB

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