webgl_animation_multiple.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 how to clone a skinned mesh using <strong>SkeletonUtils.clone()</strong><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. let camera, scene, renderer;
  27. let clock;
  28. const mixers = [];
  29. init();
  30. animate();
  31. function init() {
  32. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  33. camera.position.set( 2, 3, - 6 );
  34. camera.lookAt( 0, 1, 0 );
  35. clock = new THREE.Clock();
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.Color( 0xa0a0a0 );
  38. scene.fog = new THREE.Fog( 0xa0a0a0, 10, 50 );
  39. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 3 );
  40. hemiLight.position.set( 0, 20, 0 );
  41. scene.add( hemiLight );
  42. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  43. dirLight.position.set( - 3, 10, - 10 );
  44. dirLight.castShadow = true;
  45. dirLight.shadow.camera.top = 4;
  46. dirLight.shadow.camera.bottom = - 4;
  47. dirLight.shadow.camera.left = - 4;
  48. dirLight.shadow.camera.right = 4;
  49. dirLight.shadow.camera.near = 0.1;
  50. dirLight.shadow.camera.far = 40;
  51. scene.add( dirLight );
  52. // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
  53. // ground
  54. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshPhongMaterial( { color: 0xcbcbcb, depthWrite: false } ) );
  55. mesh.rotation.x = - Math.PI / 2;
  56. mesh.receiveShadow = true;
  57. scene.add( mesh );
  58. const loader = new GLTFLoader();
  59. loader.load( 'models/gltf/Soldier.glb', function ( gltf ) {
  60. gltf.scene.traverse( function ( object ) {
  61. if ( object.isMesh ) object.castShadow = true;
  62. } );
  63. const model1 = SkeletonUtils.clone( gltf.scene );
  64. const model2 = SkeletonUtils.clone( gltf.scene );
  65. const model3 = SkeletonUtils.clone( gltf.scene );
  66. const mixer1 = new THREE.AnimationMixer( model1 );
  67. const mixer2 = new THREE.AnimationMixer( model2 );
  68. const mixer3 = new THREE.AnimationMixer( model3 );
  69. mixer1.clipAction( gltf.animations[ 0 ] ).play(); // idle
  70. mixer2.clipAction( gltf.animations[ 1 ] ).play(); // run
  71. mixer3.clipAction( gltf.animations[ 3 ] ).play(); // walk
  72. model1.position.x = - 2;
  73. model2.position.x = 0;
  74. model3.position.x = 2;
  75. scene.add( model1, model2, model3 );
  76. mixers.push( mixer1, mixer2, mixer3 );
  77. animate();
  78. } );
  79. renderer = new THREE.WebGLRenderer( { antialias: true } );
  80. renderer.setPixelRatio( window.devicePixelRatio );
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. renderer.shadowMap.enabled = true;
  83. document.body.appendChild( renderer.domElement );
  84. window.addEventListener( 'resize', onWindowResize );
  85. }
  86. function onWindowResize() {
  87. camera.aspect = window.innerWidth / window.innerHeight;
  88. camera.updateProjectionMatrix();
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. }
  91. function animate() {
  92. requestAnimationFrame( animate );
  93. const delta = clock.getDelta();
  94. for ( const mixer of mixers ) mixer.update( delta );
  95. renderer.render( scene, camera );
  96. }
  97. </script>
  98. </body>
  99. </html>