webgl_animation_skinning_blending.html 13 KB

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