webgl_animation_skinning_morph.html 6.4 KB

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