AnimatedModel.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "Batch.h"
  28. #include "Camera.h"
  29. #include "Context.h"
  30. #include "DebugRenderer.h"
  31. #include "DrawableEvents.h"
  32. #include "Geometry.h"
  33. #include "Graphics.h"
  34. #include "IndexBuffer.h"
  35. #include "Log.h"
  36. #include "Material.h"
  37. #include "MemoryBuffer.h"
  38. #include "Octree.h"
  39. #include "Profiler.h"
  40. #include "ResourceCache.h"
  41. #include "ResourceEvents.h"
  42. #include "Scene.h"
  43. #include "Sort.h"
  44. #include "VertexBuffer.h"
  45. #include "DebugNew.h"
  46. static const Vector3 DOT_SCALE(1 / 3.0f, 1 / 3.0f, 1 / 3.0f);
  47. static bool CompareAnimationOrder(const SharedPtr<AnimationState>& lhs, const SharedPtr<AnimationState>& rhs)
  48. {
  49. return lhs->GetLayer() < rhs->GetLayer();
  50. }
  51. OBJECTTYPESTATIC(AnimatedModel);
  52. AnimatedModel::AnimatedModel(Context* context) :
  53. StaticModel(context),
  54. animationLodFrameNumber_(0),
  55. animationLodBias_(1.0f),
  56. animationLodTimer_(-1.0f),
  57. animationLodDistance_(0.0f),
  58. invisibleLodFactor_(0.0f),
  59. animationDirty_(false),
  60. animationOrderDirty_(false),
  61. morphsDirty_(true),
  62. skinningDirty_(true),
  63. isMaster_(true),
  64. loading_(false),
  65. assignBonesPending_(false)
  66. {
  67. }
  68. AnimatedModel::~AnimatedModel()
  69. {
  70. }
  71. void AnimatedModel::RegisterObject(Context* context)
  72. {
  73. context->RegisterFactory<AnimatedModel>();
  74. ACCESSOR_ATTRIBUTE(AnimatedModel, VAR_RESOURCEREF, "Model", GetModelAttr, SetModelAttr, ResourceRef, ResourceRef(Model::GetTypeStatic()), AM_DEFAULT);
  75. REF_ACCESSOR_ATTRIBUTE(AnimatedModel, VAR_RESOURCEREFLIST, "Material", GetMaterialsAttr, SetMaterialsAttr, ResourceRefList, ResourceRefList(Material::GetTypeStatic()), AM_DEFAULT);
  76. ATTRIBUTE(AnimatedModel, VAR_BOOL, "Is Visible", visible_, true, AM_DEFAULT);
  77. ATTRIBUTE(AnimatedModel, VAR_BOOL, "Is Occluder", occluder_, false, AM_DEFAULT);
  78. ACCESSOR_ATTRIBUTE(AnimatedModel, VAR_BOOL, "Can Be Occluded", IsOccludee, SetOccludee, bool, true, AM_DEFAULT);
  79. ATTRIBUTE(AnimatedModel, VAR_BOOL, "Cast Shadows", castShadows_, false, AM_DEFAULT);
  80. ACCESSOR_ATTRIBUTE(AnimatedModel, VAR_FLOAT, "Draw Distance", GetDrawDistance, SetDrawDistance, float, 0.0f, AM_DEFAULT);
  81. ACCESSOR_ATTRIBUTE(AnimatedModel, VAR_FLOAT, "Shadow Distance", GetShadowDistance, SetShadowDistance, float, 0.0f, AM_DEFAULT);
  82. ACCESSOR_ATTRIBUTE(AnimatedModel, VAR_FLOAT, "LOD Bias", GetLodBias, SetLodBias, float, 1.0f, AM_DEFAULT);
  83. ACCESSOR_ATTRIBUTE(AnimatedModel, VAR_FLOAT, "Animation LOD Bias", GetAnimationLodBias, SetAnimationLodBias, float, 1.0f, AM_DEFAULT);
  84. ACCESSOR_ATTRIBUTE(AnimatedModel, VAR_FLOAT, "Invisible Anim LOD Factor", GetInvisibleLodFactor, SetInvisibleLodFactor, float, 0.0f, AM_DEFAULT);
  85. COPY_BASE_ATTRIBUTES(AnimatedModel, Drawable);
  86. ATTRIBUTE(AnimatedModel, VAR_INT, "Ray/Occl. LOD Level", softwareLodLevel_, M_MAX_UNSIGNED, AM_DEFAULT);
  87. ACCESSOR_ATTRIBUTE(AnimatedModel, VAR_VARIANTVECTOR, "Bone Animation Enabled", GetBonesEnabledAttr, SetBonesEnabledAttr, VariantVector, VariantVector(), AM_FILE | AM_NOEDIT);
  88. ACCESSOR_ATTRIBUTE(AnimatedModel, VAR_VARIANTVECTOR, "Animation States", GetAnimationStatesAttr, SetAnimationStatesAttr, VariantVector, VariantVector(), AM_FILE | AM_NOEDIT);
  89. REF_ACCESSOR_ATTRIBUTE(AnimatedModel, VAR_BUFFER, "Morphs", GetMorphsAttr, SetMorphsAttr, PODVector<unsigned char>, PODVector<unsigned char>(), AM_DEFAULT | AM_NOEDIT);
  90. }
  91. bool AnimatedModel::Load(Deserializer& source)
  92. {
  93. loading_ = true;
  94. bool success = Component::Load(source);
  95. loading_ = false;
  96. return success;
  97. }
  98. bool AnimatedModel::LoadXML(const XMLElement& source)
  99. {
  100. loading_ = true;
  101. bool success = Component::LoadXML(source);
  102. loading_ = false;
  103. return success;
  104. }
  105. void AnimatedModel::ApplyAttributes()
  106. {
  107. if (assignBonesPending_)
  108. {
  109. AssignBoneNodes();
  110. assignBonesPending_ = false;
  111. }
  112. }
  113. void AnimatedModel::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  114. {
  115. // If no bones or no bone-level testing, use the Drawable test
  116. if (query.level_ < RAY_AABB || !skeleton_.GetRootBone() || !skeleton_.GetRootBone()->node_)
  117. {
  118. Drawable::ProcessRayQuery(query, results);
  119. return;
  120. }
  121. // Check ray hit distance to AABB before proceeding with bone-level tests
  122. if (query.ray_.HitDistance(GetWorldBoundingBox()) > query.maxDistance_)
  123. return;
  124. const Vector<Bone>& bones = skeleton_.GetBones();
  125. Sphere boneSphere;
  126. RayQueryLevel level = query.level_;
  127. for (unsigned i = 0; i < bones.Size(); ++i)
  128. {
  129. const Bone& bone = bones[i];
  130. if (!bone.node_)
  131. continue;
  132. // Use hitbox if available
  133. if (bone.collisionMask_ & BONECOLLISION_BOX)
  134. {
  135. // Do an initial crude test using the bone's AABB
  136. const BoundingBox& box = bone.boundingBox_;
  137. const Matrix3x4& transform = bone.node_->GetWorldTransform();
  138. float distance = query.ray_.HitDistance(box.Transformed(transform));
  139. if (distance <= query.maxDistance_)
  140. {
  141. if (level == RAY_AABB)
  142. {
  143. RayQueryResult result;
  144. result.drawable_ = this;
  145. result.node_ = GetNode();
  146. result.distance_ = distance;
  147. result.subObject_ = i;
  148. results.Push(result);
  149. }
  150. else
  151. {
  152. // Follow with an OBB test if required
  153. Matrix3x4 inverse = transform.Inverse();
  154. Ray localRay(inverse * query.ray_.origin_, inverse * Vector4(query.ray_.direction_, 0.0f));
  155. distance = localRay.HitDistance(box);
  156. if (distance <= query.maxDistance_)
  157. {
  158. RayQueryResult result;
  159. result.drawable_ = this;
  160. result.node_ = GetNode();
  161. result.distance_ = distance;
  162. result.subObject_ = i;
  163. results.Push(result);
  164. }
  165. }
  166. }
  167. }
  168. else if (bone.collisionMask_ & BONECOLLISION_SPHERE)
  169. {
  170. boneSphere.center_ = bone.node_->GetWorldPosition();
  171. boneSphere.radius_ = bone.radius_;
  172. float distance = query.ray_.HitDistance(boneSphere);
  173. if (distance <= query.maxDistance_)
  174. {
  175. RayQueryResult result;
  176. result.drawable_ = this;
  177. result.node_ = GetNode();
  178. result.subObject_ = i;
  179. result.distance_ = distance;
  180. results.Push(result);
  181. }
  182. }
  183. }
  184. }
  185. void AnimatedModel::Update(const FrameInfo& frame)
  186. {
  187. // Update animation here
  188. if (!animationDirty_ && !animationOrderDirty_)
  189. return;
  190. // If node was invisible last frame, need to decide animation LOD distance here
  191. // If headless, retain the current animation distance (should be 0)
  192. if (frame.camera_ && abs((int)frame.frameNumber_ - (int)viewFrameNumber_) > 1)
  193. {
  194. if (invisibleLodFactor_ == 0.0f)
  195. return;
  196. float distance = frame.camera_->GetDistance(node_->GetWorldPosition());
  197. // If distance is greater than draw distance, no need to update at all
  198. if (drawDistance_ > 0.0f && distance > drawDistance_)
  199. return;
  200. // Multiply the distance by a constant so that invisible nodes don't update that often
  201. float scale = GetWorldBoundingBox().Size().DotProduct(DOT_SCALE);
  202. animationLodDistance_ = frame.camera_->GetLodDistance(distance, scale, lodBias_) * invisibleLodFactor_;
  203. }
  204. UpdateAnimation(frame);
  205. }
  206. void AnimatedModel::UpdateBatches(const FrameInfo& frame)
  207. {
  208. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  209. distance_ = frame.camera_->GetDistance(worldTransform.Translation());
  210. // Note: per-geometry distances do not take skinning into account
  211. if (batches_.Size() > 1)
  212. {
  213. for (unsigned i = 0; i < batches_.Size(); ++i)
  214. {
  215. batches_[i].distance_ = frame.camera_->GetDistance(worldTransform * geometryData_[i].center_);
  216. batches_[i].worldTransform_ = &worldTransform;
  217. }
  218. }
  219. else if (batches_.Size() == 1)
  220. {
  221. batches_[0].distance_ = distance_;
  222. batches_[0].worldTransform_ = &worldTransform;
  223. }
  224. float scale = GetWorldBoundingBox().Size().DotProduct(DOT_SCALE);
  225. float newLodDistance = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  226. // If model is rendered from several views, use the minimum LOD distance for animation LOD
  227. if (frame.frameNumber_ != animationLodFrameNumber_)
  228. {
  229. animationLodDistance_ = newLodDistance;
  230. animationLodFrameNumber_ = frame.frameNumber_;
  231. }
  232. else
  233. animationLodDistance_ = Min(animationLodDistance_, newLodDistance);
  234. if (newLodDistance != lodDistance_)
  235. {
  236. lodDistance_ = newLodDistance;
  237. CalculateLodLevels();
  238. }
  239. // If model has morphs, check if the morph vertex buffer(s) have lost data (only possible in OpenGL mode)
  240. if (morphs_.Size())
  241. {
  242. for (unsigned i = 0; i < morphVertexBuffers_.Size(); ++i)
  243. {
  244. if (morphVertexBuffers_[i] && morphVertexBuffers_[i]->IsDataLost())
  245. {
  246. morphsDirty_ = true;
  247. break;
  248. }
  249. }
  250. }
  251. }
  252. void AnimatedModel::UpdateGeometry(const FrameInfo& frame)
  253. {
  254. if (morphsDirty_)
  255. UpdateMorphs();
  256. if (skinningDirty_)
  257. UpdateSkinning();
  258. }
  259. UpdateGeometryType AnimatedModel::GetUpdateGeometryType()
  260. {
  261. if (morphsDirty_)
  262. return UPDATE_MAIN_THREAD;
  263. else if (skinningDirty_)
  264. return UPDATE_WORKER_THREAD;
  265. else
  266. return UPDATE_NONE;
  267. }
  268. void AnimatedModel::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  269. {
  270. if (debug)
  271. {
  272. debug->AddBoundingBox(GetWorldBoundingBox(), Color(0.0f, 1.0f, 0.0f), depthTest);
  273. debug->AddSkeleton(skeleton_, Color(0.75f, 0.75f, 0.75f), depthTest);
  274. }
  275. }
  276. void AnimatedModel::SetModel(Model* model, bool createBones)
  277. {
  278. if (!model || model == model_)
  279. return;
  280. // Unsubscribe from the reload event of previous model (if any), then subscribe to the new
  281. if (model_)
  282. UnsubscribeFromEvent(model_, E_RELOADFINISHED);
  283. if (model)
  284. SubscribeToEvent(model, E_RELOADFINISHED, HANDLER(AnimatedModel, HandleModelReloadFinished));
  285. model_ = model;
  286. // Copy the subgeometry & LOD level structure
  287. SetNumGeometries(model->GetNumGeometries());
  288. const Vector<Vector<SharedPtr<Geometry> > >& geometries = model->GetGeometries();
  289. const PODVector<Vector3>& geometryCenters = model->GetGeometryCenters();
  290. for (unsigned i = 0; i < geometries.Size(); ++i)
  291. {
  292. geometries_[i] = geometries[i];
  293. geometryData_[i].center_ = geometryCenters[i];
  294. }
  295. // Copy geometry bone mappings
  296. const Vector<PODVector<unsigned> >& geometryBoneMappings = model->GetGeometryBoneMappings();
  297. geometryBoneMappings_.Clear();
  298. for (unsigned i = 0; i < geometryBoneMappings.Size(); ++i)
  299. geometryBoneMappings_.Push(geometryBoneMappings[i]);
  300. // Copy morphs
  301. morphVertexBuffers_.Clear();
  302. morphs_.Clear();
  303. const Vector<ModelMorph>& morphs = model->GetMorphs();
  304. for (unsigned i = 0; i < morphs.Size(); ++i)
  305. {
  306. ModelMorph newMorph;
  307. newMorph.name_ = morphs[i].name_;
  308. newMorph.nameHash_ = morphs[i].nameHash_;
  309. newMorph.weight_ = 0.0f;
  310. newMorph.buffers_ = morphs[i].buffers_;
  311. morphs_.Push(newMorph);
  312. }
  313. // If model has morphs, must clone all geometries & vertex buffers that refer to morphable vertex data
  314. if (morphs.Size())
  315. {
  316. CloneGeometries();
  317. MarkMorphsDirty();
  318. }
  319. // Copy bounding box & skeleton
  320. SetBoundingBox(model->GetBoundingBox());
  321. SetSkeleton(model->GetSkeleton(), createBones);
  322. ResetLodLevels();
  323. // Enable skinning in batches
  324. for (unsigned i = 0; i < batches_.Size(); ++i)
  325. {
  326. if (skinMatrices_.Size())
  327. {
  328. batches_[i].geometryType_ = GEOM_SKINNED;
  329. // Check if model has per-geometry bone mappings
  330. if (geometrySkinMatrices_.Size() && geometrySkinMatrices_[i].Size())
  331. {
  332. batches_[i].shaderData_ = geometrySkinMatrices_[i][0].Data();
  333. batches_[i].shaderDataSize_ = geometrySkinMatrices_[i].Size() * 12;
  334. }
  335. // If not, use the global skin matrices
  336. else
  337. {
  338. batches_[i].shaderData_ = skinMatrices_[0].Data();
  339. batches_[i].shaderDataSize_ = skinMatrices_.Size() * 12;
  340. }
  341. }
  342. else
  343. batches_[i].geometryType_ = GEOM_STATIC;
  344. }
  345. MarkNetworkUpdate();
  346. }
  347. AnimationState* AnimatedModel::AddAnimationState(Animation* animation)
  348. {
  349. if (!isMaster_)
  350. {
  351. LOGERROR("Can not add animation state to non-master model");
  352. return 0;
  353. }
  354. if (!animation || !skeleton_.GetNumBones())
  355. return 0;
  356. // Check for not adding twice
  357. AnimationState* existing = GetAnimationState(animation);
  358. if (existing)
  359. return existing;
  360. SharedPtr<AnimationState> newState(new AnimationState(this, animation));
  361. animationStates_.Push(newState);
  362. MarkAnimationOrderDirty();
  363. return newState;
  364. }
  365. void AnimatedModel::RemoveAnimationState(Animation* animation)
  366. {
  367. if (animation)
  368. RemoveAnimationState(animation->GetNameHash());
  369. }
  370. void AnimatedModel::RemoveAnimationState(const String& animationName)
  371. {
  372. RemoveAnimationState(StringHash(animationName));
  373. }
  374. void AnimatedModel::RemoveAnimationState(StringHash animationNameHash)
  375. {
  376. for (Vector<SharedPtr<AnimationState> >::Iterator i = animationStates_.Begin(); i != animationStates_.End(); ++i)
  377. {
  378. AnimationState* state = *i;
  379. Animation* animation = state->GetAnimation();
  380. // Check both the animation and the resource name
  381. if (animation->GetNameHash() == animationNameHash || animation->GetAnimationNameHash() == animationNameHash)
  382. {
  383. animationStates_.Erase(i);
  384. MarkAnimationDirty();
  385. }
  386. }
  387. }
  388. void AnimatedModel::RemoveAnimationState(AnimationState* state)
  389. {
  390. for (Vector<SharedPtr<AnimationState> >::Iterator i = animationStates_.Begin(); i != animationStates_.End(); ++i)
  391. {
  392. if (*i == state)
  393. {
  394. animationStates_.Erase(i);
  395. MarkAnimationDirty();
  396. return;
  397. }
  398. }
  399. }
  400. void AnimatedModel::RemoveAllAnimationStates()
  401. {
  402. animationStates_.Clear();
  403. MarkAnimationDirty();
  404. }
  405. void AnimatedModel::SetAnimationLodBias(float bias)
  406. {
  407. animationLodBias_ = Max(bias, 0.0f);
  408. MarkNetworkUpdate();
  409. }
  410. void AnimatedModel::SetInvisibleLodFactor(float factor)
  411. {
  412. if (factor < 0.0f)
  413. factor = 0.0f;
  414. else if (factor != 0.0f && factor < 1.0f)
  415. factor = 1.0f;
  416. invisibleLodFactor_ = factor;
  417. MarkNetworkUpdate();
  418. }
  419. void AnimatedModel::SetMorphWeight(unsigned index, float weight)
  420. {
  421. if (index >= morphs_.Size())
  422. return;
  423. weight = Clamp(weight, 0.0f, 1.0f);
  424. if (weight != morphs_[index].weight_)
  425. {
  426. morphs_[index].weight_ = weight;
  427. // For a master model, set the same morph weight on non-master models
  428. if (isMaster_)
  429. {
  430. PODVector<AnimatedModel*> models;
  431. GetComponents<AnimatedModel>(models);
  432. // Indexing might not be the same, so use the name hash instead
  433. for (unsigned i = 1; i < models.Size(); ++i)
  434. {
  435. if (!models[i]->isMaster_)
  436. models[i]->SetMorphWeight(morphs_[index].nameHash_, weight);
  437. }
  438. }
  439. MarkMorphsDirty();
  440. MarkNetworkUpdate();
  441. }
  442. }
  443. void AnimatedModel::SetMorphWeight(const String& name, float weight)
  444. {
  445. for (unsigned i = 0; i < morphs_.Size(); ++i)
  446. {
  447. if (morphs_[i].name_ == name)
  448. {
  449. SetMorphWeight(i, weight);
  450. return;
  451. }
  452. }
  453. }
  454. void AnimatedModel::SetMorphWeight(StringHash nameHash, float weight)
  455. {
  456. for (unsigned i = 0; i < morphs_.Size(); ++i)
  457. {
  458. if (morphs_[i].nameHash_ == nameHash)
  459. {
  460. SetMorphWeight(i, weight);
  461. return;
  462. }
  463. }
  464. }
  465. void AnimatedModel::ResetMorphWeights()
  466. {
  467. for (Vector<ModelMorph>::Iterator i = morphs_.Begin(); i != morphs_.End(); ++i)
  468. i->weight_ = 0.0f;
  469. // For a master model, reset weights on non-master models
  470. if (isMaster_)
  471. {
  472. PODVector<AnimatedModel*> models;
  473. GetComponents<AnimatedModel>(models);
  474. // Indexing might not be the same, so use the name hash instead
  475. for (unsigned i = 1; i < models.Size(); ++i)
  476. {
  477. if (!models[i]->isMaster_)
  478. models[i]->ResetMorphWeights();
  479. }
  480. }
  481. MarkMorphsDirty();
  482. MarkNetworkUpdate();
  483. }
  484. float AnimatedModel::GetMorphWeight(unsigned index) const
  485. {
  486. return index < morphs_.Size() ? morphs_[index].weight_ : 0.0f;
  487. }
  488. float AnimatedModel::GetMorphWeight(const String& name) const
  489. {
  490. for (Vector<ModelMorph>::ConstIterator i = morphs_.Begin(); i != morphs_.End(); ++i)
  491. {
  492. if (i->name_ == name)
  493. return i->weight_;
  494. }
  495. return 0.0f;
  496. }
  497. float AnimatedModel::GetMorphWeight(StringHash nameHash) const
  498. {
  499. for (Vector<ModelMorph>::ConstIterator i = morphs_.Begin(); i != morphs_.End(); ++i)
  500. {
  501. if (i->nameHash_ == nameHash)
  502. return i->weight_;
  503. }
  504. return 0.0f;
  505. }
  506. AnimationState* AnimatedModel::GetAnimationState(Animation* animation) const
  507. {
  508. for (Vector<SharedPtr<AnimationState> >::ConstIterator i = animationStates_.Begin(); i != animationStates_.End(); ++i)
  509. {
  510. if ((*i)->GetAnimation() == animation)
  511. return *i;
  512. }
  513. return 0;
  514. }
  515. AnimationState* AnimatedModel::GetAnimationState(const String& animationName) const
  516. {
  517. for (Vector<SharedPtr<AnimationState> >::ConstIterator i = animationStates_.Begin(); i != animationStates_.End(); ++i)
  518. {
  519. Animation* animation = (*i)->GetAnimation();
  520. // Check both the animation and the resource name
  521. if (animation->GetName() == animationName || animation->GetAnimationName() == animationName)
  522. return *i;
  523. }
  524. return 0;
  525. }
  526. AnimationState* AnimatedModel::GetAnimationState(StringHash animationNameHash) const
  527. {
  528. for (Vector<SharedPtr<AnimationState> >::ConstIterator i = animationStates_.Begin(); i != animationStates_.End(); ++i)
  529. {
  530. Animation* animation = (*i)->GetAnimation();
  531. // Check both the animation and the resource name
  532. if (animation->GetNameHash() == animationNameHash || animation->GetAnimationNameHash() == animationNameHash)
  533. return *i;
  534. }
  535. return 0;
  536. }
  537. AnimationState* AnimatedModel::GetAnimationState(unsigned index) const
  538. {
  539. return index < animationStates_.Size() ? animationStates_[index].Get() : 0;
  540. }
  541. void AnimatedModel::SetSkeleton(const Skeleton& skeleton, bool createBones)
  542. {
  543. if (!node_ && createBones)
  544. {
  545. LOGERROR("AnimatedModel not attached to a scene node, can not create bone nodes");
  546. return;
  547. }
  548. if (isMaster_)
  549. {
  550. // Check if bone structure has stayed compatible (reloading the model.) In that case retain the old bones and animations
  551. if (skeleton_.GetNumBones() == skeleton.GetNumBones())
  552. {
  553. Vector<Bone>& destBones = skeleton_.GetModifiableBones();
  554. const Vector<Bone>& srcBones = skeleton.GetBones();
  555. bool compatible = true;
  556. for (unsigned i = 0; i < destBones.Size(); ++i)
  557. {
  558. if (destBones[i].node_ && destBones[i].name_ == srcBones[i].name_ && destBones[i].parentIndex_ ==
  559. srcBones[i].parentIndex_)
  560. {
  561. // If compatible, just copy the values and retain the old node and animated status
  562. Node* boneNode = destBones[i].node_;
  563. bool animated = destBones[i].animated_;
  564. destBones[i] = srcBones[i];
  565. destBones[i].node_ = boneNode;
  566. destBones[i].animated_ = animated;
  567. }
  568. else
  569. {
  570. compatible = false;
  571. break;
  572. }
  573. }
  574. if (compatible)
  575. return;
  576. }
  577. RemoveAllAnimationStates();
  578. // Detach the rootbone of the previous model if any
  579. if (createBones)
  580. {
  581. Bone* rootBone = skeleton_.GetRootBone();
  582. if (rootBone)
  583. node_->RemoveChild(rootBone->node_);
  584. }
  585. skeleton_.Define(skeleton);
  586. // Remove collision information from dummy bones that do not affect skinning, to prevent them from being merged
  587. // to the bounding box
  588. Vector<Bone>& bones = skeleton_.GetModifiableBones();
  589. for (Vector<Bone>::Iterator i = bones.Begin(); i != bones.End(); ++i)
  590. {
  591. if (i->collisionMask_ & BONECOLLISION_BOX && i->boundingBox_.Size().Length() < M_EPSILON)
  592. i->collisionMask_ &= ~BONECOLLISION_BOX;
  593. if (i->collisionMask_ & BONECOLLISION_SPHERE && i->radius_ < M_EPSILON)
  594. i->collisionMask_ &= ~BONECOLLISION_SPHERE;
  595. }
  596. // Create scene nodes for the bones
  597. if (createBones)
  598. {
  599. for (Vector<Bone>::Iterator i = bones.Begin(); i != bones.End(); ++i)
  600. {
  601. // Create bones as local, as they are never to be directly synchronized over the network
  602. Node* boneNode = node_->CreateChild(i->name_, LOCAL);
  603. boneNode->AddListener(this);
  604. boneNode->SetTransform(i->initialPosition_, i->initialRotation_, i->initialScale_);
  605. i->node_ = boneNode;
  606. }
  607. for (unsigned i = 0; i < bones.Size(); ++i)
  608. {
  609. unsigned parentIndex = bones[i].parentIndex_;
  610. if (parentIndex != i && parentIndex < bones.Size())
  611. bones[parentIndex].node_->AddChild(bones[i].node_);
  612. }
  613. }
  614. MarkAnimationDirty();
  615. using namespace BoneHierarchyCreated;
  616. VariantMap eventData;
  617. eventData[P_NODE] = (void*)node_;
  618. SendEvent(E_BONEHIERARCHYCREATED, eventData);
  619. }
  620. else
  621. {
  622. // For non-master models: use the bone nodes of the master model
  623. skeleton_.Define(skeleton);
  624. if (createBones)
  625. {
  626. Vector<Bone>& bones = skeleton_.GetModifiableBones();
  627. for (Vector<Bone>::Iterator i = bones.Begin(); i != bones.End(); ++i)
  628. {
  629. Node* boneNode = node_->GetChild(i->name_, true);
  630. if (boneNode)
  631. boneNode->AddListener(this);
  632. i->node_ = boneNode;
  633. }
  634. }
  635. }
  636. // Reserve space for skinning matrices
  637. skinMatrices_.Resize(skeleton_.GetNumBones());
  638. SetGeometryBoneMappings();
  639. assignBonesPending_ = !createBones;
  640. }
  641. void AnimatedModel::SetModelAttr(ResourceRef value)
  642. {
  643. ResourceCache* cache = GetSubsystem<ResourceCache>();
  644. // When loading a scene, set model without creating the bone nodes (will be assigned later during post-load)
  645. SetModel(cache->GetResource<Model>(value.id_), !loading_);
  646. }
  647. void AnimatedModel::SetBonesEnabledAttr(VariantVector value)
  648. {
  649. Vector<Bone>& bones = skeleton_.GetModifiableBones();
  650. for (unsigned i = 0; i < bones.Size() && i < value.Size(); ++i)
  651. bones[i].animated_ = value[i].GetBool();
  652. }
  653. void AnimatedModel::SetAnimationStatesAttr(VariantVector value)
  654. {
  655. ResourceCache* cache = GetSubsystem<ResourceCache>();
  656. RemoveAllAnimationStates();
  657. unsigned index = 0;
  658. while (index < value.Size())
  659. {
  660. const ResourceRef& animRef = value[index++].GetResourceRef();
  661. AnimationState* state = AddAnimationState(cache->GetResource<Animation>(animRef.id_));
  662. if (state)
  663. {
  664. state->SetStartBone(skeleton_.GetBone(value[index++].GetStringHash()));
  665. state->SetLooped(value[index++].GetBool());
  666. state->SetWeight(value[index++].GetFloat());
  667. state->SetTime(value[index++].GetFloat());
  668. state->SetLayer(value[index++].GetInt());
  669. }
  670. else
  671. index += 5;
  672. }
  673. }
  674. void AnimatedModel::SetMorphsAttr(const PODVector<unsigned char>& value)
  675. {
  676. unsigned index = 0;
  677. while (index < value.Size())
  678. SetMorphWeight(index, (float)value[index] / 255.0f);
  679. }
  680. ResourceRef AnimatedModel::GetModelAttr() const
  681. {
  682. return GetResourceRef(model_, Model::GetTypeStatic());
  683. }
  684. VariantVector AnimatedModel::GetBonesEnabledAttr() const
  685. {
  686. VariantVector ret;
  687. const Vector<Bone>& bones = skeleton_.GetBones();
  688. for (Vector<Bone>::ConstIterator i = bones.Begin(); i != bones.End(); ++i)
  689. ret.Push(i->animated_);
  690. return ret;
  691. }
  692. VariantVector AnimatedModel::GetAnimationStatesAttr() const
  693. {
  694. VariantVector ret;
  695. for (Vector<SharedPtr<AnimationState> >::ConstIterator i = animationStates_.Begin(); i != animationStates_.End(); ++i)
  696. {
  697. AnimationState* state = *i;
  698. Bone* startBone = state->GetStartBone();
  699. ret.Push(ResourceRef(Animation::GetTypeStatic(), state->GetAnimation()->GetNameHash()));
  700. ret.Push(startBone ? startBone->nameHash_ : StringHash());
  701. ret.Push(state->IsLooped());
  702. ret.Push(state->GetWeight());
  703. ret.Push(state->GetTime());
  704. ret.Push((int)state->GetLayer());
  705. }
  706. return ret;
  707. }
  708. const PODVector<unsigned char>& AnimatedModel::GetMorphsAttr() const
  709. {
  710. attrBuffer_.Clear();
  711. for (Vector<ModelMorph>::ConstIterator i = morphs_.Begin(); i != morphs_.End(); ++i)
  712. attrBuffer_.WriteUByte((unsigned char)(i->weight_ * 255.0f));
  713. return attrBuffer_.GetBuffer();
  714. }
  715. void AnimatedModel::OnNodeSet(Node* node)
  716. {
  717. Drawable::OnNodeSet(node);
  718. // If this AnimatedModel is the first in the node, it is the master which controls animation & morphs
  719. isMaster_ = GetComponent<AnimatedModel>() == this;
  720. }
  721. void AnimatedModel::OnMarkedDirty(Node* node)
  722. {
  723. Drawable::OnMarkedDirty(node);
  724. // If the scene node or any of the bone nodes move, mark skinning dirty
  725. skinningDirty_ = true;
  726. }
  727. void AnimatedModel::OnWorldBoundingBoxUpdate()
  728. {
  729. if (!skeleton_.GetNumBones())
  730. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  731. else
  732. {
  733. // If has bones, update world bounding box based on them
  734. worldBoundingBox_.defined_ = false;
  735. const Vector<Bone>& bones = skeleton_.GetBones();
  736. for (Vector<Bone>::ConstIterator i = bones.Begin(); i != bones.End(); ++i)
  737. {
  738. Node* boneNode = i->node_;
  739. if (!boneNode)
  740. continue;
  741. // Use hitbox if available. If not, use only half of the sphere radius
  742. if (i->collisionMask_ & BONECOLLISION_BOX)
  743. worldBoundingBox_.Merge(i->boundingBox_.Transformed(boneNode->GetWorldTransform()));
  744. else if (i->collisionMask_ & BONECOLLISION_SPHERE)
  745. worldBoundingBox_.Merge(Sphere(boneNode->GetWorldPosition(), i->radius_ * 0.5f));
  746. }
  747. }
  748. }
  749. void AnimatedModel::AssignBoneNodes()
  750. {
  751. if (!node_)
  752. return;
  753. // Find the bone nodes from the node hierarchy and add listeners
  754. Vector<Bone>& bones = skeleton_.GetModifiableBones();
  755. bool boneFound = false;
  756. for (Vector<Bone>::Iterator i = bones.Begin(); i != bones.End(); ++i)
  757. {
  758. Node* boneNode = node_->GetChild(i->name_, true);
  759. if (boneNode)
  760. {
  761. boneFound = true;
  762. boneNode->AddListener(this);
  763. }
  764. i->node_ = boneNode;
  765. }
  766. // If no bones found, this may be a prefab where the bone information was left out.
  767. // In that case reassign the skeleton now if possible
  768. if (!boneFound && model_)
  769. SetSkeleton(model_->GetSkeleton(), true);
  770. // Re-assign the same start bone to animations to get the proper bone node this time
  771. for (Vector<SharedPtr<AnimationState> >::Iterator i = animationStates_.Begin(); i != animationStates_.End(); ++i)
  772. {
  773. AnimationState* state = *i;
  774. state->SetStartBone(state->GetStartBone());
  775. }
  776. MarkAnimationDirty();
  777. }
  778. void AnimatedModel::MarkAnimationDirty()
  779. {
  780. if (isMaster_)
  781. {
  782. animationDirty_ = true;
  783. // Mark for pre-octree reinsertion update (threaded)
  784. MarkForUpdate();
  785. }
  786. }
  787. void AnimatedModel::MarkAnimationOrderDirty()
  788. {
  789. if (isMaster_)
  790. {
  791. animationOrderDirty_ = true;
  792. // Mark for pre-octree reinsertion update (threaded)
  793. MarkForUpdate();
  794. }
  795. }
  796. void AnimatedModel::MarkMorphsDirty()
  797. {
  798. morphsDirty_ = true;
  799. }
  800. void AnimatedModel::CloneGeometries()
  801. {
  802. // Clone vertex buffers as necessary
  803. const Vector<SharedPtr<VertexBuffer> >& originalVertexBuffers = model_->GetVertexBuffers();
  804. Map<VertexBuffer*, SharedPtr<VertexBuffer> > clonedVertexBuffers;
  805. morphVertexBuffers_.Resize(originalVertexBuffers.Size());
  806. for (unsigned i = 0; i < originalVertexBuffers.Size(); ++i)
  807. {
  808. VertexBuffer* original = originalVertexBuffers[i];
  809. if (model_->GetMorphRangeCount(i))
  810. {
  811. SharedPtr<VertexBuffer> clone(new VertexBuffer(context_));
  812. clone->SetSize(original->GetVertexCount(), original->GetElementMask(), true);
  813. clone->SetData(original->GetShadowData());
  814. clonedVertexBuffers[original] = clone;
  815. morphVertexBuffers_[i] = clone;
  816. }
  817. else
  818. morphVertexBuffers_[i].Reset();
  819. }
  820. // Geometries will always be cloned fully. They contain only references to buffer, so they are relatively light
  821. for (unsigned i = 0; i < geometries_.Size(); ++i)
  822. {
  823. for (unsigned j = 0; j < geometries_[i].Size(); ++j)
  824. {
  825. SharedPtr<Geometry> original = geometries_[i][j];
  826. const Vector<SharedPtr<VertexBuffer> >& originalBuffers = original->GetVertexBuffers();
  827. SharedPtr<Geometry> clone(new Geometry(context_));
  828. clone->SetNumVertexBuffers(originalBuffers.Size());
  829. for (unsigned k = 0; k < originalBuffers.Size(); ++k)
  830. {
  831. VertexBuffer* originalBuffer = originalBuffers[k];
  832. if (clonedVertexBuffers.Contains(originalBuffer))
  833. clone->SetVertexBuffer(k, clonedVertexBuffers[originalBuffer], original->GetVertexElementMask(k));
  834. else
  835. clone->SetVertexBuffer(k, originalBuffers[k], original->GetVertexElementMask(k));
  836. }
  837. clone->SetIndexBuffer(original->GetIndexBuffer());
  838. clone->SetDrawRange(original->GetPrimitiveType(), original->GetIndexStart(), original->GetIndexCount());
  839. clone->SetLodDistance(original->GetLodDistance());
  840. geometries_[i][j] = clone;
  841. }
  842. }
  843. }
  844. void AnimatedModel::SetGeometryBoneMappings()
  845. {
  846. geometrySkinMatrices_.Clear();
  847. geometrySkinMatrixPtrs_.Clear();
  848. if (!geometryBoneMappings_.Size())
  849. return;
  850. // Check if all mappings are empty, then we do not need to use mapped skinning
  851. bool allEmpty = true;
  852. for (unsigned i = 0; i < geometryBoneMappings_.Size(); ++i)
  853. if (geometryBoneMappings_[i].Size())
  854. allEmpty = false;
  855. if (allEmpty)
  856. return;
  857. // Reserve space for per-geometry skinning matrices
  858. geometrySkinMatrices_.Resize(geometryBoneMappings_.Size());
  859. for (unsigned i = 0; i < geometryBoneMappings_.Size(); ++i)
  860. geometrySkinMatrices_[i].Resize(geometryBoneMappings_[i].Size());
  861. // Build original-to-skinindex matrix pointer mapping for fast copying
  862. // Note: at this point layout of geometrySkinMatrices_ cannot be modified or pointers become invalid
  863. geometrySkinMatrixPtrs_.Resize(skeleton_.GetNumBones());
  864. for (unsigned i = 0; i < geometryBoneMappings_.Size(); ++i)
  865. {
  866. for (unsigned j = 0; j < geometryBoneMappings_[i].Size(); ++j)
  867. geometrySkinMatrixPtrs_[geometryBoneMappings_[i][j]].Push(&geometrySkinMatrices_[i][j]);
  868. }
  869. }
  870. void AnimatedModel::UpdateAnimation(const FrameInfo& frame)
  871. {
  872. // If using animation LOD, accumulate time and see if it is time to update
  873. if (animationLodBias_ > 0.0f && animationLodDistance_ > 0.0f)
  874. {
  875. // Check for first time update
  876. if (animationLodTimer_ >= 0.0f)
  877. {
  878. animationLodTimer_ += animationLodBias_ * frame.timeStep_ * frame.viewSize_.y_ * ANIMATION_LOD_BASESCALE;
  879. if (animationLodTimer_ >= animationLodDistance_)
  880. animationLodTimer_ = fmodf(animationLodTimer_, animationLodDistance_);
  881. else
  882. return;
  883. }
  884. else
  885. animationLodTimer_ = 0.0f;
  886. }
  887. // Make sure animations are in ascending priority order
  888. if (animationOrderDirty_)
  889. {
  890. Sort(animationStates_.Begin(), animationStates_.End(), CompareAnimationOrder);
  891. animationOrderDirty_ = false;
  892. }
  893. // Reset skeleton, then apply all animations
  894. skeleton_.Reset();
  895. for (Vector<SharedPtr<AnimationState> >::Iterator i = animationStates_.Begin(); i != animationStates_.End(); ++i)
  896. (*i)->Apply();
  897. // Animation has changed the bounding box: mark node for octree reinsertion
  898. Drawable::OnMarkedDirty(node_);
  899. // For optimization, recalculate world bounding box already here (during the threaded update)
  900. GetWorldBoundingBox();
  901. animationDirty_ = false;
  902. }
  903. void AnimatedModel::UpdateSkinning()
  904. {
  905. // Note: the model's world transform will be baked in the skin matrices
  906. const Vector<Bone>& bones = skeleton_.GetBones();
  907. // Use model's world transform in case a bone is missing
  908. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  909. // Skinning with global matrices only
  910. if (!geometrySkinMatrices_.Size())
  911. {
  912. for (unsigned i = 0; i < bones.Size(); ++i)
  913. {
  914. const Bone& bone = bones[i];
  915. if (bone.node_)
  916. skinMatrices_[i] = bone.node_->GetWorldTransform() * bone.offsetMatrix_;
  917. else
  918. skinMatrices_[i] = worldTransform;
  919. }
  920. }
  921. // Skinning with per-geometry matrices
  922. else
  923. {
  924. for (unsigned i = 0; i < bones.Size(); ++i)
  925. {
  926. const Bone& bone = bones[i];
  927. if (bone.node_)
  928. skinMatrices_[i] = bone.node_->GetWorldTransform() * bone.offsetMatrix_;
  929. else
  930. skinMatrices_[i] = worldTransform;
  931. // Copy the skin matrix to per-geometry matrices as needed
  932. for (unsigned j = 0; j < geometrySkinMatrixPtrs_[i].Size(); ++j)
  933. *geometrySkinMatrixPtrs_[i][j] = skinMatrices_[i];
  934. }
  935. }
  936. skinningDirty_ = false;
  937. }
  938. void AnimatedModel::UpdateMorphs()
  939. {
  940. Graphics* graphics = GetSubsystem<Graphics>();
  941. if (!graphics)
  942. return;
  943. if (morphs_.Size())
  944. {
  945. // Reset the morph data range from all morphable vertex buffers, then apply morphs
  946. for (unsigned i = 0; i < morphVertexBuffers_.Size(); ++i)
  947. {
  948. VertexBuffer* buffer = morphVertexBuffers_[i];
  949. if (buffer)
  950. {
  951. VertexBuffer* originalBuffer = model_->GetVertexBuffers()[i];
  952. unsigned morphStart = model_->GetMorphRangeStart(i);
  953. unsigned morphCount = model_->GetMorphRangeCount(i);
  954. unsigned vertexSize = buffer->GetVertexSize();
  955. if (!buffer->IsDataLost())
  956. {
  957. void* dest = buffer->Lock(morphStart, morphCount);
  958. if (dest)
  959. {
  960. // Reset morph range by copying data from the original vertex buffer
  961. memcpy(dest, originalBuffer->GetShadowData() + morphStart * vertexSize, morphCount * vertexSize);
  962. for (unsigned j = 0; j < morphs_.Size(); ++j)
  963. {
  964. if (morphs_[j].weight_ > 0.0f)
  965. {
  966. Map<unsigned, VertexBufferMorph>::Iterator k = morphs_[j].buffers_.Find(i);
  967. if (k != morphs_[j].buffers_.End())
  968. ApplyMorph(buffer, dest, morphStart, k->second_, morphs_[j].weight_);
  969. }
  970. }
  971. buffer->Unlock();
  972. }
  973. }
  974. else
  975. {
  976. // Data is lost, need to copy whole original buffer
  977. unsigned vertexCount = buffer->GetVertexCount();
  978. void* dest = buffer->Lock(0, vertexCount, true);
  979. if (dest)
  980. {
  981. memcpy(dest, originalBuffer->GetShadowData(), vertexCount * vertexSize);
  982. dest = ((unsigned char*)dest) + morphStart * vertexSize;
  983. for (unsigned j = 0; j < morphs_.Size(); ++j)
  984. {
  985. if (morphs_[j].weight_ > 0.0f)
  986. {
  987. Map<unsigned, VertexBufferMorph>::Iterator k = morphs_[j].buffers_.Find(i);
  988. if (k != morphs_[j].buffers_.End())
  989. ApplyMorph(buffer, dest, morphStart, k->second_, morphs_[j].weight_);
  990. }
  991. }
  992. buffer->Unlock();
  993. }
  994. buffer->ClearDataLost();
  995. }
  996. }
  997. }
  998. }
  999. morphsDirty_ = false;
  1000. }
  1001. void AnimatedModel::ApplyMorph(VertexBuffer* buffer, void* destVertexData, unsigned morphRangeStart, const VertexBufferMorph& morph, float weight)
  1002. {
  1003. unsigned elementMask = morph.elementMask_;
  1004. unsigned vertexCount = morph.vertexCount_;
  1005. unsigned normalOffset = buffer->GetElementOffset(ELEMENT_NORMAL);
  1006. unsigned tangentOffset = buffer->GetElementOffset(ELEMENT_TANGENT);
  1007. unsigned vertexSize = buffer->GetVertexSize();
  1008. unsigned char* srcData = morph.morphData_;
  1009. unsigned char* destData = (unsigned char*)destVertexData;
  1010. while (vertexCount--)
  1011. {
  1012. unsigned vertexIndex = *((unsigned*)srcData) - morphRangeStart;
  1013. srcData += sizeof(unsigned);
  1014. if (elementMask & MASK_POSITION)
  1015. {
  1016. float* dest = (float*)(destData + vertexIndex * vertexSize);
  1017. float* src = (float*)srcData;
  1018. dest[0] += src[0] * weight;
  1019. dest[1] += src[1] * weight;
  1020. dest[2] += src[2] * weight;
  1021. srcData += 3 * sizeof(float);
  1022. }
  1023. if (elementMask & MASK_NORMAL)
  1024. {
  1025. float* dest = (float*)(destData + vertexIndex * vertexSize + normalOffset);
  1026. float* src = (float*)srcData;
  1027. dest[0] += src[0] * weight;
  1028. dest[1] += src[1] * weight;
  1029. dest[2] += src[2] * weight;
  1030. srcData += 3 * sizeof(float);
  1031. }
  1032. if (elementMask & MASK_TANGENT)
  1033. {
  1034. float* dest = (float*)(destData + vertexIndex * vertexSize + tangentOffset);
  1035. float* src = (float*)srcData;
  1036. dest[0] += src[0] * weight;
  1037. dest[1] += src[1] * weight;
  1038. dest[2] += src[2] * weight;
  1039. srcData += 3 * sizeof(float);
  1040. }
  1041. }
  1042. }
  1043. void AnimatedModel::HandleModelReloadFinished(StringHash eventType, VariantMap& eventData)
  1044. {
  1045. Model* currentModel = model_;
  1046. model_ = 0; // Set null to allow to be re-set
  1047. SetModel(currentModel);
  1048. }