webgl_animation_skinning_blending.html 13 KB

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