BsAnimation.cpp 38 KB

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