webgl_animation_skinning_additive_blending.html 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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, 0x8d8d8d, 3 );
  65. hemiLight.position.set( 0, 20, 0 );
  66. scene.add( hemiLight );
  67. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  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: 0xcbcbcb, 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.shadowMap.enabled = true;
  122. container.appendChild( renderer.domElement );
  123. // camera
  124. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 100 );
  125. camera.position.set( - 1, 2, 3 );
  126. const controls = new OrbitControls( camera, renderer.domElement );
  127. controls.enablePan = false;
  128. controls.enableZoom = false;
  129. controls.target.set( 0, 1, 0 );
  130. controls.update();
  131. stats = new Stats();
  132. container.appendChild( stats.dom );
  133. window.addEventListener( 'resize', onWindowResize );
  134. }
  135. function createPanel() {
  136. const panel = new GUI( { width: 310 } );
  137. const folder1 = panel.addFolder( 'Base Actions' );
  138. const folder2 = panel.addFolder( 'Additive Action Weights' );
  139. const folder3 = panel.addFolder( 'General Speed' );
  140. panelSettings = {
  141. 'modify time scale': 1.0
  142. };
  143. const baseNames = [ 'None', ...Object.keys( baseActions ) ];
  144. for ( let i = 0, l = baseNames.length; i !== l; ++ i ) {
  145. const name = baseNames[ i ];
  146. const settings = baseActions[ name ];
  147. panelSettings[ name ] = function () {
  148. const currentSettings = baseActions[ currentBaseAction ];
  149. const currentAction = currentSettings ? currentSettings.action : null;
  150. const action = settings ? settings.action : null;
  151. if ( currentAction !== action ) {
  152. prepareCrossFade( currentAction, action, 0.35 );
  153. }
  154. };
  155. crossFadeControls.push( folder1.add( panelSettings, name ) );
  156. }
  157. for ( const name of Object.keys( additiveActions ) ) {
  158. const settings = additiveActions[ name ];
  159. panelSettings[ name ] = settings.weight;
  160. folder2.add( panelSettings, name, 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) {
  161. setWeight( settings.action, weight );
  162. settings.weight = weight;
  163. } );
  164. }
  165. folder3.add( panelSettings, 'modify time scale', 0.0, 1.5, 0.01 ).onChange( modifyTimeScale );
  166. folder1.open();
  167. folder2.open();
  168. folder3.open();
  169. crossFadeControls.forEach( function ( control ) {
  170. control.setInactive = function () {
  171. control.domElement.classList.add( 'control-inactive' );
  172. };
  173. control.setActive = function () {
  174. control.domElement.classList.remove( 'control-inactive' );
  175. };
  176. const settings = baseActions[ control.property ];
  177. if ( ! settings || ! settings.weight ) {
  178. control.setInactive();
  179. }
  180. } );
  181. }
  182. function activateAction( action ) {
  183. const clip = action.getClip();
  184. const settings = baseActions[ clip.name ] || additiveActions[ clip.name ];
  185. setWeight( action, settings.weight );
  186. action.play();
  187. }
  188. function modifyTimeScale( speed ) {
  189. mixer.timeScale = speed;
  190. }
  191. function prepareCrossFade( startAction, endAction, duration ) {
  192. // If the current action is 'idle', execute the crossfade immediately;
  193. // else wait until the current action has finished its current loop
  194. if ( currentBaseAction === 'idle' || ! startAction || ! endAction ) {
  195. executeCrossFade( startAction, endAction, duration );
  196. } else {
  197. synchronizeCrossFade( startAction, endAction, duration );
  198. }
  199. // Update control colors
  200. if ( endAction ) {
  201. const clip = endAction.getClip();
  202. currentBaseAction = clip.name;
  203. } else {
  204. currentBaseAction = 'None';
  205. }
  206. crossFadeControls.forEach( function ( control ) {
  207. const name = control.property;
  208. if ( name === currentBaseAction ) {
  209. control.setActive();
  210. } else {
  211. control.setInactive();
  212. }
  213. } );
  214. }
  215. function synchronizeCrossFade( startAction, endAction, duration ) {
  216. mixer.addEventListener( 'loop', onLoopFinished );
  217. function onLoopFinished( event ) {
  218. if ( event.action === startAction ) {
  219. mixer.removeEventListener( 'loop', onLoopFinished );
  220. executeCrossFade( startAction, endAction, duration );
  221. }
  222. }
  223. }
  224. function executeCrossFade( startAction, endAction, duration ) {
  225. // Not only the start action, but also the end action must get a weight of 1 before fading
  226. // (concerning the start action this is already guaranteed in this place)
  227. if ( endAction ) {
  228. setWeight( endAction, 1 );
  229. endAction.time = 0;
  230. if ( startAction ) {
  231. // Crossfade with warping
  232. startAction.crossFadeTo( endAction, duration, true );
  233. } else {
  234. // Fade in
  235. endAction.fadeIn( duration );
  236. }
  237. } else {
  238. // Fade out
  239. startAction.fadeOut( duration );
  240. }
  241. }
  242. // This function is needed, since animationAction.crossFadeTo() disables its start action and sets
  243. // the start action's timeScale to ((start animation's duration) / (end animation's duration))
  244. function setWeight( action, weight ) {
  245. action.enabled = true;
  246. action.setEffectiveTimeScale( 1 );
  247. action.setEffectiveWeight( weight );
  248. }
  249. function onWindowResize() {
  250. camera.aspect = window.innerWidth / window.innerHeight;
  251. camera.updateProjectionMatrix();
  252. renderer.setSize( window.innerWidth, window.innerHeight );
  253. }
  254. function animate() {
  255. // Render loop
  256. requestAnimationFrame( animate );
  257. for ( let i = 0; i !== numAnimations; ++ i ) {
  258. const action = allActions[ i ];
  259. const clip = action.getClip();
  260. const settings = baseActions[ clip.name ] || additiveActions[ clip.name ];
  261. settings.weight = action.getEffectiveWeight();
  262. }
  263. // Get the time elapsed since the last frame, used for mixer update
  264. const mixerUpdateDelta = clock.getDelta();
  265. // Update the animation mixer, the stats panel, and render this frame
  266. mixer.update( mixerUpdateDelta );
  267. stats.update();
  268. renderer.render( scene, camera );
  269. }
  270. </script>
  271. </body>
  272. </html>