webgl_animation_skinning_morph.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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="http://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 {
  35. AnimationMixer,
  36. Clock,
  37. Color,
  38. DirectionalLight,
  39. Fog,
  40. GridHelper,
  41. HemisphereLight,
  42. LoopOnce,
  43. Mesh,
  44. MeshPhongMaterial,
  45. PerspectiveCamera,
  46. PlaneBufferGeometry,
  47. Scene,
  48. Vector3,
  49. WebGLRenderer
  50. } from "../build/three.module.js";
  51. import Stats from './jsm/libs/stats.module.js';
  52. import { GUI } from './jsm/libs/dat.gui.module.js';
  53. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  54. var container, stats, clock, gui, mixer, actions, activeAction, previousAction;
  55. var camera, scene, renderer, model, face;
  56. var api = { state: 'Walking' };
  57. init();
  58. animate();
  59. function init() {
  60. container = document.createElement( 'div' );
  61. document.body.appendChild( container );
  62. camera = new PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 100 );
  63. camera.position.set( - 5, 3, 10 );
  64. camera.lookAt( new Vector3( 0, 2, 0 ) );
  65. scene = new Scene();
  66. scene.background = new Color( 0xe0e0e0 );
  67. scene.fog = new Fog( 0xe0e0e0, 20, 100 );
  68. clock = new Clock();
  69. // lights
  70. var light = new HemisphereLight( 0xffffff, 0x444444 );
  71. light.position.set( 0, 20, 0 );
  72. scene.add( light );
  73. light = new DirectionalLight( 0xffffff );
  74. light.position.set( 0, 20, 10 );
  75. scene.add( light );
  76. // ground
  77. var mesh = new Mesh( new PlaneBufferGeometry( 2000, 2000 ), new MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  78. mesh.rotation.x = - Math.PI / 2;
  79. scene.add( mesh );
  80. var grid = new GridHelper( 200, 40, 0x000000, 0x000000 );
  81. grid.material.opacity = 0.2;
  82. grid.material.transparent = true;
  83. scene.add( grid );
  84. // model
  85. var loader = new GLTFLoader();
  86. loader.load( 'models/gltf/RobotExpressive/RobotExpressive.glb', function ( gltf ) {
  87. model = gltf.scene;
  88. scene.add( model );
  89. createGUI( model, gltf.animations );
  90. }, undefined, function ( e ) {
  91. console.error( e );
  92. } );
  93. renderer = new WebGLRenderer( { antialias: true } );
  94. renderer.setPixelRatio( window.devicePixelRatio );
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. renderer.gammaOutput = true;
  97. renderer.gammaFactor = 2.2;
  98. container.appendChild( renderer.domElement );
  99. window.addEventListener( 'resize', onWindowResize, false );
  100. // stats
  101. stats = new Stats();
  102. container.appendChild( stats.dom );
  103. }
  104. function createGUI( model, animations ) {
  105. var states = [ 'Idle', 'Walking', 'Running', 'Dance', 'Death', 'Sitting', 'Standing' ];
  106. var emotes = [ 'Jump', 'Yes', 'No', 'Wave', 'Punch', 'ThumbsUp' ];
  107. gui = new GUI();
  108. mixer = new AnimationMixer( model );
  109. actions = {};
  110. for ( var i = 0; i < animations.length; i ++ ) {
  111. var clip = animations[ i ];
  112. var action = mixer.clipAction( clip );
  113. actions[ clip.name ] = action;
  114. if ( emotes.indexOf( clip.name ) >= 0 || states.indexOf( clip.name ) >= 4 ) {
  115. action.clampWhenFinished = true;
  116. action.loop = LoopOnce;
  117. }
  118. }
  119. // states
  120. var statesFolder = gui.addFolder( 'States' );
  121. var clipCtrl = statesFolder.add( api, 'state' ).options( states );
  122. clipCtrl.onChange( function () {
  123. fadeToAction( api.state, 0.5 );
  124. } );
  125. statesFolder.open();
  126. // emotes
  127. var emoteFolder = gui.addFolder( 'Emotes' );
  128. function createEmoteCallback( name ) {
  129. api[ name ] = function () {
  130. fadeToAction( name, 0.2 );
  131. mixer.addEventListener( 'finished', restoreState );
  132. };
  133. emoteFolder.add( api, name );
  134. }
  135. function restoreState() {
  136. mixer.removeEventListener( 'finished', restoreState );
  137. fadeToAction( api.state, 0.2 );
  138. }
  139. for ( var i = 0; i < emotes.length; i ++ ) {
  140. createEmoteCallback( emotes[ i ] );
  141. }
  142. emoteFolder.open();
  143. // expressions
  144. face = model.getObjectByName( 'Head_2' );
  145. var expressions = Object.keys( face.morphTargetDictionary );
  146. var expressionFolder = gui.addFolder( 'Expressions' );
  147. for ( var i = 0; i < expressions.length; i ++ ) {
  148. expressionFolder.add( face.morphTargetInfluences, i, 0, 1, 0.01 ).name( expressions[ i ] );
  149. }
  150. activeAction = actions[ 'Walking' ];
  151. activeAction.play();
  152. expressionFolder.open();
  153. }
  154. function fadeToAction( name, duration ) {
  155. previousAction = activeAction;
  156. activeAction = actions[ name ];
  157. if ( previousAction !== activeAction ) {
  158. previousAction.fadeOut( duration );
  159. }
  160. activeAction
  161. .reset()
  162. .setEffectiveTimeScale( 1 )
  163. .setEffectiveWeight( 1 )
  164. .fadeIn( duration )
  165. .play();
  166. }
  167. function onWindowResize() {
  168. camera.aspect = window.innerWidth / window.innerHeight;
  169. camera.updateProjectionMatrix();
  170. renderer.setSize( window.innerWidth, window.innerHeight );
  171. }
  172. //
  173. function animate() {
  174. var dt = clock.getDelta();
  175. if ( mixer ) mixer.update( dt );
  176. requestAnimationFrame( animate );
  177. renderer.render( scene, camera );
  178. stats.update();
  179. }
  180. </script>
  181. </body>
  182. </html>