webgl_animation_skinning_additive_blending.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 button {
  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/lil-gui.module.min.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.setInactive = function () {
  159. control.domElement.classList.add( 'control-inactive' );
  160. };
  161. control.setActive = function () {
  162. control.domElement.classList.remove( 'control-inactive' );
  163. };
  164. const settings = baseActions[ control.property ];
  165. if ( ! settings || ! settings.weight ) {
  166. control.setInactive();
  167. }
  168. } );
  169. }
  170. function activateAction( action ) {
  171. const clip = action.getClip();
  172. const settings = baseActions[ clip.name ] || additiveActions[ clip.name ];
  173. setWeight( action, settings.weight );
  174. action.play();
  175. }
  176. function modifyTimeScale( speed ) {
  177. mixer.timeScale = speed;
  178. }
  179. function prepareCrossFade( startAction, endAction, duration ) {
  180. // If the current action is 'idle', execute the crossfade immediately;
  181. // else wait until the current action has finished its current loop
  182. if ( currentBaseAction === 'idle' || ! startAction || ! endAction ) {
  183. executeCrossFade( startAction, endAction, duration );
  184. } else {
  185. synchronizeCrossFade( startAction, endAction, duration );
  186. }
  187. // Update control colors
  188. if ( endAction ) {
  189. const clip = endAction.getClip();
  190. currentBaseAction = clip.name;
  191. } else {
  192. currentBaseAction = 'None';
  193. }
  194. crossFadeControls.forEach( function ( control ) {
  195. const name = control.property;
  196. if ( name === currentBaseAction ) {
  197. control.setActive();
  198. } else {
  199. control.setInactive();
  200. }
  201. } );
  202. }
  203. function synchronizeCrossFade( startAction, endAction, duration ) {
  204. mixer.addEventListener( 'loop', onLoopFinished );
  205. function onLoopFinished( event ) {
  206. if ( event.action === startAction ) {
  207. mixer.removeEventListener( 'loop', onLoopFinished );
  208. executeCrossFade( startAction, endAction, duration );
  209. }
  210. }
  211. }
  212. function executeCrossFade( startAction, endAction, duration ) {
  213. // Not only the start action, but also the end action must get a weight of 1 before fading
  214. // (concerning the start action this is already guaranteed in this place)
  215. if ( endAction ) {
  216. setWeight( endAction, 1 );
  217. endAction.time = 0;
  218. if ( startAction ) {
  219. // Crossfade with warping
  220. startAction.crossFadeTo( endAction, duration, true );
  221. } else {
  222. // Fade in
  223. endAction.fadeIn( duration );
  224. }
  225. } else {
  226. // Fade out
  227. startAction.fadeOut( duration );
  228. }
  229. }
  230. // This function is needed, since animationAction.crossFadeTo() disables its start action and sets
  231. // the start action's timeScale to ((start animation's duration) / (end animation's duration))
  232. function setWeight( action, weight ) {
  233. action.enabled = true;
  234. action.setEffectiveTimeScale( 1 );
  235. action.setEffectiveWeight( weight );
  236. }
  237. function onWindowResize() {
  238. camera.aspect = window.innerWidth / window.innerHeight;
  239. camera.updateProjectionMatrix();
  240. renderer.setSize( window.innerWidth, window.innerHeight );
  241. }
  242. function animate() {
  243. // Render loop
  244. requestAnimationFrame( animate );
  245. for ( let i = 0; i !== numAnimations; ++ i ) {
  246. const action = allActions[ i ];
  247. const clip = action.getClip();
  248. const settings = baseActions[ clip.name ] || additiveActions[ clip.name ];
  249. settings.weight = action.getEffectiveWeight();
  250. }
  251. // Get the time elapsed since the last frame, used for mixer update
  252. const mixerUpdateDelta = clock.getDelta();
  253. // Update the animation mixer, the stats panel, and render this frame
  254. mixer.update( mixerUpdateDelta );
  255. stats.update();
  256. renderer.render( scene, camera );
  257. }
  258. </script>
  259. </body>
  260. </html>