webgl_animation_skinning_blending.html 12 KB

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