webgl_animation_multiple.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Multiple animated skinned meshes</title>
  5. <meta charset="utf-8">
  6. <meta content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" name="viewport">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. This demo shows the usage of <strong>SkeletonUtils.clone()</strong> and how to setup a shared skeleton.<br/>
  12. Soldier model from <a href="https://www.mixamo.com" target="_blank" rel="noopener">https://www.mixamo.com</a>.
  13. </div>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  25. import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. let camera, scene, renderer, clock;
  28. let model, animations;
  29. const mixers = [], objects = [];
  30. const params = {
  31. sharedSkeleton: false
  32. };
  33. init();
  34. function init() {
  35. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  36. camera.position.set( 2, 3, - 6 );
  37. camera.lookAt( 0, 1, 0 );
  38. clock = new THREE.Clock();
  39. scene = new THREE.Scene();
  40. scene.background = new THREE.Color( 0xa0a0a0 );
  41. scene.fog = new THREE.Fog( 0xa0a0a0, 10, 50 );
  42. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 3 );
  43. hemiLight.position.set( 0, 20, 0 );
  44. scene.add( hemiLight );
  45. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  46. dirLight.position.set( - 3, 10, - 10 );
  47. dirLight.castShadow = true;
  48. dirLight.shadow.camera.top = 4;
  49. dirLight.shadow.camera.bottom = - 4;
  50. dirLight.shadow.camera.left = - 4;
  51. dirLight.shadow.camera.right = 4;
  52. dirLight.shadow.camera.near = 0.1;
  53. dirLight.shadow.camera.far = 40;
  54. scene.add( dirLight );
  55. // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
  56. // ground
  57. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshPhongMaterial( { color: 0xcbcbcb, depthWrite: false } ) );
  58. mesh.rotation.x = - Math.PI / 2;
  59. mesh.receiveShadow = true;
  60. scene.add( mesh );
  61. const loader = new GLTFLoader();
  62. loader.load( 'models/gltf/Soldier.glb', function ( gltf ) {
  63. model = gltf.scene;
  64. animations = gltf.animations;
  65. model.traverse( function ( object ) {
  66. if ( object.isMesh ) object.castShadow = true;
  67. } );
  68. setupDefaultScene();
  69. } );
  70. renderer = new THREE.WebGLRenderer( { antialias: true } );
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. renderer.setAnimationLoop( animate );
  74. renderer.shadowMap.enabled = true;
  75. document.body.appendChild( renderer.domElement );
  76. window.addEventListener( 'resize', onWindowResize );
  77. const gui = new GUI();
  78. gui.add( params, 'sharedSkeleton' ).onChange( function () {
  79. clearScene();
  80. if ( params.sharedSkeleton === true ) {
  81. setupSharedSkeletonScene();
  82. } else {
  83. setupDefaultScene();
  84. }
  85. } );
  86. gui.open();
  87. }
  88. function clearScene() {
  89. for ( const mixer of mixers ) {
  90. mixer.stopAllAction();
  91. }
  92. mixers.length = 0;
  93. //
  94. for ( const object of objects ) {
  95. scene.remove( object );
  96. scene.traverse( function ( child ) {
  97. if ( child.isSkinnedMesh ) child.skeleton.dispose();
  98. } );
  99. }
  100. }
  101. function setupDefaultScene() {
  102. // three cloned models with independent skeletons.
  103. // each model can have its own animation state
  104. const model1 = SkeletonUtils.clone( model );
  105. const model2 = SkeletonUtils.clone( model );
  106. const model3 = SkeletonUtils.clone( model );
  107. model1.position.x = - 2;
  108. model2.position.x = 0;
  109. model3.position.x = 2;
  110. const mixer1 = new THREE.AnimationMixer( model1 );
  111. const mixer2 = new THREE.AnimationMixer( model2 );
  112. const mixer3 = new THREE.AnimationMixer( model3 );
  113. mixer1.clipAction( animations[ 0 ] ).play(); // idle
  114. mixer2.clipAction( animations[ 1 ] ).play(); // run
  115. mixer3.clipAction( animations[ 3 ] ).play(); // walk
  116. scene.add( model1, model2, model3 );
  117. objects.push( model1, model2, model3 );
  118. mixers.push( mixer1, mixer2, mixer3 );
  119. }
  120. function setupSharedSkeletonScene() {
  121. // three cloned models with a single shared skeleton.
  122. // all models share the same animation state
  123. const sharedModel = SkeletonUtils.clone( model );
  124. const shareSkinnedMesh = sharedModel.getObjectByName( 'vanguard_Mesh' );
  125. const sharedSkeleton = shareSkinnedMesh.skeleton;
  126. const sharedParentBone = sharedModel.getObjectByName( 'mixamorigHips' );
  127. scene.add( sharedParentBone ); // the bones need to be in the scene for the animation to work
  128. const model1 = shareSkinnedMesh.clone();
  129. const model2 = shareSkinnedMesh.clone();
  130. const model3 = shareSkinnedMesh.clone();
  131. model1.bindMode = THREE.DetachedBindMode;
  132. model2.bindMode = THREE.DetachedBindMode;
  133. model3.bindMode = THREE.DetachedBindMode;
  134. const identity = new THREE.Matrix4();
  135. model1.bind( sharedSkeleton, identity );
  136. model2.bind( sharedSkeleton, identity );
  137. model3.bind( sharedSkeleton, identity );
  138. model1.position.x = - 2;
  139. model2.position.x = 0;
  140. model3.position.x = 2;
  141. // apply transformation from the glTF asset
  142. model1.scale.setScalar( 0.01 );
  143. model1.rotation.x = - Math.PI * 0.5;
  144. model2.scale.setScalar( 0.01 );
  145. model2.rotation.x = - Math.PI * 0.5;
  146. model3.scale.setScalar( 0.01 );
  147. model3.rotation.x = - Math.PI * 0.5;
  148. //
  149. const mixer = new THREE.AnimationMixer( sharedParentBone );
  150. mixer.clipAction( animations[ 1 ] ).play();
  151. scene.add( sharedParentBone, model1, model2, model3 );
  152. objects.push( sharedParentBone, model1, model2, model3 );
  153. mixers.push( mixer );
  154. }
  155. function onWindowResize() {
  156. camera.aspect = window.innerWidth / window.innerHeight;
  157. camera.updateProjectionMatrix();
  158. renderer.setSize( window.innerWidth, window.innerHeight );
  159. }
  160. function animate() {
  161. const delta = clock.getDelta();
  162. for ( const mixer of mixers ) mixer.update( delta );
  163. renderer.render( scene, camera );
  164. }
  165. </script>
  166. </body>
  167. </html>