webgl_animation_skinning_additive_blending.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.module.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import Stats from 'three/addons/libs/stats.module.js';
  34. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  37. let scene, renderer, camera, stats;
  38. let model, skeleton, mixer, clock;
  39. const crossFadeControls = [];
  40. let currentBaseAction = 'idle';
  41. const allActions = [];
  42. const baseActions = {
  43. idle: { weight: 1 },
  44. walk: { weight: 0 },
  45. run: { weight: 0 }
  46. };
  47. const additiveActions = {
  48. sneak_pose: { weight: 0 },
  49. sad_pose: { weight: 0 },
  50. agree: { weight: 0 },
  51. headShake: { weight: 0 }
  52. };
  53. let panelSettings, numAnimations;
  54. init();
  55. function init() {
  56. const container = document.getElementById( 'container' );
  57. clock = new THREE.Clock();
  58. scene = new THREE.Scene();
  59. scene.background = new THREE.Color( 0xa0a0a0 );
  60. scene.fog = new THREE.Fog( 0xa0a0a0, 10, 50 );
  61. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 3 );
  62. hemiLight.position.set( 0, 20, 0 );
  63. scene.add( hemiLight );
  64. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  65. dirLight.position.set( 3, 10, 10 );
  66. dirLight.castShadow = true;
  67. dirLight.shadow.camera.top = 2;
  68. dirLight.shadow.camera.bottom = - 2;
  69. dirLight.shadow.camera.left = - 2;
  70. dirLight.shadow.camera.right = 2;
  71. dirLight.shadow.camera.near = 0.1;
  72. dirLight.shadow.camera.far = 40;
  73. scene.add( dirLight );
  74. // ground
  75. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 100, 100 ), new THREE.MeshPhongMaterial( { color: 0xcbcbcb, depthWrite: false } ) );
  76. mesh.rotation.x = - Math.PI / 2;
  77. mesh.receiveShadow = true;
  78. scene.add( mesh );
  79. const loader = new GLTFLoader();
  80. loader.load( 'models/gltf/Xbot.glb', function ( gltf ) {
  81. model = gltf.scene;
  82. scene.add( model );
  83. model.traverse( function ( object ) {
  84. if ( object.isMesh ) object.castShadow = true;
  85. } );
  86. skeleton = new THREE.SkeletonHelper( model );
  87. skeleton.visible = false;
  88. scene.add( skeleton );
  89. const animations = gltf.animations;
  90. mixer = new THREE.AnimationMixer( model );
  91. numAnimations = animations.length;
  92. for ( let i = 0; i !== numAnimations; ++ i ) {
  93. let clip = animations[ i ];
  94. const name = clip.name;
  95. if ( baseActions[ name ] ) {
  96. const action = mixer.clipAction( clip );
  97. activateAction( action );
  98. baseActions[ name ].action = action;
  99. allActions.push( action );
  100. } else if ( additiveActions[ name ] ) {
  101. // Make the clip additive and remove the reference frame
  102. THREE.AnimationUtils.makeClipAdditive( clip );
  103. if ( clip.name.endsWith( '_pose' ) ) {
  104. clip = THREE.AnimationUtils.subclip( clip, clip.name, 2, 3, 30 );
  105. }
  106. const action = mixer.clipAction( clip );
  107. activateAction( action );
  108. additiveActions[ name ].action = action;
  109. allActions.push( action );
  110. }
  111. }
  112. createPanel();
  113. renderer.setAnimationLoop( animate );
  114. } );
  115. renderer = new THREE.WebGLRenderer( { antialias: true } );
  116. renderer.setPixelRatio( window.devicePixelRatio );
  117. renderer.setSize( window.innerWidth, window.innerHeight );
  118. renderer.shadowMap.enabled = true;
  119. container.appendChild( renderer.domElement );
  120. // camera
  121. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 100 );
  122. camera.position.set( - 1, 2, 3 );
  123. const controls = new OrbitControls( camera, renderer.domElement );
  124. controls.enablePan = false;
  125. controls.enableZoom = false;
  126. controls.target.set( 0, 1, 0 );
  127. controls.update();
  128. stats = new Stats();
  129. container.appendChild( stats.dom );
  130. window.addEventListener( 'resize', onWindowResize );
  131. }
  132. function createPanel() {
  133. const panel = new GUI( { width: 310 } );
  134. const folder1 = panel.addFolder( 'Base Actions' );
  135. const folder2 = panel.addFolder( 'Additive Action Weights' );
  136. const folder3 = panel.addFolder( 'General Speed' );
  137. panelSettings = {
  138. 'modify time scale': 1.0
  139. };
  140. const baseNames = [ 'None', ...Object.keys( baseActions ) ];
  141. for ( let i = 0, l = baseNames.length; i !== l; ++ i ) {
  142. const name = baseNames[ i ];
  143. const settings = baseActions[ name ];
  144. panelSettings[ name ] = function () {
  145. const currentSettings = baseActions[ currentBaseAction ];
  146. const currentAction = currentSettings ? currentSettings.action : null;
  147. const action = settings ? settings.action : null;
  148. if ( currentAction !== action ) {
  149. prepareCrossFade( currentAction, action, 0.35 );
  150. }
  151. };
  152. crossFadeControls.push( folder1.add( panelSettings, name ) );
  153. }
  154. for ( const name of Object.keys( additiveActions ) ) {
  155. const settings = additiveActions[ name ];
  156. panelSettings[ name ] = settings.weight;
  157. folder2.add( panelSettings, name, 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) {
  158. setWeight( settings.action, weight );
  159. settings.weight = weight;
  160. } );
  161. }
  162. folder3.add( panelSettings, 'modify time scale', 0.0, 1.5, 0.01 ).onChange( modifyTimeScale );
  163. folder1.open();
  164. folder2.open();
  165. folder3.open();
  166. crossFadeControls.forEach( function ( control ) {
  167. control.setInactive = function () {
  168. control.domElement.classList.add( 'control-inactive' );
  169. };
  170. control.setActive = function () {
  171. control.domElement.classList.remove( 'control-inactive' );
  172. };
  173. const settings = baseActions[ control.property ];
  174. if ( ! settings || ! settings.weight ) {
  175. control.setInactive();
  176. }
  177. } );
  178. }
  179. function activateAction( action ) {
  180. const clip = action.getClip();
  181. const settings = baseActions[ clip.name ] || additiveActions[ clip.name ];
  182. setWeight( action, settings.weight );
  183. action.play();
  184. }
  185. function modifyTimeScale( speed ) {
  186. mixer.timeScale = speed;
  187. }
  188. function prepareCrossFade( startAction, endAction, duration ) {
  189. // If the current action is 'idle', execute the crossfade immediately;
  190. // else wait until the current action has finished its current loop
  191. if ( currentBaseAction === 'idle' || ! startAction || ! endAction ) {
  192. executeCrossFade( startAction, endAction, duration );
  193. } else {
  194. synchronizeCrossFade( startAction, endAction, duration );
  195. }
  196. // Update control colors
  197. if ( endAction ) {
  198. const clip = endAction.getClip();
  199. currentBaseAction = clip.name;
  200. } else {
  201. currentBaseAction = 'None';
  202. }
  203. crossFadeControls.forEach( function ( control ) {
  204. const name = control.property;
  205. if ( name === currentBaseAction ) {
  206. control.setActive();
  207. } else {
  208. control.setInactive();
  209. }
  210. } );
  211. }
  212. function synchronizeCrossFade( startAction, endAction, duration ) {
  213. mixer.addEventListener( 'loop', onLoopFinished );
  214. function onLoopFinished( event ) {
  215. if ( event.action === startAction ) {
  216. mixer.removeEventListener( 'loop', onLoopFinished );
  217. executeCrossFade( startAction, endAction, duration );
  218. }
  219. }
  220. }
  221. function executeCrossFade( startAction, endAction, duration ) {
  222. // Not only the start action, but also the end action must get a weight of 1 before fading
  223. // (concerning the start action this is already guaranteed in this place)
  224. if ( endAction ) {
  225. setWeight( endAction, 1 );
  226. endAction.time = 0;
  227. if ( startAction ) {
  228. // Crossfade with warping
  229. startAction.crossFadeTo( endAction, duration, true );
  230. } else {
  231. // Fade in
  232. endAction.fadeIn( duration );
  233. }
  234. } else {
  235. // Fade out
  236. startAction.fadeOut( duration );
  237. }
  238. }
  239. // This function is needed, since animationAction.crossFadeTo() disables its start action and sets
  240. // the start action's timeScale to ((start animation's duration) / (end animation's duration))
  241. function setWeight( action, weight ) {
  242. action.enabled = true;
  243. action.setEffectiveTimeScale( 1 );
  244. action.setEffectiveWeight( weight );
  245. }
  246. function onWindowResize() {
  247. camera.aspect = window.innerWidth / window.innerHeight;
  248. camera.updateProjectionMatrix();
  249. renderer.setSize( window.innerWidth, window.innerHeight );
  250. }
  251. function animate() {
  252. // Render loop
  253. for ( let i = 0; i !== numAnimations; ++ i ) {
  254. const action = allActions[ i ];
  255. const clip = action.getClip();
  256. const settings = baseActions[ clip.name ] || additiveActions[ clip.name ];
  257. settings.weight = action.getEffectiveWeight();
  258. }
  259. // Get the time elapsed since the last frame, used for mixer update
  260. const mixerUpdateDelta = clock.getDelta();
  261. // Update the animation mixer, the stats panel, and render this frame
  262. mixer.update( mixerUpdateDelta );
  263. renderer.render( scene, camera );
  264. stats.update();
  265. }
  266. </script>
  267. </body>
  268. </html>