AnimatedModel.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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 | 32;
  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 useNlerp = baseRevision.readBool();
  225. bool found = false;
  226. for (unsigned j = 0; j < mAnimationStates.size(); ++j)
  227. {
  228. AnimationState* state = mAnimationStates[j];
  229. if (state->getAnimation()->getNameHash() == animation)
  230. {
  231. // If animation exists in base state, update only the necessary properties
  232. found = true;
  233. // If zero weight (disabled), do not update anything
  234. if (!state->isEnabled())
  235. continue;
  236. animBits[j] = 0;
  237. if (startBone != state->getStartBone()->getNameHash())
  238. animBits[j] |= 1;
  239. if (looped != state->isLooped())
  240. animBits[j] |= 2;
  241. if (weight != state->getWeight())
  242. animBits[j] |= 4;
  243. if (time != state->getTime())
  244. animBits[j] |= 8;
  245. if (priority != state->getPriority())
  246. animBits[j] |= 16;
  247. if (useNlerp != state->getUseNlerp())
  248. animBits[j] |= 32;
  249. if (animBits[j])
  250. bits |= 8;
  251. break;
  252. }
  253. }
  254. // If not found, the number of animations has stayed same, but the animations are different
  255. if (!found)
  256. bits |= 8;
  257. }
  258. // Morphs
  259. unsigned numBaseMorphs = baseRevision.getSize() ? baseRevision.readVLE() : 0;
  260. if (mMorphs.size() != numBaseMorphs)
  261. bits |= 16;
  262. for (unsigned i = 0; i < numBaseMorphs; ++i)
  263. {
  264. if (i < mMorphs.size())
  265. checkUByte((unsigned char)(mMorphs[i].mWeight * 255.0f), baseRevision, bits, 16);
  266. else
  267. baseRevision.readUByte();
  268. }
  269. // If local animation, do not send even if changed. It is slightly unoptimal to first check, then disable, but it ensures
  270. // that the base revision data stays the same (otherwise out of bounds reads might result when toggling local animation)
  271. if (mLocalAnimation)
  272. bits &= ~(8 | 16);
  273. // Update replication state fully, and network stream by delta
  274. dest.writeUByte(bits);
  275. writeStringHashDelta(getResourceHash(mModel), dest, destRevision, bits & 1);
  276. writeVLEDelta(mMaterials.size(), dest, destRevision, bits & 2);
  277. for (unsigned i = 0; i < mMaterials.size(); ++i)
  278. writeStringHashDelta(getResourceHash(mMaterials[i]), dest, destRevision, bits & 2);
  279. writeFloatDelta(mAnimationLodBias, dest, destRevision, bits & 4);
  280. // Write all animations to the base replication state
  281. destRevision.writeVLE(mAnimationStates.size());
  282. for (unsigned i = 0; i < mAnimationStates.size(); ++i)
  283. {
  284. AnimationState* state = mAnimationStates[i];
  285. destRevision.writeStringHash(state->getAnimation()->getNameHash());
  286. destRevision.writeStringHash(state->getStartBone()->getNameHash());
  287. destRevision.writeBool(state->isLooped());
  288. destRevision.writeFloat(state->getWeight());
  289. destRevision.writeFloat(state->getTime());
  290. destRevision.writeUByte(state->getPriority());
  291. destRevision.writeBool(state->getUseNlerp());
  292. }
  293. // Then write delta of enabled animations to the net stream
  294. if (bits & 8)
  295. {
  296. dest.writeVLE(enabledAnimations);
  297. for (unsigned i = 0; i < mAnimationStates.size(); ++i)
  298. {
  299. AnimationState* state = mAnimationStates[i];
  300. if (!state->isEnabled())
  301. continue;
  302. dest.writeStringHash(state->getAnimation()->getNameHash());
  303. dest.writeUByte(animBits[i]);
  304. if (animBits[i] & 1)
  305. dest.writeStringHash(state->getStartBone()->getNameHash());
  306. if (animBits[i] & 2)
  307. dest.writeBool(state->isLooped());
  308. if (animBits[i] & 4)
  309. dest.writeUByte((unsigned char)(state->getWeight() * 255.0f));
  310. if (animBits[i] & 8)
  311. dest.writeUShort((unsigned short)(state->getTime() * 65535.0f / state->getAnimation()->getLength()));
  312. if (animBits[i] & 16)
  313. dest.writeUByte(state->getPriority());
  314. if (animBits[i] & 32)
  315. dest.writeBool(state->getUseNlerp());
  316. }
  317. }
  318. writeVLEDelta(mMorphs.size(), dest, destRevision, bits & 16);
  319. for (unsigned i = 0; i < mMorphs.size(); ++i)
  320. writeUByteDelta((unsigned char)(mMorphs[i].mWeight * 255.0f), dest, destRevision, bits & 16);
  321. return prevBits || (bits != 0);
  322. }
  323. void AnimatedModel::readNetUpdate(Deserializer& source, ResourceCache* cache, const NetUpdateInfo& info)
  324. {
  325. // Read GeometryNode properties
  326. GeometryNode::readNetUpdate(source, cache, info);
  327. unsigned char bits = source.readUByte();
  328. if (bits & 1)
  329. setModel(cache->getResource<Model>(source.readStringHash()));
  330. if (bits & 2)
  331. {
  332. unsigned numMaterials = source.readVLE();
  333. for (unsigned i = 0; i < numMaterials; ++i)
  334. setMaterial(i, cache->getResource<Material>(source.readStringHash()));
  335. }
  336. readFloatDelta(mAnimationLodBias, source, bits & 4);
  337. if (bits & 8)
  338. {
  339. unsigned numAnimations = source.readVLE();
  340. std::set<StringHash> processed;
  341. for (unsigned i = 0; i < numAnimations; ++i)
  342. {
  343. StringHash animation = source.readStringHash();
  344. unsigned char animBits = source.readUByte();
  345. processed.insert(animation);
  346. // Find the animation state. If not found, create new
  347. AnimationState* state = getAnimationState(animation);
  348. bool newState = false;
  349. if (!state)
  350. {
  351. state = addAnimationState(cache->getResource<Animation>(animation));
  352. newState = true;
  353. }
  354. if (state)
  355. {
  356. if (animBits & 1)
  357. state->setStartBone(mSkeleton.getBone(source.readStringHash()));
  358. if (animBits & 2)
  359. state->setLooped(source.readBool());
  360. if (animBits & 4)
  361. state->setWeight(source.readUByte() / 255.0f);
  362. if (animBits & 8)
  363. state->setTime(source.readUShort() * state->getAnimation()->getLength() / 65535.0f);
  364. if (animBits & 16)
  365. state->setPriority(source.readUByte());
  366. if (animBits & 32)
  367. state->setUseNlerp(source.readBool());
  368. // If state is new, interpolate directly to the end
  369. if (newState)
  370. state->interpolate(true, mEntity->getScene()->getInterpolationLerpFactor());
  371. }
  372. else
  373. {
  374. // If state could not be added, just read the data to avoid desyncing the stream
  375. if (animBits & 1)
  376. source.readStringHash();
  377. if (animBits & 2)
  378. source.readBool();
  379. if (animBits & 4)
  380. source.readUByte();
  381. if (animBits & 8)
  382. source.readUShort();
  383. if (animBits & 16)
  384. source.readUByte();
  385. if (animBits & 32)
  386. source.readBool();
  387. }
  388. }
  389. removeExtraAnimations(processed);
  390. }
  391. if (bits & 16)
  392. {
  393. unsigned numMorphs = source.readVLE();
  394. for (unsigned i = 0; i < numMorphs; ++i)
  395. setMorphWeight(i, source.readUByte() / 255.0f);
  396. }
  397. }
  398. void AnimatedModel::interpolate(bool snapToEnd)
  399. {
  400. Node::interpolate(snapToEnd);
  401. float t = mEntity->getScene()->getInterpolationLerpFactor();
  402. for (std::vector<AnimationState*>::iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  403. (*i)->interpolate(snapToEnd, t);
  404. }
  405. void AnimatedModel::processRayQuery(RayOctreeQuery& query, float initialDistance)
  406. {
  407. // If no bones or no bone-level testing, use the GeometryNode test
  408. if ((!mSkeleton.getNumBones()) || (query.mLevel < RAY_AABB))
  409. {
  410. GeometryNode::processRayQuery(query, initialDistance);
  411. return;
  412. }
  413. PROFILE(AnimatedModel_Raycast);
  414. const std::vector<SharedPtr<Bone> >& bones = mSkeleton.getBones();
  415. Sphere boneSphere;
  416. RayQueryLevel level = query.mLevel;
  417. for (unsigned i = 0; i < bones.size(); ++i)
  418. {
  419. Bone* bone = bones[i];
  420. unsigned collisionMask = bone->getCollisionMask();
  421. // Use hitbox if available
  422. if (collisionMask & BONECOLLISION_BOX)
  423. {
  424. // Do an initial crude test using the bone's AABB
  425. const Matrix4x3& transform = bone->getWorldTransform();
  426. const BoundingBox& box = bone->getBoundingBox();
  427. float distance = box.getTransformed(transform).getDistance(query.mRay);
  428. if (distance < query.mMaxDistance)
  429. {
  430. if (level == RAY_AABB)
  431. {
  432. RayQueryResult result;
  433. result.mNode = this;
  434. result.mDistance = distance;
  435. result.mSubObject = i;
  436. query.mResult.push_back(result);
  437. }
  438. else
  439. {
  440. // Follow with an OBB test if required
  441. Matrix4x3 inverse = transform.getInverse();
  442. Ray localRay(inverse * query.mRay.mOrigin, inverse * Vector4(query.mRay.mDirection, 0.0f));
  443. distance = box.getDistance(localRay);
  444. if (distance < query.mMaxDistance)
  445. {
  446. RayQueryResult result;
  447. result.mNode = this;
  448. result.mDistance = distance;
  449. result.mSubObject = i;
  450. query.mResult.push_back(result);
  451. }
  452. }
  453. }
  454. }
  455. else if (collisionMask & BONECOLLISION_SPHERE)
  456. {
  457. boneSphere.mCenter = bone->getWorldPosition();
  458. boneSphere.mRadius = bone->getRadius();
  459. float distance = boneSphere.getDistance(query.mRay);
  460. if (distance < query.mMaxDistance)
  461. {
  462. RayQueryResult result;
  463. result.mNode = this;
  464. result.mSubObject = i;
  465. result.mDistance = distance;
  466. query.mResult.push_back(result);
  467. }
  468. }
  469. }
  470. }
  471. void AnimatedModel::updateNode(const FrameInfo& frame)
  472. {
  473. // Update animation here
  474. if ((!mAnimationDirty) && (!mAnimationOrderDirty))
  475. return;
  476. // If node was invisible last frame, need to decide animation LOD distance here
  477. // If headless, retain the current animation distance (should be 0)
  478. if ((frame.mCamera) && (abs((int)frame.mFrameNumber - (int)mAnimationLodFrameNumber) > 1))
  479. {
  480. float distance = frame.mCamera->getDistance(getWorldPosition());
  481. // If distance is greater than draw distance, no need to update at all
  482. if ((mDrawDistance != 0.0f) && (distance > mDrawDistance))
  483. return;
  484. // Multiply the distance by a constant so that invisible nodes don't update that often
  485. static const Vector3 dotScale(1 / 3.0f, 1 / 3.0f, 1 / 3.0f);
  486. float scale = getWorldBoundingBox().getSize().dotProduct(dotScale);
  487. mAnimationLodDistance = frame.mCamera->getLodDistance(distance, scale, mLodBias) * ANIMATION_LOD_INVISIBLE_FACTOR;
  488. }
  489. updateAnimation(frame);
  490. }
  491. void AnimatedModel::updateDistance(const FrameInfo& frame)
  492. {
  493. mDistance = frame.mCamera->getDistance(getWorldPosition());
  494. static const Vector3 dotScale(1 / 3.0f, 1 / 3.0f, 1 / 3.0f);
  495. float scale = getWorldBoundingBox().getSize().dotProduct(dotScale);
  496. float newLodDistance = frame.mCamera->getLodDistance(mDistance, scale, mLodBias);
  497. // If model is rendered from several views, use the minimum LOD distance for animation LOD
  498. if (frame.mFrameNumber != mAnimationLodFrameNumber)
  499. {
  500. mAnimationLodDistance = newLodDistance;
  501. mAnimationLodFrameNumber = frame.mFrameNumber;
  502. }
  503. else
  504. mAnimationLodDistance = min(mAnimationLodDistance, newLodDistance);
  505. if (newLodDistance != mLodDistance)
  506. {
  507. mLodDistance = newLodDistance;
  508. mLodLevelsDirty = true;
  509. }
  510. }
  511. void AnimatedModel::updateGeometry(const FrameInfo& frame, Renderer* renderer)
  512. {
  513. if (mLodLevelsDirty)
  514. calculateLodLevels();
  515. if ((mMorphsDirty) && (mMorphs.size()))
  516. updateMorphs();
  517. if ((mSkeleton.getNumBones()) && (mSkeleton.getRootBone()->isSkinningDirty()))
  518. updateSkinning();
  519. }
  520. bool AnimatedModel::getVertexShaderParameter(unsigned batchIndex, VSParameter parameter, const float** data, unsigned* count)
  521. {
  522. if ((parameter == VSP_MODELSKINMATRICES) && (mSkinMatrices.size()))
  523. {
  524. // Check if model has per-geometry bone mappings
  525. if ((mGeometrySkinMatrices.size()) && (mGeometrySkinMatrices[batchIndex].size()))
  526. {
  527. *count = mGeometrySkinMatrices[batchIndex].size() * 12;
  528. *data = mGeometrySkinMatrices[batchIndex][0].getData();
  529. }
  530. // If not, use the global skin matrices
  531. else
  532. {
  533. *count = mSkinMatrices.size() * 12;
  534. *data = mSkinMatrices[0].getData();
  535. }
  536. return true;
  537. }
  538. return false;
  539. }
  540. void AnimatedModel::drawDebugGeometry(DebugRenderer* debug)
  541. {
  542. debug->addBoundingBox(getWorldBoundingBox(), Color(0.0f, 1.0f, 0.0f), false);
  543. debug->addSkeleton(mSkeleton, Color(0.75f, 0.75f, 0.75f), false);
  544. }
  545. bool AnimatedModel::setModel(Model* model)
  546. {
  547. if (model == mModel)
  548. return true;
  549. PROFILE(AnimatedModel_SetModel);
  550. if (!model)
  551. {
  552. LOGERROR("Null model for AnimatedModel");
  553. return false;
  554. }
  555. mModel = model;
  556. // Copy the subgeometry & LOD level structure
  557. setNumGeometries(model->getNumGeometries());
  558. const std::vector<std::vector<SharedPtr<Geometry> > >& geometries = model->getGeometries();
  559. for (unsigned i = 0; i < geometries.size(); ++i)
  560. mGeometries[i] = geometries[i];
  561. // Copy geometry bone mappings
  562. const std::vector<std::vector<unsigned> >& geometryBoneMappings = model->getGeometryBoneMappings();
  563. mGeometryBoneMappings.clear();
  564. for (unsigned i = 0; i < geometryBoneMappings.size(); ++i)
  565. mGeometryBoneMappings.push_back(geometryBoneMappings[i]);
  566. // Copy morphs
  567. mMorphVertexBuffers.clear();
  568. mMorphs.clear();
  569. const std::vector<ModelMorph>& morphs = model->getMorphs();
  570. for (unsigned i = 0; i < morphs.size(); ++i)
  571. {
  572. ModelMorph newMorph;
  573. newMorph.mName = morphs[i].mName;
  574. newMorph.mNameHash = morphs[i].mNameHash;
  575. newMorph.mWeight = 0.0f;
  576. newMorph.mBuffers = morphs[i].mBuffers;
  577. mMorphs.push_back(newMorph);
  578. }
  579. // If model has morphs, must clone all geometries & vertex buffers that refer to morphable vertex data
  580. if (morphs.size())
  581. {
  582. cloneGeometries();
  583. markMorphsDirty();
  584. }
  585. // Copy bounding box & skeleton
  586. setBoundingBox(model->getBoundingBox());
  587. setSkeleton(model->getSkeleton());
  588. return true;
  589. }
  590. AnimationState* AnimatedModel::addAnimationState(Animation* animation)
  591. {
  592. if (!animation)
  593. return 0;
  594. if (!mSkeleton.getNumBones())
  595. {
  596. LOGERROR("Animations can not be added to a model without skeleton");
  597. return 0;
  598. }
  599. // Check for not adding twice
  600. AnimationState* existing = getAnimationState(animation);
  601. if (existing)
  602. return existing;
  603. AnimationState* newState = new AnimationState(this, animation);
  604. mAnimationStates.push_back(newState);
  605. markAnimationOrderDirty();
  606. return newState;
  607. }
  608. void AnimatedModel::removeAnimationState(Animation* animation)
  609. {
  610. if (animation)
  611. removeAnimationState(animation->getNameHash());
  612. }
  613. void AnimatedModel::removeAnimationState(const std::string& animationName)
  614. {
  615. removeAnimationState(StringHash(animationName));
  616. }
  617. void AnimatedModel::removeAnimationState(StringHash animationNameHash)
  618. {
  619. for (std::vector<AnimationState*>::iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  620. {
  621. AnimationState* state = *i;
  622. Animation* animation = state->getAnimation();
  623. // Check both the animation and the resource name
  624. if ((animation->getNameHash() == animationNameHash) || (animation->getAnimationNameHash() == animationNameHash))
  625. {
  626. // If animation is still interpolating, do not remove immediately, but set target weight to zero
  627. if ((isProxy()) && (state->isInterpolating()))
  628. state->setWeight(0.0f);
  629. else
  630. {
  631. delete state;
  632. mAnimationStates.erase(i);
  633. markAnimationDirty();
  634. }
  635. return;
  636. }
  637. }
  638. }
  639. void AnimatedModel::removeAnimationState(AnimationState* state)
  640. {
  641. for (std::vector<AnimationState*>::iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  642. {
  643. if (*i == state)
  644. {
  645. // If animation is still interpolating, do not remove immediately, but set target weight to zero
  646. if ((isProxy()) && (state->isInterpolating()))
  647. state->setWeight(0.0f);
  648. else
  649. {
  650. delete state;
  651. mAnimationStates.erase(i);
  652. markAnimationDirty();
  653. }
  654. return;
  655. }
  656. }
  657. }
  658. void AnimatedModel::removeAllAnimationStates()
  659. {
  660. for (std::vector<AnimationState*>::iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  661. delete *i;
  662. mAnimationStates.clear();
  663. markAnimationDirty();
  664. }
  665. void AnimatedModel::setAnimationLodBias(float bias)
  666. {
  667. mAnimationLodBias = max(bias, 0.0f);
  668. }
  669. void AnimatedModel::setMorphWeight(unsigned index, float weight)
  670. {
  671. if (index >= mMorphs.size())
  672. return;
  673. weight = clamp(weight, 0.0f, 1.0f);
  674. if (weight != mMorphs[index].mWeight)
  675. {
  676. mMorphs[index].mWeight = weight;
  677. markMorphsDirty();
  678. }
  679. }
  680. void AnimatedModel::setMorphWeight(const std::string& name, float weight)
  681. {
  682. weight = clamp(weight, 0.0f, 1.0f);
  683. for (std::vector<ModelMorph>::iterator i = mMorphs.begin(); i != mMorphs.end(); ++i)
  684. {
  685. if (i->mName == name)
  686. {
  687. if (weight != i->mWeight)
  688. {
  689. i->mWeight = weight;
  690. markMorphsDirty();
  691. }
  692. return;
  693. }
  694. }
  695. }
  696. void AnimatedModel::setMorphWeight(StringHash nameHash, float weight)
  697. {
  698. weight = clamp(weight, 0.0f, 1.0f);
  699. for (std::vector<ModelMorph>::iterator i = mMorphs.begin(); i != mMorphs.end(); ++i)
  700. {
  701. if (i->mNameHash == nameHash)
  702. {
  703. if (weight != i->mWeight)
  704. {
  705. i->mWeight = weight;
  706. markMorphsDirty();
  707. }
  708. return;
  709. }
  710. }
  711. }
  712. void AnimatedModel::resetMorphWeights()
  713. {
  714. for (std::vector<ModelMorph>::iterator i = mMorphs.begin(); i != mMorphs.end(); ++i)
  715. i->mWeight = 0.0f;
  716. markMorphsDirty();
  717. }
  718. void AnimatedModel::syncAnimation(AnimatedModel* srcNode)
  719. {
  720. if (!srcNode)
  721. return;
  722. // Make sure the animation proceeds at the same rate as in the source
  723. mAnimationLodBias = srcNode->mAnimationLodBias;
  724. mAnimationLodDistance = srcNode->mAnimationLodDistance;
  725. mAnimationLodFrameNumber = srcNode->mAnimationLodFrameNumber;
  726. const std::vector<AnimationState*>& srcStates = srcNode->getAnimationStates();
  727. std::set<Animation*> srcAnimations;
  728. for (unsigned i = 0; i < srcStates.size(); ++i)
  729. {
  730. AnimationState* src = srcStates[i];
  731. Animation* anim = src->getAnimation();
  732. srcAnimations.insert(anim);
  733. AnimationState* dest = getAnimationState(anim);
  734. if (!dest)
  735. dest = addAnimationState(anim);
  736. dest->sync(src);
  737. }
  738. // Check for extra states in destination and remove them
  739. if (mAnimationStates.size() > srcStates.size())
  740. {
  741. for (unsigned i = 0; i < mAnimationStates.size(); ++i)
  742. {
  743. AnimationState* state = mAnimationStates[i];
  744. if (srcAnimations.find(state->getAnimation()) == srcAnimations.end())
  745. removeAnimationState(state);
  746. }
  747. }
  748. }
  749. void AnimatedModel::syncMorphs(AnimatedModel* srcNode)
  750. {
  751. for (unsigned i = 0; i < mMorphs.size(); ++i)
  752. {
  753. float srcWeight = srcNode->getMorphWeight(mMorphs[i].mName);
  754. setMorphWeight(i, srcWeight);
  755. }
  756. }
  757. void AnimatedModel::setLocalAnimation(bool enable)
  758. {
  759. mLocalAnimation = enable;
  760. }
  761. float AnimatedModel::getMorphWeight(unsigned index) const
  762. {
  763. if (index >= mMorphs.size())
  764. return 0.0f;
  765. return mMorphs[index].mWeight;
  766. }
  767. float AnimatedModel::getMorphWeight(const std::string& name) const
  768. {
  769. for (std::vector<ModelMorph>::const_iterator i = mMorphs.begin(); i != mMorphs.end(); ++i)
  770. {
  771. if (i->mName == name)
  772. return i->mWeight;
  773. }
  774. return 0.0f;
  775. }
  776. float AnimatedModel::getMorphWeight(StringHash nameHash) const
  777. {
  778. for (std::vector<ModelMorph>::const_iterator i = mMorphs.begin(); i != mMorphs.end(); ++i)
  779. {
  780. if (i->mNameHash == nameHash)
  781. return i->mWeight;
  782. }
  783. return 0.0f;
  784. }
  785. AnimationState* AnimatedModel::getAnimationState(Animation* animation) const
  786. {
  787. for (std::vector<AnimationState*>::const_iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  788. {
  789. if ((*i)->getAnimation() == animation)
  790. return *i;
  791. }
  792. return 0;
  793. }
  794. AnimationState* AnimatedModel::getAnimationState(const std::string& animationName) const
  795. {
  796. for (std::vector<AnimationState*>::const_iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  797. {
  798. Animation* animation = (*i)->getAnimation();
  799. // Check both the animation and the resource name
  800. if ((animation->getName() == animationName) || (animation->getAnimationName() == animationName))
  801. return *i;
  802. }
  803. return 0;
  804. }
  805. AnimationState* AnimatedModel::getAnimationState(StringHash animationNameHash) const
  806. {
  807. for (std::vector<AnimationState*>::const_iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  808. {
  809. Animation* animation = (*i)->getAnimation();
  810. // Check both the animation and the resource name
  811. if ((animation->getNameHash() == animationNameHash) || (animation->getAnimationNameHash() == animationNameHash))
  812. return *i;
  813. }
  814. return 0;
  815. }
  816. void AnimatedModel::setSkeleton(const Skeleton& skeleton)
  817. {
  818. PROFILE(AnimatedModel_SetSkeleton);
  819. removeAllAnimationStates();
  820. mSkeleton.define(skeleton.getBones());
  821. // Parent the root bone to the this node, so that objects can further be properly attached to bones
  822. Bone* rootBone = mSkeleton.getRootBone();
  823. if (rootBone)
  824. addChild(rootBone);
  825. // Reserve space for skinning matrices
  826. mSkinMatrices.resize(mSkeleton.getNumBones());
  827. refreshGeometryBoneMappings();
  828. }
  829. void AnimatedModel::onWorldBoundingBoxUpdate(BoundingBox& worldBoundingBox)
  830. {
  831. if (!mSkeleton.getNumBones())
  832. worldBoundingBox = mBoundingBox.getTransformed(getWorldTransform());
  833. else
  834. {
  835. // If has bones, update world bounding box based on them
  836. worldBoundingBox.mDefined = false;
  837. const std::vector<SharedPtr<Bone> >& bones = mSkeleton.getBones();
  838. for (std::vector<SharedPtr<Bone> >::const_iterator i = bones.begin(); i != bones.end(); ++i)
  839. {
  840. Bone* bone = *i;
  841. unsigned collisionMask = bone->getCollisionMask();
  842. // Use hitbox if available. If not, use only half of the sphere radius
  843. if (collisionMask & BONECOLLISION_BOX)
  844. worldBoundingBox.merge(bone->getBoundingBox().getTransformed(bone->getWorldTransform()));
  845. else if (collisionMask & BONECOLLISION_SPHERE)
  846. worldBoundingBox.merge(Sphere(bone->getWorldPosition(), bone->getRadius() * 0.5f));
  847. }
  848. }
  849. }
  850. void AnimatedModel::markAnimationDirty()
  851. {
  852. mAnimationDirty = true;
  853. // Mark for octree update, as animation is updated before octree reinsertion
  854. if (mOctant)
  855. mOctant->getRoot()->markNodeForUpdate(this);
  856. }
  857. void AnimatedModel::markAnimationOrderDirty()
  858. {
  859. mAnimationOrderDirty = true;
  860. // Mark for octree update, as animation is updated before octree reinsertion
  861. if (mOctant)
  862. mOctant->getRoot()->markNodeForUpdate(this);
  863. }
  864. void AnimatedModel::markMorphsDirty()
  865. {
  866. mMorphsDirty = true;
  867. }
  868. void AnimatedModel::cloneGeometries()
  869. {
  870. PROFILE(AnimatedModel_CloneGeometries);
  871. // Clone vertex buffers as necessary
  872. const std::vector<SharedPtr<VertexBuffer> >& originalVertexBuffers = mModel->getVertexBuffers();
  873. std::map<VertexBuffer*, SharedPtr<VertexBuffer> > clonedVertexBuffers;
  874. mMorphVertexBuffers.resize(originalVertexBuffers.size());
  875. for (unsigned i = 0; i < originalVertexBuffers.size(); ++i)
  876. {
  877. VertexBuffer* original = originalVertexBuffers[i];
  878. if (original->hasMorphRange())
  879. {
  880. SharedPtr<VertexBuffer> clone(new VertexBuffer(original->getRenderer()));
  881. clone->setSize(original->getVertexCount(), original->getElementMask());
  882. void* originalData = original->lock(0, original->getVertexCount(), LOCK_NORMAL);
  883. clone->setData(originalData);
  884. original->unlock();
  885. clone->setMorphRange(original->getMorphRangeStart(), original->getMorphRangeCount());
  886. clone->setMorphRangeResetData(original->getMorphRangeResetData());
  887. clonedVertexBuffers[original] = clone;
  888. mMorphVertexBuffers[i] = clone;
  889. }
  890. }
  891. // Geometries will always be cloned fully. They contain only references to buffer, so they are relatively light
  892. for (unsigned i = 0; i < mGeometries.size(); ++i)
  893. {
  894. for (unsigned j = 0; j < mGeometries[i].size(); ++j)
  895. {
  896. SharedPtr<Geometry> original = mGeometries[i][j];
  897. const std::vector<SharedPtr<VertexBuffer> >& originalBuffers = original->getVertexBuffers();
  898. SharedPtr<Geometry> clone(new Geometry());
  899. clone->setNumVertexBuffers(originalVertexBuffers.size());
  900. for (unsigned k = 0; k < originalVertexBuffers.size(); ++k)
  901. {
  902. VertexBuffer* originalBuffer = originalBuffers[k];
  903. if (clonedVertexBuffers.find(originalBuffer) != clonedVertexBuffers.end())
  904. clone->setVertexBuffer(k, clonedVertexBuffers[originalBuffer], original->getVertexElementMask(k));
  905. else
  906. clone->setVertexBuffer(k, originalBuffers[k], original->getVertexElementMask(k));
  907. }
  908. clone->setIndexBuffer(original->getIndexBuffer());
  909. clone->setDrawRange(original->getPrimitiveType(), original->getIndexStart(), original->getIndexCount());
  910. clone->setLodDistance(original->getLodDistance());
  911. mGeometries[i][j] = clone;
  912. }
  913. }
  914. }
  915. void AnimatedModel::refreshGeometryBoneMappings()
  916. {
  917. mGeometrySkinMatrices.clear();
  918. mGeometrySkinMatrixPtrs.clear();
  919. if (!mGeometryBoneMappings.size())
  920. return;
  921. // Check if all mappings are empty, then we do not need to use mapped skinning
  922. bool allEmpty = true;
  923. for (unsigned i = 0; i < mGeometryBoneMappings.size(); ++i)
  924. if (mGeometryBoneMappings[i].size())
  925. allEmpty = false;
  926. if (allEmpty)
  927. return;
  928. // Reserve space for per-geometry skinning matrices
  929. mGeometrySkinMatrices.resize(mGeometryBoneMappings.size());
  930. for (unsigned i = 0; i < mGeometryBoneMappings.size(); ++i)
  931. mGeometrySkinMatrices[i].resize(mGeometryBoneMappings[i].size());
  932. // Build original-to-skinindex matrix pointer mapping for fast copying
  933. // Note: at this point layout of mGeometrySkinMatrices cannot be modified or pointers become invalid
  934. mGeometrySkinMatrixPtrs.resize(mSkeleton.getNumBones());
  935. for (unsigned i = 0; i < mGeometryBoneMappings.size(); ++i)
  936. {
  937. for (unsigned j = 0; j < mGeometryBoneMappings[i].size(); ++j)
  938. mGeometrySkinMatrixPtrs[mGeometryBoneMappings[i][j]].push_back(&mGeometrySkinMatrices[i][j]);
  939. }
  940. }
  941. void AnimatedModel::updateAnimation(const FrameInfo& frame)
  942. {
  943. // If using animation LOD, accumulate time and see if it is time to update
  944. if ((mAnimationLodBias > 0.0f) && (mAnimationLodDistance > 0.0f))
  945. {
  946. mAnimationLodTimer += mAnimationLodBias * frame.mTimeStep * ANIMATION_LOD_BASESCALE;
  947. if (mAnimationLodTimer >= mAnimationLodDistance)
  948. mAnimationLodTimer = fmodf(mAnimationLodTimer, mAnimationLodDistance);
  949. else
  950. {
  951. // If animation order changed (animations added/removed), force always an immediate update
  952. if (!mAnimationOrderDirty)
  953. return;
  954. }
  955. }
  956. PROFILE(AnimatedModel_UpdateAnimation);
  957. // Make sure animations are in ascending priority order
  958. if (mAnimationOrderDirty)
  959. {
  960. std::sort(mAnimationStates.begin(), mAnimationStates.end(), compareAnimationOrder);
  961. mAnimationOrderDirty = false;
  962. }
  963. // Reset skeleton, then apply all animations
  964. mSkeleton.reset();
  965. for (std::vector<AnimationState*>::iterator i = mAnimationStates.begin(); i != mAnimationStates.end(); ++i)
  966. (*i)->apply();
  967. // Animation has changed the bounding box: mark node for octree reinsertion
  968. VolumeNode::onMarkedDirty();
  969. mAnimationDirty = false;
  970. }
  971. void AnimatedModel::updateSkinning()
  972. {
  973. PROFILE(AnimatedModel_UpdateSkinning);
  974. // Note: the model's world transform will be baked in the skin matrices. When preparing to render,
  975. // the model transform will then be set to identity
  976. const std::vector<SharedPtr<Bone > >& bones = mSkeleton.getBones();
  977. // Skinning with global matrices only
  978. if (!mGeometrySkinMatrices.size())
  979. {
  980. for (unsigned i = 0; i < bones.size(); ++i)
  981. {
  982. Bone* bone = bones[i];
  983. if (bone->isSkinningDirty())
  984. {
  985. mSkinMatrices[i] = bone->getWorldTransform() * bone->getOffsetMatrix();
  986. bone->clearSkinningDirty();
  987. }
  988. }
  989. }
  990. // Skinning with per-geometry matrices
  991. else
  992. {
  993. for (unsigned i = 0; i < bones.size(); ++i)
  994. {
  995. Bone* bone = bones[i];
  996. if (bone->isSkinningDirty())
  997. {
  998. mSkinMatrices[i] = bone->getWorldTransform() * bone->getOffsetMatrix();
  999. // Copy the skin matrix to per-geometry matrices as needed
  1000. for (unsigned j = 0; j < mGeometrySkinMatrixPtrs[i].size(); ++j)
  1001. *mGeometrySkinMatrixPtrs[i][j] = mSkinMatrices[i];
  1002. bone->clearSkinningDirty();
  1003. }
  1004. }
  1005. }
  1006. }
  1007. void AnimatedModel::updateMorphs()
  1008. {
  1009. PROFILE(AnimatedModel_UpdateMorphs);
  1010. if (mMorphs.size())
  1011. {
  1012. // Reset the morph data range from all morphable vertex buffers, then apply morphs
  1013. for (unsigned i = 0; i < mMorphVertexBuffers.size(); ++i)
  1014. {
  1015. VertexBuffer* buffer = mMorphVertexBuffers[i];
  1016. if (buffer)
  1017. {
  1018. void* lockedMorphRange = buffer->lockMorphRange();
  1019. buffer->resetMorphRange(lockedMorphRange);
  1020. for (unsigned j = 0; j < mMorphs.size(); ++j)
  1021. {
  1022. if (mMorphs[j].mWeight > 0.0f)
  1023. {
  1024. std::map<unsigned, VertexBufferMorph>::iterator k = mMorphs[j].mBuffers.find(i);
  1025. if (k != mMorphs[j].mBuffers.end())
  1026. applyMorph(buffer, lockedMorphRange, k->second, mMorphs[j].mWeight);
  1027. }
  1028. }
  1029. buffer->unlock();
  1030. }
  1031. }
  1032. }
  1033. mMorphsDirty = false;
  1034. }
  1035. void AnimatedModel::applyMorph(VertexBuffer* buffer, void* lockedMorphRange, const VertexBufferMorph& morph, float weight)
  1036. {
  1037. unsigned elementMask = morph.mElementMask;
  1038. unsigned vertexCount = morph.mVertexCount;
  1039. unsigned normalOffset = buffer->getElementOffset(ELEMENT_NORMAL);
  1040. unsigned tangentOffset = buffer->getElementOffset(ELEMENT_TANGENT);
  1041. unsigned morphRangeStart = buffer->getMorphRangeStart();
  1042. unsigned vertexSize = buffer->getVertexSize();
  1043. unsigned char* srcData = morph.mMorphData;
  1044. unsigned char* destData = (unsigned char*)lockedMorphRange;
  1045. while (vertexCount--)
  1046. {
  1047. unsigned vertexIndex = *((unsigned*)srcData) - morphRangeStart;
  1048. srcData += sizeof(unsigned);
  1049. if (elementMask & MASK_POSITION)
  1050. {
  1051. float* dest = (float*)(destData + vertexIndex * vertexSize);
  1052. float* src = (float*)srcData;
  1053. dest[0] += src[0] * weight;
  1054. dest[1] += src[1] * weight;
  1055. dest[2] += src[2] * weight;
  1056. srcData += 3 * sizeof(float);
  1057. }
  1058. if (elementMask & MASK_NORMAL)
  1059. {
  1060. float* dest = (float*)(destData + vertexIndex * vertexSize + normalOffset);
  1061. float* src = (float*)srcData;
  1062. dest[0] += src[0] * weight;
  1063. dest[1] += src[1] * weight;
  1064. dest[2] += src[2] * weight;
  1065. srcData += 3 * sizeof(float);
  1066. }
  1067. if (elementMask & MASK_TANGENT)
  1068. {
  1069. float* dest = (float*)(destData + vertexIndex * vertexSize + tangentOffset);
  1070. float* src = (float*)srcData;
  1071. dest[0] += src[0] * weight;
  1072. dest[1] += src[1] * weight;
  1073. dest[2] += src[2] * weight;
  1074. srcData += 3 * sizeof(float);
  1075. }
  1076. }
  1077. }
  1078. void AnimatedModel::removeExtraAnimations(const std::set<StringHash>& animations)
  1079. {
  1080. bool removedAny = false;
  1081. for (std::vector<AnimationState*>::iterator i = mAnimationStates.begin(); i != mAnimationStates.end();)
  1082. {
  1083. AnimationState* state = *i;
  1084. if (animations.find(state->getAnimation()->getNameHash()) == animations.end())
  1085. {
  1086. // If animation is still interpolating, do not remove immediately, but set target weight to zero
  1087. if ((isProxy()) && (state->isInterpolating()))
  1088. {
  1089. state->setWeight(0.0f);
  1090. ++i;
  1091. }
  1092. else
  1093. {
  1094. delete state;
  1095. i = mAnimationStates.erase(i);
  1096. removedAny = true;
  1097. }
  1098. }
  1099. else
  1100. ++i;
  1101. }
  1102. if (removedAny)
  1103. markAnimationDirty();
  1104. }