2
0

webgl_animation_skinning_additive_blending.html 9.9 KB

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