webgl_loader_fbx.html 5.9 KB

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