webgl_loader_fbx.html 5.9 KB

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