webgl_animation_skinning_morph.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. <meta property="og:title" content="three.js webgl - skinning and morphing">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_animation_skinning_morph.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_animation_skinning_morph.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. color: #222;
  15. }
  16. a {
  17. color: #2fa1d6;
  18. }
  19. p {
  20. max-width: 600px;
  21. margin-left: auto;
  22. margin-right: auto;
  23. padding: 0 2em;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="info">
  29. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - skinning and morphing<br />
  30. <p>
  31. 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.
  32. </p>
  33. Model by
  34. <a href="https://www.patreon.com/quaternius" target="_blank" rel="noopener">Tomás Laulhé</a>,
  35. modifications by <a href="https://donmccurdy.com/" target="_blank" rel="noopener">Don McCurdy</a>. CC0.<br />
  36. </div>
  37. <script type="importmap">
  38. {
  39. "imports": {
  40. "three": "../build/three.module.js",
  41. "three/addons/": "./jsm/"
  42. }
  43. }
  44. </script>
  45. <script type="module">
  46. import * as THREE from 'three';
  47. import Stats from 'three/addons/libs/stats.module.js';
  48. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  49. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  50. let container, stats, timer, gui, mixer, actions, activeAction, previousAction;
  51. let camera, scene, renderer, model, face;
  52. const api = { state: 'Walking' };
  53. init();
  54. function init() {
  55. container = document.createElement( 'div' );
  56. document.body.appendChild( container );
  57. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 100 );
  58. camera.position.set( - 5, 3, 10 );
  59. camera.lookAt( 0, 2, 0 );
  60. scene = new THREE.Scene();
  61. scene.background = new THREE.Color( 0xe0e0e0 );
  62. scene.fog = new THREE.Fog( 0xe0e0e0, 20, 100 );
  63. timer = new THREE.Timer();
  64. timer.connect( document );
  65. // lights
  66. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 3 );
  67. hemiLight.position.set( 0, 20, 0 );
  68. scene.add( hemiLight );
  69. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  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: 0xcbcbcb, 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. renderer.setAnimationLoop( animate );
  93. container.appendChild( renderer.domElement );
  94. window.addEventListener( 'resize', onWindowResize );
  95. // stats
  96. stats = new Stats();
  97. container.appendChild( stats.dom );
  98. }
  99. function createGUI( model, animations ) {
  100. const states = [ 'Idle', 'Walking', 'Running', 'Dance', 'Death', 'Sitting', 'Standing' ];
  101. const emotes = [ 'Jump', 'Yes', 'No', 'Wave', 'Punch', 'ThumbsUp' ];
  102. gui = new GUI();
  103. mixer = new THREE.AnimationMixer( model );
  104. actions = {};
  105. for ( let i = 0; i < animations.length; i ++ ) {
  106. const clip = animations[ i ];
  107. const action = mixer.clipAction( clip );
  108. actions[ clip.name ] = action;
  109. if ( emotes.indexOf( clip.name ) >= 0 || states.indexOf( clip.name ) >= 4 ) {
  110. action.clampWhenFinished = true;
  111. action.loop = THREE.LoopOnce;
  112. }
  113. }
  114. // states
  115. const statesFolder = gui.addFolder( 'States' );
  116. const clipCtrl = statesFolder.add( api, 'state' ).options( states );
  117. clipCtrl.onChange( function () {
  118. fadeToAction( api.state, 0.5 );
  119. } );
  120. statesFolder.open();
  121. // emotes
  122. const emoteFolder = gui.addFolder( 'Emotes' );
  123. function createEmoteCallback( name ) {
  124. api[ name ] = function () {
  125. fadeToAction( name, 0.2 );
  126. mixer.addEventListener( 'finished', restoreState );
  127. };
  128. emoteFolder.add( api, name );
  129. }
  130. function restoreState() {
  131. mixer.removeEventListener( 'finished', restoreState );
  132. fadeToAction( api.state, 0.2 );
  133. }
  134. for ( let i = 0; i < emotes.length; i ++ ) {
  135. createEmoteCallback( emotes[ i ] );
  136. }
  137. emoteFolder.open();
  138. // expressions
  139. face = model.getObjectByName( 'Head_4' );
  140. const expressions = Object.keys( face.morphTargetDictionary );
  141. const expressionFolder = gui.addFolder( 'Expressions' );
  142. for ( let i = 0; i < expressions.length; i ++ ) {
  143. expressionFolder.add( face.morphTargetInfluences, i, 0, 1, 0.01 ).name( expressions[ i ] );
  144. }
  145. activeAction = actions[ 'Walking' ];
  146. activeAction.play();
  147. expressionFolder.open();
  148. }
  149. function fadeToAction( name, duration ) {
  150. previousAction = activeAction;
  151. activeAction = actions[ name ];
  152. if ( previousAction !== activeAction ) {
  153. previousAction.fadeOut( duration );
  154. }
  155. activeAction
  156. .reset()
  157. .setEffectiveTimeScale( 1 )
  158. .setEffectiveWeight( 1 )
  159. .fadeIn( duration )
  160. .play();
  161. }
  162. function onWindowResize() {
  163. camera.aspect = window.innerWidth / window.innerHeight;
  164. camera.updateProjectionMatrix();
  165. renderer.setSize( window.innerWidth, window.innerHeight );
  166. }
  167. //
  168. function animate() {
  169. timer.update();
  170. const dt = timer.getDelta();
  171. if ( mixer ) mixer.update( dt );
  172. renderer.render( scene, camera );
  173. stats.update();
  174. }
  175. </script>
  176. </body>
  177. </html>
粤ICP备19079148号