2
0

webgl_animation_skinning_blending.html 12 KB

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