webgl_animation_skinning_blending.html 12 KB

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