webgl_animation_skinning_blending.html 12 KB

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