webgl_animation_skinning_morph.html 6.1 KB

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