webgl_animation_multiple.html 3.7 KB

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