webgl_animation_skinning_morph.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - skinning and morphing</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. body {
  10. color: #222;
  11. }
  12. a {
  13. color: #2fa1d6;
  14. }
  15. p {
  16. max-width: 600px;
  17. margin-left: auto;
  18. margin-right: auto;
  19. padding: 0 2em;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="info">
  25. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - skinning and morphing<br />
  26. <p>
  27. 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.
  28. </p>
  29. Model by
  30. <a href="https://www.patreon.com/quaternius" target="_blank" rel="noopener">Tomás Laulhé</a>,
  31. modifications by <a href="https://donmccurdy.com/" target="_blank" rel="noopener">Don McCurdy</a>. CC0.<br />
  32. </div>
  33. <script type="module">
  34. import * as THREE from '../build/three.module.js';
  35. import Stats from './jsm/libs/stats.module.js';
  36. import { GUI } from './jsm/libs/dat.gui.module.js';
  37. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  38. var container, stats, clock, gui, mixer, actions, activeAction, previousAction;
  39. var camera, scene, renderer, model, face;
  40. var api = { state: 'Walking' };
  41. init();
  42. animate();
  43. function init() {
  44. container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 100 );
  47. camera.position.set( - 5, 3, 10 );
  48. camera.lookAt( new THREE.Vector3( 0, 2, 0 ) );
  49. scene = new THREE.Scene();
  50. scene.background = new THREE.Color( 0xe0e0e0 );
  51. scene.fog = new THREE.Fog( 0xe0e0e0, 20, 100 );
  52. clock = new THREE.Clock();
  53. // lights
  54. var light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  55. light.position.set( 0, 20, 0 );
  56. scene.add( light );
  57. light = new THREE.DirectionalLight( 0xffffff );
  58. light.position.set( 0, 20, 10 );
  59. scene.add( light );
  60. // ground
  61. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  62. mesh.rotation.x = - Math.PI / 2;
  63. scene.add( mesh );
  64. var grid = new THREE.GridHelper( 200, 40, 0x000000, 0x000000 );
  65. grid.material.opacity = 0.2;
  66. grid.material.transparent = true;
  67. scene.add( grid );
  68. // model
  69. var loader = new GLTFLoader();
  70. loader.load( 'models/gltf/RobotExpressive/RobotExpressive.glb', function ( gltf ) {
  71. model = gltf.scene;
  72. scene.add( model );
  73. createGUI( model, gltf.animations );
  74. }, undefined, function ( e ) {
  75. console.error( e );
  76. } );
  77. renderer = new THREE.WebGLRenderer( { antialias: true } );
  78. renderer.setPixelRatio( window.devicePixelRatio );
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. renderer.outputEncoding = THREE.sRGBEncoding;
  81. container.appendChild( renderer.domElement );
  82. window.addEventListener( 'resize', onWindowResize, false );
  83. // stats
  84. stats = new Stats();
  85. container.appendChild( stats.dom );
  86. }
  87. function createGUI( model, animations ) {
  88. var states = [ 'Idle', 'Walking', 'Running', 'Dance', 'Death', 'Sitting', 'Standing' ];
  89. var emotes = [ 'Jump', 'Yes', 'No', 'Wave', 'Punch', 'ThumbsUp' ];
  90. gui = new GUI();
  91. mixer = new THREE.AnimationMixer( model );
  92. actions = {};
  93. for ( var i = 0; i < animations.length; i ++ ) {
  94. var clip = animations[ i ];
  95. var action = mixer.clipAction( clip );
  96. actions[ clip.name ] = action;
  97. if ( emotes.indexOf( clip.name ) >= 0 || states.indexOf( clip.name ) >= 4 ) {
  98. action.clampWhenFinished = true;
  99. action.loop = THREE.LoopOnce;
  100. }
  101. }
  102. // states
  103. var statesFolder = gui.addFolder( 'States' );
  104. var clipCtrl = statesFolder.add( api, 'state' ).options( states );
  105. clipCtrl.onChange( function () {
  106. fadeToAction( api.state, 0.5 );
  107. } );
  108. statesFolder.open();
  109. // emotes
  110. var emoteFolder = gui.addFolder( 'Emotes' );
  111. function createEmoteCallback( name ) {
  112. api[ name ] = function () {
  113. fadeToAction( name, 0.2 );
  114. mixer.addEventListener( 'finished', restoreState );
  115. };
  116. emoteFolder.add( api, name );
  117. }
  118. function restoreState() {
  119. mixer.removeEventListener( 'finished', restoreState );
  120. fadeToAction( api.state, 0.2 );
  121. }
  122. for ( var i = 0; i < emotes.length; i ++ ) {
  123. createEmoteCallback( emotes[ i ] );
  124. }
  125. emoteFolder.open();
  126. // expressions
  127. face = model.getObjectByName( 'Head_2' );
  128. var expressions = Object.keys( face.morphTargetDictionary );
  129. var expressionFolder = gui.addFolder( 'Expressions' );
  130. for ( var i = 0; i < expressions.length; i ++ ) {
  131. expressionFolder.add( face.morphTargetInfluences, i, 0, 1, 0.01 ).name( expressions[ i ] );
  132. }
  133. activeAction = actions[ 'Walking' ];
  134. activeAction.play();
  135. expressionFolder.open();
  136. }
  137. function fadeToAction( name, duration ) {
  138. previousAction = activeAction;
  139. activeAction = actions[ name ];
  140. if ( previousAction !== activeAction ) {
  141. previousAction.fadeOut( duration );
  142. }
  143. activeAction
  144. .reset()
  145. .setEffectiveTimeScale( 1 )
  146. .setEffectiveWeight( 1 )
  147. .fadeIn( duration )
  148. .play();
  149. }
  150. function onWindowResize() {
  151. camera.aspect = window.innerWidth / window.innerHeight;
  152. camera.updateProjectionMatrix();
  153. renderer.setSize( window.innerWidth, window.innerHeight );
  154. }
  155. //
  156. function animate() {
  157. var dt = clock.getDelta();
  158. if ( mixer ) mixer.update( dt );
  159. requestAnimationFrame( animate );
  160. renderer.render( scene, camera );
  161. stats.update();
  162. }
  163. </script>
  164. </body>
  165. </html>