webgl_animation_skinning_morph.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. position: absolute;
  16. top: 10px;
  17. width: 100%;
  18. text-align: center;
  19. z-index: 100;
  20. display:block;
  21. }
  22. #info p {
  23. max-width: 600px;
  24. margin-left: auto;
  25. margin-right: auto;
  26. padding: 0 2em;
  27. }
  28. #info a {
  29. color: #2fa1d6;
  30. font-weight: bold;
  31. }
  32. .dg.ac {
  33. z-index: 999 !important;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div id="info">
  39. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - skinning and morphing<br />
  40. <p>
  41. The animation system allows clips to be played individually, looped, or crossfaded with
  42. other clips. This example shows a character looping in one of several base animation states,
  43. then transitioning smoothly to one-time actions. Facial expressions are controlled
  44. independently with morph targets.
  45. </p>
  46. Model by
  47. <a href="https://www.patreon.com/quaternius" target="_blank" rel="noopener">Tomás Laulhé</a>,
  48. modifications by <a href="https://donmccurdy.com/" target="_blank" rel="noopener">Don McCurdy</a>. CC0.<br />
  49. </div>
  50. <script src="../build/three.js"></script>
  51. <script src="js/loaders/GLTFLoader.js"></script>
  52. <script src="js/WebGL.js"></script>
  53. <script src="js/libs/stats.min.js"></script>
  54. <script src="js/libs/dat.gui.min.js"></script>
  55. <script>
  56. if ( WEBGL.isWebGLAvailable() === false ) {
  57. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  58. }
  59. var container, stats, clock, gui, mixer, actions, activeAction, previousAction;
  60. var camera, scene, renderer, model, face;
  61. var api = { state: 'Walking' };
  62. init();
  63. animate();
  64. function init() {
  65. container = document.createElement( 'div' );
  66. document.body.appendChild( container );
  67. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 100 );
  68. camera.position.set( - 5, 3, 10 );
  69. camera.lookAt( new THREE.Vector3( 0, 2, 0 ) );
  70. scene = new THREE.Scene();
  71. scene.background = new THREE.Color( 0xe0e0e0 );
  72. scene.fog = new THREE.Fog( 0xe0e0e0, 20, 100 );
  73. clock = new THREE.Clock();
  74. // lights
  75. var light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  76. light.position.set( 0, 20, 0 );
  77. scene.add( light );
  78. light = new THREE.DirectionalLight( 0xffffff );
  79. light.position.set( 0, 20, 10 );
  80. scene.add( light );
  81. // ground
  82. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  83. mesh.rotation.x = - Math.PI / 2;
  84. scene.add( mesh );
  85. var grid = new THREE.GridHelper( 200, 40, 0x000000, 0x000000 );
  86. grid.material.opacity = 0.2;
  87. grid.material.transparent = true;
  88. scene.add( grid );
  89. // model
  90. var loader = new THREE.GLTFLoader();
  91. loader.load( 'models/gltf/RobotExpressive/RobotExpressive.glb', function( gltf ) {
  92. model = gltf.scene;
  93. scene.add( model );
  94. createGUI( model, gltf.animations );
  95. }, undefined, function( e ) {
  96. console.error( e );
  97. } );
  98. renderer = new THREE.WebGLRenderer( { antialias: true } );
  99. renderer.setPixelRatio( window.devicePixelRatio );
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. renderer.gammaOutput = true;
  102. renderer.gammaFactor = 2.2;
  103. container.appendChild( renderer.domElement );
  104. window.addEventListener( 'resize', onWindowResize, false );
  105. // stats
  106. stats = new Stats();
  107. container.appendChild( stats.dom );
  108. }
  109. function createGUI( model, animations ) {
  110. var states = [ 'Idle', 'Walking', 'Running', 'Dance', 'Death', 'Sitting', 'Standing' ];
  111. var emotes = [ 'Jump', 'Yes', 'No', 'Wave', 'Punch', 'ThumbsUp' ];
  112. gui = new dat.GUI();
  113. mixer = new THREE.AnimationMixer( model );
  114. actions = {};
  115. for ( var i = 0; i < animations.length; i++ ) {
  116. var clip = animations[ i ];
  117. var action = mixer.clipAction( clip );
  118. actions[ clip.name ] = action;
  119. if ( emotes.indexOf( clip.name ) >= 0 || states.indexOf( clip.name ) >= 4 ) {
  120. action.clampWhenFinished = true;
  121. action.loop = THREE.LoopOnce;
  122. }
  123. }
  124. // states
  125. var statesFolder = gui.addFolder( 'States' );
  126. var clipCtrl = statesFolder.add( api, 'state' ).options( states );
  127. clipCtrl.onChange( function() {
  128. fadeToAction( api.state, 0.5 );
  129. } );
  130. statesFolder.open();
  131. // emotes
  132. var emoteFolder = gui.addFolder( 'Emotes' );
  133. function createEmoteCallback( name ) {
  134. api[ name ] = function() {
  135. fadeToAction( name, 0.2 );
  136. mixer.addEventListener( 'finished', restoreState );
  137. };
  138. emoteFolder.add( api, name );
  139. }
  140. function restoreState() {
  141. mixer.removeEventListener( 'finished', restoreState );
  142. fadeToAction( api.state, 0.2 );
  143. }
  144. for ( var i = 0; i < emotes.length; i++ ) {
  145. createEmoteCallback( emotes[ i ] );
  146. }
  147. emoteFolder.open();
  148. // expressions
  149. face = model.getObjectByName( 'Head_2' );
  150. var expressions = Object.keys( face.morphTargetDictionary );
  151. var expressionFolder = gui.addFolder('Expressions');
  152. for ( var i = 0; i < expressions.length; i++ ) {
  153. expressionFolder.add( face.morphTargetInfluences, i, 0, 1, 0.01 ).name( expressions[ i ] );
  154. }
  155. activeAction = actions['Walking'];
  156. activeAction.play();
  157. expressionFolder.open();
  158. }
  159. function fadeToAction( name, duration ) {
  160. previousAction = activeAction;
  161. activeAction = actions[ name ];
  162. if ( previousAction !== activeAction ) {
  163. previousAction.fadeOut( duration );
  164. }
  165. activeAction
  166. .reset()
  167. .setEffectiveTimeScale( 1 )
  168. .setEffectiveWeight( 1 )
  169. .fadeIn( duration )
  170. .play();
  171. }
  172. function onWindowResize() {
  173. camera.aspect = window.innerWidth / window.innerHeight;
  174. camera.updateProjectionMatrix();
  175. renderer.setSize( window.innerWidth, window.innerHeight );
  176. }
  177. //
  178. function animate() {
  179. var dt = clock.getDelta();
  180. if ( mixer ) mixer.update( dt );
  181. requestAnimationFrame( animate );
  182. renderer.render( scene, camera );
  183. stats.update();
  184. }
  185. </script>
  186. </body>
  187. </html>