webgl_animation_multiple.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  28. import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js';
  29. let camera, scene, renderer;
  30. let clock;
  31. const mixers = [];
  32. init();
  33. animate();
  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. gltf.scene.traverse( function ( object ) {
  64. if ( object.isMesh ) object.castShadow = true;
  65. } );
  66. const model1 = SkeletonUtils.clone( gltf.scene );
  67. const model2 = SkeletonUtils.clone( gltf.scene );
  68. const model3 = SkeletonUtils.clone( gltf.scene );
  69. const mixer1 = new THREE.AnimationMixer( model1 );
  70. const mixer2 = new THREE.AnimationMixer( model2 );
  71. const mixer3 = new THREE.AnimationMixer( model3 );
  72. mixer1.clipAction( gltf.animations[ 0 ] ).play(); // idle
  73. mixer2.clipAction( gltf.animations[ 1 ] ).play(); // run
  74. mixer3.clipAction( gltf.animations[ 3 ] ).play(); // walk
  75. model1.position.x = - 2;
  76. model2.position.x = 0;
  77. model3.position.x = 2;
  78. scene.add( model1, model2, model3 );
  79. mixers.push( mixer1, mixer2, mixer3 );
  80. animate();
  81. } );
  82. renderer = new THREE.WebGLRenderer( { antialias: true } );
  83. renderer.setPixelRatio( window.devicePixelRatio );
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. renderer.shadowMap.enabled = true;
  86. renderer.useLegacyLights = false;
  87. document.body.appendChild( renderer.domElement );
  88. window.addEventListener( 'resize', onWindowResize );
  89. }
  90. function onWindowResize() {
  91. camera.aspect = window.innerWidth / window.innerHeight;
  92. camera.updateProjectionMatrix();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. }
  95. function animate() {
  96. requestAnimationFrame( animate );
  97. const delta = clock.getDelta();
  98. for ( const mixer of mixers ) mixer.update( delta );
  99. renderer.render( scene, camera );
  100. }
  101. </script>
  102. </body>
  103. </html>