2
0

webgl_animation_multiple.html 4.0 KB

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