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