123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - additive animation - skinning</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- a {
- color: blue;
- }
- .control-inactive button {
- color: #888;
- }
- </style>
- </head>
- <body>
- <div id="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Skeletal Additive Animation Blending
- (model from <a href="https://www.mixamo.com/" target="_blank" rel="noopener">mixamo.com</a>)<br/>
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import Stats from 'three/addons/libs/stats.module.js';
- import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
- let scene, renderer, camera, stats;
- let model, skeleton, mixer, clock;
- const crossFadeControls = [];
- let currentBaseAction = 'idle';
- const allActions = [];
- const baseActions = {
- idle: { weight: 1 },
- walk: { weight: 0 },
- run: { weight: 0 }
- };
- const additiveActions = {
- sneak_pose: { weight: 0 },
- sad_pose: { weight: 0 },
- agree: { weight: 0 },
- headShake: { weight: 0 }
- };
- let panelSettings, numAnimations;
- init();
- function init() {
- const container = document.getElementById( 'container' );
- clock = new THREE.Clock();
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xa0a0a0 );
- scene.fog = new THREE.Fog( 0xa0a0a0, 10, 50 );
- const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 3 );
- hemiLight.position.set( 0, 20, 0 );
- scene.add( hemiLight );
- const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
- dirLight.position.set( 3, 10, 10 );
- dirLight.castShadow = true;
- dirLight.shadow.camera.top = 2;
- dirLight.shadow.camera.bottom = - 2;
- dirLight.shadow.camera.left = - 2;
- dirLight.shadow.camera.right = 2;
- dirLight.shadow.camera.near = 0.1;
- dirLight.shadow.camera.far = 40;
- scene.add( dirLight );
- // ground
- const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 100, 100 ), new THREE.MeshPhongMaterial( { color: 0xcbcbcb, depthWrite: false } ) );
- mesh.rotation.x = - Math.PI / 2;
- mesh.receiveShadow = true;
- scene.add( mesh );
- const loader = new GLTFLoader();
- loader.load( 'models/gltf/Xbot.glb', function ( gltf ) {
- model = gltf.scene;
- scene.add( model );
- model.traverse( function ( object ) {
- if ( object.isMesh ) object.castShadow = true;
- } );
- skeleton = new THREE.SkeletonHelper( model );
- skeleton.visible = false;
- scene.add( skeleton );
- const animations = gltf.animations;
- mixer = new THREE.AnimationMixer( model );
- numAnimations = animations.length;
- for ( let i = 0; i !== numAnimations; ++ i ) {
- let clip = animations[ i ];
- const name = clip.name;
- if ( baseActions[ name ] ) {
- const action = mixer.clipAction( clip );
- activateAction( action );
- baseActions[ name ].action = action;
- allActions.push( action );
- } else if ( additiveActions[ name ] ) {
- // Make the clip additive and remove the reference frame
- THREE.AnimationUtils.makeClipAdditive( clip );
- if ( clip.name.endsWith( '_pose' ) ) {
- clip = THREE.AnimationUtils.subclip( clip, clip.name, 2, 3, 30 );
- }
- const action = mixer.clipAction( clip );
- activateAction( action );
- additiveActions[ name ].action = action;
- allActions.push( action );
- }
- }
- createPanel();
- renderer.setAnimationLoop( animate );
- } );
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.shadowMap.enabled = true;
- container.appendChild( renderer.domElement );
- // camera
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 100 );
- camera.position.set( - 1, 2, 3 );
- const controls = new OrbitControls( camera, renderer.domElement );
- controls.enablePan = false;
- controls.enableZoom = false;
- controls.target.set( 0, 1, 0 );
- controls.update();
- stats = new Stats();
- container.appendChild( stats.dom );
- window.addEventListener( 'resize', onWindowResize );
- }
- function createPanel() {
- const panel = new GUI( { width: 310 } );
- const folder1 = panel.addFolder( 'Base Actions' );
- const folder2 = panel.addFolder( 'Additive Action Weights' );
- const folder3 = panel.addFolder( 'General Speed' );
- panelSettings = {
- 'modify time scale': 1.0
- };
- const baseNames = [ 'None', ...Object.keys( baseActions ) ];
- for ( let i = 0, l = baseNames.length; i !== l; ++ i ) {
- const name = baseNames[ i ];
- const settings = baseActions[ name ];
- panelSettings[ name ] = function () {
- const currentSettings = baseActions[ currentBaseAction ];
- const currentAction = currentSettings ? currentSettings.action : null;
- const action = settings ? settings.action : null;
- if ( currentAction !== action ) {
- prepareCrossFade( currentAction, action, 0.35 );
- }
- };
- crossFadeControls.push( folder1.add( panelSettings, name ) );
- }
- for ( const name of Object.keys( additiveActions ) ) {
- const settings = additiveActions[ name ];
- panelSettings[ name ] = settings.weight;
- folder2.add( panelSettings, name, 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) {
- setWeight( settings.action, weight );
- settings.weight = weight;
- } );
- }
- folder3.add( panelSettings, 'modify time scale', 0.0, 1.5, 0.01 ).onChange( modifyTimeScale );
- folder1.open();
- folder2.open();
- folder3.open();
- crossFadeControls.forEach( function ( control ) {
- control.setInactive = function () {
- control.domElement.classList.add( 'control-inactive' );
- };
- control.setActive = function () {
- control.domElement.classList.remove( 'control-inactive' );
- };
- const settings = baseActions[ control.property ];
- if ( ! settings || ! settings.weight ) {
- control.setInactive();
- }
- } );
- }
- function activateAction( action ) {
- const clip = action.getClip();
- const settings = baseActions[ clip.name ] || additiveActions[ clip.name ];
- setWeight( action, settings.weight );
- action.play();
- }
- function modifyTimeScale( speed ) {
- mixer.timeScale = speed;
- }
- function prepareCrossFade( startAction, endAction, duration ) {
- // If the current action is 'idle', execute the crossfade immediately;
- // else wait until the current action has finished its current loop
- if ( currentBaseAction === 'idle' || ! startAction || ! endAction ) {
- executeCrossFade( startAction, endAction, duration );
- } else {
- synchronizeCrossFade( startAction, endAction, duration );
- }
- // Update control colors
- if ( endAction ) {
- const clip = endAction.getClip();
- currentBaseAction = clip.name;
- } else {
- currentBaseAction = 'None';
- }
- crossFadeControls.forEach( function ( control ) {
- const name = control.property;
- if ( name === currentBaseAction ) {
- control.setActive();
- } else {
- control.setInactive();
- }
- } );
- }
- function synchronizeCrossFade( startAction, endAction, duration ) {
- mixer.addEventListener( 'loop', onLoopFinished );
- function onLoopFinished( event ) {
- if ( event.action === startAction ) {
- mixer.removeEventListener( 'loop', onLoopFinished );
- executeCrossFade( startAction, endAction, duration );
- }
- }
- }
- function executeCrossFade( startAction, endAction, duration ) {
- // Not only the start action, but also the end action must get a weight of 1 before fading
- // (concerning the start action this is already guaranteed in this place)
- if ( endAction ) {
- setWeight( endAction, 1 );
- endAction.time = 0;
- if ( startAction ) {
- // Crossfade with warping
- startAction.crossFadeTo( endAction, duration, true );
- } else {
- // Fade in
- endAction.fadeIn( duration );
- }
- } else {
- // Fade out
- startAction.fadeOut( duration );
- }
- }
- // This function is needed, since animationAction.crossFadeTo() disables its start action and sets
- // the start action's timeScale to ((start animation's duration) / (end animation's duration))
- function setWeight( action, weight ) {
- action.enabled = true;
- action.setEffectiveTimeScale( 1 );
- action.setEffectiveWeight( weight );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- // Render loop
- for ( let i = 0; i !== numAnimations; ++ i ) {
- const action = allActions[ i ];
- const clip = action.getClip();
- const settings = baseActions[ clip.name ] || additiveActions[ clip.name ];
- settings.weight = action.getEffectiveWeight();
- }
- // Get the time elapsed since the last frame, used for mixer update
- const mixerUpdateDelta = clock.getDelta();
- // Update the animation mixer, the stats panel, and render this frame
- mixer.update( mixerUpdateDelta );
- renderer.render( scene, camera );
- stats.update();
- }
- </script>
- </body>
- </html>
|