webgl_animation_skinning_blending.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - animation - skinning</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 - animation - skinning">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_animation_skinning_blending.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_animation_skinning_blending.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. a {
  14. color: #f00;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="container"></div>
  20. <div id="info">
  21. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Skeletal Animation Blending
  22. (model from <a href="https://www.mixamo.com/" target="_blank" rel="noopener">mixamo.com</a>)<br/>
  23. Note: crossfades are possible with blend weights being set to (1,0,0), (0,1,0) or (0,0,1)
  24. </div>
  25. <script type="importmap">
  26. {
  27. "imports": {
  28. "three": "../build/three.module.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import Stats from 'three/addons/libs/stats.module.js';
  36. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  37. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  38. let scene, renderer, camera, stats;
  39. let model, skeleton, mixer, timer;
  40. const crossFadeControls = [];
  41. let idleAction, walkAction, runAction;
  42. let idleWeight, walkWeight, runWeight;
  43. let actions, settings;
  44. let singleStepMode = false;
  45. let sizeOfNextStep = 0;
  46. init();
  47. function init() {
  48. const container = document.getElementById( 'container' );
  49. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 100 );
  50. camera.position.set( 1, 2, - 3 );
  51. camera.lookAt( 0, 1, 0 );
  52. timer = new THREE.Timer();
  53. timer.connect( document );
  54. scene = new THREE.Scene();
  55. scene.background = new THREE.Color( 0xa0a0a0 );
  56. scene.fog = new THREE.Fog( 0xa0a0a0, 10, 50 );
  57. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 3 );
  58. hemiLight.position.set( 0, 20, 0 );
  59. scene.add( hemiLight );
  60. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  61. dirLight.position.set( - 3, 10, - 10 );
  62. dirLight.castShadow = true;
  63. dirLight.shadow.camera.top = 2;
  64. dirLight.shadow.camera.bottom = - 2;
  65. dirLight.shadow.camera.left = - 2;
  66. dirLight.shadow.camera.right = 2;
  67. dirLight.shadow.camera.near = 0.1;
  68. dirLight.shadow.camera.far = 40;
  69. scene.add( dirLight );
  70. // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
  71. // ground
  72. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 100, 100 ), new THREE.MeshPhongMaterial( { color: 0xcbcbcb, depthWrite: false } ) );
  73. mesh.rotation.x = - Math.PI / 2;
  74. mesh.receiveShadow = true;
  75. scene.add( mesh );
  76. const loader = new GLTFLoader();
  77. loader.load( 'models/gltf/Soldier.glb', function ( gltf ) {
  78. model = gltf.scene;
  79. scene.add( model );
  80. model.traverse( function ( object ) {
  81. if ( object.isMesh ) object.castShadow = true;
  82. } );
  83. //
  84. skeleton = new THREE.SkeletonHelper( model );
  85. skeleton.visible = false;
  86. scene.add( skeleton );
  87. //
  88. createPanel();
  89. //
  90. const animations = gltf.animations;
  91. mixer = new THREE.AnimationMixer( model );
  92. idleAction = mixer.clipAction( animations[ 0 ] );
  93. walkAction = mixer.clipAction( animations[ 3 ] );
  94. runAction = mixer.clipAction( animations[ 1 ] );
  95. actions = [ idleAction, walkAction, runAction ];
  96. activateAllActions();
  97. renderer.setAnimationLoop( animate );
  98. } );
  99. renderer = new THREE.WebGLRenderer( { antialias: true } );
  100. renderer.setPixelRatio( window.devicePixelRatio );
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. renderer.shadowMap.enabled = true;
  103. container.appendChild( renderer.domElement );
  104. stats = new Stats();
  105. container.appendChild( stats.dom );
  106. window.addEventListener( 'resize', onWindowResize );
  107. }
  108. function createPanel() {
  109. const panel = new GUI( { width: 310 } );
  110. const folder1 = panel.addFolder( 'Visibility' );
  111. const folder2 = panel.addFolder( 'Activation/Deactivation' );
  112. const folder3 = panel.addFolder( 'Pausing/Stepping' );
  113. const folder4 = panel.addFolder( 'Crossfading' );
  114. const folder5 = panel.addFolder( 'Blend Weights' );
  115. const folder6 = panel.addFolder( 'General Speed' );
  116. settings = {
  117. 'show model': true,
  118. 'show skeleton': false,
  119. 'deactivate all': deactivateAllActions,
  120. 'activate all': activateAllActions,
  121. 'pause/continue': pauseContinue,
  122. 'make single step': toSingleStepMode,
  123. 'modify step size': 0.05,
  124. 'from walk to idle': function () {
  125. prepareCrossFade( walkAction, idleAction, 1.0 );
  126. },
  127. 'from idle to walk': function () {
  128. prepareCrossFade( idleAction, walkAction, 0.5 );
  129. },
  130. 'from walk to run': function () {
  131. prepareCrossFade( walkAction, runAction, 2.5 );
  132. },
  133. 'from run to walk': function () {
  134. prepareCrossFade( runAction, walkAction, 5.0 );
  135. },
  136. 'use default duration': true,
  137. 'set custom duration': 3.5,
  138. 'modify idle weight': 0.0,
  139. 'modify walk weight': 1.0,
  140. 'modify run weight': 0.0,
  141. 'modify time scale': 1.0
  142. };
  143. folder1.add( settings, 'show model' ).onChange( showModel );
  144. folder1.add( settings, 'show skeleton' ).onChange( showSkeleton );
  145. folder2.add( settings, 'deactivate all' );
  146. folder2.add( settings, 'activate all' );
  147. folder3.add( settings, 'pause/continue' );
  148. folder3.add( settings, 'make single step' );
  149. folder3.add( settings, 'modify step size', 0.01, 0.1, 0.001 );
  150. crossFadeControls.push( folder4.add( settings, 'from walk to idle' ) );
  151. crossFadeControls.push( folder4.add( settings, 'from idle to walk' ) );
  152. crossFadeControls.push( folder4.add( settings, 'from walk to run' ) );
  153. crossFadeControls.push( folder4.add( settings, 'from run to walk' ) );
  154. folder4.add( settings, 'use default duration' );
  155. folder4.add( settings, 'set custom duration', 0, 10, 0.01 );
  156. folder5.add( settings, 'modify idle weight', 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) {
  157. setWeight( idleAction, weight );
  158. } );
  159. folder5.add( settings, 'modify walk weight', 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) {
  160. setWeight( walkAction, weight );
  161. } );
  162. folder5.add( settings, 'modify run weight', 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) {
  163. setWeight( runAction, weight );
  164. } );
  165. folder6.add( settings, 'modify time scale', 0.0, 1.5, 0.01 ).onChange( modifyTimeScale );
  166. folder1.open();
  167. folder2.open();
  168. folder3.open();
  169. folder4.open();
  170. folder5.open();
  171. folder6.open();
  172. }
  173. function showModel( visibility ) {
  174. model.visible = visibility;
  175. }
  176. function showSkeleton( visibility ) {
  177. skeleton.visible = visibility;
  178. }
  179. function modifyTimeScale( speed ) {
  180. mixer.timeScale = speed;
  181. }
  182. function deactivateAllActions() {
  183. actions.forEach( function ( action ) {
  184. action.stop();
  185. } );
  186. }
  187. function activateAllActions() {
  188. setWeight( idleAction, settings[ 'modify idle weight' ] );
  189. setWeight( walkAction, settings[ 'modify walk weight' ] );
  190. setWeight( runAction, settings[ 'modify run weight' ] );
  191. actions.forEach( function ( action ) {
  192. action.play();
  193. } );
  194. }
  195. function pauseContinue() {
  196. if ( singleStepMode ) {
  197. singleStepMode = false;
  198. unPauseAllActions();
  199. } else {
  200. if ( idleAction.paused ) {
  201. unPauseAllActions();
  202. } else {
  203. pauseAllActions();
  204. }
  205. }
  206. }
  207. function pauseAllActions() {
  208. actions.forEach( function ( action ) {
  209. action.paused = true;
  210. } );
  211. }
  212. function unPauseAllActions() {
  213. actions.forEach( function ( action ) {
  214. action.paused = false;
  215. } );
  216. }
  217. function toSingleStepMode() {
  218. unPauseAllActions();
  219. singleStepMode = true;
  220. sizeOfNextStep = settings[ 'modify step size' ];
  221. }
  222. function prepareCrossFade( startAction, endAction, defaultDuration ) {
  223. // Switch default / custom crossfade duration (according to the user's choice)
  224. const duration = setCrossFadeDuration( defaultDuration );
  225. // Make sure that we don't go on in singleStepMode, and that all actions are unpaused
  226. singleStepMode = false;
  227. unPauseAllActions();
  228. // If the current action is 'idle' (duration 4 sec), execute the crossfade immediately;
  229. // else wait until the current action has finished its current loop
  230. if ( startAction === idleAction ) {
  231. executeCrossFade( startAction, endAction, duration );
  232. } else {
  233. synchronizeCrossFade( startAction, endAction, duration );
  234. }
  235. }
  236. function setCrossFadeDuration( defaultDuration ) {
  237. // Switch default crossfade duration <-> custom crossfade duration
  238. if ( settings[ 'use default duration' ] ) {
  239. return defaultDuration;
  240. } else {
  241. return settings[ 'set custom duration' ];
  242. }
  243. }
  244. function synchronizeCrossFade( startAction, endAction, duration ) {
  245. mixer.addEventListener( 'loop', onLoopFinished );
  246. function onLoopFinished( event ) {
  247. if ( event.action === startAction ) {
  248. mixer.removeEventListener( 'loop', onLoopFinished );
  249. executeCrossFade( startAction, endAction, duration );
  250. }
  251. }
  252. }
  253. function executeCrossFade( startAction, endAction, duration ) {
  254. // Not only the start action, but also the end action must get a weight of 1 before fading
  255. // (concerning the start action this is already guaranteed in this place)
  256. setWeight( endAction, 1 );
  257. endAction.time = 0;
  258. // Crossfade with warping - you can also try without warping by setting the third parameter to false
  259. startAction.crossFadeTo( endAction, duration, true );
  260. }
  261. // This function is needed, since animationAction.crossFadeTo() disables its start action and sets
  262. // the start action's timeScale to ((start animation's duration) / (end animation's duration))
  263. function setWeight( action, weight ) {
  264. action.enabled = true;
  265. action.setEffectiveTimeScale( 1 );
  266. action.setEffectiveWeight( weight );
  267. }
  268. // Called by the render loop
  269. function updateWeightSliders() {
  270. settings[ 'modify idle weight' ] = idleWeight;
  271. settings[ 'modify walk weight' ] = walkWeight;
  272. settings[ 'modify run weight' ] = runWeight;
  273. }
  274. // Called by the render loop
  275. function updateCrossFadeControls() {
  276. if ( idleWeight === 1 && walkWeight === 0 && runWeight === 0 ) {
  277. crossFadeControls[ 0 ].disable();
  278. crossFadeControls[ 1 ].enable();
  279. crossFadeControls[ 2 ].disable();
  280. crossFadeControls[ 3 ].disable();
  281. }
  282. if ( idleWeight === 0 && walkWeight === 1 && runWeight === 0 ) {
  283. crossFadeControls[ 0 ].enable();
  284. crossFadeControls[ 1 ].disable();
  285. crossFadeControls[ 2 ].enable();
  286. crossFadeControls[ 3 ].disable();
  287. }
  288. if ( idleWeight === 0 && walkWeight === 0 && runWeight === 1 ) {
  289. crossFadeControls[ 0 ].disable();
  290. crossFadeControls[ 1 ].disable();
  291. crossFadeControls[ 2 ].disable();
  292. crossFadeControls[ 3 ].enable();
  293. }
  294. }
  295. function onWindowResize() {
  296. camera.aspect = window.innerWidth / window.innerHeight;
  297. camera.updateProjectionMatrix();
  298. renderer.setSize( window.innerWidth, window.innerHeight );
  299. }
  300. function animate() {
  301. timer.update();
  302. idleWeight = idleAction.getEffectiveWeight();
  303. walkWeight = walkAction.getEffectiveWeight();
  304. runWeight = runAction.getEffectiveWeight();
  305. // Update the panel values if weights are modified from "outside" (by crossfadings)
  306. updateWeightSliders();
  307. // Enable/disable crossfade controls according to current weight values
  308. updateCrossFadeControls();
  309. // Get the time elapsed since the last frame, used for mixer update (if not in single step mode)
  310. let mixerUpdateDelta = timer.getDelta();
  311. // If in single step mode, make one step and then do nothing (until the user clicks again)
  312. if ( singleStepMode ) {
  313. mixerUpdateDelta = sizeOfNextStep;
  314. sizeOfNextStep = 0;
  315. }
  316. // Update the animation mixer, the stats panel, and render this frame
  317. mixer.update( mixerUpdateDelta );
  318. renderer.render( scene, camera );
  319. stats.update();
  320. }
  321. </script>
  322. </body>
  323. </html>
粤ICP备19079148号