webgl_animation_skinning_blending.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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.outputEncoding = THREE.sRGBEncoding;
  103. renderer.shadowMap.enabled = true;
  104. container.appendChild( renderer.domElement );
  105. stats = new Stats();
  106. container.appendChild( stats.dom );
  107. window.addEventListener( 'resize', onWindowResize, false );
  108. }
  109. function createPanel() {
  110. var panel = new GUI( { width: 310 } );
  111. var folder1 = panel.addFolder( 'Visibility' );
  112. var folder2 = panel.addFolder( 'Activation/Deactivation' );
  113. var folder3 = panel.addFolder( 'Pausing/Stepping' );
  114. var folder4 = panel.addFolder( 'Crossfading' );
  115. var folder5 = panel.addFolder( 'Blend Weights' );
  116. var folder6 = panel.addFolder( 'General Speed' );
  117. settings = {
  118. 'show model': true,
  119. 'show skeleton': false,
  120. 'deactivate all': deactivateAllActions,
  121. 'activate all': activateAllActions,
  122. 'pause/continue': pauseContinue,
  123. 'make single step': toSingleStepMode,
  124. 'modify step size': 0.05,
  125. 'from walk to idle': function () {
  126. prepareCrossFade( walkAction, idleAction, 1.0 );
  127. },
  128. 'from idle to walk': function () {
  129. prepareCrossFade( idleAction, walkAction, 0.5 );
  130. },
  131. 'from walk to run': function () {
  132. prepareCrossFade( walkAction, runAction, 2.5 );
  133. },
  134. 'from run to walk': function () {
  135. prepareCrossFade( runAction, walkAction, 5.0 );
  136. },
  137. 'use default duration': true,
  138. 'set custom duration': 3.5,
  139. 'modify idle weight': 0.0,
  140. 'modify walk weight': 1.0,
  141. 'modify run weight': 0.0,
  142. 'modify time scale': 1.0
  143. };
  144. folder1.add( settings, 'show model' ).onChange( showModel );
  145. folder1.add( settings, 'show skeleton' ).onChange( showSkeleton );
  146. folder2.add( settings, 'deactivate all' );
  147. folder2.add( settings, 'activate all' );
  148. folder3.add( settings, 'pause/continue' );
  149. folder3.add( settings, 'make single step' );
  150. folder3.add( settings, 'modify step size', 0.01, 0.1, 0.001 );
  151. crossFadeControls.push( folder4.add( settings, 'from walk to idle' ) );
  152. crossFadeControls.push( folder4.add( settings, 'from idle to walk' ) );
  153. crossFadeControls.push( folder4.add( settings, 'from walk to run' ) );
  154. crossFadeControls.push( folder4.add( settings, 'from run to walk' ) );
  155. folder4.add( settings, 'use default duration' );
  156. folder4.add( settings, 'set custom duration', 0, 10, 0.01 );
  157. folder5.add( settings, 'modify idle weight', 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) {
  158. setWeight( idleAction, weight );
  159. } );
  160. folder5.add( settings, 'modify walk weight', 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) {
  161. setWeight( walkAction, weight );
  162. } );
  163. folder5.add( settings, 'modify run weight', 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) {
  164. setWeight( runAction, weight );
  165. } );
  166. folder6.add( settings, 'modify time scale', 0.0, 1.5, 0.01 ).onChange( modifyTimeScale );
  167. folder1.open();
  168. folder2.open();
  169. folder3.open();
  170. folder4.open();
  171. folder5.open();
  172. folder6.open();
  173. crossFadeControls.forEach( function ( control ) {
  174. control.classList1 = control.domElement.parentElement.parentElement.classList;
  175. control.classList2 = control.domElement.previousElementSibling.classList;
  176. control.setDisabled = function () {
  177. control.classList1.add( 'no-pointer-events' );
  178. control.classList2.add( 'control-disabled' );
  179. };
  180. control.setEnabled = function () {
  181. control.classList1.remove( 'no-pointer-events' );
  182. control.classList2.remove( 'control-disabled' );
  183. };
  184. } );
  185. }
  186. function showModel( visibility ) {
  187. model.visible = visibility;
  188. }
  189. function showSkeleton( visibility ) {
  190. skeleton.visible = visibility;
  191. }
  192. function modifyTimeScale( speed ) {
  193. mixer.timeScale = speed;
  194. }
  195. function deactivateAllActions() {
  196. actions.forEach( function ( action ) {
  197. action.stop();
  198. } );
  199. }
  200. function activateAllActions() {
  201. setWeight( idleAction, settings[ 'modify idle weight' ] );
  202. setWeight( walkAction, settings[ 'modify walk weight' ] );
  203. setWeight( runAction, settings[ 'modify run weight' ] );
  204. actions.forEach( function ( action ) {
  205. action.play();
  206. } );
  207. }
  208. function pauseContinue() {
  209. if ( singleStepMode ) {
  210. singleStepMode = false;
  211. unPauseAllActions();
  212. } else {
  213. if ( idleAction.paused ) {
  214. unPauseAllActions();
  215. } else {
  216. pauseAllActions();
  217. }
  218. }
  219. }
  220. function pauseAllActions() {
  221. actions.forEach( function ( action ) {
  222. action.paused = true;
  223. } );
  224. }
  225. function unPauseAllActions() {
  226. actions.forEach( function ( action ) {
  227. action.paused = false;
  228. } );
  229. }
  230. function toSingleStepMode() {
  231. unPauseAllActions();
  232. singleStepMode = true;
  233. sizeOfNextStep = settings[ 'modify step size' ];
  234. }
  235. function prepareCrossFade( startAction, endAction, defaultDuration ) {
  236. // Switch default / custom crossfade duration (according to the user's choice)
  237. var duration = setCrossFadeDuration( defaultDuration );
  238. // Make sure that we don't go on in singleStepMode, and that all actions are unpaused
  239. singleStepMode = false;
  240. unPauseAllActions();
  241. // If the current action is 'idle' (duration 4 sec), execute the crossfade immediately;
  242. // else wait until the current action has finished its current loop
  243. if ( startAction === idleAction ) {
  244. executeCrossFade( startAction, endAction, duration );
  245. } else {
  246. synchronizeCrossFade( startAction, endAction, duration );
  247. }
  248. }
  249. function setCrossFadeDuration( defaultDuration ) {
  250. // Switch default crossfade duration <-> custom crossfade duration
  251. if ( settings[ 'use default duration' ] ) {
  252. return defaultDuration;
  253. } else {
  254. return settings[ 'set custom duration' ];
  255. }
  256. }
  257. function synchronizeCrossFade( startAction, endAction, duration ) {
  258. mixer.addEventListener( 'loop', onLoopFinished );
  259. function onLoopFinished( event ) {
  260. if ( event.action === startAction ) {
  261. mixer.removeEventListener( 'loop', onLoopFinished );
  262. executeCrossFade( startAction, endAction, duration );
  263. }
  264. }
  265. }
  266. function executeCrossFade( startAction, endAction, duration ) {
  267. // Not only the start action, but also the end action must get a weight of 1 before fading
  268. // (concerning the start action this is already guaranteed in this place)
  269. setWeight( endAction, 1 );
  270. endAction.time = 0;
  271. // Crossfade with warping - you can also try without warping by setting the third parameter to false
  272. startAction.crossFadeTo( endAction, duration, true );
  273. }
  274. // This function is needed, since animationAction.crossFadeTo() disables its start action and sets
  275. // the start action's timeScale to ((start animation's duration) / (end animation's duration))
  276. function setWeight( action, weight ) {
  277. action.enabled = true;
  278. action.setEffectiveTimeScale( 1 );
  279. action.setEffectiveWeight( weight );
  280. }
  281. // Called by the render loop
  282. function updateWeightSliders() {
  283. settings[ 'modify idle weight' ] = idleWeight;
  284. settings[ 'modify walk weight' ] = walkWeight;
  285. settings[ 'modify run weight' ] = runWeight;
  286. }
  287. // Called by the render loop
  288. function updateCrossFadeControls() {
  289. crossFadeControls.forEach( function ( control ) {
  290. control.setDisabled();
  291. } );
  292. if ( idleWeight === 1 && walkWeight === 0 && runWeight === 0 ) {
  293. crossFadeControls[ 1 ].setEnabled();
  294. }
  295. if ( idleWeight === 0 && walkWeight === 1 && runWeight === 0 ) {
  296. crossFadeControls[ 0 ].setEnabled();
  297. crossFadeControls[ 2 ].setEnabled();
  298. }
  299. if ( idleWeight === 0 && walkWeight === 0 && runWeight === 1 ) {
  300. crossFadeControls[ 3 ].setEnabled();
  301. }
  302. }
  303. function onWindowResize() {
  304. camera.aspect = window.innerWidth / window.innerHeight;
  305. camera.updateProjectionMatrix();
  306. renderer.setSize( window.innerWidth, window.innerHeight );
  307. }
  308. function animate() {
  309. // Render loop
  310. requestAnimationFrame( animate );
  311. idleWeight = idleAction.getEffectiveWeight();
  312. walkWeight = walkAction.getEffectiveWeight();
  313. runWeight = runAction.getEffectiveWeight();
  314. // Update the panel values if weights are modified from "outside" (by crossfadings)
  315. updateWeightSliders();
  316. // Enable/disable crossfade controls according to current weight values
  317. updateCrossFadeControls();
  318. // Get the time elapsed since the last frame, used for mixer update (if not in single step mode)
  319. var mixerUpdateDelta = clock.getDelta();
  320. // If in single step mode, make one step and then do nothing (until the user clicks again)
  321. if ( singleStepMode ) {
  322. mixerUpdateDelta = sizeOfNextStep;
  323. sizeOfNextStep = 0;
  324. }
  325. // Update the animation mixer, the stats panel, and render this frame
  326. mixer.update( mixerUpdateDelta );
  327. stats.update();
  328. renderer.render( scene, camera );
  329. }
  330. </script>
  331. </body>
  332. </html>