webgl_animation_skinning_additive_blending.html 9.9 KB

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