webgl_animation_skinning_blending.html 13 KB

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