BsAnimation.cpp 39 KB

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