123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - 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: #f00;
- }
- .ac { /* prevent dat-gui from being selected */
- -webkit-user-select: none;
- -moz-user-select: none;
- -ms-user-select: none;
- user-select: none;
- }
- .no-pointer-events {
- pointer-events: none;
- }
- .control-disabled {
- color: #888;
- text-decoration: line-through;
- }
- </style>
- </head>
- <body>
- <div id="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Skeletal Animation Blending
- (model from <a href="https://www.mixamo.com/" target="_blank" rel="noopener">mixamo.com</a>)<br/>
- Note: crossfades are possible with blend weights being set to (1,0,0), (0,1,0) or (0,0,1)
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
- var scene, renderer, camera, stats;
- var model, skeleton, mixer, clock;
- var crossFadeControls = [];
- var idleAction, walkAction, runAction;
- var idleWeight, walkWeight, runWeight;
- var actions, settings;
- var singleStepMode = false;
- var sizeOfNextStep = 0;
- init();
- function init() {
- var container = document.getElementById( 'container' );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.set( 1, 2, - 3 );
- camera.lookAt( 0, 1, 0 );
- clock = new THREE.Clock();
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xa0a0a0 );
- scene.fog = new THREE.Fog( 0xa0a0a0, 10, 50 );
- var hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
- hemiLight.position.set( 0, 20, 0 );
- scene.add( hemiLight );
- var dirLight = new THREE.DirectionalLight( 0xffffff );
- 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 );
- // scene.add( new CameraHelper( light.shadow.camera ) );
- // ground
- var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 100, 100 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
- mesh.rotation.x = - Math.PI / 2;
- mesh.receiveShadow = true;
- scene.add( mesh );
- var loader = new GLTFLoader();
- loader.load( 'models/gltf/Soldier.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 );
- //
- createPanel();
- //
- var animations = gltf.animations;
- mixer = new THREE.AnimationMixer( model );
- idleAction = mixer.clipAction( animations[ 0 ] );
- walkAction = mixer.clipAction( animations[ 3 ] );
- runAction = mixer.clipAction( animations[ 1 ] );
- actions = [ idleAction, walkAction, runAction ];
- activateAllActions();
- animate();
- } );
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.outputEncoding = THREE.sRGBEncoding;
- renderer.shadowMap.enabled = true;
- container.appendChild( renderer.domElement );
- stats = new Stats();
- container.appendChild( stats.dom );
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function createPanel() {
- var panel = new GUI( { width: 310 } );
- var folder1 = panel.addFolder( 'Visibility' );
- var folder2 = panel.addFolder( 'Activation/Deactivation' );
- var folder3 = panel.addFolder( 'Pausing/Stepping' );
- var folder4 = panel.addFolder( 'Crossfading' );
- var folder5 = panel.addFolder( 'Blend Weights' );
- var folder6 = panel.addFolder( 'General Speed' );
- settings = {
- 'show model': true,
- 'show skeleton': false,
- 'deactivate all': deactivateAllActions,
- 'activate all': activateAllActions,
- 'pause/continue': pauseContinue,
- 'make single step': toSingleStepMode,
- 'modify step size': 0.05,
- 'from walk to idle': function () {
- prepareCrossFade( walkAction, idleAction, 1.0 );
- },
- 'from idle to walk': function () {
- prepareCrossFade( idleAction, walkAction, 0.5 );
- },
- 'from walk to run': function () {
- prepareCrossFade( walkAction, runAction, 2.5 );
- },
- 'from run to walk': function () {
- prepareCrossFade( runAction, walkAction, 5.0 );
- },
- 'use default duration': true,
- 'set custom duration': 3.5,
- 'modify idle weight': 0.0,
- 'modify walk weight': 1.0,
- 'modify run weight': 0.0,
- 'modify time scale': 1.0
- };
- folder1.add( settings, 'show model' ).onChange( showModel );
- folder1.add( settings, 'show skeleton' ).onChange( showSkeleton );
- folder2.add( settings, 'deactivate all' );
- folder2.add( settings, 'activate all' );
- folder3.add( settings, 'pause/continue' );
- folder3.add( settings, 'make single step' );
- folder3.add( settings, 'modify step size', 0.01, 0.1, 0.001 );
- crossFadeControls.push( folder4.add( settings, 'from walk to idle' ) );
- crossFadeControls.push( folder4.add( settings, 'from idle to walk' ) );
- crossFadeControls.push( folder4.add( settings, 'from walk to run' ) );
- crossFadeControls.push( folder4.add( settings, 'from run to walk' ) );
- folder4.add( settings, 'use default duration' );
- folder4.add( settings, 'set custom duration', 0, 10, 0.01 );
- folder5.add( settings, 'modify idle weight', 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) {
- setWeight( idleAction, weight );
- } );
- folder5.add( settings, 'modify walk weight', 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) {
- setWeight( walkAction, weight );
- } );
- folder5.add( settings, 'modify run weight', 0.0, 1.0, 0.01 ).listen().onChange( function ( weight ) {
- setWeight( runAction, weight );
- } );
- folder6.add( settings, 'modify time scale', 0.0, 1.5, 0.01 ).onChange( modifyTimeScale );
- folder1.open();
- folder2.open();
- folder3.open();
- folder4.open();
- folder5.open();
- folder6.open();
- crossFadeControls.forEach( function ( control ) {
- control.classList1 = control.domElement.parentElement.parentElement.classList;
- control.classList2 = control.domElement.previousElementSibling.classList;
- control.setDisabled = function () {
- control.classList1.add( 'no-pointer-events' );
- control.classList2.add( 'control-disabled' );
- };
- control.setEnabled = function () {
- control.classList1.remove( 'no-pointer-events' );
- control.classList2.remove( 'control-disabled' );
- };
- } );
- }
- function showModel( visibility ) {
- model.visible = visibility;
- }
- function showSkeleton( visibility ) {
- skeleton.visible = visibility;
- }
- function modifyTimeScale( speed ) {
- mixer.timeScale = speed;
- }
- function deactivateAllActions() {
- actions.forEach( function ( action ) {
- action.stop();
- } );
- }
- function activateAllActions() {
- setWeight( idleAction, settings[ 'modify idle weight' ] );
- setWeight( walkAction, settings[ 'modify walk weight' ] );
- setWeight( runAction, settings[ 'modify run weight' ] );
- actions.forEach( function ( action ) {
- action.play();
- } );
- }
- function pauseContinue() {
- if ( singleStepMode ) {
- singleStepMode = false;
- unPauseAllActions();
- } else {
- if ( idleAction.paused ) {
- unPauseAllActions();
- } else {
- pauseAllActions();
- }
- }
- }
- function pauseAllActions() {
- actions.forEach( function ( action ) {
- action.paused = true;
- } );
- }
- function unPauseAllActions() {
- actions.forEach( function ( action ) {
- action.paused = false;
- } );
- }
- function toSingleStepMode() {
- unPauseAllActions();
- singleStepMode = true;
- sizeOfNextStep = settings[ 'modify step size' ];
- }
- function prepareCrossFade( startAction, endAction, defaultDuration ) {
- // Switch default / custom crossfade duration (according to the user's choice)
- var duration = setCrossFadeDuration( defaultDuration );
- // Make sure that we don't go on in singleStepMode, and that all actions are unpaused
- singleStepMode = false;
- unPauseAllActions();
- // If the current action is 'idle' (duration 4 sec), execute the crossfade immediately;
- // else wait until the current action has finished its current loop
- if ( startAction === idleAction ) {
- executeCrossFade( startAction, endAction, duration );
- } else {
- synchronizeCrossFade( startAction, endAction, duration );
- }
- }
- function setCrossFadeDuration( defaultDuration ) {
- // Switch default crossfade duration <-> custom crossfade duration
- if ( settings[ 'use default duration' ] ) {
- return defaultDuration;
- } else {
- return settings[ 'set custom duration' ];
- }
- }
- 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)
- setWeight( endAction, 1 );
- endAction.time = 0;
- // Crossfade with warping - you can also try without warping by setting the third parameter to false
- startAction.crossFadeTo( endAction, duration, true );
- }
- // 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 );
- }
- // Called by the render loop
- function updateWeightSliders() {
- settings[ 'modify idle weight' ] = idleWeight;
- settings[ 'modify walk weight' ] = walkWeight;
- settings[ 'modify run weight' ] = runWeight;
- }
- // Called by the render loop
- function updateCrossFadeControls() {
- crossFadeControls.forEach( function ( control ) {
- control.setDisabled();
- } );
- if ( idleWeight === 1 && walkWeight === 0 && runWeight === 0 ) {
- crossFadeControls[ 1 ].setEnabled();
- }
- if ( idleWeight === 0 && walkWeight === 1 && runWeight === 0 ) {
- crossFadeControls[ 0 ].setEnabled();
- crossFadeControls[ 2 ].setEnabled();
- }
- if ( idleWeight === 0 && walkWeight === 0 && runWeight === 1 ) {
- crossFadeControls[ 3 ].setEnabled();
- }
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- // Render loop
- requestAnimationFrame( animate );
- idleWeight = idleAction.getEffectiveWeight();
- walkWeight = walkAction.getEffectiveWeight();
- runWeight = runAction.getEffectiveWeight();
- // Update the panel values if weights are modified from "outside" (by crossfadings)
- updateWeightSliders();
- // Enable/disable crossfade controls according to current weight values
- updateCrossFadeControls();
- // Get the time elapsed since the last frame, used for mixer update (if not in single step mode)
- var mixerUpdateDelta = clock.getDelta();
- // If in single step mode, make one step and then do nothing (until the user clicks again)
- if ( singleStepMode ) {
- mixerUpdateDelta = sizeOfNextStep;
- sizeOfNextStep = 0;
- }
- // Update the animation mixer, the stats panel, and render this frame
- mixer.update( mixerUpdateDelta );
- stats.update();
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|