BsAnimation.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Animation/BsAnimation.h"
  4. #include "Animation/BsAnimationManager.h"
  5. #include "Animation/BsAnimationClip.h"
  6. #include "Animation/BsAnimationUtility.h"
  7. #include "Scene/BsSceneObject.h"
  8. #include "Animation/BsMorphShapes.h"
  9. namespace bs
  10. {
  11. AnimationClipInfo::AnimationClipInfo()
  12. : playbackType(AnimPlaybackType::Normal), fadeDirection(0.0f), fadeTime(0.0f), fadeLength(0.0f), curveVersion(0)
  13. , layerIdx((UINT32)-1), stateIdx((UINT32)-1)
  14. { }
  15. AnimationClipInfo::AnimationClipInfo(const HAnimationClip& clip)
  16. : clip(clip), playbackType(AnimPlaybackType::Normal), fadeDirection(0.0f), fadeTime(0.0f), fadeLength(0.0f)
  17. , curveVersion(0), layerIdx((UINT32)-1), stateIdx((UINT32)-1)
  18. { }
  19. AnimationProxy::AnimationProxy(UINT64 id)
  20. : id(id), layers(nullptr), numLayers(0), numSceneObjects(0), sceneObjectInfos(nullptr)
  21. , sceneObjectTransforms(nullptr), morphChannelInfos(nullptr), morphShapeInfos(nullptr), numMorphChannels(0)
  22. , numMorphShapes(0), numMorphVertices(0), morphChannelWeightsDirty(false), mCullEnabled(true), numGenericCurves(0)
  23. , genericCurveOutputs(nullptr)
  24. { }
  25. AnimationProxy::~AnimationProxy()
  26. {
  27. clear();
  28. }
  29. void AnimationProxy::clear()
  30. {
  31. if (layers == nullptr)
  32. return;
  33. for(UINT32 i = 0; i < numLayers; i++)
  34. {
  35. AnimationStateLayer& layer = layers[i];
  36. for(UINT32 j = 0; j < layer.numStates; j++)
  37. {
  38. AnimationState& state = layer.states[j];
  39. if(state.curves != nullptr)
  40. {
  41. {
  42. UINT32 numCurves = (UINT32)state.curves->position.size();
  43. for (UINT32 k = 0; k < numCurves; k++)
  44. state.positionCaches[k].~TCurveCache();
  45. }
  46. {
  47. UINT32 numCurves = (UINT32)state.curves->rotation.size();
  48. for (UINT32 k = 0; k < numCurves; k++)
  49. state.rotationCaches[k].~TCurveCache();
  50. }
  51. {
  52. UINT32 numCurves = (UINT32)state.curves->scale.size();
  53. for (UINT32 k = 0; k < numCurves; k++)
  54. state.scaleCaches[k].~TCurveCache();
  55. }
  56. {
  57. UINT32 numCurves = (UINT32)state.curves->generic.size();
  58. for (UINT32 k = 0; k < numCurves; k++)
  59. state.genericCaches[k].~TCurveCache();
  60. }
  61. }
  62. if(skeleton != nullptr)
  63. {
  64. UINT32 numBones = skeleton->getNumBones();
  65. for (UINT32 k = 0; k < numBones; k++)
  66. state.boneToCurveMapping[k].~AnimationCurveMapping();
  67. }
  68. if(state.soToCurveMapping != nullptr)
  69. {
  70. for(UINT32 k = 0; k < numSceneObjects; k++)
  71. state.soToCurveMapping[k].~AnimationCurveMapping();
  72. }
  73. state.~AnimationState();
  74. }
  75. layer.~AnimationStateLayer();
  76. }
  77. for(UINT32 i = 0; i < numMorphShapes; i++)
  78. {
  79. morphShapeInfos[i].shape.~SPtr<MorphShape>();
  80. }
  81. // All of the memory is part of the same buffer, so we only need to free the first element
  82. bs_free(layers);
  83. layers = nullptr;
  84. genericCurveOutputs = nullptr;
  85. sceneObjectInfos = nullptr;
  86. sceneObjectTransforms = nullptr;
  87. numLayers = 0;
  88. numGenericCurves = 0;
  89. }
  90. void AnimationProxy::rebuild(const SPtr<Skeleton>& skeleton, const SkeletonMask& mask,
  91. Vector<AnimationClipInfo>& clipInfos, const Vector<AnimatedSceneObject>& sceneObjects,
  92. const SPtr<MorphShapes>& morphShapes)
  93. {
  94. this->skeleton = skeleton;
  95. this->skeletonMask = mask;
  96. // Note: I could avoid having a separate allocation for LocalSkeletonPoses and use the same buffer as the rest
  97. // of AnimationProxy
  98. if (skeleton != nullptr)
  99. skeletonPose = LocalSkeletonPose(skeleton->getNumBones());
  100. numSceneObjects = (UINT32)sceneObjects.size();
  101. if (numSceneObjects > 0)
  102. sceneObjectPose = LocalSkeletonPose(numSceneObjects);
  103. else
  104. sceneObjectPose = LocalSkeletonPose();
  105. rebuild(clipInfos, sceneObjects, morphShapes);
  106. }
  107. void AnimationProxy::rebuild(Vector<AnimationClipInfo>& clipInfos, const Vector<AnimatedSceneObject>& sceneObjects,
  108. const SPtr<MorphShapes>& morphShapes)
  109. {
  110. clear();
  111. bs_frame_mark();
  112. {
  113. FrameVector<bool> clipLoadState(clipInfos.size());
  114. FrameVector<AnimationStateLayer> tempLayers;
  115. UINT32 clipIdx = 0;
  116. for (auto& clipInfo : clipInfos)
  117. {
  118. UINT32 layer = clipInfo.state.layer;
  119. if (layer == (UINT32)-1)
  120. layer = 0;
  121. else
  122. layer += 1;
  123. auto iterFind = std::find_if(tempLayers.begin(), tempLayers.end(),
  124. [&](auto& x)
  125. {
  126. return x.index == layer;
  127. });
  128. bool isLoaded = clipInfo.clip.isLoaded();
  129. clipLoadState[clipIdx] = isLoaded;
  130. if (iterFind == tempLayers.end())
  131. {
  132. tempLayers.push_back(AnimationStateLayer());
  133. AnimationStateLayer& newLayer = tempLayers.back();
  134. newLayer.index = layer;
  135. newLayer.additive = isLoaded && clipInfo.clip->isAdditive();
  136. }
  137. clipIdx++;
  138. }
  139. std::sort(tempLayers.begin(), tempLayers.end(),
  140. [&](auto& x, auto& y)
  141. {
  142. return x.index < y.index;
  143. });
  144. numLayers = (UINT32)tempLayers.size();
  145. UINT32 numClips = (UINT32)clipInfos.size();
  146. UINT32 numBones;
  147. if (skeleton != nullptr)
  148. numBones = skeleton->getNumBones();
  149. else
  150. numBones = 0;
  151. UINT32 numPosCurves = 0;
  152. UINT32 numRotCurves = 0;
  153. UINT32 numScaleCurves = 0;
  154. clipIdx = 0;
  155. for (auto& clipInfo : clipInfos)
  156. {
  157. bool isLoaded = clipLoadState[clipIdx++];
  158. if (!isLoaded)
  159. continue;
  160. SPtr<AnimationCurves> curves = clipInfo.clip->getCurves();
  161. numPosCurves += (UINT32)curves->position.size();
  162. numRotCurves += (UINT32)curves->rotation.size();
  163. numScaleCurves += (UINT32)curves->scale.size();
  164. }
  165. numGenericCurves = 0;
  166. if(clipInfos.size() > 0 && clipLoadState[0])
  167. {
  168. SPtr<AnimationCurves> curves = clipInfos[0].clip->getCurves();
  169. numGenericCurves = (UINT32)curves->generic.size();
  170. }
  171. UINT32* mappedBoneIndices = (UINT32*)bs_frame_alloc(sizeof(UINT32) * numSceneObjects);
  172. for (UINT32 i = 0; i < numSceneObjects; i++)
  173. mappedBoneIndices[i] = -1;
  174. UINT32 numBoneMappedSOs = 0;
  175. if (skeleton != nullptr)
  176. {
  177. for (UINT32 i = 0; i < numSceneObjects; i++)
  178. {
  179. if (sceneObjects[i].so.isDestroyed(true))
  180. continue;
  181. // Empty string always means root bone
  182. if (sceneObjects[i].curveName.empty())
  183. {
  184. UINT32 rootBoneIdx = skeleton->getRootBoneIndex();
  185. if (rootBoneIdx != (UINT32)-1)
  186. {
  187. mappedBoneIndices[i] = rootBoneIdx;
  188. numBoneMappedSOs++;
  189. }
  190. }
  191. else
  192. {
  193. for (UINT32 j = 0; j < numBones; j++)
  194. {
  195. if (skeleton->getBoneInfo(j).name == sceneObjects[i].curveName)
  196. {
  197. mappedBoneIndices[i] = j;
  198. numBoneMappedSOs++;
  199. break;
  200. }
  201. }
  202. }
  203. }
  204. }
  205. if (morphShapes != nullptr)
  206. {
  207. numMorphChannels = morphShapes->getNumChannels();
  208. numMorphVertices = morphShapes->getNumVertices();
  209. numMorphShapes = 0;
  210. for (UINT32 i = 0; i < numMorphChannels; i++)
  211. numMorphShapes += morphShapes->getChannel(i)->getNumShapes();
  212. }
  213. else
  214. {
  215. numMorphChannels = 0;
  216. numMorphShapes = 0;
  217. numMorphVertices = 0;
  218. }
  219. UINT32 numBoneMappings = numBones * numClips;
  220. UINT32 layersSize = sizeof(AnimationStateLayer) * numLayers;
  221. UINT32 clipsSize = sizeof(AnimationState) * numClips;
  222. UINT32 boneMappingSize = numBoneMappings * sizeof(AnimationCurveMapping);
  223. UINT32 posCacheSize = numPosCurves * sizeof(TCurveCache<Vector3>);
  224. UINT32 rotCacheSize = numRotCurves * sizeof(TCurveCache<Quaternion>);
  225. UINT32 scaleCacheSize = numScaleCurves * sizeof(TCurveCache<Vector3>);
  226. UINT32 genCacheSize = numGenericCurves * sizeof(TCurveCache<float>);
  227. UINT32 genericCurveOutputSize = numGenericCurves * sizeof(float);
  228. UINT32 sceneObjectIdsSize = numSceneObjects * sizeof(AnimatedSceneObjectInfo);
  229. UINT32 sceneObjectTransformsSize = numBoneMappedSOs * sizeof(Matrix4);
  230. UINT32 morphChannelSize = numMorphChannels * sizeof(MorphChannelInfo);
  231. UINT32 morphShapeSize = numMorphShapes * sizeof(MorphShapeInfo);
  232. UINT8* data = (UINT8*)bs_alloc(layersSize + clipsSize + boneMappingSize + posCacheSize + rotCacheSize +
  233. scaleCacheSize + genCacheSize + genericCurveOutputSize + sceneObjectIdsSize + sceneObjectTransformsSize +
  234. morphChannelSize + morphShapeSize);
  235. layers = (AnimationStateLayer*)data;
  236. memcpy(layers, tempLayers.data(), layersSize);
  237. data += layersSize;
  238. AnimationState* states = (AnimationState*)data;
  239. for(UINT32 i = 0; i < numClips; i++)
  240. new (&states[i]) AnimationState();
  241. data += clipsSize;
  242. AnimationCurveMapping* boneMappings = (AnimationCurveMapping*)data;
  243. for (UINT32 i = 0; i < numBoneMappings; i++)
  244. new (&boneMappings[i]) AnimationCurveMapping();
  245. data += boneMappingSize;
  246. TCurveCache<Vector3>* posCache = (TCurveCache<Vector3>*)data;
  247. for (UINT32 i = 0; i < numPosCurves; i++)
  248. new (&posCache[i]) TCurveCache<Vector3>();
  249. data += posCacheSize;
  250. TCurveCache<Quaternion>* rotCache = (TCurveCache<Quaternion>*)data;
  251. for (UINT32 i = 0; i < numRotCurves; i++)
  252. new (&rotCache[i]) TCurveCache<Quaternion>();
  253. data += rotCacheSize;
  254. TCurveCache<Vector3>* scaleCache = (TCurveCache<Vector3>*)data;
  255. for (UINT32 i = 0; i < numScaleCurves; i++)
  256. new (&scaleCache[i]) TCurveCache<Vector3>();
  257. data += scaleCacheSize;
  258. TCurveCache<float>* genCache = (TCurveCache<float>*)data;
  259. for (UINT32 i = 0; i < numGenericCurves; i++)
  260. new (&genCache[i]) TCurveCache<float>();
  261. data += genCacheSize;
  262. genericCurveOutputs = (float*)data;
  263. data += genericCurveOutputSize;
  264. sceneObjectInfos = (AnimatedSceneObjectInfo*)data;
  265. data += sceneObjectIdsSize;
  266. sceneObjectTransforms = (Matrix4*)data;
  267. for (UINT32 i = 0; i < numBoneMappedSOs; i++)
  268. sceneObjectTransforms[i] = Matrix4::IDENTITY;
  269. data += sceneObjectTransformsSize;
  270. morphChannelInfos = (MorphChannelInfo*)data;
  271. data += morphChannelSize;
  272. morphShapeInfos = (MorphShapeInfo*)data;
  273. data += morphShapeSize;
  274. // Generate data required for morph shape animation
  275. if (morphShapes != nullptr)
  276. {
  277. UINT32 currentShapeIdx = 0;
  278. for (UINT32 i = 0; i < numMorphChannels; i++)
  279. {
  280. SPtr<MorphChannel> morphChannel = morphShapes->getChannel(i);
  281. UINT32 numShapes = morphChannel->getNumShapes();
  282. MorphChannelInfo& channelInfo = morphChannelInfos[i];
  283. channelInfo.weight = 0.0f;
  284. channelInfo.shapeStart = currentShapeIdx;
  285. channelInfo.shapeCount = numShapes;
  286. channelInfo.frameCurveIdx = (UINT32)-1;
  287. channelInfo.weightCurveIdx = (UINT32)-1;
  288. for (UINT32 j = 0; j < numShapes; j++)
  289. {
  290. MorphShapeInfo& shapeInfo = morphShapeInfos[currentShapeIdx];
  291. new (&shapeInfo.shape) SPtr<MorphShape>();
  292. SPtr<MorphShape> shape = morphChannel->getShape(j);
  293. shapeInfo.shape = shape;
  294. shapeInfo.frameWeight = shape->getWeight();
  295. shapeInfo.finalWeight = 0.0f;
  296. currentShapeIdx++;
  297. }
  298. }
  299. // Find any curves affecting morph shape animation
  300. if (!clipInfos.empty())
  301. {
  302. bool isClipValid = clipLoadState[0];
  303. if (isClipValid)
  304. {
  305. AnimationClipInfo& clipInfo = clipInfos[0];
  306. for (UINT32 i = 0; i < numMorphChannels; i++)
  307. {
  308. SPtr<MorphChannel> morphChannel = morphShapes->getChannel(i);
  309. MorphChannelInfo& channelInfo = morphChannelInfos[i];
  310. clipInfo.clip->getMorphMapping(morphChannel->getName(), channelInfo.frameCurveIdx,
  311. channelInfo.weightCurveIdx);
  312. }
  313. }
  314. }
  315. morphChannelWeightsDirty = true;
  316. }
  317. UINT32 curLayerIdx = 0;
  318. UINT32 curStateIdx = 0;
  319. // Note: Hidden dependency. First clip info must be in layers[0].states[0] (needed for generic curves which only
  320. // use the primary clip).
  321. for(UINT32 i = 0; i < numLayers; i++)
  322. {
  323. AnimationStateLayer& layer = layers[i];
  324. layer.states = &states[curStateIdx];
  325. layer.numStates = 0;
  326. UINT32 localStateIdx = 0;
  327. for(UINT32 j = 0; j < (UINT32)clipInfos.size(); j++)
  328. {
  329. AnimationClipInfo& clipInfo = clipInfos[j];
  330. UINT32 clipLayer = clipInfo.state.layer;
  331. if (clipLayer == (UINT32)-1)
  332. clipLayer = 0;
  333. else
  334. clipLayer += 1;
  335. if (clipLayer != layer.index)
  336. continue;
  337. AnimationState& state = states[curStateIdx];
  338. state.loop = clipInfo.state.wrapMode == AnimWrapMode::Loop;
  339. state.time = clipInfo.state.time;
  340. // Calculate weight if fading is active
  341. float weight = clipInfo.state.weight;
  342. //// Assumes time is clamped to [0, fadeLength] and fadeLength != 0
  343. if(clipInfo.fadeDirection < 0.0f)
  344. {
  345. float t = clipInfo.fadeTime / clipInfo.fadeLength;
  346. weight *= (1.0f - t);
  347. }
  348. else if(clipInfo.fadeDirection > 0.0f)
  349. {
  350. float t = clipInfo.fadeTime / clipInfo.fadeLength;
  351. weight *= t;
  352. }
  353. state.weight = weight;
  354. // Set up individual curves and their caches
  355. bool isClipValid = clipLoadState[j];
  356. if (isClipValid)
  357. {
  358. state.curves = clipInfo.clip->getCurves();
  359. state.disabled = clipInfo.playbackType == AnimPlaybackType::None;
  360. }
  361. else
  362. {
  363. static SPtr<AnimationCurves> zeroCurves = bs_shared_ptr_new<AnimationCurves>();
  364. state.curves = zeroCurves;
  365. state.disabled = true;
  366. }
  367. state.positionCaches = posCache;
  368. posCache += state.curves->position.size();
  369. state.rotationCaches = rotCache;
  370. rotCache += state.curves->rotation.size();
  371. state.scaleCaches = scaleCache;
  372. scaleCache += state.curves->scale.size();
  373. state.genericCaches = genCache;
  374. genCache += state.curves->generic.size();
  375. clipInfo.layerIdx = curLayerIdx;
  376. clipInfo.stateIdx = localStateIdx;
  377. if(isClipValid)
  378. clipInfo.curveVersion = clipInfo.clip->getVersion();
  379. // Set up bone mapping
  380. if (skeleton != nullptr)
  381. {
  382. state.boneToCurveMapping = &boneMappings[curStateIdx * numBones];
  383. if (isClipValid)
  384. {
  385. clipInfo.clip->getBoneMapping(*skeleton, state.boneToCurveMapping);
  386. }
  387. else
  388. {
  389. AnimationCurveMapping emptyMapping = { (UINT32)-1, (UINT32)-1, (UINT32)-1 };
  390. for (UINT32 i = 0; i < numBones; i++)
  391. state.boneToCurveMapping[i] = emptyMapping;
  392. }
  393. }
  394. else
  395. state.boneToCurveMapping = nullptr;
  396. layer.numStates++;
  397. curStateIdx++;
  398. localStateIdx++;
  399. }
  400. curLayerIdx++;
  401. // Must be larger than zero otherwise the layer.states pointer will point to data held by some other layer
  402. assert(layer.numStates > 0);
  403. }
  404. Matrix4 invRootTransform(BsIdentity);
  405. for (UINT32 i = 0; i < numSceneObjects; i++)
  406. {
  407. if(sceneObjects[i].curveName.empty())
  408. {
  409. HSceneObject so = sceneObjects[i].so;
  410. if (!so.isDestroyed(true))
  411. invRootTransform = so->getWorldMatrix().inverseAffine();
  412. break;
  413. }
  414. }
  415. UINT32 boneIdx = 0;
  416. for(UINT32 i = 0; i < numSceneObjects; i++)
  417. {
  418. HSceneObject so = sceneObjects[i].so;
  419. AnimatedSceneObjectInfo& soInfo = sceneObjectInfos[i];
  420. soInfo.id = so.getInstanceId();
  421. soInfo.boneIdx = mappedBoneIndices[i];
  422. bool isSOValid = !so.isDestroyed(true);
  423. if (isSOValid)
  424. soInfo.hash = so->getTransformHash();
  425. else
  426. soInfo.hash = 0;
  427. soInfo.layerIdx = (UINT32)-1;
  428. soInfo.stateIdx = (UINT32)-1;
  429. // If no bone mapping, find curves directly
  430. if(soInfo.boneIdx == -1)
  431. {
  432. soInfo.curveIndices = { (UINT32)-1, (UINT32)-1, (UINT32)-1 };
  433. if (isSOValid)
  434. {
  435. for (UINT32 j = 0; j < (UINT32)clipInfos.size(); j++)
  436. {
  437. AnimationClipInfo& clipInfo = clipInfos[j];
  438. soInfo.layerIdx = clipInfo.layerIdx;
  439. soInfo.stateIdx = clipInfo.stateIdx;
  440. bool isClipValid = clipLoadState[j];
  441. if (isClipValid)
  442. {
  443. // Note: If there are multiple clips with the relevant curve name, we only use the first
  444. clipInfo.clip->getCurveMapping(sceneObjects[i].curveName, soInfo.curveIndices);
  445. break;
  446. }
  447. }
  448. }
  449. }
  450. else
  451. {
  452. // No need to check if SO is valid, if it has a bone connection it must be
  453. sceneObjectTransforms[boneIdx] = so->getWorldMatrix() * invRootTransform;
  454. boneIdx++;
  455. }
  456. }
  457. bs_frame_free(mappedBoneIndices);
  458. }
  459. bs_frame_clear();
  460. }
  461. void AnimationProxy::updateClipInfos(const Vector<AnimationClipInfo>& clipInfos)
  462. {
  463. for(auto& clipInfo : clipInfos)
  464. {
  465. AnimationState& state = layers[clipInfo.layerIdx].states[clipInfo.stateIdx];
  466. state.loop = clipInfo.state.wrapMode == AnimWrapMode::Loop;
  467. state.weight = clipInfo.state.weight;
  468. state.time = clipInfo.state.time;
  469. bool isLoaded = clipInfo.clip.isLoaded();
  470. state.disabled = !isLoaded || clipInfo.playbackType == AnimPlaybackType::None;
  471. }
  472. }
  473. void AnimationProxy::updateMorphChannelWeights(const Vector<float>& weights)
  474. {
  475. UINT32 numWeights = (UINT32)weights.size();
  476. for(UINT32 i = 0; i < numMorphChannels; i++)
  477. {
  478. if (i < numWeights)
  479. morphChannelInfos[i].weight = weights[i];
  480. else
  481. morphChannelInfos[i].weight = 0.0f;
  482. }
  483. morphChannelWeightsDirty = true;
  484. }
  485. void AnimationProxy::updateTransforms(const Vector<AnimatedSceneObject>& sceneObjects)
  486. {
  487. Matrix4 invRootTransform(BsIdentity);
  488. for (UINT32 i = 0; i < numSceneObjects; i++)
  489. {
  490. if (sceneObjects[i].curveName.empty())
  491. {
  492. HSceneObject so = sceneObjects[i].so;
  493. if (!so.isDestroyed(true))
  494. invRootTransform = so->getWorldMatrix().inverseAffine();
  495. break;
  496. }
  497. }
  498. UINT32 boneIdx = 0;
  499. for (UINT32 i = 0; i < numSceneObjects; i++)
  500. {
  501. HSceneObject so = sceneObjects[i].so;
  502. if (so.isDestroyed(true))
  503. {
  504. sceneObjectInfos[i].hash = 0;
  505. continue;
  506. }
  507. sceneObjectInfos[i].hash = so->getTransformHash();
  508. if (sceneObjectInfos[i].boneIdx == -1)
  509. continue;
  510. sceneObjectTransforms[boneIdx] = sceneObjects[i].so->getWorldMatrix() * invRootTransform;
  511. boneIdx++;
  512. }
  513. }
  514. void AnimationProxy::updateTime(const Vector<AnimationClipInfo>& clipInfos)
  515. {
  516. for (auto& clipInfo : clipInfos)
  517. {
  518. AnimationState& state = layers[clipInfo.layerIdx].states[clipInfo.stateIdx];
  519. state.time = clipInfo.state.time;
  520. bool isLoaded = clipInfo.clip.isLoaded();
  521. state.disabled = !isLoaded || clipInfo.playbackType == AnimPlaybackType::None;
  522. }
  523. }
  524. Animation::Animation()
  525. : mDefaultWrapMode(AnimWrapMode::Loop), mDefaultSpeed(1.0f), mCull(true), mDirty(AnimDirtyStateFlag::All)
  526. , mGenericCurveValuesValid(false)
  527. {
  528. mId = AnimationManager::instance().registerAnimation(this);
  529. mAnimProxy = bs_shared_ptr_new<AnimationProxy>(mId);
  530. }
  531. Animation::~Animation()
  532. {
  533. AnimationManager::instance().unregisterAnimation(mId);
  534. }
  535. void Animation::setSkeleton(const SPtr<Skeleton>& skeleton)
  536. {
  537. mSkeleton = skeleton;
  538. mDirty |= AnimDirtyStateFlag::All;
  539. }
  540. void Animation::setMorphShapes(const SPtr<MorphShapes>& morphShapes)
  541. {
  542. mMorphShapes = morphShapes;
  543. UINT32 numChannels;
  544. if (mMorphShapes != nullptr)
  545. numChannels = mMorphShapes->getNumChannels();
  546. else
  547. numChannels = 0;
  548. mMorphChannelWeights.assign(numChannels, 0.0f);
  549. if (numChannels > 0)
  550. mMorphChannelWeights[0] = 1.0f;
  551. mDirty |= AnimDirtyStateFlag::Layout;
  552. mDirty |= AnimDirtyStateFlag::MorphWeights;
  553. }
  554. void Animation::setMorphChannelWeight(UINT32 idx, float weight)
  555. {
  556. UINT32 numShapes = (UINT32)mMorphChannelWeights.size();
  557. if (idx >= numShapes)
  558. return;
  559. mMorphChannelWeights[idx] = weight;
  560. mDirty |= AnimDirtyStateFlag::MorphWeights;
  561. }
  562. void Animation::setMask(const SkeletonMask& mask)
  563. {
  564. mSkeletonMask = mask;
  565. mDirty |= AnimDirtyStateFlag::All;
  566. }
  567. void Animation::setWrapMode(AnimWrapMode wrapMode)
  568. {
  569. mDefaultWrapMode = wrapMode;
  570. for (auto& clipInfo : mClipInfos)
  571. clipInfo.state.wrapMode = wrapMode;
  572. mDirty |= AnimDirtyStateFlag::Value;
  573. }
  574. void Animation::setSpeed(float speed)
  575. {
  576. mDefaultSpeed = speed;
  577. for (auto& clipInfo : mClipInfos)
  578. {
  579. // Special case: Ignore non-moving ones
  580. if(!clipInfo.state.stopped)
  581. clipInfo.state.speed = speed;
  582. }
  583. mDirty |= AnimDirtyStateFlag::Value;
  584. }
  585. void Animation::setBounds(const AABox& bounds)
  586. {
  587. mBounds = bounds;
  588. mDirty |= AnimDirtyStateFlag::Culling;
  589. }
  590. void Animation::setCulling(bool cull)
  591. {
  592. mCull = cull;
  593. mDirty |= AnimDirtyStateFlag::Culling;
  594. }
  595. void Animation::play(const HAnimationClip& clip)
  596. {
  597. AnimationClipInfo* clipInfo = addClip(clip, (UINT32)-1);
  598. if(clipInfo != nullptr)
  599. {
  600. clipInfo->state.time = 0.0f;
  601. clipInfo->state.speed = mDefaultSpeed;
  602. clipInfo->state.weight = 1.0f;
  603. clipInfo->state.wrapMode = mDefaultWrapMode;
  604. clipInfo->playbackType = AnimPlaybackType::Normal;
  605. }
  606. mDirty |= AnimDirtyStateFlag::Value;
  607. }
  608. void Animation::blendAdditive(const HAnimationClip& clip, float weight, float fadeLength, UINT32 layer)
  609. {
  610. if(clip != nullptr && !clip->isAdditive())
  611. {
  612. LOGWRN("blendAdditive() called with a clip that doesn't contain additive animation. Ignoring.");
  613. // Stop any clips on this layer, even if invalid
  614. HAnimationClip nullClip;
  615. addClip(nullClip, layer);
  616. return;
  617. }
  618. AnimationClipInfo* clipInfo = addClip(clip, layer);
  619. if (clipInfo != nullptr)
  620. {
  621. clipInfo->state.time = 0.0f;
  622. clipInfo->state.speed = mDefaultSpeed;
  623. clipInfo->state.weight = weight;
  624. clipInfo->state.wrapMode = mDefaultWrapMode;
  625. if(fadeLength > 0.0f)
  626. {
  627. clipInfo->fadeDirection = 1.0f;
  628. clipInfo->fadeTime = 0.0f;
  629. clipInfo->fadeLength = fadeLength;
  630. }
  631. clipInfo->playbackType = AnimPlaybackType::Normal;
  632. mDirty |= AnimDirtyStateFlag::Value;
  633. }
  634. }
  635. void Animation::blend1D(const Blend1DInfo& info, float t)
  636. {
  637. if (info.clips.size() == 0)
  638. return;
  639. // Find valid range
  640. float startPos = 0.0f;
  641. float endPos = 0.0f;
  642. for (UINT32 i = 0; i < (UINT32)info.clips.size(); i++)
  643. {
  644. startPos = std::min(startPos, info.clips[i].position);
  645. endPos = std::min(endPos, info.clips[i].position);
  646. }
  647. float length = endPos - startPos;
  648. if(Math::approxEquals(length, 0.0f) || info.clips.size() < 2)
  649. {
  650. play(info.clips[0].clip);
  651. return;
  652. }
  653. // Clamp or loop time
  654. bool loop = mDefaultWrapMode == AnimWrapMode::Loop;
  655. if (t < startPos)
  656. {
  657. if (loop)
  658. t = t - std::floor(t / length) * length;
  659. else // Clamping
  660. t = startPos;
  661. }
  662. if (t > endPos)
  663. {
  664. if (loop)
  665. t = t - std::floor(t / length) * length;
  666. else // Clamping
  667. t = endPos;
  668. }
  669. // Find keys to blend between
  670. UINT32 leftKey = 0;
  671. UINT32 rightKey = 0;
  672. INT32 start = 0;
  673. INT32 searchLength = (INT32)info.clips.size();
  674. while (searchLength > 0)
  675. {
  676. INT32 half = searchLength >> 1;
  677. INT32 mid = start + half;
  678. if (t < info.clips[mid].position)
  679. {
  680. searchLength = half;
  681. }
  682. else
  683. {
  684. start = mid + 1;
  685. searchLength -= (half + 1);
  686. }
  687. }
  688. leftKey = std::max(0, start - 1);
  689. rightKey = std::min(start, (INT32)info.clips.size() - 1);
  690. float interpLength = info.clips[rightKey].position - info.clips[leftKey].position;
  691. t = (t - info.clips[leftKey].position) / interpLength;
  692. // Add clips and set weights
  693. for(UINT32 i = 0; i < (UINT32)info.clips.size(); i++)
  694. {
  695. AnimationClipInfo* clipInfo = addClip(info.clips[i].clip, (UINT32)-1, i == 0);
  696. if (clipInfo != nullptr)
  697. {
  698. clipInfo->state.time = 0.0f;
  699. clipInfo->state.stopped = true;
  700. clipInfo->state.speed = 0.0f;
  701. clipInfo->state.wrapMode = AnimWrapMode::Clamp;
  702. if (i == leftKey)
  703. clipInfo->state.weight = 1.0f - t;
  704. else if (i == rightKey)
  705. clipInfo->state.weight = t;
  706. else
  707. clipInfo->state.weight = 0.0f;
  708. clipInfo->playbackType = AnimPlaybackType::Normal;
  709. }
  710. }
  711. mDirty |= AnimDirtyStateFlag::Value;
  712. }
  713. void Animation::blend2D(const Blend2DInfo& info, const Vector2& t)
  714. {
  715. AnimationClipInfo* topLeftClipInfo = addClip(info.topLeftClip, (UINT32)-1, true);
  716. if (topLeftClipInfo != nullptr)
  717. {
  718. topLeftClipInfo->state.time = 0.0f;
  719. topLeftClipInfo->state.stopped = true;
  720. topLeftClipInfo->state.speed = 0.0f;
  721. topLeftClipInfo->state.weight = (1.0f - t.x) * (1.0f - t.y);
  722. topLeftClipInfo->state.wrapMode = AnimWrapMode::Clamp;
  723. topLeftClipInfo->playbackType = AnimPlaybackType::Normal;
  724. }
  725. AnimationClipInfo* topRightClipInfo = addClip(info.topRightClip, (UINT32)-1, false);
  726. if (topRightClipInfo != nullptr)
  727. {
  728. topRightClipInfo->state.time = 0.0f;
  729. topRightClipInfo->state.stopped = true;
  730. topLeftClipInfo->state.speed = 0.0f;
  731. topRightClipInfo->state.weight = t.x * (1.0f - t.y);
  732. topRightClipInfo->state.wrapMode = AnimWrapMode::Clamp;
  733. topRightClipInfo->playbackType = AnimPlaybackType::Normal;
  734. }
  735. AnimationClipInfo* botLeftClipInfo = addClip(info.botLeftClip, (UINT32)-1, false);
  736. if (botLeftClipInfo != nullptr)
  737. {
  738. botLeftClipInfo->state.time = 0.0f;
  739. botLeftClipInfo->state.stopped = true;
  740. topLeftClipInfo->state.speed = 0.0f;
  741. botLeftClipInfo->state.weight = (1.0f - t.x) * t.y;
  742. botLeftClipInfo->state.wrapMode = AnimWrapMode::Clamp;
  743. botLeftClipInfo->playbackType = AnimPlaybackType::Normal;
  744. }
  745. AnimationClipInfo* botRightClipInfo = addClip(info.botRightClip, (UINT32)-1, false);
  746. if (botRightClipInfo != nullptr)
  747. {
  748. botRightClipInfo->state.time = 0.0f;
  749. botRightClipInfo->state.stopped = true;
  750. botRightClipInfo->state.speed = 0.0f;
  751. botRightClipInfo->state.weight = t.x * t.y;
  752. botRightClipInfo->state.wrapMode = AnimWrapMode::Clamp;
  753. botRightClipInfo->playbackType = AnimPlaybackType::Normal;
  754. }
  755. mDirty |= AnimDirtyStateFlag::Value;
  756. }
  757. void Animation::crossFade(const HAnimationClip& clip, float fadeLength)
  758. {
  759. bool isFading = fadeLength > 0.0f;
  760. if(!isFading)
  761. {
  762. play(clip);
  763. return;
  764. }
  765. AnimationClipInfo* clipInfo = addClip(clip, (UINT32)-1, false);
  766. if (clipInfo != nullptr)
  767. {
  768. clipInfo->state.time = 0.0f;
  769. clipInfo->state.speed = mDefaultSpeed;
  770. clipInfo->state.weight = 1.0f;
  771. clipInfo->state.wrapMode = mDefaultWrapMode;
  772. clipInfo->playbackType = AnimPlaybackType::Normal;
  773. // Set up fade lengths
  774. clipInfo->fadeDirection = 1.0f;
  775. clipInfo->fadeTime = 0.0f;
  776. clipInfo->fadeLength = fadeLength;
  777. for (auto& entry : mClipInfos)
  778. {
  779. if (entry.state.layer == (UINT32)-1 && entry.clip != clip)
  780. {
  781. // If other clips are already cross-fading, we need to persist their current weight before starting
  782. // a new crossfade. We do that by adjusting the fade times.
  783. if(clipInfo->fadeDirection != 0 && clipInfo->fadeTime < clipInfo->fadeLength)
  784. {
  785. float t = clipInfo->fadeTime / clipInfo->fadeLength;
  786. if (clipInfo->fadeDirection < 0.0f)
  787. t = (1.0f - t);
  788. clipInfo->state.weight *= t;
  789. }
  790. clipInfo->fadeDirection = -1.0f;
  791. clipInfo->fadeTime = 0.0f;
  792. clipInfo->fadeLength = fadeLength;
  793. }
  794. }
  795. }
  796. mDirty |= AnimDirtyStateFlag::Value;
  797. }
  798. void Animation::sample(const HAnimationClip& clip, float time)
  799. {
  800. AnimationClipInfo* clipInfo = addClip(clip, (UINT32)-1);
  801. if (clipInfo != nullptr)
  802. {
  803. clipInfo->state.time = time;
  804. clipInfo->state.speed = 0.0f;
  805. clipInfo->state.weight = 1.0f;
  806. clipInfo->state.wrapMode = mDefaultWrapMode;
  807. clipInfo->playbackType = AnimPlaybackType::Sampled;
  808. }
  809. mDirty |= AnimDirtyStateFlag::Value;
  810. }
  811. void Animation::stop(UINT32 layer)
  812. {
  813. bs_frame_mark();
  814. {
  815. FrameVector<AnimationClipInfo> newClips;
  816. for (auto& clipInfo : mClipInfos)
  817. {
  818. if (clipInfo.state.layer != layer)
  819. newClips.push_back(clipInfo);
  820. else
  821. mDirty |= AnimDirtyStateFlag::Layout;
  822. }
  823. mClipInfos.resize(newClips.size());
  824. for(UINT32 i = 0; i < (UINT32)newClips.size(); i++)
  825. mClipInfos[i] = newClips[i];
  826. }
  827. bs_frame_clear();
  828. }
  829. void Animation::stopAll()
  830. {
  831. mClipInfos.clear();
  832. mDirty |= AnimDirtyStateFlag::Layout;
  833. }
  834. AnimationClipInfo* Animation::addClip(const HAnimationClip& clip, UINT32 layer, bool stopExisting)
  835. {
  836. AnimationClipInfo* output = nullptr;
  837. bool hasExisting = false;
  838. // Search for existing
  839. for (auto& clipInfo : mClipInfos)
  840. {
  841. if (clipInfo.state.layer == layer)
  842. {
  843. if (clipInfo.clip == clip)
  844. output = &clipInfo;
  845. else if (stopExisting)
  846. hasExisting = true;
  847. }
  848. }
  849. // Doesn't exist or found extra animations, rebuild
  850. if (output == nullptr || hasExisting)
  851. {
  852. bs_frame_mark();
  853. {
  854. FrameVector<AnimationClipInfo> newClips;
  855. for (auto& clipInfo : mClipInfos)
  856. {
  857. if (!stopExisting || clipInfo.state.layer != layer || clipInfo.clip == clip)
  858. newClips.push_back(clipInfo);
  859. }
  860. if (output == nullptr && clip != nullptr)
  861. newClips.push_back(AnimationClipInfo());
  862. mClipInfos.resize(newClips.size());
  863. for(UINT32 i = 0; i < (UINT32)newClips.size(); i++)
  864. mClipInfos[i] = newClips[i];
  865. mDirty |= AnimDirtyStateFlag::Layout;
  866. }
  867. bs_frame_clear();
  868. }
  869. // If new clip was added, get its address
  870. if (output == nullptr && clip != nullptr)
  871. {
  872. AnimationClipInfo& newInfo = mClipInfos.back();
  873. newInfo.clip = clip;
  874. newInfo.state.layer = layer;
  875. output = &newInfo;
  876. }
  877. return output;
  878. }
  879. bool Animation::_getAnimatesRoot() const
  880. {
  881. if (mSkeleton == nullptr)
  882. return false;
  883. UINT32 rootBoneIdx = mSkeleton->getRootBoneIndex();
  884. if (rootBoneIdx == (UINT32)-1)
  885. return false;
  886. String rootBoneName = mSkeleton->getBoneInfo(rootBoneIdx).name;
  887. for (auto& entry : mClipInfos)
  888. {
  889. if (entry.clip.isLoaded())
  890. {
  891. HAnimationClip clip = entry.clip;
  892. if(!clip->hasRootMotion())
  893. {
  894. AnimationCurveMapping mapping;
  895. clip->getCurveMapping(rootBoneName, mapping);
  896. if (mapping.position != (UINT32)-1)
  897. return true;
  898. if (mapping.rotation != (UINT32)-1)
  899. return true;
  900. if (mapping.scale != (UINT32)-1)
  901. return true;
  902. }
  903. }
  904. }
  905. return false;
  906. }
  907. void Animation::getListenerResources(Vector<HResource>& resources)
  908. {
  909. for (auto& entry : mClipInfos)
  910. {
  911. if(entry.clip != nullptr)
  912. resources.push_back(entry.clip);
  913. }
  914. }
  915. void Animation::notifyResourceLoaded(const HResource& resource)
  916. {
  917. mDirty |= AnimDirtyStateFlag::Layout;
  918. }
  919. void Animation::notifyResourceChanged(const HResource& resource)
  920. {
  921. mDirty |= AnimDirtyStateFlag::Layout;
  922. }
  923. bool Animation::isPlaying() const
  924. {
  925. for(auto& clipInfo : mClipInfos)
  926. {
  927. if (clipInfo.clip.isLoaded())
  928. return true;
  929. }
  930. return false;
  931. }
  932. bool Animation::getState(const HAnimationClip& clip, AnimationClipState& state)
  933. {
  934. if (clip == nullptr)
  935. return false;
  936. for (auto& clipInfo : mClipInfos)
  937. {
  938. if (clipInfo.clip == clip)
  939. {
  940. state = clipInfo.state;
  941. // Internally we store unclamped time, so clamp/loop it
  942. float clipLength = 0.0f;
  943. if (clip.isLoaded())
  944. clipLength = clip->getLength();
  945. bool loop = clipInfo.state.wrapMode == AnimWrapMode::Loop;
  946. AnimationUtility::wrapTime(clipInfo.state.time, 0.0f, clipLength, loop);
  947. return true;
  948. }
  949. }
  950. return false;
  951. }
  952. void Animation::setState(const HAnimationClip& clip, AnimationClipState state)
  953. {
  954. if (state.layer == 0)
  955. state.layer = (UINT32)-1;
  956. else
  957. state.layer -= 1;
  958. AnimationClipInfo* clipInfo = addClip(clip, state.layer, false);
  959. if (clipInfo == nullptr)
  960. return;
  961. clipInfo->state = state;
  962. clipInfo->playbackType = AnimPlaybackType::Normal;
  963. mDirty |= AnimDirtyStateFlag::Value;
  964. }
  965. UINT32 Animation::getNumClips() const
  966. {
  967. return (UINT32)mClipInfos.size();
  968. }
  969. HAnimationClip Animation::getClip(UINT32 idx) const
  970. {
  971. if (idx >= (UINT32)mClipInfos.size())
  972. return HAnimationClip();
  973. return mClipInfos[idx].clip;
  974. }
  975. void Animation::triggerEvents(float lastFrameTime, float delta)
  976. {
  977. for (auto& clipInfo : mClipInfos)
  978. {
  979. if (!clipInfo.clip.isLoaded())
  980. continue;
  981. const Vector<AnimationEvent>& events = clipInfo.clip->getEvents();
  982. bool loop = clipInfo.state.wrapMode == AnimWrapMode::Loop;
  983. float start = lastFrameTime;
  984. float end = start + delta;
  985. float clipLength = clipInfo.clip->getLength();
  986. AnimationUtility::wrapTime(start, 0.0f, clipLength, loop);
  987. AnimationUtility::wrapTime(end, 0.0f, clipLength, loop);
  988. if (start < end)
  989. {
  990. for (auto& event : events)
  991. {
  992. if (event.time > start && event.time <= end)
  993. onEventTriggered(clipInfo.clip, event.name);
  994. }
  995. }
  996. else if(end < start) // End is looped, but start is not
  997. {
  998. for (auto& event : events)
  999. {
  1000. if (event.time > start && event.time < clipLength && event.time > 0 && event.time <= end)
  1001. onEventTriggered(clipInfo.clip, event.name);
  1002. }
  1003. }
  1004. }
  1005. }
  1006. void Animation::mapCurveToSceneObject(const String& curve, const HSceneObject& so)
  1007. {
  1008. AnimatedSceneObject animSo = { so, curve };
  1009. mSceneObjects[so.getInstanceId()] = animSo;
  1010. mDirty |= AnimDirtyStateFlag::All;
  1011. }
  1012. void Animation::unmapSceneObject(const HSceneObject& so)
  1013. {
  1014. mSceneObjects.erase(so.getInstanceId());
  1015. mDirty |= AnimDirtyStateFlag::All;
  1016. }
  1017. bool Animation::getGenericCurveValue(UINT32 curveIdx, float& value)
  1018. {
  1019. if (!mGenericCurveValuesValid || curveIdx >= (UINT32)mGenericCurveOutputs.size())
  1020. return false;
  1021. value = mGenericCurveOutputs[curveIdx];
  1022. return true;
  1023. }
  1024. SPtr<Animation> Animation::create()
  1025. {
  1026. Animation* anim = new (bs_alloc<Animation>()) Animation();
  1027. SPtr<Animation> animPtr = bs_core_ptr(anim);
  1028. animPtr->_setThisPtr(animPtr);
  1029. animPtr->initialize();
  1030. return animPtr;
  1031. }
  1032. void Animation::updateAnimProxy(float timeDelta)
  1033. {
  1034. // Check if any of the clip curves are dirty and advance time, perform fading
  1035. for (auto& clipInfo : mClipInfos)
  1036. {
  1037. float scaledTimeDelta = timeDelta * clipInfo.state.speed;
  1038. clipInfo.state.time += scaledTimeDelta;
  1039. HAnimationClip clip = clipInfo.clip;
  1040. if (clip.isLoaded())
  1041. {
  1042. if (clipInfo.curveVersion != clip->getVersion())
  1043. mDirty |= AnimDirtyStateFlag::Layout;
  1044. }
  1045. float fadeTime = clipInfo.fadeTime + scaledTimeDelta;
  1046. clipInfo.fadeTime = Math::clamp(fadeTime, 0.0f, clipInfo.fadeLength);
  1047. }
  1048. if (mDirty.isSet(AnimDirtyStateFlag::Culling))
  1049. {
  1050. mAnimProxy->mCullEnabled = mCull;
  1051. mAnimProxy->mBounds = mBounds;
  1052. mDirty.unset(AnimDirtyStateFlag::Culling);
  1053. }
  1054. auto getAnimatedSOList = [&]()
  1055. {
  1056. Vector<AnimatedSceneObject> animatedSO(mSceneObjects.size());
  1057. UINT32 idx = 0;
  1058. for (auto& entry : mSceneObjects)
  1059. animatedSO[idx++] = entry.second;
  1060. return animatedSO;
  1061. };
  1062. bool didFullRebuild = false;
  1063. if((UINT32)mDirty == 0) // Clean
  1064. {
  1065. mAnimProxy->updateTime(mClipInfos);
  1066. }
  1067. else
  1068. {
  1069. if (mDirty.isSet(AnimDirtyStateFlag::All))
  1070. {
  1071. Vector<AnimatedSceneObject> animatedSOs = getAnimatedSOList();
  1072. mAnimProxy->rebuild(mSkeleton, mSkeletonMask, mClipInfos, animatedSOs, mMorphShapes);
  1073. didFullRebuild = true;
  1074. }
  1075. else if (mDirty.isSet(AnimDirtyStateFlag::Layout))
  1076. {
  1077. Vector<AnimatedSceneObject> animatedSOs = getAnimatedSOList();
  1078. mAnimProxy->rebuild(mClipInfos, animatedSOs, mMorphShapes);
  1079. didFullRebuild = true;
  1080. }
  1081. else if(mDirty.isSet(AnimDirtyStateFlag::Value))
  1082. mAnimProxy->updateClipInfos(mClipInfos);
  1083. if (mDirty.isSet(AnimDirtyStateFlag::MorphWeights) || didFullRebuild)
  1084. mAnimProxy->updateMorphChannelWeights(mMorphChannelWeights);
  1085. }
  1086. // Check if there are dirty transforms
  1087. if (!didFullRebuild)
  1088. {
  1089. for (UINT32 i = 0; i < mAnimProxy->numSceneObjects; i++)
  1090. {
  1091. AnimatedSceneObjectInfo& soInfo = mAnimProxy->sceneObjectInfos[i];
  1092. auto iterFind = mSceneObjects.find(soInfo.id);
  1093. if (iterFind == mSceneObjects.end())
  1094. {
  1095. assert(false); // Should never happen
  1096. continue;
  1097. }
  1098. UINT32 hash;
  1099. HSceneObject so = iterFind->second.so;
  1100. if (so.isDestroyed(true))
  1101. hash = 0;
  1102. else
  1103. hash = so->getTransformHash();
  1104. if (hash != mAnimProxy->sceneObjectInfos[i].hash)
  1105. {
  1106. Vector<AnimatedSceneObject> animatedSOs = getAnimatedSOList();
  1107. mAnimProxy->updateTransforms(animatedSOs);
  1108. break;
  1109. }
  1110. }
  1111. }
  1112. mDirty = AnimDirtyState();
  1113. }
  1114. void Animation::updateFromProxy()
  1115. {
  1116. HSceneObject rootSO;
  1117. // Write TRS animation results to relevant SceneObjects
  1118. for(UINT32 i = 0; i < mAnimProxy->numSceneObjects; i++)
  1119. {
  1120. AnimatedSceneObjectInfo& soInfo = mAnimProxy->sceneObjectInfos[i];
  1121. auto iterFind = mSceneObjects.find(soInfo.id);
  1122. if (iterFind == mSceneObjects.end())
  1123. continue;
  1124. HSceneObject so = iterFind->second.so;
  1125. if (iterFind->second.curveName.empty())
  1126. rootSO = so;
  1127. if (so.isDestroyed(true))
  1128. continue;
  1129. if(soInfo.boneIdx != -1)
  1130. {
  1131. if (mAnimProxy->skeletonPose.hasOverride[soInfo.boneIdx])
  1132. continue;
  1133. Vector3 position = mAnimProxy->skeletonPose.positions[soInfo.boneIdx];
  1134. Quaternion rotation = mAnimProxy->skeletonPose.rotations[soInfo.boneIdx];
  1135. Vector3 scale = mAnimProxy->skeletonPose.scales[soInfo.boneIdx];
  1136. const SPtr<Skeleton>& skeleton = mAnimProxy->skeleton;
  1137. UINT32 parentBoneIdx = skeleton->getBoneInfo(soInfo.boneIdx).parent;
  1138. if (parentBoneIdx == (UINT32)-1)
  1139. {
  1140. so->setPosition(position);
  1141. so->setRotation(rotation);
  1142. so->setScale(scale);
  1143. }
  1144. else
  1145. {
  1146. while(parentBoneIdx != (UINT32)-1)
  1147. {
  1148. // Update rotation
  1149. const Quaternion& parentOrientation = mAnimProxy->skeletonPose.rotations[parentBoneIdx];
  1150. rotation = parentOrientation * rotation;
  1151. // Update scale
  1152. const Vector3& parentScale = mAnimProxy->skeletonPose.scales[parentBoneIdx];
  1153. scale = parentScale * scale;
  1154. // Update position
  1155. position = parentOrientation.rotate(parentScale * position);
  1156. position += mAnimProxy->skeletonPose.positions[parentBoneIdx];
  1157. parentBoneIdx = skeleton->getBoneInfo(parentBoneIdx).parent;
  1158. }
  1159. // Search for root if not already found
  1160. if(rootSO == nullptr)
  1161. {
  1162. for(auto& entry : mSceneObjects)
  1163. {
  1164. if (entry.second.curveName.empty())
  1165. rootSO = entry.second.so;
  1166. }
  1167. }
  1168. Vector3 rootParentPos = Vector3::ZERO;
  1169. Quaternion rootParentRot = Quaternion::IDENTITY;
  1170. Vector3 rootParentScale = Vector3::ONE;
  1171. if(rootSO.isDestroyed(true))
  1172. {
  1173. HSceneObject rootParent = rootSO->getParent();
  1174. if(!rootParent.isDestroyed(true))
  1175. {
  1176. const Transform& tfrm = rootParent->getTransform();
  1177. rootParentPos = tfrm.getPosition();
  1178. rootParentRot = tfrm.getRotation();
  1179. rootParentScale = tfrm.getScale();
  1180. }
  1181. }
  1182. // Transform from space relative to root's parent to world space
  1183. rotation = rootParentRot * rotation;
  1184. scale = rootParentScale * scale;
  1185. position = rootParentRot.rotate(rootParentScale * position);
  1186. position += rootParentPos;
  1187. so->setWorldPosition(position);
  1188. so->setWorldRotation(rotation);
  1189. so->setWorldScale(scale);
  1190. }
  1191. }
  1192. else
  1193. {
  1194. if (mAnimProxy->sceneObjectPose.hasOverride[i])
  1195. continue;
  1196. so->setPosition(mAnimProxy->sceneObjectPose.positions[i]);
  1197. so->setRotation(mAnimProxy->sceneObjectPose.rotations[i]);
  1198. so->setScale(mAnimProxy->sceneObjectPose.scales[i]);
  1199. }
  1200. }
  1201. // Must ensure that clip in the proxy and current primary clip are the same
  1202. mGenericCurveValuesValid = false;
  1203. if(mAnimProxy->numLayers > 0 && mAnimProxy->layers[0].numStates > 0)
  1204. {
  1205. const AnimationState& state = mAnimProxy->layers[0].states[0];
  1206. if(!state.disabled && mClipInfos.size() > 0)
  1207. {
  1208. const AnimationClipInfo& clipInfo = mClipInfos[0];
  1209. if (clipInfo.stateIdx == 0 && clipInfo.layerIdx == 0)
  1210. {
  1211. if (clipInfo.clip.isLoaded() && clipInfo.curveVersion == clipInfo.clip->getVersion())
  1212. {
  1213. UINT32 numGenericCurves = (UINT32)clipInfo.clip->getCurves()->generic.size();
  1214. mGenericCurveValuesValid = numGenericCurves == mAnimProxy->numGenericCurves;
  1215. }
  1216. }
  1217. }
  1218. }
  1219. if(mGenericCurveValuesValid)
  1220. {
  1221. mGenericCurveOutputs.resize(mAnimProxy->numGenericCurves);
  1222. memcpy(mGenericCurveOutputs.data(), mAnimProxy->genericCurveOutputs, mAnimProxy->numGenericCurves * sizeof(float));
  1223. }
  1224. }
  1225. }