webgl_animation_skinning_additive_blending.html 10 KB

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