webgl_animation_skinning_blending.html 13 KB

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