BsAnimation.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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. // Disable sampled animations
  1113. for (auto& clipInfo : mClipInfos)
  1114. {
  1115. if (clipInfo.playbackType == AnimPlaybackType::Sampled)
  1116. clipInfo.playbackType = AnimPlaybackType::None;
  1117. }
  1118. mDirty = AnimDirtyState();
  1119. }
  1120. void Animation::updateFromProxy()
  1121. {
  1122. HSceneObject rootSO;
  1123. // Write TRS animation results to relevant SceneObjects
  1124. for(UINT32 i = 0; i < mAnimProxy->numSceneObjects; i++)
  1125. {
  1126. AnimatedSceneObjectInfo& soInfo = mAnimProxy->sceneObjectInfos[i];
  1127. auto iterFind = mSceneObjects.find(soInfo.id);
  1128. if (iterFind == mSceneObjects.end())
  1129. continue;
  1130. HSceneObject so = iterFind->second.so;
  1131. if (iterFind->second.curveName.empty())
  1132. rootSO = so;
  1133. if (so.isDestroyed(true))
  1134. continue;
  1135. if(soInfo.boneIdx != -1)
  1136. {
  1137. if (mAnimProxy->skeletonPose.hasOverride[soInfo.boneIdx])
  1138. continue;
  1139. Vector3 position = mAnimProxy->skeletonPose.positions[soInfo.boneIdx];
  1140. Quaternion rotation = mAnimProxy->skeletonPose.rotations[soInfo.boneIdx];
  1141. Vector3 scale = mAnimProxy->skeletonPose.scales[soInfo.boneIdx];
  1142. const SPtr<Skeleton>& skeleton = mAnimProxy->skeleton;
  1143. UINT32 parentBoneIdx = skeleton->getBoneInfo(soInfo.boneIdx).parent;
  1144. if (parentBoneIdx == (UINT32)-1)
  1145. {
  1146. so->setPosition(position);
  1147. so->setRotation(rotation);
  1148. so->setScale(scale);
  1149. }
  1150. else
  1151. {
  1152. while(parentBoneIdx != (UINT32)-1)
  1153. {
  1154. // Update rotation
  1155. const Quaternion& parentOrientation = mAnimProxy->skeletonPose.rotations[parentBoneIdx];
  1156. rotation = parentOrientation * rotation;
  1157. // Update scale
  1158. const Vector3& parentScale = mAnimProxy->skeletonPose.scales[parentBoneIdx];
  1159. scale = parentScale * scale;
  1160. // Update position
  1161. position = parentOrientation.rotate(parentScale * position);
  1162. position += mAnimProxy->skeletonPose.positions[parentBoneIdx];
  1163. parentBoneIdx = skeleton->getBoneInfo(parentBoneIdx).parent;
  1164. }
  1165. // Search for root if not already found
  1166. if(rootSO == nullptr)
  1167. {
  1168. for(auto& entry : mSceneObjects)
  1169. {
  1170. if (entry.second.curveName.empty())
  1171. rootSO = entry.second.so;
  1172. }
  1173. }
  1174. Vector3 rootParentPos = Vector3::ZERO;
  1175. Quaternion rootParentRot = Quaternion::IDENTITY;
  1176. Vector3 rootParentScale = Vector3::ONE;
  1177. if(rootSO.isDestroyed(true))
  1178. {
  1179. HSceneObject rootParent = rootSO->getParent();
  1180. if(!rootParent.isDestroyed(true))
  1181. {
  1182. const Transform& tfrm = rootParent->getTransform();
  1183. rootParentPos = tfrm.getPosition();
  1184. rootParentRot = tfrm.getRotation();
  1185. rootParentScale = tfrm.getScale();
  1186. }
  1187. }
  1188. // Transform from space relative to root's parent to world space
  1189. rotation = rootParentRot * rotation;
  1190. scale = rootParentScale * scale;
  1191. position = rootParentRot.rotate(rootParentScale * position);
  1192. position += rootParentPos;
  1193. so->setWorldPosition(position);
  1194. so->setWorldRotation(rotation);
  1195. so->setWorldScale(scale);
  1196. }
  1197. }
  1198. else
  1199. {
  1200. if (mAnimProxy->sceneObjectPose.hasOverride[i])
  1201. continue;
  1202. so->setPosition(mAnimProxy->sceneObjectPose.positions[i]);
  1203. so->setRotation(mAnimProxy->sceneObjectPose.rotations[i]);
  1204. so->setScale(mAnimProxy->sceneObjectPose.scales[i]);
  1205. }
  1206. }
  1207. // Must ensure that clip in the proxy and current primary clip are the same
  1208. mGenericCurveValuesValid = false;
  1209. if(mAnimProxy->numLayers > 0 && mAnimProxy->layers[0].numStates > 0)
  1210. {
  1211. const AnimationState& state = mAnimProxy->layers[0].states[0];
  1212. if(!state.disabled && mClipInfos.size() > 0)
  1213. {
  1214. const AnimationClipInfo& clipInfo = mClipInfos[0];
  1215. if (clipInfo.stateIdx == 0 && clipInfo.layerIdx == 0)
  1216. {
  1217. if (clipInfo.clip.isLoaded() && clipInfo.curveVersion == clipInfo.clip->getVersion())
  1218. {
  1219. UINT32 numGenericCurves = (UINT32)clipInfo.clip->getCurves()->generic.size();
  1220. mGenericCurveValuesValid = numGenericCurves == mAnimProxy->numGenericCurves;
  1221. }
  1222. }
  1223. }
  1224. }
  1225. if(mGenericCurveValuesValid)
  1226. {
  1227. mGenericCurveOutputs.resize(mAnimProxy->numGenericCurves);
  1228. memcpy(mGenericCurveOutputs.data(), mAnimProxy->genericCurveOutputs, mAnimProxy->numGenericCurves * sizeof(float));
  1229. }
  1230. }
  1231. }