webgl_animation_skinning_blending.html 12 KB

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