webgl_animation_skinning_blending.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 () { prepareCrossFade( walkAction, idleAction, 1.0 ) },
  135. 'from idle to walk': function () { prepareCrossFade( idleAction, walkAction, 0.5 ) },
  136. 'from walk to run': function () { prepareCrossFade( walkAction, runAction, 2.5 ) },
  137. 'from run to walk': function () { prepareCrossFade( runAction, walkAction, 5.0 ) },
  138. 'use default duration': true,
  139. 'set custom duration': 3.5,
  140. 'modify idle weight': 0.0,
  141. 'modify walk weight': 1.0,
  142. 'modify run weight': 0.0,
  143. 'modify time scale': 1.0
  144. };
  145. folder1.add( settings, 'show model' ).onChange( showModel );
  146. folder1.add( settings, 'show skeleton' ).onChange( showSkeleton );
  147. folder2.add( settings, 'deactivate all' );
  148. folder2.add( settings, 'activate all' );
  149. folder3.add( settings, 'pause/continue' );
  150. folder3.add( settings, 'make single step' );
  151. folder3.add( settings, 'modify step size', 0.01, 0.1, 0.001 );
  152. crossFadeControls.push( folder4.add( settings, 'from walk to idle' ) );
  153. crossFadeControls.push( folder4.add( settings, 'from idle to walk' ) );
  154. crossFadeControls.push( folder4.add( settings, 'from walk to run' ) );
  155. crossFadeControls.push( folder4.add( settings, 'from run to walk' ) );
  156. folder4.add( settings, 'use default duration' );
  157. folder4.add( settings, 'set custom duration', 0, 10, 0.01 );
  158. folder5.add( settings, 'modify idle weight', 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) { setWeight( idleAction, weight ) } );
  159. folder5.add( settings, 'modify walk weight', 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) { setWeight( walkAction, weight ) } );
  160. folder5.add( settings, 'modify run weight', 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) { setWeight( runAction, weight ) } );
  161. folder6.add( settings, 'modify time scale', 0.0, 1.5, 0.01 ).onChange( modifyTimeScale );
  162. folder1.open();
  163. folder2.open();
  164. folder3.open();
  165. folder4.open();
  166. folder5.open();
  167. folder6.open();
  168. crossFadeControls.forEach( function ( control ) {
  169. control.classList1 = control.domElement.parentElement.parentElement.classList;
  170. control.classList2 = control.domElement.previousElementSibling.classList;
  171. control.setDisabled = function () {
  172. control.classList1.add( 'no-pointer-events' );
  173. control.classList2.add( 'control-disabled' );
  174. };
  175. control.setEnabled = function () {
  176. control.classList1.remove( 'no-pointer-events' );
  177. control.classList2.remove( 'control-disabled' );
  178. };
  179. } );
  180. }
  181. function showModel( visibility ) {
  182. mesh.visible = visibility;
  183. }
  184. function showSkeleton( visibility ) {
  185. skeleton.visible = visibility;
  186. }
  187. function modifyTimeScale( speed ) {
  188. mixer.timeScale = speed;
  189. }
  190. function deactivateAllActions() {
  191. actions.forEach( function ( action ) {
  192. action.stop();
  193. } );
  194. }
  195. function activateAllActions() {
  196. setWeight( idleAction, settings[ 'modify idle weight' ] );
  197. setWeight( walkAction, settings[ 'modify walk weight' ] );
  198. setWeight( runAction, settings[ 'modify run weight' ] );
  199. actions.forEach( function ( action ) {
  200. action.play();
  201. } );
  202. }
  203. function pauseContinue() {
  204. if ( singleStepMode ) {
  205. singleStepMode = false;
  206. unPauseAllActions();
  207. } else {
  208. if ( idleAction.paused ) {
  209. unPauseAllActions();
  210. } else {
  211. pauseAllActions();
  212. }
  213. }
  214. }
  215. function pauseAllActions() {
  216. actions.forEach( function ( action ) {
  217. action.paused = true;
  218. } );
  219. }
  220. function unPauseAllActions() {
  221. actions.forEach( function ( action ) {
  222. action.paused = false;
  223. } );
  224. }
  225. function toSingleStepMode() {
  226. unPauseAllActions();
  227. singleStepMode = true;
  228. sizeOfNextStep = settings[ 'modify step size' ];
  229. }
  230. function prepareCrossFade( startAction, endAction, defaultDuration ) {
  231. // Switch default / custom crossfade duration (according to the user's choice)
  232. var duration = setCrossFadeDuration( defaultDuration );
  233. // Make sure that we don't go on in singleStepMode, and that all actions are unpaused
  234. singleStepMode = false;
  235. unPauseAllActions();
  236. // If the current action is 'idle' (duration 4 sec), execute the crossfade immediately;
  237. // else wait until the current action has finished its current loop
  238. if ( startAction === idleAction ) {
  239. executeCrossFade( startAction, endAction, duration );
  240. } else {
  241. synchronizeCrossFade( startAction, endAction, duration );
  242. }
  243. }
  244. function setCrossFadeDuration( defaultDuration ) {
  245. // Switch default crossfade duration <-> custom crossfade duration
  246. if ( settings[ 'use default duration' ] ) {
  247. return defaultDuration;
  248. } else {
  249. return settings[ 'set custom duration' ];
  250. }
  251. }
  252. function synchronizeCrossFade( startAction, endAction, duration ) {
  253. mixer.addEventListener( 'loop', onLoopFinished );
  254. function onLoopFinished( event ) {
  255. if ( event.action === startAction ) {
  256. mixer.removeEventListener( 'loop', onLoopFinished );
  257. executeCrossFade( startAction, endAction, duration );
  258. }
  259. }
  260. }
  261. function executeCrossFade( startAction, endAction, duration ) {
  262. // Not only the start action, but also the end action must get a weight of 1 before fading
  263. // (concerning the start action this is already guaranteed in this place)
  264. setWeight( endAction, 1 );
  265. endAction.time = 0;
  266. // Crossfade with warping - you can also try without warping by setting the third parameter to false
  267. startAction.crossFadeTo( endAction, duration, true );
  268. }
  269. // This function is needed, since animationAction.crossFadeTo() disables its start action and sets
  270. // the start action's timeScale to ((start animation's duration) / (end animation's duration))
  271. function setWeight( action, weight ) {
  272. action.enabled = true;
  273. action.setEffectiveTimeScale( 1 );
  274. action.setEffectiveWeight( weight );
  275. }
  276. // Called by the render loop
  277. function updateWeightSliders() {
  278. settings[ 'modify idle weight' ] = idleWeight;
  279. settings[ 'modify walk weight' ] = walkWeight;
  280. settings[ 'modify run weight' ] = runWeight;
  281. }
  282. // Called by the render loop
  283. function updateCrossFadeControls() {
  284. crossFadeControls.forEach( function ( control ) {
  285. control.setDisabled();
  286. } );
  287. if ( idleWeight === 1 && walkWeight === 0 && runWeight === 0 ) {
  288. crossFadeControls[ 1 ].setEnabled();
  289. }
  290. if ( idleWeight === 0 && walkWeight === 1 && runWeight === 0 ) {
  291. crossFadeControls[ 0 ].setEnabled();
  292. crossFadeControls[ 2 ].setEnabled();
  293. }
  294. if ( idleWeight === 0 && walkWeight === 0 && runWeight === 1 ) {
  295. crossFadeControls[ 3 ].setEnabled();
  296. }
  297. }
  298. function onWindowResize() {
  299. camera.aspect = window.innerWidth / window.innerHeight;
  300. camera.updateProjectionMatrix();
  301. renderer.setSize( window.innerWidth, window.innerHeight );
  302. }
  303. function animate() {
  304. // Render loop
  305. requestAnimationFrame( animate );
  306. idleWeight = idleAction.getEffectiveWeight();
  307. walkWeight = walkAction.getEffectiveWeight();
  308. runWeight = runAction.getEffectiveWeight();
  309. // Update the panel values if weights are modified from "outside" (by crossfadings)
  310. updateWeightSliders();
  311. // Enable/disable crossfade controls according to current weight values
  312. updateCrossFadeControls();
  313. // Get the time elapsed since the last frame, used for mixer update (if not in single step mode)
  314. var mixerUpdateDelta = clock.getDelta();
  315. // If in single step mode, make one step and then do nothing (until the user clicks again)
  316. if ( singleStepMode ) {
  317. mixerUpdateDelta = sizeOfNextStep;
  318. sizeOfNextStep = 0;
  319. }
  320. // Update the animation mixer, the stats panel, and render this frame
  321. mixer.update( mixerUpdateDelta );
  322. stats.update();
  323. renderer.render( scene, camera );
  324. }
  325. </script>
  326. </body>
  327. </html>