webgl_animation_skinning_blending.html 12 KB

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