123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - skinning and morphing</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>
- body {
- color: #222;
- }
- a {
- color: #2fa1d6;
- }
- p {
- max-width: 600px;
- margin-left: auto;
- margin-right: auto;
- padding: 0 2em;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - skinning and morphing<br />
- <p>
- The animation system allows clips to be played individually, looped, or crossfaded with other clips. This example shows a character looping in one of several base animation states, then transitioning smoothly to one-time actions. Facial expressions are controlled independently with morph targets.
- </p>
- Model by
- <a href="https://www.patreon.com/quaternius" target="_blank" rel="noopener">Tomás Laulhé</a>,
- modifications by <a href="https://donmccurdy.com/" target="_blank" rel="noopener">Don McCurdy</a>. CC0.<br />
- </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 container, stats, clock, gui, mixer, actions, activeAction, previousAction;
- var camera, scene, renderer, model, face;
- var api = { state: 'Walking' };
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 100 );
- camera.position.set( - 5, 3, 10 );
- camera.lookAt( new THREE.Vector3( 0, 2, 0 ) );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xe0e0e0 );
- scene.fog = new THREE.Fog( 0xe0e0e0, 20, 100 );
- clock = new THREE.Clock();
- // lights
- var light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
- light.position.set( 0, 20, 0 );
- scene.add( light );
- light = new THREE.DirectionalLight( 0xffffff );
- light.position.set( 0, 20, 10 );
- scene.add( light );
- // ground
- var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
- mesh.rotation.x = - Math.PI / 2;
- scene.add( mesh );
- var grid = new THREE.GridHelper( 200, 40, 0x000000, 0x000000 );
- grid.material.opacity = 0.2;
- grid.material.transparent = true;
- scene.add( grid );
- // model
- var loader = new GLTFLoader();
- loader.load( 'models/gltf/RobotExpressive/RobotExpressive.glb', function ( gltf ) {
- model = gltf.scene;
- scene.add( model );
- createGUI( model, gltf.animations );
- }, undefined, function ( e ) {
- console.error( e );
- } );
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.outputEncoding = THREE.sRGBEncoding;
- container.appendChild( renderer.domElement );
- window.addEventListener( 'resize', onWindowResize, false );
- // stats
- stats = new Stats();
- container.appendChild( stats.dom );
- }
- function createGUI( model, animations ) {
- var states = [ 'Idle', 'Walking', 'Running', 'Dance', 'Death', 'Sitting', 'Standing' ];
- var emotes = [ 'Jump', 'Yes', 'No', 'Wave', 'Punch', 'ThumbsUp' ];
- gui = new GUI();
- mixer = new THREE.AnimationMixer( model );
- actions = {};
- for ( var i = 0; i < animations.length; i ++ ) {
- var clip = animations[ i ];
- var action = mixer.clipAction( clip );
- actions[ clip.name ] = action;
- if ( emotes.indexOf( clip.name ) >= 0 || states.indexOf( clip.name ) >= 4 ) {
- action.clampWhenFinished = true;
- action.loop = THREE.LoopOnce;
- }
- }
- // states
- var statesFolder = gui.addFolder( 'States' );
- var clipCtrl = statesFolder.add( api, 'state' ).options( states );
- clipCtrl.onChange( function () {
- fadeToAction( api.state, 0.5 );
- } );
- statesFolder.open();
- // emotes
- var emoteFolder = gui.addFolder( 'Emotes' );
- function createEmoteCallback( name ) {
- api[ name ] = function () {
- fadeToAction( name, 0.2 );
- mixer.addEventListener( 'finished', restoreState );
- };
- emoteFolder.add( api, name );
- }
- function restoreState() {
- mixer.removeEventListener( 'finished', restoreState );
- fadeToAction( api.state, 0.2 );
- }
- for ( var i = 0; i < emotes.length; i ++ ) {
- createEmoteCallback( emotes[ i ] );
- }
- emoteFolder.open();
- // expressions
- face = model.getObjectByName( 'Head_2' );
- var expressions = Object.keys( face.morphTargetDictionary );
- var expressionFolder = gui.addFolder( 'Expressions' );
- for ( var i = 0; i < expressions.length; i ++ ) {
- expressionFolder.add( face.morphTargetInfluences, i, 0, 1, 0.01 ).name( expressions[ i ] );
- }
- activeAction = actions[ 'Walking' ];
- activeAction.play();
- expressionFolder.open();
- }
- function fadeToAction( name, duration ) {
- previousAction = activeAction;
- activeAction = actions[ name ];
- if ( previousAction !== activeAction ) {
- previousAction.fadeOut( duration );
- }
- activeAction
- .reset()
- .setEffectiveTimeScale( 1 )
- .setEffectiveWeight( 1 )
- .fadeIn( duration )
- .play();
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- var dt = clock.getDelta();
- if ( mixer ) mixer.update( dt );
- requestAnimationFrame( animate );
- renderer.render( scene, camera );
- stats.update();
- }
- </script>
- </body>
- </html>
|