webgl_animation_skinning_morph.html 6.1 KB

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