BsAnimation.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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. if (numChannels > 0)
  561. mMorphChannelWeights[0] = 1.0f;
  562. mDirty |= AnimDirtyStateFlag::Layout;
  563. mDirty |= AnimDirtyStateFlag::MorphWeights;
  564. }
  565. void Animation::setMorphChannelWeight(UINT32 idx, float weight)
  566. {
  567. UINT32 numShapes = (UINT32)mMorphChannelWeights.size();
  568. if (idx >= numShapes)
  569. return;
  570. mMorphChannelWeights[idx] = weight;
  571. mDirty |= AnimDirtyStateFlag::MorphWeights;
  572. }
  573. void Animation::setMask(const SkeletonMask& mask)
  574. {
  575. mSkeletonMask = mask;
  576. mDirty |= AnimDirtyStateFlag::All;
  577. }
  578. void Animation::setWrapMode(AnimWrapMode wrapMode)
  579. {
  580. mDefaultWrapMode = wrapMode;
  581. for (auto& clipInfo : mClipInfos)
  582. clipInfo.state.wrapMode = wrapMode;
  583. mDirty |= AnimDirtyStateFlag::Value;
  584. }
  585. void Animation::setSpeed(float speed)
  586. {
  587. mDefaultSpeed = speed;
  588. for (auto& clipInfo : mClipInfos)
  589. {
  590. // Special case: Ignore non-moving ones
  591. if(!clipInfo.state.stopped)
  592. clipInfo.state.speed = speed;
  593. }
  594. mDirty |= AnimDirtyStateFlag::Value;
  595. }
  596. void Animation::setBounds(const AABox& bounds)
  597. {
  598. mBounds = bounds;
  599. mDirty |= AnimDirtyStateFlag::Culling;
  600. }
  601. void Animation::setCulling(bool cull)
  602. {
  603. mCull = cull;
  604. mDirty |= AnimDirtyStateFlag::Culling;
  605. }
  606. void Animation::play(const HAnimationClip& clip)
  607. {
  608. AnimationClipInfo* clipInfo = addClip(clip, (UINT32)-1);
  609. if(clipInfo != nullptr)
  610. {
  611. clipInfo->state.time = 0.0f;
  612. clipInfo->state.speed = mDefaultSpeed;
  613. clipInfo->state.weight = 1.0f;
  614. clipInfo->state.wrapMode = mDefaultWrapMode;
  615. clipInfo->playbackType = AnimPlaybackType::Normal;
  616. }
  617. mDirty |= AnimDirtyStateFlag::Value;
  618. }
  619. void Animation::blendAdditive(const HAnimationClip& clip, float weight, float fadeLength, UINT32 layer)
  620. {
  621. if(clip != nullptr && !clip->isAdditive())
  622. {
  623. LOGWRN("blendAdditive() called with a clip that doesn't contain additive animation. Ignoring.");
  624. // Stop any clips on this layer, even if invalid
  625. HAnimationClip nullClip;
  626. addClip(nullClip, layer);
  627. return;
  628. }
  629. AnimationClipInfo* clipInfo = addClip(clip, layer);
  630. if (clipInfo != nullptr)
  631. {
  632. clipInfo->state.time = 0.0f;
  633. clipInfo->state.speed = mDefaultSpeed;
  634. clipInfo->state.weight = weight;
  635. clipInfo->state.wrapMode = mDefaultWrapMode;
  636. if(fadeLength > 0.0f)
  637. {
  638. clipInfo->fadeDirection = 1.0f;
  639. clipInfo->fadeTime = 0.0f;
  640. clipInfo->fadeLength = fadeLength;
  641. }
  642. clipInfo->playbackType = AnimPlaybackType::Normal;
  643. mDirty |= AnimDirtyStateFlag::Value;
  644. }
  645. }
  646. void Animation::blend1D(const Blend1DInfo& info, float t)
  647. {
  648. if (info.numClips == 0)
  649. return;
  650. // Find valid range
  651. float startPos = 0.0f;
  652. float endPos = 0.0f;
  653. for (UINT32 i = 0; i < info.numClips; i++)
  654. {
  655. startPos = std::min(startPos, info.clips[i].position);
  656. endPos = std::min(endPos, info.clips[i].position);
  657. }
  658. float length = endPos - startPos;
  659. if(Math::approxEquals(length, 0.0f) || info.numClips < 2)
  660. {
  661. play(info.clips[0].clip);
  662. return;
  663. }
  664. // Clamp or loop time
  665. bool loop = mDefaultWrapMode == AnimWrapMode::Loop;
  666. if (t < startPos)
  667. {
  668. if (loop)
  669. t = t - std::floor(t / length) * length;
  670. else // Clamping
  671. t = startPos;
  672. }
  673. if (t > endPos)
  674. {
  675. if (loop)
  676. t = t - std::floor(t / length) * length;
  677. else // Clamping
  678. t = endPos;
  679. }
  680. // Find keys to blend between
  681. UINT32 leftKey = 0;
  682. UINT32 rightKey = 0;
  683. INT32 start = 0;
  684. INT32 searchLength = (INT32)info.numClips;
  685. while (searchLength > 0)
  686. {
  687. INT32 half = searchLength >> 1;
  688. INT32 mid = start + half;
  689. if (t < info.clips[mid].position)
  690. {
  691. searchLength = half;
  692. }
  693. else
  694. {
  695. start = mid + 1;
  696. searchLength -= (half + 1);
  697. }
  698. }
  699. leftKey = std::max(0, start - 1);
  700. rightKey = std::min(start, (INT32)info.numClips - 1);
  701. float interpLength = info.clips[rightKey].position - info.clips[leftKey].position;
  702. t = (t - info.clips[leftKey].position) / interpLength;
  703. // Add clips and set weights
  704. for(UINT32 i = 0; i < info.numClips; i++)
  705. {
  706. AnimationClipInfo* clipInfo = addClip(info.clips[i].clip, (UINT32)-1, i == 0);
  707. if (clipInfo != nullptr)
  708. {
  709. clipInfo->state.time = 0.0f;
  710. clipInfo->state.stopped = true;
  711. clipInfo->state.speed = 0.0f;
  712. clipInfo->state.wrapMode = AnimWrapMode::Clamp;
  713. if (i == leftKey)
  714. clipInfo->state.weight = 1.0f - t;
  715. else if (i == rightKey)
  716. clipInfo->state.weight = t;
  717. else
  718. clipInfo->state.weight = 0.0f;
  719. clipInfo->playbackType = AnimPlaybackType::Normal;
  720. }
  721. }
  722. mDirty |= AnimDirtyStateFlag::Value;
  723. }
  724. void Animation::blend2D(const Blend2DInfo& info, const Vector2& t)
  725. {
  726. AnimationClipInfo* topLeftClipInfo = addClip(info.topLeftClip, (UINT32)-1, true);
  727. if (topLeftClipInfo != nullptr)
  728. {
  729. topLeftClipInfo->state.time = 0.0f;
  730. topLeftClipInfo->state.stopped = true;
  731. topLeftClipInfo->state.speed = 0.0f;
  732. topLeftClipInfo->state.weight = (1.0f - t.x) * (1.0f - t.y);
  733. topLeftClipInfo->state.wrapMode = AnimWrapMode::Clamp;
  734. topLeftClipInfo->playbackType = AnimPlaybackType::Normal;
  735. }
  736. AnimationClipInfo* topRightClipInfo = addClip(info.topRightClip, (UINT32)-1, false);
  737. if (topRightClipInfo != nullptr)
  738. {
  739. topRightClipInfo->state.time = 0.0f;
  740. topRightClipInfo->state.stopped = true;
  741. topLeftClipInfo->state.speed = 0.0f;
  742. topRightClipInfo->state.weight = t.x * (1.0f - t.y);
  743. topRightClipInfo->state.wrapMode = AnimWrapMode::Clamp;
  744. topRightClipInfo->playbackType = AnimPlaybackType::Normal;
  745. }
  746. AnimationClipInfo* botLeftClipInfo = addClip(info.botLeftClip, (UINT32)-1, false);
  747. if (botLeftClipInfo != nullptr)
  748. {
  749. botLeftClipInfo->state.time = 0.0f;
  750. botLeftClipInfo->state.stopped = true;
  751. topLeftClipInfo->state.speed = 0.0f;
  752. botLeftClipInfo->state.weight = (1.0f - t.x) * t.y;
  753. botLeftClipInfo->state.wrapMode = AnimWrapMode::Clamp;
  754. botLeftClipInfo->playbackType = AnimPlaybackType::Normal;
  755. }
  756. AnimationClipInfo* botRightClipInfo = addClip(info.botRightClip, (UINT32)-1, false);
  757. if (botRightClipInfo != nullptr)
  758. {
  759. botRightClipInfo->state.time = 0.0f;
  760. botRightClipInfo->state.stopped = true;
  761. botRightClipInfo->state.speed = 0.0f;
  762. botRightClipInfo->state.weight = t.x * t.y;
  763. botRightClipInfo->state.wrapMode = AnimWrapMode::Clamp;
  764. botRightClipInfo->playbackType = AnimPlaybackType::Normal;
  765. }
  766. mDirty |= AnimDirtyStateFlag::Value;
  767. }
  768. void Animation::crossFade(const HAnimationClip& clip, float fadeLength)
  769. {
  770. bool isFading = fadeLength > 0.0f;
  771. if(!isFading)
  772. {
  773. play(clip);
  774. return;
  775. }
  776. AnimationClipInfo* clipInfo = addClip(clip, (UINT32)-1, false);
  777. if (clipInfo != nullptr)
  778. {
  779. clipInfo->state.time = 0.0f;
  780. clipInfo->state.speed = mDefaultSpeed;
  781. clipInfo->state.weight = 1.0f;
  782. clipInfo->state.wrapMode = mDefaultWrapMode;
  783. clipInfo->playbackType = AnimPlaybackType::Normal;
  784. // Set up fade lengths
  785. clipInfo->fadeDirection = 1.0f;
  786. clipInfo->fadeTime = 0.0f;
  787. clipInfo->fadeLength = fadeLength;
  788. for (auto& entry : mClipInfos)
  789. {
  790. if (entry.state.layer == (UINT32)-1 && entry.clip != clip)
  791. {
  792. // If other clips are already cross-fading, we need to persist their current weight before starting
  793. // a new crossfade. We do that by adjusting the fade times.
  794. if(clipInfo->fadeDirection != 0 && clipInfo->fadeTime < clipInfo->fadeLength)
  795. {
  796. float t = clipInfo->fadeTime / clipInfo->fadeLength;
  797. if (clipInfo->fadeDirection < 0.0f)
  798. t = (1.0f - t);
  799. clipInfo->state.weight *= t;
  800. }
  801. clipInfo->fadeDirection = -1.0f;
  802. clipInfo->fadeTime = 0.0f;
  803. clipInfo->fadeLength = fadeLength;
  804. }
  805. }
  806. }
  807. mDirty |= AnimDirtyStateFlag::Value;
  808. }
  809. void Animation::sample(const HAnimationClip& clip, float time)
  810. {
  811. AnimationClipInfo* clipInfo = addClip(clip, (UINT32)-1);
  812. if (clipInfo != nullptr)
  813. {
  814. clipInfo->state.time = time;
  815. clipInfo->state.speed = 0.0f;
  816. clipInfo->state.weight = 1.0f;
  817. clipInfo->state.wrapMode = mDefaultWrapMode;
  818. clipInfo->playbackType = AnimPlaybackType::Sampled;
  819. }
  820. mDirty |= AnimDirtyStateFlag::Value;
  821. }
  822. void Animation::stop(UINT32 layer)
  823. {
  824. bs_frame_mark();
  825. {
  826. FrameVector<AnimationClipInfo> newClips;
  827. for (auto& clipInfo : mClipInfos)
  828. {
  829. if (clipInfo.state.layer != layer)
  830. newClips.push_back(clipInfo);
  831. else
  832. mDirty |= AnimDirtyStateFlag::Layout;
  833. }
  834. mClipInfos.resize(newClips.size());
  835. memcpy(mClipInfos.data(), newClips.data(), sizeof(AnimationClipInfo) * newClips.size());
  836. }
  837. bs_frame_clear();
  838. }
  839. void Animation::stopAll()
  840. {
  841. mClipInfos.clear();
  842. mDirty |= AnimDirtyStateFlag::Layout;
  843. }
  844. AnimationClipInfo* Animation::addClip(const HAnimationClip& clip, UINT32 layer, bool stopExisting)
  845. {
  846. AnimationClipInfo* output = nullptr;
  847. bool hasExisting = false;
  848. // Search for existing
  849. for (auto& clipInfo : mClipInfos)
  850. {
  851. if (clipInfo.state.layer == layer)
  852. {
  853. if (clipInfo.clip == clip)
  854. output = &clipInfo;
  855. else if (stopExisting)
  856. hasExisting = true;
  857. }
  858. }
  859. // Doesn't exist or found extra animations, rebuild
  860. if (output == nullptr || hasExisting)
  861. {
  862. bs_frame_mark();
  863. {
  864. FrameVector<AnimationClipInfo> newClips;
  865. for (auto& clipInfo : mClipInfos)
  866. {
  867. if (!stopExisting || clipInfo.state.layer != layer || clipInfo.clip == clip)
  868. newClips.push_back(clipInfo);
  869. }
  870. if (output == nullptr && clip != nullptr)
  871. newClips.push_back(AnimationClipInfo());
  872. mClipInfos.resize(newClips.size());
  873. memcpy(mClipInfos.data(), newClips.data(), sizeof(AnimationClipInfo) * newClips.size());
  874. mDirty |= AnimDirtyStateFlag::Layout;
  875. }
  876. bs_frame_clear();
  877. }
  878. // If new clip was added, get its address
  879. if (output == nullptr && clip != nullptr)
  880. {
  881. AnimationClipInfo& newInfo = mClipInfos.back();
  882. newInfo.clip = clip;
  883. newInfo.state.layer = layer;
  884. output = &newInfo;
  885. }
  886. return output;
  887. }
  888. bool Animation::_getAnimatesRoot() const
  889. {
  890. if (mSkeleton == nullptr)
  891. return false;
  892. UINT32 rootBoneIdx = mSkeleton->getRootBoneIndex();
  893. if (rootBoneIdx == (UINT32)-1)
  894. return false;
  895. String rootBoneName = mSkeleton->getBoneInfo(rootBoneIdx).name;
  896. for (auto& entry : mClipInfos)
  897. {
  898. if (entry.clip.isLoaded())
  899. {
  900. HAnimationClip clip = entry.clip;
  901. if(!clip->hasRootMotion())
  902. {
  903. AnimationCurveMapping mapping;
  904. clip->getCurveMapping(rootBoneName, mapping);
  905. if (mapping.position != (UINT32)-1)
  906. return true;
  907. if (mapping.rotation != (UINT32)-1)
  908. return true;
  909. if (mapping.scale != (UINT32)-1)
  910. return true;
  911. }
  912. }
  913. }
  914. return false;
  915. }
  916. void Animation::getListenerResources(Vector<HResource>& resources)
  917. {
  918. for (auto& entry : mClipInfos)
  919. {
  920. if(entry.clip != nullptr)
  921. resources.push_back(entry.clip);
  922. }
  923. }
  924. void Animation::notifyResourceLoaded(const HResource& resource)
  925. {
  926. mDirty |= AnimDirtyStateFlag::Layout;
  927. }
  928. void Animation::notifyResourceChanged(const HResource& resource)
  929. {
  930. mDirty |= AnimDirtyStateFlag::Layout;
  931. }
  932. bool Animation::isPlaying() const
  933. {
  934. for(auto& clipInfo : mClipInfos)
  935. {
  936. if (clipInfo.clip.isLoaded())
  937. return true;
  938. }
  939. return false;
  940. }
  941. bool Animation::getState(const HAnimationClip& clip, AnimationClipState& state)
  942. {
  943. if (clip == nullptr)
  944. return false;
  945. for (auto& clipInfo : mClipInfos)
  946. {
  947. if (clipInfo.clip == clip)
  948. {
  949. state = clipInfo.state;
  950. // Internally we store unclamped time, so clamp/loop it
  951. float clipLength = 0.0f;
  952. if (clip.isLoaded())
  953. clipLength = clip->getLength();
  954. bool loop = clipInfo.state.wrapMode == AnimWrapMode::Loop;
  955. AnimationUtility::wrapTime(clipInfo.state.time, 0.0f, clipLength, loop);
  956. return true;
  957. }
  958. }
  959. return false;
  960. }
  961. void Animation::setState(const HAnimationClip& clip, AnimationClipState state)
  962. {
  963. if (state.layer == 0)
  964. state.layer = (UINT32)-1;
  965. else
  966. state.layer -= 1;
  967. AnimationClipInfo* clipInfo = addClip(clip, state.layer, false);
  968. if (clipInfo == nullptr)
  969. return;
  970. clipInfo->state = state;
  971. clipInfo->playbackType = AnimPlaybackType::Normal;
  972. mDirty |= AnimDirtyStateFlag::Value;
  973. }
  974. UINT32 Animation::getNumClips() const
  975. {
  976. return (UINT32)mClipInfos.size();
  977. }
  978. HAnimationClip Animation::getClip(UINT32 idx) const
  979. {
  980. if (idx >= (UINT32)mClipInfos.size())
  981. return HAnimationClip();
  982. return mClipInfos[idx].clip;
  983. }
  984. void Animation::triggerEvents(float lastFrameTime, float delta)
  985. {
  986. for (auto& clipInfo : mClipInfos)
  987. {
  988. if (!clipInfo.clip.isLoaded())
  989. continue;
  990. const Vector<AnimationEvent>& events = clipInfo.clip->getEvents();
  991. bool loop = clipInfo.state.wrapMode == AnimWrapMode::Loop;
  992. float start = lastFrameTime;
  993. float end = start + delta;
  994. float clipLength = clipInfo.clip->getLength();
  995. AnimationUtility::wrapTime(start, 0.0f, clipLength, loop);
  996. AnimationUtility::wrapTime(end, 0.0f, clipLength, loop);
  997. if (start < end)
  998. {
  999. for (auto& event : events)
  1000. {
  1001. if (event.time > start && event.time <= end)
  1002. onEventTriggered(clipInfo.clip, event.name);
  1003. }
  1004. }
  1005. else if(end < start) // End is looped, but start is not
  1006. {
  1007. for (auto& event : events)
  1008. {
  1009. if (event.time > start && event.time < clipLength && event.time > 0 && event.time <= end)
  1010. onEventTriggered(clipInfo.clip, event.name);
  1011. }
  1012. }
  1013. }
  1014. }
  1015. void Animation::mapCurveToSceneObject(const String& curve, const HSceneObject& so)
  1016. {
  1017. AnimatedSceneObject animSo = { so, curve };
  1018. mSceneObjects[so.getInstanceId()] = animSo;
  1019. mDirty |= AnimDirtyStateFlag::All;
  1020. }
  1021. void Animation::unmapSceneObject(const HSceneObject& so)
  1022. {
  1023. mSceneObjects.erase(so.getInstanceId());
  1024. mDirty |= AnimDirtyStateFlag::All;
  1025. }
  1026. bool Animation::getGenericCurveValue(UINT32 curveIdx, float& value)
  1027. {
  1028. if (!mGenericCurveValuesValid || curveIdx >= (UINT32)mGenericCurveOutputs.size())
  1029. return false;
  1030. value = mGenericCurveOutputs[curveIdx];
  1031. return true;
  1032. }
  1033. SPtr<Animation> Animation::create()
  1034. {
  1035. Animation* anim = new (bs_alloc<Animation>()) Animation();
  1036. SPtr<Animation> animPtr = bs_core_ptr(anim);
  1037. animPtr->_setThisPtr(animPtr);
  1038. animPtr->initialize();
  1039. return animPtr;
  1040. }
  1041. void Animation::updateAnimProxy(float timeDelta)
  1042. {
  1043. // Check if any of the clip curves are dirty and advance time, perform fading
  1044. for (auto& clipInfo : mClipInfos)
  1045. {
  1046. float scaledTimeDelta = timeDelta * clipInfo.state.speed;
  1047. clipInfo.state.time += scaledTimeDelta;
  1048. HAnimationClip clip = clipInfo.clip;
  1049. float clipLength = 0.0f;
  1050. if (clip.isLoaded())
  1051. {
  1052. if (clipInfo.curveVersion != clip->getVersion())
  1053. mDirty |= AnimDirtyStateFlag::Layout;
  1054. clipLength = clip->getLength();
  1055. }
  1056. float fadeTime = clipInfo.fadeTime + scaledTimeDelta;
  1057. clipInfo.fadeTime = Math::clamp(fadeTime, 0.0f, clipInfo.fadeLength);
  1058. }
  1059. if (mDirty.isSet(AnimDirtyStateFlag::Culling))
  1060. {
  1061. mAnimProxy->mCullEnabled = mCull;
  1062. mAnimProxy->mBounds = mBounds;
  1063. mDirty.unset(AnimDirtyStateFlag::Culling);
  1064. }
  1065. auto getAnimatedSOList = [&]()
  1066. {
  1067. Vector<AnimatedSceneObject> animatedSO(mSceneObjects.size());
  1068. UINT32 idx = 0;
  1069. for (auto& entry : mSceneObjects)
  1070. animatedSO[idx++] = entry.second;
  1071. return animatedSO;
  1072. };
  1073. bool didFullRebuild = false;
  1074. if((UINT32)mDirty == 0) // Clean
  1075. {
  1076. mAnimProxy->updateTime(mClipInfos);
  1077. }
  1078. else
  1079. {
  1080. if (mDirty.isSet(AnimDirtyStateFlag::All))
  1081. {
  1082. Vector<AnimatedSceneObject> animatedSOs = getAnimatedSOList();
  1083. mAnimProxy->rebuild(mSkeleton, mSkeletonMask, mClipInfos, animatedSOs, mMorphShapes);
  1084. didFullRebuild = true;
  1085. }
  1086. else if (mDirty.isSet(AnimDirtyStateFlag::Layout))
  1087. {
  1088. Vector<AnimatedSceneObject> animatedSOs = getAnimatedSOList();
  1089. mAnimProxy->rebuild(mClipInfos, animatedSOs, mMorphShapes);
  1090. didFullRebuild = true;
  1091. }
  1092. else if(mDirty.isSet(AnimDirtyStateFlag::Value))
  1093. mAnimProxy->updateClipInfos(mClipInfos);
  1094. if (mDirty.isSet(AnimDirtyStateFlag::MorphWeights) || didFullRebuild)
  1095. mAnimProxy->updateMorphChannelWeights(mMorphChannelWeights);
  1096. }
  1097. // Check if there are dirty transforms
  1098. if (!didFullRebuild)
  1099. {
  1100. for (UINT32 i = 0; i < mAnimProxy->numSceneObjects; i++)
  1101. {
  1102. AnimatedSceneObjectInfo& soInfo = mAnimProxy->sceneObjectInfos[i];
  1103. auto iterFind = mSceneObjects.find(soInfo.id);
  1104. if (iterFind == mSceneObjects.end())
  1105. {
  1106. assert(false); // Should never happen
  1107. continue;
  1108. }
  1109. UINT32 hash;
  1110. HSceneObject so = iterFind->second.so;
  1111. if (so.isDestroyed(true))
  1112. hash = 0;
  1113. else
  1114. hash = so->getTransformHash();
  1115. if (hash != mAnimProxy->sceneObjectInfos[i].hash)
  1116. {
  1117. Vector<AnimatedSceneObject> animatedSOs = getAnimatedSOList();
  1118. mAnimProxy->updateTransforms(animatedSOs);
  1119. break;
  1120. }
  1121. }
  1122. }
  1123. // Disable sampled animations
  1124. for (auto& clipInfo : mClipInfos)
  1125. {
  1126. if (clipInfo.playbackType == AnimPlaybackType::Sampled)
  1127. clipInfo.playbackType = AnimPlaybackType::None;
  1128. }
  1129. mDirty = AnimDirtyState();
  1130. }
  1131. void Animation::updateFromProxy()
  1132. {
  1133. HSceneObject rootSO;
  1134. // Write TRS animation results to relevant SceneObjects
  1135. for(UINT32 i = 0; i < mAnimProxy->numSceneObjects; i++)
  1136. {
  1137. AnimatedSceneObjectInfo& soInfo = mAnimProxy->sceneObjectInfos[i];
  1138. auto iterFind = mSceneObjects.find(soInfo.id);
  1139. if (iterFind == mSceneObjects.end())
  1140. continue;
  1141. HSceneObject so = iterFind->second.so;
  1142. if (iterFind->second.curveName.empty())
  1143. rootSO = so;
  1144. if (so.isDestroyed(true))
  1145. continue;
  1146. if(soInfo.boneIdx != -1)
  1147. {
  1148. if (mAnimProxy->skeletonPose.hasOverride[soInfo.boneIdx])
  1149. continue;
  1150. Vector3 position = mAnimProxy->skeletonPose.positions[soInfo.boneIdx];
  1151. Quaternion rotation = mAnimProxy->skeletonPose.rotations[soInfo.boneIdx];
  1152. Vector3 scale = mAnimProxy->skeletonPose.scales[soInfo.boneIdx];
  1153. const SPtr<Skeleton>& skeleton = mAnimProxy->skeleton;
  1154. UINT32 parentBoneIdx = skeleton->getBoneInfo(soInfo.boneIdx).parent;
  1155. if (parentBoneIdx == -1)
  1156. {
  1157. so->setPosition(position);
  1158. so->setRotation(rotation);
  1159. so->setScale(scale);
  1160. }
  1161. else
  1162. {
  1163. while(parentBoneIdx != -1)
  1164. {
  1165. // Update rotation
  1166. const Quaternion& parentOrientation = mAnimProxy->skeletonPose.rotations[parentBoneIdx];
  1167. rotation = parentOrientation * rotation;
  1168. // Update scale
  1169. const Vector3& parentScale = mAnimProxy->skeletonPose.scales[parentBoneIdx];
  1170. scale = parentScale * scale;
  1171. // Update position
  1172. position = parentOrientation.rotate(parentScale * position);
  1173. position += mAnimProxy->skeletonPose.positions[parentBoneIdx];
  1174. parentBoneIdx = skeleton->getBoneInfo(parentBoneIdx).parent;
  1175. }
  1176. // Search for root if not already found
  1177. if(rootSO == nullptr)
  1178. {
  1179. for(auto& entry : mSceneObjects)
  1180. {
  1181. if (entry.second.curveName.empty())
  1182. rootSO = entry.second.so;
  1183. }
  1184. }
  1185. Vector3 rootParentPos = Vector3::ZERO;
  1186. Quaternion rootParentRot = Quaternion::IDENTITY;
  1187. Vector3 rootParentScale = Vector3::ONE;
  1188. if(rootSO.isDestroyed(true))
  1189. {
  1190. HSceneObject rootParent = rootSO->getParent();
  1191. if(!rootParent.isDestroyed(true))
  1192. {
  1193. rootParentPos = rootParent->getWorldPosition();
  1194. rootParentRot = rootParent->getWorldRotation();
  1195. rootParentScale = rootParent->getWorldScale();
  1196. }
  1197. }
  1198. // Transform from space relative to root's parent to world space
  1199. rotation = rootParentRot * rotation;
  1200. scale = rootParentScale * scale;
  1201. position = rootParentRot.rotate(rootParentScale * position);
  1202. position += rootParentPos;
  1203. so->setWorldPosition(position);
  1204. so->setWorldRotation(rotation);
  1205. so->setWorldScale(scale);
  1206. }
  1207. }
  1208. else
  1209. {
  1210. if (mAnimProxy->sceneObjectPose.hasOverride[i])
  1211. continue;
  1212. so->setPosition(mAnimProxy->sceneObjectPose.positions[i]);
  1213. so->setRotation(mAnimProxy->sceneObjectPose.rotations[i]);
  1214. so->setScale(mAnimProxy->sceneObjectPose.scales[i]);
  1215. }
  1216. }
  1217. // Must ensure that clip in the proxy and current primary clip are the same
  1218. mGenericCurveValuesValid = false;
  1219. if(mAnimProxy->numLayers > 0 && mAnimProxy->layers[0].numStates > 0)
  1220. {
  1221. const AnimationState& state = mAnimProxy->layers[0].states[0];
  1222. if(!state.disabled && mClipInfos.size() > 0)
  1223. {
  1224. const AnimationClipInfo& clipInfo = mClipInfos[0];
  1225. if (clipInfo.stateIdx == 0 && clipInfo.layerIdx == 0)
  1226. {
  1227. if (clipInfo.clip.isLoaded() && clipInfo.curveVersion == clipInfo.clip->getVersion())
  1228. {
  1229. UINT32 numGenericCurves = (UINT32)clipInfo.clip->getCurves()->generic.size();
  1230. mGenericCurveValuesValid = numGenericCurves == mAnimProxy->numGenericCurves;
  1231. }
  1232. }
  1233. }
  1234. }
  1235. if(mGenericCurveValuesValid)
  1236. {
  1237. mGenericCurveOutputs.resize(mAnimProxy->numGenericCurves);
  1238. memcpy(mGenericCurveOutputs.data(), mAnimProxy->genericCurveOutputs, mAnimProxy->numGenericCurves * sizeof(float));
  1239. }
  1240. }
  1241. }