webgl_animation_skinning_additive_blending.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - additive 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: blue;
  11. }
  12. .control-inactive {
  13. color: #888;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="container"></div>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Skeletal Additive Animation Blending
  21. (model from <a href="https://www.mixamo.com/" target="_blank" rel="noopener">mixamo.com</a>)<br/>
  22. </div>
  23. <script type="module">
  24. import * as THREE from '../build/three.module.js';
  25. import Stats from './jsm/libs/stats.module.js';
  26. import { GUI } from './jsm/libs/dat.gui.module.js';
  27. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  28. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  29. let scene, renderer, camera, stats;
  30. let model, skeleton, mixer, clock;
  31. const crossFadeControls = [];
  32. let currentBaseAction = 'idle';
  33. const allActions = [];
  34. const baseActions = {
  35. idle: { weight: 1 },
  36. walk: { weight: 0 },
  37. run: { weight: 0 }
  38. };
  39. const additiveActions = {
  40. sneak_pose: { weight: 0 },
  41. sad_pose: { weight: 0 },
  42. agree: { weight: 0 },
  43. headShake: { weight: 0 }
  44. };
  45. let panelSettings, numAnimations;
  46. init();
  47. function init() {
  48. const container = document.getElementById( 'container' );
  49. clock = new THREE.Clock();
  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, 0x444444 );
  54. hemiLight.position.set( 0, 20, 0 );
  55. scene.add( hemiLight );
  56. const dirLight = new THREE.DirectionalLight( 0xffffff );
  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. // ground
  67. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 100, 100 ), new THREE.MeshPhongMaterial( { color: 0x999999, 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/Xbot.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. skeleton = new THREE.SkeletonHelper( model );
  79. skeleton.visible = false;
  80. scene.add( skeleton );
  81. const animations = gltf.animations;
  82. mixer = new THREE.AnimationMixer( model );
  83. numAnimations = animations.length;
  84. for ( let i = 0; i !== numAnimations; ++ i ) {
  85. let clip = animations[ i ];
  86. const name = clip.name;
  87. if ( baseActions[ name ] ) {
  88. const action = mixer.clipAction( clip );
  89. activateAction( action );
  90. baseActions[ name ].action = action;
  91. allActions.push( action );
  92. } else if ( additiveActions[ name ] ) {
  93. // Make the clip additive and remove the reference frame
  94. THREE.AnimationUtils.makeClipAdditive( clip );
  95. if ( clip.name.endsWith( '_pose' ) ) {
  96. clip = THREE.AnimationUtils.subclip( clip, clip.name, 2, 3, 30 );
  97. }
  98. const action = mixer.clipAction( clip );
  99. activateAction( action );
  100. additiveActions[ name ].action = action;
  101. allActions.push( action );
  102. }
  103. }
  104. createPanel();
  105. animate();
  106. } );
  107. renderer = new THREE.WebGLRenderer( { antialias: true } );
  108. renderer.setPixelRatio( window.devicePixelRatio );
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. renderer.outputEncoding = THREE.sRGBEncoding;
  111. renderer.shadowMap.enabled = true;
  112. container.appendChild( renderer.domElement );
  113. // camera
  114. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 100 );
  115. camera.position.set( - 1, 2, 3 );
  116. const controls = new OrbitControls( camera, renderer.domElement );
  117. controls.enablePan = false;
  118. controls.enableZoom = false;
  119. controls.target.set( 0, 1, 0 );
  120. controls.update();
  121. stats = new Stats();
  122. container.appendChild( stats.dom );
  123. window.addEventListener( 'resize', onWindowResize );
  124. }
  125. function createPanel() {
  126. const panel = new GUI( { width: 310 } );
  127. const folder1 = panel.addFolder( 'Base Actions' );
  128. const folder2 = panel.addFolder( 'Additive Action Weights' );
  129. const folder3 = panel.addFolder( 'General Speed' );
  130. panelSettings = {
  131. 'modify time scale': 1.0
  132. };
  133. const baseNames = [ 'None', ...Object.keys( baseActions ) ];
  134. for ( let i = 0, l = baseNames.length; i !== l; ++ i ) {
  135. const name = baseNames[ i ];
  136. const settings = baseActions[ name ];
  137. panelSettings[ name ] = function () {
  138. const currentSettings = baseActions[ currentBaseAction ];
  139. const currentAction = currentSettings ? currentSettings.action : null;
  140. const action = settings ? settings.action : null;
  141. prepareCrossFade( currentAction, action, 0.35 );
  142. };
  143. crossFadeControls.push( folder1.add( panelSettings, name ) );
  144. }
  145. for ( const name of Object.keys( additiveActions ) ) {
  146. const settings = additiveActions[ name ];
  147. panelSettings[ name ] = settings.weight;
  148. folder2.add( panelSettings, name, 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) {
  149. setWeight( settings.action, weight );
  150. settings.weight = weight;
  151. } );
  152. }
  153. folder3.add( panelSettings, 'modify time scale', 0.0, 1.5, 0.01 ).onChange( modifyTimeScale );
  154. folder1.open();
  155. folder2.open();
  156. folder3.open();
  157. crossFadeControls.forEach( function ( control ) {
  158. control.classList1 = control.domElement.parentElement.parentElement.classList;
  159. control.classList2 = control.domElement.previousElementSibling.classList;
  160. control.setInactive = function () {
  161. control.classList2.add( 'control-inactive' );
  162. };
  163. control.setActive = function () {
  164. control.classList2.remove( 'control-inactive' );
  165. };
  166. const settings = baseActions[ control.property ];
  167. if ( ! settings || ! settings.weight ) {
  168. control.setInactive();
  169. }
  170. } );
  171. }
  172. function activateAction( action ) {
  173. const clip = action.getClip();
  174. const settings = baseActions[ clip.name ] || additiveActions[ clip.name ];
  175. setWeight( action, settings.weight );
  176. action.play();
  177. }
  178. function modifyTimeScale( speed ) {
  179. mixer.timeScale = speed;
  180. }
  181. function prepareCrossFade( startAction, endAction, duration ) {
  182. // If the current action is 'idle', execute the crossfade immediately;
  183. // else wait until the current action has finished its current loop
  184. if ( currentBaseAction === 'idle' || ! startAction || ! endAction ) {
  185. executeCrossFade( startAction, endAction, duration );
  186. } else {
  187. synchronizeCrossFade( startAction, endAction, duration );
  188. }
  189. // Update control colors
  190. if ( endAction ) {
  191. const clip = endAction.getClip();
  192. currentBaseAction = clip.name;
  193. } else {
  194. currentBaseAction = 'None';
  195. }
  196. crossFadeControls.forEach( function ( control ) {
  197. const name = control.property;
  198. if ( name === currentBaseAction ) {
  199. control.setActive();
  200. } else {
  201. control.setInactive();
  202. }
  203. } );
  204. }
  205. function synchronizeCrossFade( startAction, endAction, duration ) {
  206. mixer.addEventListener( 'loop', onLoopFinished );
  207. function onLoopFinished( event ) {
  208. if ( event.action === startAction ) {
  209. mixer.removeEventListener( 'loop', onLoopFinished );
  210. executeCrossFade( startAction, endAction, duration );
  211. }
  212. }
  213. }
  214. function executeCrossFade( startAction, endAction, duration ) {
  215. // Not only the start action, but also the end action must get a weight of 1 before fading
  216. // (concerning the start action this is already guaranteed in this place)
  217. if ( endAction ) {
  218. setWeight( endAction, 1 );
  219. endAction.time = 0;
  220. if ( startAction ) {
  221. // Crossfade with warping
  222. startAction.crossFadeTo( endAction, duration, true );
  223. } else {
  224. // Fade in
  225. endAction.fadeIn( duration );
  226. }
  227. } else {
  228. // Fade out
  229. startAction.fadeOut( duration );
  230. }
  231. }
  232. // This function is needed, since animationAction.crossFadeTo() disables its start action and sets
  233. // the start action's timeScale to ((start animation's duration) / (end animation's duration))
  234. function setWeight( action, weight ) {
  235. action.enabled = true;
  236. action.setEffectiveTimeScale( 1 );
  237. action.setEffectiveWeight( weight );
  238. }
  239. function onWindowResize() {
  240. camera.aspect = window.innerWidth / window.innerHeight;
  241. camera.updateProjectionMatrix();
  242. renderer.setSize( window.innerWidth, window.innerHeight );
  243. }
  244. function animate() {
  245. // Render loop
  246. requestAnimationFrame( animate );
  247. for ( let i = 0; i !== numAnimations; ++ i ) {
  248. const action = allActions[ i ];
  249. const clip = action.getClip();
  250. const settings = baseActions[ clip.name ] || additiveActions[ clip.name ];
  251. settings.weight = action.getEffectiveWeight();
  252. }
  253. // Get the time elapsed since the last frame, used for mixer update
  254. const mixerUpdateDelta = clock.getDelta();
  255. // Update the animation mixer, the stats panel, and render this frame
  256. mixer.update( mixerUpdateDelta );
  257. stats.update();
  258. renderer.render( scene, camera );
  259. }
  260. </script>
  261. </body>
  262. </html>