AnimatedModel.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "AnimatedModel.h"
  25. #include "Animation.h"
  26. #include "AnimationState.h"
  27. #include "Camera.h"
  28. #include "DebugRenderer.h"
  29. #include "Geometry.h"
  30. #include "IndexBuffer.h"
  31. #include "Log.h"
  32. #include "Material.h"
  33. #include "Octree.h"
  34. #include "OctreeQuery.h"
  35. #include "Profiler.h"
  36. #include "Renderer.h"
  37. #include "ReplicationUtils.h"
  38. #include "ResourceCache.h"
  39. #include "Scene.h"
  40. #include "StringUtils.h"
  41. #include "VertexBuffer.h"
  42. #include <algorithm>
  43. #include "DebugNew.h"
  44. static bool compareAnimationOrder(AnimationState* lhs, AnimationState* rhs)
  45. {
  46. return lhs->getPriority() < rhs->getPriority();
  47. }
  48. AnimatedModel::AnimatedModel(Octant* octant, const std::string& name) :
  49. StaticModel(NODE_ANIMATEDMODEL, octant, name),
  50. mAnimationLodDistance(0.0f),
  51. mAnimationLodFrameNumber(M_MAX_UNSIGNED),
  52. mAnimationLodBias(1.0f),
  53. mAnimationLodTimer(0.0f),
  54. mAnimationDirty(true),
  55. mAnimationOrderDirty(true),
  56. mMorphsDirty(true),
  57. mLocalAnimation(false)
  58. {
  59. }
  60. AnimatedModel::~AnimatedModel()
  61. {
  62. removeAllAnimationStates();
  63. }
  64. void AnimatedModel::save(Serializer& dest)
  65. {
  66. // Write GeometryNode properties
  67. GeometryNode::save(dest);
  68. // Write StaticModel properties
  69. dest.writeStringHash(getResourceHash(mModel));
  70. dest.writeVLE(mMaterials.size());
  71. for (unsigned i = 0; i < mMaterials.size(); ++i)
  72. dest.writeStringHash(getResourceHash(mMaterials[i]));
  73. // Write skeletal animation properties
  74. dest.writeFloat(mAnimationLodBias);
  75. dest.writeVLE(mAnimationStates.size());
  76. for (unsigned i = 0; i < mAnimationStates.size(); ++i)
  77. {
  78. AnimationState* state = mAnimationStates[i];
  79. state->save(dest);
  80. }
  81. // Write morph properties
  82. dest.writeVLE(mMorphs.size());
  83. for (unsigned i = 0; i < mMorphs.size(); ++i)
  84. dest.writeFloat(mMorphs[i].mWeight);
  85. }
  86. void AnimatedModel::load(Deserializer& source, ResourceCache* cache)
  87. {
  88. // Read GeometryNode properties
  89. GeometryNode::load(source, cache);
  90. // Read StaticModel properties
  91. // Note: can not use StaticModel::load() because we need to set the skeleton
  92. setModel(cache->getResource<Model>(source.readStringHash()));
  93. unsigned numMaterials = source.readVLE();
  94. for (unsigned i = 0; i < numMaterials; ++i)
  95. setMaterial(i, cache->getResource<Material>(source.readStringHash()));
  96. // Read skeletal animation properties
  97. mAnimationLodBias = source.readFloat();
  98. std::set<StringHash> processed;
  99. unsigned numAnimationStates = source.readVLE();
  100. for (unsigned i = 0; i < numAnimationStates; ++i)
  101. {
  102. StringHash animation = source.readStringHash();
  103. AnimationState* newState = addAnimationState(cache->getResource<Animation>(animation));
  104. newState->load(source);
  105. processed.insert(animation);
  106. }
  107. removeExtraAnimations(processed);
  108. // Read morph properties
  109. unsigned numMorphs = source.readVLE();
  110. for (unsigned i = 0; i < numMorphs; ++i)
  111. setMorphWeight(i, source.readFloat());
  112. }
  113. void AnimatedModel::saveXML(XMLElement& dest)
  114. {
  115. // Write GeometryNode properties
  116. GeometryNode::saveXML(dest);
  117. // Write StaticModel properties
  118. XMLElement modelElem = dest.createChildElement("model");
  119. modelElem.setString("name", getResourceName(mModel));
  120. for (unsigned i = 0; i < mMaterials.size(); ++i)
  121. {
  122. XMLElement materialElem = dest.createChildElement("material");
  123. materialElem.setInt("index", i);
  124. materialElem.setString("name", getResourceName(mMaterials[i]));
  125. }
  126. // Write skeletal animation properties
  127. XMLElement lodElem = dest.getChildElement("lod");
  128. lodElem.setFloat("animlodbias", mAnimationLodBias);
  129. for (unsigned i = 0; i < mAnimationStates.size(); ++i)
  130. {
  131. XMLElement animationElem = dest.createChildElement("animation");
  132. mAnimationStates[i]->saveXML(animationElem);
  133. }
  134. // Write morph properties
  135. for (unsigned i = 0; i < mMorphs.size(); ++i)
  136. {
  137. XMLElement morphElem = dest.createChildElement("morph");
  138. morphElem.setInt("index", i);
  139. morphElem.setFloat("weight", mMorphs[i].mWeight);
  140. }
  141. }
  142. void AnimatedModel::loadXML(const XMLElement& source, ResourceCache* cache)
  143. {
  144. // Read GeometryNode properties
  145. GeometryNode::loadXML(source, cache);
  146. // Read StaticModel properties
  147. // Note: can not use StaticModel::loadXML() because we need to set the skeleton
  148. XMLElement modelElem = source.getChildElement("model");
  149. setModel(cache->getResource<Model>(modelElem.getString("name")));
  150. XMLElement materialElem = source.getChildElement("material", false);
  151. while (materialElem.notNull())
  152. {
  153. unsigned index = materialElem.getInt("index");
  154. setMaterial(index, cache->getResource<Material>(materialElem.getString("name")));
  155. materialElem = materialElem.getNextElement("material");
  156. }
  157. // Read skeletal animation properties
  158. XMLElement lodElem = source.getChildElement("lod");
  159. mAnimationLodBias = lodElem.getFloat("animlodbias");
  160. removeAllAnimationStates();
  161. XMLElement animationElem = source.getChildElement("animation", false);
  162. while (animationElem.notNull())
  163. {
  164. AnimationState* newState = addAnimationState(cache->getResource<Animation>(animationElem.getString("name")));
  165. newState->loadXML(animationElem);
  166. animationElem = animationElem.getNextElement("animation");
  167. }
  168. // Read morph properties
  169. XMLElement morphElem = source.getChildElement("morph", false);
  170. while (morphElem.notNull())
  171. {
  172. unsigned index = morphElem.getInt("index");
  173. setMorphWeight(index, morphElem.getFloat("weight"));
  174. morphElem = morphElem.getNextElement("morph");
  175. }
  176. }
  177. bool AnimatedModel::writeNetUpdate(Serializer& dest, Serializer& destRevision, Deserializer& baseRevision, const NetUpdateInfo& info)
  178. {
  179. // Write GeometryNode properties and see if there were any changes
  180. bool prevBits = GeometryNode::writeNetUpdate(dest, destRevision, baseRevision, info);
  181. // Build bitmask of changed properties
  182. unsigned char bits = 0;
  183. // Model
  184. checkStringHash(getResourceHash(mModel), baseRevision, bits, 1);
  185. // Materials
  186. unsigned numBaseMaterials = baseRevision.getSize() ? baseRevision.readVLE() : 0;
  187. if (mMaterials.size() != numBaseMaterials)
  188. bits |= 2;
  189. for (unsigned i = 0; i < numBaseMaterials; ++i)
  190. {
  191. if (i < mMaterials.size())
  192. checkStringHash(getResourceHash(mMaterials[i]), baseRevision, bits, 2);
  193. else
  194. baseRevision.readStringHash();
  195. }
  196. // Animation LOD bias
  197. checkFloat(mAnimationLodBias, baseRevision, bits, 4);
  198. // Animations
  199. unsigned numBaseAnimations = baseRevision.getSize() ? baseRevision.readVLE() : 0;
  200. unsigned enabledAnimations = 0;
  201. if (mAnimationStates.size() != numBaseAnimations)
  202. bits |= 8;
  203. static std::vector<unsigned char> animBits;
  204. animBits.resize(mAnimationStates.size());
  205. // Assume that animations are new ie. require update of all fields
  206. for (unsigned i = 0; i < mAnimationStates.size(); ++i)
  207. {
  208. if (mAnimationStates[i]->isEnabled())
  209. {
  210. animBits[i] = 1 | 2 | 4 | 8 | 16;
  211. ++enabledAnimations;
  212. }
  213. else
  214. animBits[i] = 0;
  215. }
  216. for (unsigned i = 0; i < numBaseAnimations; ++i)
  217. {
  218. StringHash animation = baseRevision.readStringHash();
  219. StringHash startBone = baseRevision.readStringHash();
  220. bool looped = baseRevision.readBool();
  221. float weight = baseRevision.readFloat();
  222. float time = baseRevision.readFloat();
  223. int priority = baseRevision.readUByte();
  224. bool found = false;
  225. for (unsigned j = 0; j < mAnimationStates.size(); ++j)
  226. {
  227. AnimationState* state = mAnimationStates[j];
  228. if (state->getAnimation()->getNameHash() == animation)
  229. {
  230. // If animation exists in base state, update only the necessary properties
  231. found = true;
  232. // If zero weight (disabled), do not update anything
  233. if (!state->isEnabled())
  234. continue;
  235. animBits[j] = 0;
  236. if (startBone != state->getStartBone()->getNameHash())
  237. animBits[j] |= 1;
  238. if (looped != state->isLooped())
  239. animBits[j] |= 2;
  240. if (weight != state->getWeight())
  241. animBits[j] |= 4;
  242. if (time != state->getTime())
  243. animBits[j] |= 8;
  244. if (priority != state->getPriority())
  245. animBits[j] |= 16;
  246. if (animBits[j])
  247. bits |= 8;
  248. break;
  249. }
  250. }
  251. // If not found, the number of animations has stayed same, but the animations are different
  252. if (!found)
  253. bits |= 8;
  254. }
  255. // Morphs
  256. unsigned numBaseMorphs = baseRevision.getSize() ? baseRevision.readVLE() : 0;
  257. if (mMorphs.size() != numBaseMorphs)
  258. bits |= 16;
  259. for (unsigned i = 0; i < numBaseMorphs; ++i)
  260. {
  261. if (i < mMorphs.size())
  262. checkUByte((unsigned char)(mMorphs[i].mWeight * 255.0f), baseRevision, bits, 16);
  263. else
  264. baseRevision.readUByte();
  265. }
  266. // If local animation, do not send even if changed. It is slightly unoptimal to first check, then disable, but it ensures
  267. // that the base revision data stays the same (otherwise out of bounds reads might result when toggling local animation)
  268. if (mLocalAnimation)
  269. bits &= ~(8 | 16);
  270. // Update replication state fully, and network stream by delta
  271. dest.writeUByte(bits);
  272. writeStringHashDelta(getResourceHash(mModel), dest, destRevision, bits & 1);
  273. writeVLEDelta(mMaterials.size(), dest, destRevision, bits & 2);
  274. for (unsigned i = 0; i < mMaterials.size(); ++i)
  275. writeStringHashDelta(getResourceHash(mMaterials[i]), dest, destRevision, bits & 2);
  276. writeFloatDelta(mAnimationLodBias, dest, destRevision, bits & 4);
  277. // Write all animations to the base replication state
  278. destRevision.writeVLE(mAnimationStates.size());
  279. for (unsigned i = 0; i < mAnimationStates.size(); ++i)
  280. {
  281. AnimationState* state = mAnimationStates[i];
  282. destRevision.writeStringHash(state->getAnimation()->getNameHash());
  283. destRevision.writeStringHash(state->getStartBone()->getNameHash());
  284. destRevision.writeBool(state->isLooped());
  285. destRevision.writeFloat(state->getWeight());
  286. destRevision.writeFloat(state->getTime());
  287. destRevision.writeUByte(state->getPriority());
  288. }
  289. // Then write delta of enabled animations to the net stream
  290. if (bits & 8)
  291. {
  292. dest.writeVLE(enabledAnimations);
  293. for (unsigned i = 0; i < mAnimationStates.size(); ++i)
  294. {
  295. AnimationState* state = mAnimationStates[i];
  296. if (!state->isEnabled())
  297. continue;
  298. dest.writeStringHash(state->getAnimation()->getNameHash());
  299. dest.writeUByte(animBits[i]);
  300. if (animBits[i] & 1)
  301. dest.writeStringHash(state->getStartBone()->getNameHash());
  302. if (animBits[i] & 2)
  303. dest.writeBool(state->isLooped());
  304. if (animBits[i] & 4)
  305. dest.writeUByte((unsigned char)(state->getWeight() * 255.0f));
  306. if (animBits[i] & 8)
  307. dest.writeUShort((unsigned short)(state->getTime() * 65535.0f / state->getAnimation()->getLength()));
  308. if (animBits[i] & 16)
  309. dest.writeUByte(state->getPriority());
  310. }
  311. }
  312. writeVLEDelta(mMorphs.size(), dest, destRevision, bits & 16);
  313. for (unsigned i = 0; i < mMorphs.size(); ++i)
  314. writeUByteDelta((unsigned char)(mMorphs[i].mWeight * 255.0f), dest, destRevision, bits & 16);
  315. return prevBits || (bits != 0);
  316. }
  317. void AnimatedModel::readNetUpdate(Deserializer& source, ResourceCache* cache, const NetUpdateInfo& info)
  318. {
  319. // Read GeometryNode properties
  320. GeometryNode::readNetUpdate(source, cache, info);
  321. unsigned char bits = source.readUByte();
  322. if (bits & 1)
  323. setModel(cache->getResource<Model>(source.readStringHash()));
  324. if (bits & 2)
  325. {
  326. unsigned numMaterials = source.readVLE();
  327. for (unsigned i = 0; i < numMaterials; ++i)
  328. setMaterial(i, cache->getResource<Material>(source.readStringHash()));
  329. }
  330. readFloatDelta(mAnimationLodBias, source, bits & 4);
  331. if (bits & 8)
  332. {
  333. unsigned numAnimations = source.readVLE();
  334. std::set<StringHash> processed;
  335. for (unsigned i = 0; i < numAnimations; ++i)
  336. {
  337. StringHash animation = source.readStringHash();
  338. unsigned char animBits = source.readUByte();
  339. processed.insert(animation);
  340. // Find the animation state. If not found, create new
  341. AnimationState* state = getAnimationState(animation);
  342. bool newState = false;
  343. if (!state)
  344. {
  345. state = addAnimationState(cache->getResource<Animation>(animation));
  346. newState = true;
  347. }
  348. if (state)
  349. {
  350. if (animBits & 1)
  351. state->setStartBone(mSkeleton.getBone(source.readStringHash()));
  352. if (animBits & 2)
  353. state->setLooped(source.readBool());
  354. if (animBits & 4)
  355. state->setWeight(source.readUByte() / 255.0f);
  356. if (animBits & 8)
  357. state->setTime(source.readUShort() * state->getAnimation()->getLength() / 65535.0f);
  358. if (animBits & 16)
  359. state->setPriority(source.readUByte());
  360. // If state is new, interpolate directly to the end
  361. if (newState)
  362. state->interpolate(true, mEntity->getScene()->getInterpolationLerpFactor());
  363. }
  364. else
  365. {
  366. // If state could not be added, just read the data to avoid desyncing the stream
  367. if (animBits & 1)
  368. source.readStringHash();
  369. if (animBits & 2)
  370. source.readBool();
  371. if (animBits & 4)
  372. source.readUByte();
  373. if (animBits & 8)
  374. source.readUShort();
  375. if (animBits & 16)
  376. source.readUByte();
  377. }
  378. }
  379. removeExtraAnimations(processed);
  380. }
  381. if (bits & 16)
  382. {
  383. unsigned numMorphs = source.readVLE();
  384. for (unsigned i = 0; i < numMorphs; ++i)
  385. setMorphWeight(i, source.readUByte() / 255.0f);
  386. }
  387. }
  388. void AnimatedModel::interpolate(bool snapToEnd)
  389. {
  390. Node::interpolate(snapToEnd);
  391. float t = mEntity->getScene()->getInterpolationLerpFactor();
  392. for (std::vector<AnimationState*>::iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  393. (*i)->interpolate(snapToEnd, t);
  394. }
  395. void AnimatedModel::processRayQuery(RayOctreeQuery& query, float initialDistance)
  396. {
  397. // If no bones or no bone-level testing, use the GeometryNode test
  398. if ((!mSkeleton.getNumBones()) || (query.mLevel < RAY_AABB))
  399. {
  400. GeometryNode::processRayQuery(query, initialDistance);
  401. return;
  402. }
  403. PROFILE(AnimatedModel_Raycast);
  404. const std::vector<SharedPtr<Bone> >& bones = mSkeleton.getBones();
  405. Sphere boneSphere;
  406. RayQueryLevel level = query.mLevel;
  407. for (unsigned i = 0; i < bones.size(); ++i)
  408. {
  409. Bone* bone = bones[i];
  410. unsigned collisionMask = bone->getCollisionMask();
  411. // Use hitbox if available
  412. if (collisionMask & BONECOLLISION_BOX)
  413. {
  414. // Do an initial crude test using the bone's AABB
  415. const Matrix4x3& transform = bone->getWorldTransform();
  416. const BoundingBox& box = bone->getBoundingBox();
  417. float distance = box.getTransformed(transform).getDistance(query.mRay);
  418. if (distance < query.mMaxDistance)
  419. {
  420. if (level == RAY_AABB)
  421. {
  422. RayQueryResult result;
  423. result.mNode = this;
  424. result.mDistance = distance;
  425. result.mSubObject = i;
  426. query.mResult.push_back(result);
  427. }
  428. else
  429. {
  430. // Follow with an OBB test if required
  431. Matrix4x3 inverse = transform.getInverse();
  432. Ray localRay(inverse * query.mRay.mOrigin, inverse * Vector4(query.mRay.mDirection, 0.0f));
  433. distance = box.getDistance(localRay);
  434. if (distance < query.mMaxDistance)
  435. {
  436. RayQueryResult result;
  437. result.mNode = this;
  438. result.mDistance = distance;
  439. result.mSubObject = i;
  440. query.mResult.push_back(result);
  441. }
  442. }
  443. }
  444. }
  445. else if (collisionMask & BONECOLLISION_SPHERE)
  446. {
  447. boneSphere.mCenter = bone->getWorldPosition();
  448. boneSphere.mRadius = bone->getRadius();
  449. float distance = boneSphere.getDistance(query.mRay);
  450. if (distance < query.mMaxDistance)
  451. {
  452. RayQueryResult result;
  453. result.mNode = this;
  454. result.mSubObject = i;
  455. result.mDistance = distance;
  456. query.mResult.push_back(result);
  457. }
  458. }
  459. }
  460. }
  461. void AnimatedModel::updateNode(const FrameInfo& frame)
  462. {
  463. // Update animation here
  464. if ((!mAnimationDirty) && (!mAnimationOrderDirty))
  465. return;
  466. // If node was invisible last frame, need to decide animation LOD distance here
  467. // If headless, retain the current animation distance (should be 0)
  468. if ((frame.mCamera) && (abs((int)frame.mFrameNumber - (int)mAnimationLodFrameNumber) > 1))
  469. {
  470. float distance = frame.mCamera->getDistance(getWorldPosition());
  471. // If distance is greater than draw distance, no need to update at all
  472. if ((mDrawDistance != 0.0f) && (distance > mDrawDistance))
  473. return;
  474. // Multiply the distance by a constant so that invisible nodes don't update that often
  475. static const Vector3 dotScale(1 / 3.0f, 1 / 3.0f, 1 / 3.0f);
  476. float scale = getWorldScale().dotProduct(dotScale);
  477. mAnimationLodDistance = frame.mCamera->getLodDistance(ANIMATION_LOD_INVISIBLE_FACTOR * distance, scale, mLodBias);
  478. }
  479. updateAnimation(frame);
  480. }
  481. void AnimatedModel::updateDistance(const FrameInfo& frame)
  482. {
  483. mDistance = frame.mCamera->getDistance(getWorldPosition());
  484. static const Vector3 dotScale(1 / 3.0f, 1 / 3.0f, 1 / 3.0f);
  485. float scale = getWorldScale().dotProduct(dotScale);
  486. float newLodDistance = frame.mCamera->getLodDistance(mDistance, scale, mLodBias);
  487. // If model is rendered from several views, use the minimum LOD distance for animation LOD
  488. if (frame.mFrameNumber != mAnimationLodFrameNumber)
  489. {
  490. mAnimationLodDistance = newLodDistance;
  491. mAnimationLodFrameNumber = frame.mFrameNumber;
  492. }
  493. else
  494. mAnimationLodDistance = min(mAnimationLodDistance, newLodDistance);
  495. if (newLodDistance != mLodDistance)
  496. {
  497. mLodDistance = newLodDistance;
  498. mLodLevelsDirty = true;
  499. }
  500. }
  501. void AnimatedModel::updateGeometry(const FrameInfo& frame, Renderer* renderer)
  502. {
  503. if (mLodLevelsDirty)
  504. calculateLodLevels();
  505. if ((mMorphsDirty) && (mMorphs.size()))
  506. updateMorphs();
  507. if ((mSkeleton.getNumBones()) && (mSkeleton.getRootBone()->isSkinningDirty()))
  508. updateSkinning();
  509. }
  510. bool AnimatedModel::getVertexShaderParameter(unsigned batchIndex, VSParameter parameter, const float** data, unsigned* count)
  511. {
  512. if ((parameter == VSP_MODELSKINMATRICES) && (mSkinMatrices.size()))
  513. {
  514. // Check if model has per-geometry bone mappings
  515. if ((mGeometrySkinMatrices.size()) && (mGeometrySkinMatrices[batchIndex].size()))
  516. {
  517. *count = mGeometrySkinMatrices[batchIndex].size() * 12;
  518. *data = mGeometrySkinMatrices[batchIndex][0].getData();
  519. }
  520. // If not, use the global skin matrices
  521. else
  522. {
  523. *count = mSkinMatrices.size() * 12;
  524. *data = mSkinMatrices[0].getData();
  525. }
  526. return true;
  527. }
  528. return false;
  529. }
  530. void AnimatedModel::drawDebugGeometry(DebugRenderer* debug)
  531. {
  532. debug->addBoundingBox(getWorldBoundingBox(), Color(0.0f, 1.0f, 0.0f), false);
  533. debug->addSkeleton(mSkeleton, Color(0.75f, 0.75f, 0.75f), false);
  534. }
  535. bool AnimatedModel::setModel(Model* model)
  536. {
  537. if (model == mModel)
  538. return true;
  539. PROFILE(AnimatedModel_SetModel);
  540. if (!model)
  541. {
  542. LOGERROR("Null model for AnimatedModel");
  543. return false;
  544. }
  545. mModel = model;
  546. // Copy the subgeometry & LOD level structure
  547. setNumGeometries(model->getNumGeometries());
  548. const std::vector<std::vector<SharedPtr<Geometry> > >& geometries = model->getGeometries();
  549. for (unsigned i = 0; i < geometries.size(); ++i)
  550. mGeometries[i] = geometries[i];
  551. // Copy geometry bone mappings
  552. const std::vector<std::vector<unsigned> >& geometryBoneMappings = model->getGeometryBoneMappings();
  553. mGeometryBoneMappings.clear();
  554. for (unsigned i = 0; i < geometryBoneMappings.size(); ++i)
  555. mGeometryBoneMappings.push_back(geometryBoneMappings[i]);
  556. // Copy morphs
  557. mMorphVertexBuffers.clear();
  558. mMorphs.clear();
  559. const std::vector<ModelMorph>& morphs = model->getMorphs();
  560. for (unsigned i = 0; i < morphs.size(); ++i)
  561. {
  562. ModelMorph newMorph;
  563. newMorph.mName = morphs[i].mName;
  564. newMorph.mNameHash = morphs[i].mNameHash;
  565. newMorph.mWeight = 0.0f;
  566. newMorph.mBuffers = morphs[i].mBuffers;
  567. mMorphs.push_back(newMorph);
  568. }
  569. // If model has morphs, must clone all geometries & vertex buffers that refer to morphable vertex data
  570. if (morphs.size())
  571. {
  572. cloneGeometries();
  573. markMorphsDirty();
  574. }
  575. // Copy bounding box & skeleton
  576. setBoundingBox(model->getBoundingBox());
  577. setSkeleton(model->getSkeleton());
  578. return true;
  579. }
  580. AnimationState* AnimatedModel::addAnimationState(Animation* animation)
  581. {
  582. if (!animation)
  583. return 0;
  584. if (!mSkeleton.getNumBones())
  585. {
  586. LOGERROR("Animations can not be added to a model without skeleton");
  587. return 0;
  588. }
  589. // Check for not adding twice
  590. AnimationState* existing = getAnimationState(animation);
  591. if (existing)
  592. return existing;
  593. AnimationState* newState = new AnimationState(this, animation);
  594. mAnimationStates.push_back(newState);
  595. markAnimationOrderDirty();
  596. return newState;
  597. }
  598. void AnimatedModel::removeAnimationState(Animation* animation)
  599. {
  600. if (animation)
  601. removeAnimationState(animation->getNameHash());
  602. }
  603. void AnimatedModel::removeAnimationState(const std::string& animationName)
  604. {
  605. removeAnimationState(StringHash(animationName));
  606. }
  607. void AnimatedModel::removeAnimationState(StringHash animationNameHash)
  608. {
  609. for (std::vector<AnimationState*>::iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  610. {
  611. AnimationState* state = *i;
  612. Animation* animation = state->getAnimation();
  613. // Check both the animation and the resource name
  614. if ((animation->getNameHash() == animationNameHash) || (animation->getAnimationNameHash() == animationNameHash))
  615. {
  616. // If animation is still interpolating, do not remove immediately, but set target weight to zero
  617. if ((isProxy()) && (state->isInterpolating()))
  618. state->setWeight(0.0f);
  619. else
  620. {
  621. delete state;
  622. mAnimationStates.erase(i);
  623. markAnimationDirty();
  624. }
  625. return;
  626. }
  627. }
  628. }
  629. void AnimatedModel::removeAnimationState(AnimationState* state)
  630. {
  631. for (std::vector<AnimationState*>::iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  632. {
  633. if (*i == state)
  634. {
  635. // If animation is still interpolating, do not remove immediately, but set target weight to zero
  636. if ((isProxy()) && (state->isInterpolating()))
  637. state->setWeight(0.0f);
  638. else
  639. {
  640. delete state;
  641. mAnimationStates.erase(i);
  642. markAnimationDirty();
  643. }
  644. return;
  645. }
  646. }
  647. }
  648. void AnimatedModel::removeAllAnimationStates()
  649. {
  650. for (std::vector<AnimationState*>::iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  651. delete *i;
  652. mAnimationStates.clear();
  653. markAnimationDirty();
  654. }
  655. void AnimatedModel::setAnimationLodBias(float bias)
  656. {
  657. mAnimationLodBias = max(bias, 0.0f);
  658. }
  659. void AnimatedModel::setMorphWeight(unsigned index, float weight)
  660. {
  661. if (index >= mMorphs.size())
  662. return;
  663. weight = clamp(weight, 0.0f, 1.0f);
  664. if (weight != mMorphs[index].mWeight)
  665. {
  666. mMorphs[index].mWeight = weight;
  667. markMorphsDirty();
  668. }
  669. }
  670. void AnimatedModel::setMorphWeight(const std::string& name, float weight)
  671. {
  672. weight = clamp(weight, 0.0f, 1.0f);
  673. for (std::vector<ModelMorph>::iterator i = mMorphs.begin(); i != mMorphs.end(); ++i)
  674. {
  675. if (i->mName == name)
  676. {
  677. if (weight != i->mWeight)
  678. {
  679. i->mWeight = weight;
  680. markMorphsDirty();
  681. }
  682. return;
  683. }
  684. }
  685. }
  686. void AnimatedModel::setMorphWeight(StringHash nameHash, float weight)
  687. {
  688. weight = clamp(weight, 0.0f, 1.0f);
  689. for (std::vector<ModelMorph>::iterator i = mMorphs.begin(); i != mMorphs.end(); ++i)
  690. {
  691. if (i->mNameHash == nameHash)
  692. {
  693. if (weight != i->mWeight)
  694. {
  695. i->mWeight = weight;
  696. markMorphsDirty();
  697. }
  698. return;
  699. }
  700. }
  701. }
  702. void AnimatedModel::resetMorphWeights()
  703. {
  704. for (std::vector<ModelMorph>::iterator i = mMorphs.begin(); i != mMorphs.end(); ++i)
  705. i->mWeight = 0.0f;
  706. markMorphsDirty();
  707. }
  708. void AnimatedModel::syncAnimation(AnimatedModel* srcNode)
  709. {
  710. if (!srcNode)
  711. return;
  712. // Make sure the animation proceeds at the same rate as in the source
  713. mAnimationLodBias = srcNode->mAnimationLodBias;
  714. mAnimationLodDistance = srcNode->mAnimationLodDistance;
  715. mAnimationLodFrameNumber = srcNode->mAnimationLodFrameNumber;
  716. const std::vector<AnimationState*>& srcStates = srcNode->getAnimationStates();
  717. std::set<Animation*> srcAnimations;
  718. for (unsigned i = 0; i < srcStates.size(); ++i)
  719. {
  720. AnimationState* src = srcStates[i];
  721. Animation* anim = src->getAnimation();
  722. srcAnimations.insert(anim);
  723. AnimationState* dest = getAnimationState(anim);
  724. if (!dest)
  725. dest = addAnimationState(anim);
  726. dest->sync(src);
  727. }
  728. // Check for extra states in destination and remove them
  729. if (mAnimationStates.size() > srcStates.size())
  730. {
  731. for (unsigned i = 0; i < mAnimationStates.size(); ++i)
  732. {
  733. AnimationState* state = mAnimationStates[i];
  734. if (srcAnimations.find(state->getAnimation()) == srcAnimations.end())
  735. removeAnimationState(state);
  736. }
  737. }
  738. }
  739. void AnimatedModel::syncMorphs(AnimatedModel* srcNode)
  740. {
  741. for (unsigned i = 0; i < mMorphs.size(); ++i)
  742. {
  743. float srcWeight = srcNode->getMorphWeight(mMorphs[i].mName);
  744. setMorphWeight(i, srcWeight);
  745. }
  746. }
  747. void AnimatedModel::setLocalAnimation(bool enable)
  748. {
  749. mLocalAnimation = enable;
  750. }
  751. float AnimatedModel::getMorphWeight(unsigned index) const
  752. {
  753. if (index >= mMorphs.size())
  754. return 0.0f;
  755. return mMorphs[index].mWeight;
  756. }
  757. float AnimatedModel::getMorphWeight(const std::string& name) const
  758. {
  759. for (std::vector<ModelMorph>::const_iterator i = mMorphs.begin(); i != mMorphs.end(); ++i)
  760. {
  761. if (i->mName == name)
  762. return i->mWeight;
  763. }
  764. return 0.0f;
  765. }
  766. float AnimatedModel::getMorphWeight(StringHash nameHash) const
  767. {
  768. for (std::vector<ModelMorph>::const_iterator i = mMorphs.begin(); i != mMorphs.end(); ++i)
  769. {
  770. if (i->mNameHash == nameHash)
  771. return i->mWeight;
  772. }
  773. return 0.0f;
  774. }
  775. AnimationState* AnimatedModel::getAnimationState(Animation* animation) const
  776. {
  777. for (std::vector<AnimationState*>::const_iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  778. {
  779. if ((*i)->getAnimation() == animation)
  780. return *i;
  781. }
  782. return 0;
  783. }
  784. AnimationState* AnimatedModel::getAnimationState(const std::string& animationName) const
  785. {
  786. for (std::vector<AnimationState*>::const_iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  787. {
  788. Animation* animation = (*i)->getAnimation();
  789. // Check both the animation and the resource name
  790. if ((animation->getName() == animationName) || (animation->getAnimationName() == animationName))
  791. return *i;
  792. }
  793. return 0;
  794. }
  795. AnimationState* AnimatedModel::getAnimationState(StringHash animationNameHash) const
  796. {
  797. for (std::vector<AnimationState*>::const_iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  798. {
  799. Animation* animation = (*i)->getAnimation();
  800. // Check both the animation and the resource name
  801. if ((animation->getNameHash() == animationNameHash) || (animation->getAnimationNameHash() == animationNameHash))
  802. return *i;
  803. }
  804. return 0;
  805. }
  806. void AnimatedModel::setSkeleton(const Skeleton& skeleton)
  807. {
  808. PROFILE(AnimatedModel_SetSkeleton);
  809. removeAllAnimationStates();
  810. mSkeleton.define(skeleton.getBones());
  811. // Parent the root bone to the this node, so that objects can further be properly attached to bones
  812. Bone* rootBone = mSkeleton.getRootBone();
  813. if (rootBone)
  814. addChild(rootBone);
  815. // Reserve space for skinning matrices
  816. mSkinMatrices.resize(mSkeleton.getNumBones());
  817. refreshGeometryBoneMappings();
  818. }
  819. void AnimatedModel::onWorldBoundingBoxUpdate(BoundingBox& worldBoundingBox)
  820. {
  821. if (!mSkeleton.getNumBones())
  822. worldBoundingBox = mBoundingBox.getTransformed(getWorldTransform());
  823. else
  824. {
  825. // If has bones, update world bounding box based on them
  826. worldBoundingBox.mDefined = false;
  827. const std::vector<SharedPtr<Bone> >& bones = mSkeleton.getBones();
  828. for (std::vector<SharedPtr<Bone> >::const_iterator i = bones.begin(); i != bones.end(); ++i)
  829. {
  830. Bone* bone = *i;
  831. unsigned collisionMask = bone->getCollisionMask();
  832. // Use hitbox if available. If not, use only half of the sphere radius
  833. if (collisionMask & BONECOLLISION_BOX)
  834. worldBoundingBox.merge(bone->getBoundingBox().getTransformed(bone->getWorldTransform()));
  835. else if (collisionMask & BONECOLLISION_SPHERE)
  836. worldBoundingBox.merge(Sphere(bone->getWorldPosition(), bone->getRadius() * 0.5f));
  837. }
  838. }
  839. }
  840. void AnimatedModel::markAnimationDirty()
  841. {
  842. mAnimationDirty = true;
  843. // Mark for octree update, as animation is updated before octree reinsertion
  844. if (mOctant)
  845. mOctant->getRoot()->markNodeForUpdate(this);
  846. }
  847. void AnimatedModel::markAnimationOrderDirty()
  848. {
  849. mAnimationOrderDirty = true;
  850. // Mark for octree update, as animation is updated before octree reinsertion
  851. if (mOctant)
  852. mOctant->getRoot()->markNodeForUpdate(this);
  853. }
  854. void AnimatedModel::markMorphsDirty()
  855. {
  856. mMorphsDirty = true;
  857. }
  858. void AnimatedModel::cloneGeometries()
  859. {
  860. PROFILE(AnimatedModel_CloneGeometries);
  861. // Clone vertex buffers as necessary
  862. const std::vector<SharedPtr<VertexBuffer> >& originalVertexBuffers = mModel->getVertexBuffers();
  863. std::map<VertexBuffer*, SharedPtr<VertexBuffer> > clonedVertexBuffers;
  864. mMorphVertexBuffers.resize(originalVertexBuffers.size());
  865. for (unsigned i = 0; i < originalVertexBuffers.size(); ++i)
  866. {
  867. VertexBuffer* original = originalVertexBuffers[i];
  868. if (original->hasMorphRange())
  869. {
  870. SharedPtr<VertexBuffer> clone(new VertexBuffer(original->getRenderer()));
  871. clone->setSize(original->getVertexCount(), original->getElementMask());
  872. void* originalData = original->lock(0, original->getVertexCount(), LOCK_NORMAL);
  873. clone->setData(originalData);
  874. original->unlock();
  875. clone->setMorphRange(original->getMorphRangeStart(), original->getMorphRangeCount());
  876. clone->setMorphRangeResetData(original->getMorphRangeResetData());
  877. clonedVertexBuffers[original] = clone;
  878. mMorphVertexBuffers[i] = clone;
  879. }
  880. }
  881. // Geometries will always be cloned fully. They contain only references to buffer, so they are relatively light
  882. for (unsigned i = 0; i < mGeometries.size(); ++i)
  883. {
  884. for (unsigned j = 0; j < mGeometries[i].size(); ++j)
  885. {
  886. SharedPtr<Geometry> original = mGeometries[i][j];
  887. const std::vector<SharedPtr<VertexBuffer> >& originalBuffers = original->getVertexBuffers();
  888. SharedPtr<Geometry> clone(new Geometry());
  889. clone->setNumVertexBuffers(originalVertexBuffers.size());
  890. for (unsigned k = 0; k < originalVertexBuffers.size(); ++k)
  891. {
  892. VertexBuffer* originalBuffer = originalBuffers[k];
  893. if (clonedVertexBuffers.find(originalBuffer) != clonedVertexBuffers.end())
  894. clone->setVertexBuffer(k, clonedVertexBuffers[originalBuffer], original->getVertexElementMask(k));
  895. else
  896. clone->setVertexBuffer(k, originalBuffers[k], original->getVertexElementMask(k));
  897. }
  898. clone->setIndexBuffer(original->getIndexBuffer());
  899. clone->setDrawRange(original->getPrimitiveType(), original->getIndexStart(), original->getIndexCount());
  900. clone->setLodDistance(original->getLodDistance());
  901. mGeometries[i][j] = clone;
  902. }
  903. }
  904. }
  905. void AnimatedModel::refreshGeometryBoneMappings()
  906. {
  907. mGeometrySkinMatrices.clear();
  908. mGeometrySkinMatrixPtrs.clear();
  909. if (!mGeometryBoneMappings.size())
  910. return;
  911. // Check if all mappings are empty, then we do not need to use mapped skinning
  912. bool allEmpty = true;
  913. for (unsigned i = 0; i < mGeometryBoneMappings.size(); ++i)
  914. if (mGeometryBoneMappings[i].size())
  915. allEmpty = false;
  916. if (allEmpty)
  917. return;
  918. // Reserve space for per-geometry skinning matrices
  919. mGeometrySkinMatrices.resize(mGeometryBoneMappings.size());
  920. for (unsigned i = 0; i < mGeometryBoneMappings.size(); ++i)
  921. mGeometrySkinMatrices[i].resize(mGeometryBoneMappings[i].size());
  922. // Build original-to-skinindex matrix pointer mapping for fast copying
  923. // Note: at this point layout of mGeometrySkinMatrices cannot be modified or pointers become invalid
  924. mGeometrySkinMatrixPtrs.resize(mSkeleton.getNumBones());
  925. for (unsigned i = 0; i < mGeometryBoneMappings.size(); ++i)
  926. {
  927. for (unsigned j = 0; j < mGeometryBoneMappings[i].size(); ++j)
  928. mGeometrySkinMatrixPtrs[mGeometryBoneMappings[i][j]].push_back(&mGeometrySkinMatrices[i][j]);
  929. }
  930. }
  931. void AnimatedModel::updateAnimation(const FrameInfo& frame)
  932. {
  933. // If using animation LOD, accumulate time and see if it is time to update
  934. if ((mAnimationLodBias > 0.0f) && (mAnimationLodDistance > 0.0f))
  935. {
  936. mAnimationLodTimer += mAnimationLodBias * frame.mTimeStep * ANIMATION_LOD_BASESCALE;
  937. if (mAnimationLodTimer >= mAnimationLodDistance)
  938. mAnimationLodTimer = fmodf(mAnimationLodTimer, mAnimationLodDistance);
  939. else
  940. {
  941. // If animation order changed (animations added/removed), force always an immediate update
  942. if (!mAnimationOrderDirty)
  943. return;
  944. }
  945. }
  946. PROFILE(AnimatedModel_UpdateAnimation);
  947. // Make sure animations are in ascending priority order
  948. if (mAnimationOrderDirty)
  949. {
  950. std::sort(mAnimationStates.begin(), mAnimationStates.end(), compareAnimationOrder);
  951. mAnimationOrderDirty = false;
  952. }
  953. // Reset skeleton, then apply all animations
  954. mSkeleton.reset();
  955. for (std::vector<AnimationState*>::iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  956. (*i)->apply();
  957. // Animation has changed the bounding box: mark node for octree reinsertion
  958. VolumeNode::onMarkedDirty();
  959. mAnimationDirty = false;
  960. }
  961. void AnimatedModel::updateSkinning()
  962. {
  963. PROFILE(AnimatedModel_UpdateSkinning);
  964. // Note: the model's world transform will be baked in the skin matrices. When preparing to render,
  965. // the model transform will then be set to identity
  966. const std::vector<SharedPtr<Bone > >& bones = mSkeleton.getBones();
  967. // Skinning with global matrices only
  968. if (!mGeometrySkinMatrices.size())
  969. {
  970. for (unsigned i = 0; i < bones.size(); ++i)
  971. {
  972. Bone* bone = bones[i];
  973. if (bone->isSkinningDirty())
  974. {
  975. mSkinMatrices[i] = bone->getWorldTransform() * bone->getBindInverseTransform();
  976. bone->clearSkinningDirty();
  977. }
  978. }
  979. }
  980. // Skinning with per-geometry matrices
  981. else
  982. {
  983. for (unsigned i = 0; i < bones.size(); ++i)
  984. {
  985. Bone* bone = bones[i];
  986. if (bone->isSkinningDirty())
  987. {
  988. mSkinMatrices[i] = bone->getWorldTransform() * bone->getBindInverseTransform();
  989. // Copy the skin matrix to per-geometry matrices as needed
  990. for (unsigned j = 0; j < mGeometrySkinMatrixPtrs[i].size(); ++j)
  991. *mGeometrySkinMatrixPtrs[i][j] = mSkinMatrices[i];
  992. bone->clearSkinningDirty();
  993. }
  994. }
  995. }
  996. }
  997. void AnimatedModel::updateMorphs()
  998. {
  999. PROFILE(AnimatedModel_UpdateMorphs);
  1000. if (mMorphs.size())
  1001. {
  1002. // Reset the morph data range from all morphable vertex buffers, then apply morphs
  1003. for (unsigned i = 0; i < mMorphVertexBuffers.size(); ++i)
  1004. {
  1005. VertexBuffer* buffer = mMorphVertexBuffers[i];
  1006. if (buffer)
  1007. {
  1008. void* lockedMorphRange = buffer->lockMorphRange();
  1009. buffer->resetMorphRange(lockedMorphRange);
  1010. for (unsigned j = 0; j < mMorphs.size(); ++j)
  1011. {
  1012. if (mMorphs[j].mWeight > 0.0f)
  1013. {
  1014. std::map<unsigned, VertexBufferMorph>::iterator k = mMorphs[j].mBuffers.find(i);
  1015. if (k != mMorphs[j].mBuffers.end())
  1016. applyMorph(buffer, lockedMorphRange, k->second, mMorphs[j].mWeight);
  1017. }
  1018. }
  1019. buffer->unlock();
  1020. }
  1021. }
  1022. }
  1023. mMorphsDirty = false;
  1024. }
  1025. void AnimatedModel::applyMorph(VertexBuffer* buffer, void* lockedMorphRange, const VertexBufferMorph& morph, float weight)
  1026. {
  1027. unsigned elementMask = morph.mElementMask;
  1028. unsigned vertexCount = morph.mVertexCount;
  1029. unsigned normalOffset = buffer->getElementOffset(ELEMENT_NORMAL);
  1030. unsigned tangentOffset = buffer->getElementOffset(ELEMENT_TANGENT);
  1031. unsigned morphRangeStart = buffer->getMorphRangeStart();
  1032. unsigned vertexSize = buffer->getVertexSize();
  1033. unsigned char* srcData = morph.mMorphData;
  1034. unsigned char* destData = (unsigned char*)lockedMorphRange;
  1035. while (vertexCount--)
  1036. {
  1037. unsigned vertexIndex = *((unsigned*)srcData) - morphRangeStart;
  1038. srcData += sizeof(unsigned);
  1039. if (elementMask & MASK_POSITION)
  1040. {
  1041. float* dest = (float*)(destData + vertexIndex * vertexSize);
  1042. float* src = (float*)srcData;
  1043. dest[0] += src[0] * weight;
  1044. dest[1] += src[1] * weight;
  1045. dest[2] += src[2] * weight;
  1046. srcData += 3 * sizeof(float);
  1047. }
  1048. if (elementMask & MASK_NORMAL)
  1049. {
  1050. float* dest = (float*)(destData + vertexIndex * vertexSize + normalOffset);
  1051. float* src = (float*)srcData;
  1052. dest[0] += src[0] * weight;
  1053. dest[1] += src[1] * weight;
  1054. dest[2] += src[2] * weight;
  1055. srcData += 3 * sizeof(float);
  1056. }
  1057. if (elementMask & MASK_TANGENT)
  1058. {
  1059. float* dest = (float*)(destData + vertexIndex * vertexSize + tangentOffset);
  1060. float* src = (float*)srcData;
  1061. dest[0] += src[0] * weight;
  1062. dest[1] += src[1] * weight;
  1063. dest[2] += src[2] * weight;
  1064. srcData += 3 * sizeof(float);
  1065. }
  1066. }
  1067. }
  1068. void AnimatedModel::removeExtraAnimations(const std::set<StringHash>& animations)
  1069. {
  1070. bool removedAny = false;
  1071. for (std::vector<AnimationState*>::iterator i = mAnimationStates.begin(); i != mAnimationStates.end();)
  1072. {
  1073. AnimationState* state = *i;
  1074. if (animations.find(state->getAnimation()->getNameHash()) == animations.end())
  1075. {
  1076. // If animation is still interpolating, do not remove immediately, but set target weight to zero
  1077. if ((isProxy()) && (state->isInterpolating()))
  1078. {
  1079. state->setWeight(0.0f);
  1080. ++i;
  1081. }
  1082. else
  1083. {
  1084. delete state;
  1085. i = mAnimationStates.erase(i);
  1086. removedAny = true;
  1087. }
  1088. }
  1089. else
  1090. ++i;
  1091. }
  1092. if (removedAny)
  1093. markAnimationDirty();
  1094. }