webgl_animation_skinning_blending.html 13 KB

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