webgl_animation_multiple.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Multiple animated objects</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="container"></div>
  11. <div id="info">
  12. This demo shows how clone a skinned 3d model using <strong>SkeletonUtils.clone()</strong><br/>
  13. Soldier model from <a href="https://www.mixamo.com" target="_blank" rel="noopener">https://www.mixamo.com</a>.
  14. </div>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  18. import { SkeletonUtils } from './jsm/utils/SkeletonUtils.js';
  19. //////////////////////////////
  20. // Global objects
  21. //////////////////////////////
  22. var worldScene = null; // THREE.Scene where it all will be rendered
  23. var renderer = null;
  24. var camera = null;
  25. var clock = null;
  26. var mixers = []; // All the THREE.AnimationMixer objects for all the animations in the scene
  27. //////////////////////////////
  28. //////////////////////////////
  29. // Information about our 3D models and units
  30. //////////////////////////////
  31. // The names of the 3D models to load. One-per file.
  32. // A model may have multiple SkinnedMesh objects as well as several rigs (armatures). Units will define which
  33. // meshes, armatures and animations to use. We will load the whole scene for each object and clone it for each unit.
  34. // Models are from https://www.mixamo.com/
  35. var MODELS = [
  36. { name: "Soldier" },
  37. { name: "Parrot" },
  38. // { name: "RiflePunch" },
  39. ];
  40. // Here we define instances of the models that we want to place in the scene, their position, scale and the animations
  41. // that must be played.
  42. var UNITS = [
  43. {
  44. modelName: "Soldier", // Will use the 3D model from file models/gltf/Soldier.glb
  45. meshName: "vanguard_Mesh", // Name of the main mesh to animate
  46. position: { x: 0, y: 0, z: 0 }, // Where to put the unit in the scene
  47. scale: 1, // Scaling of the unit. 1.0 means: use original size, 0.1 means "10 times smaller", etc.
  48. animationName: "Idle" // Name of animation to run
  49. },
  50. {
  51. modelName: "Soldier",
  52. meshName: "vanguard_Mesh",
  53. position: { x: 3, y: 0, z: 0 },
  54. scale: 2,
  55. animationName: "Walk"
  56. },
  57. {
  58. modelName: "Soldier",
  59. meshName: "vanguard_Mesh",
  60. position: { x: 1, y: 0, z: 0 },
  61. scale: 1,
  62. animationName: "Run"
  63. },
  64. {
  65. modelName: "Parrot",
  66. meshName: "mesh_0",
  67. position: { x: - 4, y: 0, z: 0 },
  68. rotation: { x: 0, y: Math.PI, z: 0 },
  69. scale: 0.01,
  70. animationName: "parrot_A_"
  71. },
  72. {
  73. modelName: "Parrot",
  74. meshName: "mesh_0",
  75. position: { x: - 2, y: 0, z: 0 },
  76. rotation: { x: 0, y: Math.PI / 2, z: 0 },
  77. scale: 0.02,
  78. animationName: null
  79. },
  80. ];
  81. //////////////////////////////
  82. // The main setup happens here
  83. //////////////////////////////
  84. var numLoadedModels = 0;
  85. initScene();
  86. initRenderer();
  87. loadModels();
  88. animate();
  89. //////////////////////////////
  90. //////////////////////////////
  91. // Function implementations
  92. //////////////////////////////
  93. /**
  94. * Function that starts loading process for the next model in the queue. The loading process is
  95. * asynchronous: it happens "in the background". Therefore we don't load all the models at once. We load one,
  96. * wait until it is done, then load the next one. When all models are loaded, we call loadUnits().
  97. */
  98. function loadModels() {
  99. for ( var i = 0; i < MODELS.length; ++ i ) {
  100. var m = MODELS[ i ];
  101. loadGltfModel( m, function () {
  102. ++ numLoadedModels;
  103. if ( numLoadedModels === MODELS.length ) {
  104. console.log( "All models loaded, time to instantiate units..." );
  105. instantiateUnits();
  106. }
  107. } );
  108. }
  109. }
  110. /**
  111. * Look at UNITS configuration, clone necessary 3D model scenes, place the armatures and meshes in the scene and
  112. * launch necessary animations
  113. */
  114. function instantiateUnits() {
  115. var numSuccess = 0;
  116. for ( var i = 0; i < UNITS.length; ++ i ) {
  117. var u = UNITS[ i ];
  118. var model = getModelByName( u.modelName );
  119. if ( model ) {
  120. var clonedScene = SkeletonUtils.clone( model.scene );
  121. if ( clonedScene ) {
  122. // THREE.Scene is cloned properly, let's find one mesh and launch animation for it
  123. var clonedMesh = clonedScene.getObjectByName( u.meshName );
  124. if ( clonedMesh ) {
  125. var mixer = startAnimation( clonedMesh, model.animations, u.animationName );
  126. // Save the animation mixer in the list, will need it in the animation loop
  127. mixers.push( mixer );
  128. numSuccess ++;
  129. }
  130. // Different models can have different configurations of armatures and meshes. Therefore,
  131. // We can't set position, scale or rotation to individual mesh objects. Instead we set
  132. // it to the whole cloned scene and then add the whole scene to the game world
  133. // Note: this may have weird effects if you have lights or other items in the GLTF file's scene!
  134. worldScene.add( clonedScene );
  135. if ( u.position ) {
  136. clonedScene.position.set( u.position.x, u.position.y, u.position.z );
  137. }
  138. if ( u.scale ) {
  139. clonedScene.scale.set( u.scale, u.scale, u.scale );
  140. }
  141. if ( u.rotation ) {
  142. clonedScene.rotation.x = u.rotation.x;
  143. clonedScene.rotation.y = u.rotation.y;
  144. clonedScene.rotation.z = u.rotation.z;
  145. }
  146. }
  147. } else {
  148. console.error( "Can not find model", u.modelName );
  149. }
  150. }
  151. console.log( `Successfully instantiated ${numSuccess} units` );
  152. }
  153. /**
  154. * Start animation for a specific mesh object. Find the animation by name in the 3D model's animation array
  155. * @param skinnedMesh {THREE.SkinnedMesh} The mesh to animate
  156. * @param animations {Array} Array containing all the animations for this model
  157. * @param animationName {string} Name of the animation to launch
  158. * @return {THREE.AnimationMixer} Mixer to be used in the render loop
  159. */
  160. function startAnimation( skinnedMesh, animations, animationName ) {
  161. var mixer = new THREE.AnimationMixer( skinnedMesh );
  162. var clip = THREE.AnimationClip.findByName( animations, animationName );
  163. if ( clip ) {
  164. var action = mixer.clipAction( clip );
  165. action.play();
  166. }
  167. return mixer;
  168. }
  169. /**
  170. * Find a model object by name
  171. * @param name
  172. * @returns {object|null}
  173. */
  174. function getModelByName( name ) {
  175. for ( var i = 0; i < MODELS.length; ++ i ) {
  176. if ( MODELS[ i ].name === name ) {
  177. return MODELS[ i ];
  178. }
  179. }
  180. return null;
  181. }
  182. /**
  183. * Load a 3D model from a GLTF file. Use the GLTFLoader.
  184. * @param model {object} Model config, one item from the MODELS array. It will be updated inside the function!
  185. * @param onLoaded {function} A callback function that will be called when the model is loaded
  186. */
  187. function loadGltfModel( model, onLoaded ) {
  188. var loader = new GLTFLoader();
  189. var modelName = "models/gltf/" + model.name + ".glb";
  190. loader.load( modelName, function ( gltf ) {
  191. var scene = gltf.scene;
  192. model.animations = gltf.animations;
  193. model.scene = scene;
  194. // Enable Shadows
  195. gltf.scene.traverse( function ( object ) {
  196. if ( object.isMesh ) {
  197. object.castShadow = true;
  198. }
  199. } );
  200. console.log( "Done loading model", model.name );
  201. onLoaded();
  202. } );
  203. }
  204. /**
  205. * Render loop. Renders the next frame of all animations
  206. */
  207. function animate() {
  208. requestAnimationFrame( animate );
  209. // Get the time elapsed since the last frame
  210. var mixerUpdateDelta = clock.getDelta();
  211. // Update all the animation frames
  212. for ( var i = 0; i < mixers.length; ++ i ) {
  213. mixers[ i ].update( mixerUpdateDelta );
  214. }
  215. renderer.render( worldScene, camera );
  216. }
  217. //////////////////////////////
  218. // General Three.JS stuff
  219. //////////////////////////////
  220. // This part is not anyhow related to the cloning of models, it's just setting up the scene.
  221. /**
  222. * Initialize ThreeJS scene renderer
  223. */
  224. function initRenderer() {
  225. var container = document.getElementById( 'container' );
  226. renderer = new THREE.WebGLRenderer( { antialias: true } );
  227. renderer.setPixelRatio( window.devicePixelRatio );
  228. renderer.setSize( window.innerWidth, window.innerHeight );
  229. renderer.outputEncoding = THREE.sRGBEncoding;
  230. renderer.shadowMap.enabled = true;
  231. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  232. container.appendChild( renderer.domElement );
  233. }
  234. /**
  235. * Initialize ThreeJS THREE.Scene
  236. */
  237. function initScene() {
  238. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  239. camera.position.set( 3, 6, - 10 );
  240. camera.lookAt( 0, 1, 0 );
  241. clock = new THREE.Clock();
  242. worldScene = new THREE.Scene();
  243. worldScene.background = new THREE.Color( 0xa0a0a0 );
  244. worldScene.fog = new THREE.Fog( 0xa0a0a0, 10, 22 );
  245. var hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  246. hemiLight.position.set( 0, 20, 0 );
  247. worldScene.add( hemiLight );
  248. var dirLight = new THREE.DirectionalLight( 0xffffff );
  249. dirLight.position.set( - 3, 10, - 10 );
  250. dirLight.castShadow = true;
  251. dirLight.shadow.camera.top = 10;
  252. dirLight.shadow.camera.bottom = - 10;
  253. dirLight.shadow.camera.left = - 10;
  254. dirLight.shadow.camera.right = 10;
  255. dirLight.shadow.camera.near = 0.1;
  256. dirLight.shadow.camera.far = 40;
  257. worldScene.add( dirLight );
  258. // ground
  259. var groundMesh = new THREE.Mesh(
  260. new THREE.PlaneBufferGeometry( 40, 40 ),
  261. new THREE.MeshPhongMaterial( {
  262. color: 0x999999,
  263. depthWrite: false
  264. } )
  265. );
  266. groundMesh.rotation.x = - Math.PI / 2;
  267. groundMesh.receiveShadow = true;
  268. worldScene.add( groundMesh );
  269. window.addEventListener( 'resize', onWindowResize, false );
  270. }
  271. /**
  272. * A callback that will be called whenever the browser window is resized.
  273. */
  274. function onWindowResize() {
  275. camera.aspect = window.innerWidth / window.innerHeight;
  276. camera.updateProjectionMatrix();
  277. renderer.setSize( window.innerWidth, window.innerHeight );
  278. }
  279. </script>
  280. </body>
  281. </html>