| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "BsAnimation.h"
- #include "BsAnimationManager.h"
- #include "BsAnimationClip.h"
- #include "BsAnimationUtility.h"
- #include "BsSceneObject.h"
- namespace BansheeEngine
- {
- AnimationClipInfo::AnimationClipInfo()
- : fadeDirection(0.0f), fadeTime(0.0f), fadeLength(0.0f), curveVersion(0), layerIdx((UINT32)-1), stateIdx((UINT32)-1)
- { }
- AnimationClipInfo::AnimationClipInfo(const HAnimationClip& clip)
- : fadeDirection(0.0f), fadeTime(0.0f), fadeLength(0.0f), clip(clip), curveVersion(0), layerIdx((UINT32)-1), stateIdx((UINT32)-1)
- { }
- Blend1DInfo::Blend1DInfo(UINT32 numClips)
- : clips(nullptr), numClips(numClips)
- {
- if (numClips > 0)
- clips = bs_newN<BlendClipInfo>(numClips);
- }
- Blend1DInfo::~Blend1DInfo()
- {
- if(clips != nullptr)
- bs_deleteN(clips, numClips);
- }
- AnimationProxy::AnimationProxy(UINT64 id)
- : id(id), layers(nullptr), numLayers(0), numSceneObjects(0), sceneObjectInfos(nullptr)
- , sceneObjectTransforms(nullptr), genericCurveOutputs(nullptr)
- { }
- AnimationProxy::~AnimationProxy()
- {
- clear();
- }
- void AnimationProxy::clear()
- {
- if (layers == nullptr)
- return;
- for(UINT32 i = 0; i < numLayers; i++)
- {
- AnimationStateLayer& layer = layers[i];
- for(UINT32 j = 0; j < layer.numStates; j++)
- {
- AnimationState& state = layer.states[j];
- if(state.curves != nullptr)
- {
- {
- UINT32 numCurves = (UINT32)state.curves->position.size();
- for (UINT32 k = 0; k < numCurves; k++)
- state.positionCaches[k].~TCurveCache();
- }
- {
- UINT32 numCurves = (UINT32)state.curves->rotation.size();
- for (UINT32 k = 0; k < numCurves; k++)
- state.rotationCaches[k].~TCurveCache();
- }
- {
- UINT32 numCurves = (UINT32)state.curves->scale.size();
- for (UINT32 k = 0; k < numCurves; k++)
- state.scaleCaches[k].~TCurveCache();
- }
- {
- UINT32 numCurves = (UINT32)state.curves->generic.size();
- for (UINT32 k = 0; k < numCurves; k++)
- state.genericCaches[k].~TCurveCache();
- }
- }
- if(skeleton != nullptr)
- {
- UINT32 numBones = skeleton->getNumBones();
- for (UINT32 k = 0; k < numBones; k++)
- state.boneToCurveMapping[k].~AnimationCurveMapping();
- }
- if(state.soToCurveMapping != nullptr)
- {
- for(UINT32 k = 0; k < numSceneObjects; k++)
- state.soToCurveMapping[k].~AnimationCurveMapping();
- }
- state.~AnimationState();
- }
- layer.~AnimationStateLayer();
- }
- // All of the memory is part of the same buffer, so we only need to free the first element
- bs_free(layers);
- layers = nullptr;
- genericCurveOutputs = nullptr;
- sceneObjectInfos = nullptr;
- sceneObjectTransforms = nullptr;
- numLayers = 0;
- numSceneObjects = 0;
- numGenericCurves = 0;
- }
- void AnimationProxy::rebuild(const SPtr<Skeleton>& skeleton, const SkeletonMask& mask,
- Vector<AnimationClipInfo>& clipInfos, const Vector<AnimatedSceneObject>& sceneObjects)
- {
- this->skeleton = skeleton;
- this->skeletonMask = skeletonMask;
- // Note: I could avoid having a separate allocation for LocalSkeletonPoses and use the same buffer as the rest
- // of AnimationProxy
- if (skeleton != nullptr)
- skeletonPose = LocalSkeletonPose(skeleton->getNumBones());
- numSceneObjects = (UINT32)sceneObjects.size();
- if (numSceneObjects > 0)
- sceneObjectPose = LocalSkeletonPose(numSceneObjects);
- else
- sceneObjectPose = LocalSkeletonPose();
- rebuild(clipInfos, sceneObjects);
- }
- void AnimationProxy::rebuild(Vector<AnimationClipInfo>& clipInfos, const Vector<AnimatedSceneObject>& sceneObjects)
- {
- clear();
- bs_frame_mark();
- {
- FrameVector<bool> clipLoadState(clipInfos.size());
- FrameVector<AnimationStateLayer> tempLayers;
- UINT32 clipIdx = 0;
- for (auto& clipInfo : clipInfos)
- {
- UINT32 layer = clipInfo.state.layer;
- if (layer == (UINT32)-1)
- layer = 0;
- else
- layer += 1;
- auto iterFind = std::find_if(tempLayers.begin(), tempLayers.end(),
- [&](auto& x)
- {
- return x.index == layer;
- });
- bool isLoaded = clipInfo.clip.isLoaded();
- clipLoadState[clipIdx] = isLoaded;
- if (iterFind == tempLayers.end())
- {
- tempLayers.push_back(AnimationStateLayer());
- AnimationStateLayer& newLayer = tempLayers.back();
- newLayer.index = layer;
- newLayer.additive = isLoaded && clipInfo.clip->isAdditive();
- }
- clipIdx++;
- }
- std::sort(tempLayers.begin(), tempLayers.end(),
- [&](auto& x, auto& y)
- {
- return x.index < y.index;
- });
- numLayers = (UINT32)tempLayers.size();
- UINT32 numClips = (UINT32)clipInfos.size();
- UINT32 numBones;
-
- if (skeleton != nullptr)
- numBones = skeleton->getNumBones();
- else
- numBones = 0;
- UINT32 numPosCurves = 0;
- UINT32 numRotCurves = 0;
- UINT32 numScaleCurves = 0;
- clipIdx = 0;
- for (auto& clipInfo : clipInfos)
- {
- bool isLoaded = clipLoadState[clipIdx++];
- if (!isLoaded)
- continue;
- SPtr<AnimationCurves> curves = clipInfo.clip->getCurves();
- numPosCurves += (UINT32)curves->position.size();
- numRotCurves += (UINT32)curves->rotation.size();
- numScaleCurves += (UINT32)curves->scale.size();
- }
- numGenericCurves = 0;
- if(clipInfos.size() > 0 && clipLoadState[0])
- {
- SPtr<AnimationCurves> curves = clipInfos[0].clip->getCurves();
- numGenericCurves = (UINT32)curves->generic.size();
- }
- UINT32* mappedBoneIndices = (UINT32*)bs_frame_alloc(sizeof(UINT32) * numSceneObjects);
- for (UINT32 i = 0; i < numSceneObjects; i++)
- mappedBoneIndices[i] = -1;
- UINT32 numBoneMappedSOs = 0;
- if (skeleton != nullptr)
- {
- for (UINT32 i = 0; i < numSceneObjects; i++)
- {
- if (sceneObjects[i].so.isDestroyed(true))
- continue;
- // Empty string always means root bone
- if (sceneObjects[i].curveName.empty())
- {
- UINT32 rootBoneIdx = skeleton->getRootBoneIndex();
- if (rootBoneIdx != (UINT32)-1)
- {
- mappedBoneIndices[i] = rootBoneIdx;
- numBoneMappedSOs++;
- }
- }
- else
- {
- for (UINT32 j = 0; j < numBones; j++)
- {
- if (skeleton->getBoneInfo(j).name == sceneObjects[i].curveName)
- {
- mappedBoneIndices[i] = j;
- numBoneMappedSOs++;
- break;
- }
- }
- }
- }
- }
- UINT32 numBoneMappings = numBones * numClips;
- UINT32 layersSize = sizeof(AnimationStateLayer) * numLayers;
- UINT32 clipsSize = sizeof(AnimationState) * numClips;
- UINT32 boneMappingSize = numBoneMappings * sizeof(AnimationCurveMapping);
- UINT32 posCacheSize = numPosCurves * sizeof(TCurveCache<Vector3>);
- UINT32 rotCacheSize = numRotCurves * sizeof(TCurveCache<Quaternion>);
- UINT32 scaleCacheSize = numScaleCurves * sizeof(TCurveCache<Vector3>);
- UINT32 genCacheSize = numGenericCurves * sizeof(TCurveCache<float>);
- UINT32 genericCurveOutputSize = numGenericCurves * sizeof(float);
- UINT32 sceneObjectIdsSize = numSceneObjects * sizeof(AnimatedSceneObjectInfo);
- UINT32 sceneObjectTransformsSize = numBoneMappedSOs * sizeof(Matrix4);
- UINT8* data = (UINT8*)bs_alloc(layersSize + clipsSize + boneMappingSize + posCacheSize + rotCacheSize +
- scaleCacheSize + genCacheSize + genericCurveOutputSize + sceneObjectIdsSize + sceneObjectTransformsSize);
- layers = (AnimationStateLayer*)data;
- memcpy(layers, tempLayers.data(), layersSize);
- data += layersSize;
- AnimationState* states = (AnimationState*)data;
- for(UINT32 i = 0; i < numClips; i++)
- new (&states[i]) AnimationState();
- data += clipsSize;
- AnimationCurveMapping* boneMappings = (AnimationCurveMapping*)data;
- for (UINT32 i = 0; i < numBoneMappings; i++)
- new (&boneMappings[i]) AnimationCurveMapping();
- data += boneMappingSize;
- TCurveCache<Vector3>* posCache = (TCurveCache<Vector3>*)data;
- for (UINT32 i = 0; i < numPosCurves; i++)
- new (&posCache[i]) TCurveCache<Vector3>();
- data += posCacheSize;
- TCurveCache<Quaternion>* rotCache = (TCurveCache<Quaternion>*)data;
- for (UINT32 i = 0; i < numRotCurves; i++)
- new (&rotCache[i]) TCurveCache<Quaternion>();
- data += rotCacheSize;
- TCurveCache<Vector3>* scaleCache = (TCurveCache<Vector3>*)data;
- for (UINT32 i = 0; i < numScaleCurves; i++)
- new (&scaleCache[i]) TCurveCache<Vector3>();
- data += scaleCacheSize;
- TCurveCache<float>* genCache = (TCurveCache<float>*)data;
- for (UINT32 i = 0; i < numGenericCurves; i++)
- new (&genCache[i]) TCurveCache<float>();
- data += genCacheSize;
- genericCurveOutputs = (float*)data;
- data += genericCurveOutputSize;
- sceneObjectInfos = (AnimatedSceneObjectInfo*)data;
- data += sceneObjectIdsSize;
- sceneObjectTransforms = (Matrix4*)data;
- for (UINT32 i = 0; i < numBoneMappedSOs; i++)
- sceneObjectTransforms[i] = Matrix4::IDENTITY;
- data += sceneObjectTransformsSize;
- UINT32 curLayerIdx = 0;
- UINT32 curStateIdx = 0;
- // Note: Hidden dependency. First clip info must be in layers[0].states[0] (needed for generic curves which only
- // use the primary clip).
- for(UINT32 i = 0; i < numLayers; i++)
- {
- AnimationStateLayer& layer = layers[i];
- layer.states = &states[curStateIdx];
- layer.numStates = 0;
- UINT32 localStateIdx = 0;
- for(UINT32 j = 0; j < (UINT32)clipInfos.size(); j++)
- {
- AnimationClipInfo& clipInfo = clipInfos[j];
- UINT32 clipLayer = clipInfo.state.layer;
- if (clipLayer == (UINT32)-1)
- clipLayer = 0;
- else
- clipLayer += 1;
- if (clipLayer != layer.index)
- continue;
- AnimationState& state = states[curStateIdx];
- state.loop = clipInfo.state.wrapMode == AnimWrapMode::Loop;
- state.time = clipInfo.state.time;
- // Calculate weight if fading is active
- float weight = clipInfo.state.weight;
- //// Assumes time is clamped to [0, fadeLength] and fadeLength != 0
- if(clipInfo.fadeDirection < 0.0f)
- {
- float t = clipInfo.fadeTime / clipInfo.fadeLength;
- weight *= (1.0f - t);
- }
- else if(clipInfo.fadeDirection > 0.0f)
- {
- float t = clipInfo.fadeTime / clipInfo.fadeLength;
- weight *= t;
- }
- state.weight = weight;
- // Set up individual curves and their caches
- bool isClipValid = clipLoadState[j];
- if (isClipValid)
- {
- state.curves = clipInfo.clip->getCurves();
- state.disabled = false;
- }
- else
- {
- static SPtr<AnimationCurves> zeroCurves = bs_shared_ptr_new<AnimationCurves>();
- state.curves = zeroCurves;
- state.disabled = true;
- }
- state.positionCaches = posCache;
- posCache += state.curves->position.size();
- state.rotationCaches = rotCache;
- rotCache += state.curves->rotation.size();
- state.scaleCaches = scaleCache;
- scaleCache += state.curves->scale.size();
- state.genericCaches = genCache;
- genCache += state.curves->generic.size();
- clipInfo.layerIdx = curLayerIdx;
- clipInfo.stateIdx = localStateIdx;
- if(isClipValid)
- clipInfo.curveVersion = clipInfo.clip->getVersion();
- // Set up bone mapping
- if (skeleton != nullptr)
- {
- state.boneToCurveMapping = &boneMappings[curStateIdx * numBones];
- if (isClipValid)
- {
- clipInfo.clip->getBoneMapping(*skeleton, state.boneToCurveMapping);
- }
- else
- {
- AnimationCurveMapping emptyMapping = { (UINT32)-1, (UINT32)-1, (UINT32)-1 };
- for (UINT32 i = 0; i < numBones; i++)
- state.boneToCurveMapping[i] = emptyMapping;
- }
- }
- else
- state.boneToCurveMapping = nullptr;
- layer.numStates++;
- curStateIdx++;
- localStateIdx++;
- }
- curLayerIdx++;
- // Must be larger than zero otherwise the layer.states pointer will point to data held by some other layer
- assert(layer.numStates > 0);
- }
- UINT32 boneIdx = 0;
- for(UINT32 i = 0; i < numSceneObjects; i++)
- {
- HSceneObject so = sceneObjects[i].so;
- AnimatedSceneObjectInfo& soInfo = sceneObjectInfos[i];
- soInfo.id = so.getInstanceId();
- soInfo.boneIdx = mappedBoneIndices[i];
- bool isSOValid = !so.isDestroyed(true);
- if (isSOValid)
- soInfo.hash = so->getTransformHash();
- else
- soInfo.hash = 0;
- // If no bone mapping, find curves directly
- if(soInfo.boneIdx == -1)
- {
- soInfo.curveIndices = { (UINT32)-1, (UINT32)-1, (UINT32)-1 };
- if (isSOValid)
- {
- for (UINT32 j = 0; j < (UINT32)clipInfos.size(); j++)
- {
- AnimationClipInfo& clipInfo = clipInfos[j];
- soInfo.layerIdx = clipInfo.layerIdx;
- soInfo.stateIdx = clipInfo.stateIdx;
- bool isClipValid = clipLoadState[j];
- if (isClipValid)
- {
- // Note: If there are multiple clips with the relevant curve name, we only use the first
- clipInfo.clip->getCurveMapping(sceneObjects[i].curveName, soInfo.curveIndices);
- break;
- }
- }
- }
- }
- else
- {
- // No need to check if SO is valid, if it has a bone connection it must be
- sceneObjectTransforms[boneIdx] = so->getWorldTfrm();
- boneIdx++;
- }
- }
- bs_frame_free(mappedBoneIndices);
- }
- bs_frame_clear();
- }
- void AnimationProxy::updateValues(const Vector<AnimationClipInfo>& clipInfos)
- {
- for(auto& clipInfo : clipInfos)
- {
- AnimationState& state = layers[clipInfo.layerIdx].states[clipInfo.stateIdx];
- state.loop = clipInfo.state.wrapMode == AnimWrapMode::Loop;
- state.weight = clipInfo.state.weight;
- state.time = clipInfo.state.time;
- }
- }
- void AnimationProxy::updateTransforms(const Vector<AnimatedSceneObject>& sceneObjects)
- {
- UINT32 boneIdx = 0;
- for (UINT32 i = 0; i < numSceneObjects; i++)
- {
- HSceneObject so = sceneObjects[i].so;
- if (so.isDestroyed(true))
- {
- sceneObjectInfos[i].hash = 0;
- continue;
- }
- sceneObjectInfos[i].hash = so->getTransformHash();
- if (sceneObjectInfos[i].boneIdx == -1)
- continue;
- sceneObjectTransforms[boneIdx] = sceneObjects[i].so->getWorldTfrm();
- boneIdx++;
- }
- }
- void AnimationProxy::updateTime(const Vector<AnimationClipInfo>& clipInfos)
- {
- for (auto& clipInfo : clipInfos)
- {
- AnimationState& state = layers[clipInfo.layerIdx].states[clipInfo.stateIdx];
- state.time = clipInfo.state.time;
- }
- }
- Animation::Animation()
- : mDefaultWrapMode(AnimWrapMode::Loop), mDefaultSpeed(1.0f), mDirty(AnimDirtyStateFlag::Skeleton)
- , mGenericCurveValuesValid(false)
- {
- mId = AnimationManager::instance().registerAnimation(this);
- mAnimProxy = bs_shared_ptr_new<AnimationProxy>(mId);
- }
- Animation::~Animation()
- {
- AnimationManager::instance().unregisterAnimation(mId);
- }
- void Animation::setSkeleton(const SPtr<Skeleton>& skeleton)
- {
- mSkeleton = skeleton;
- mDirty |= AnimDirtyStateFlag::Skeleton;
- }
- void Animation::setMask(const SkeletonMask& mask)
- {
- mSkeletonMask = mask;
- mDirty |= AnimDirtyStateFlag::Skeleton;
- }
- void Animation::setWrapMode(AnimWrapMode wrapMode)
- {
- mDefaultWrapMode = wrapMode;
- for (auto& clipInfo : mClipInfos)
- clipInfo.state.wrapMode = wrapMode;
- mDirty |= AnimDirtyStateFlag::Value;
- }
- void Animation::setSpeed(float speed)
- {
- mDefaultSpeed = speed;
- for (auto& clipInfo : mClipInfos)
- {
- // Special case: Ignore non-moving ones
- if(!clipInfo.state.stopped)
- clipInfo.state.speed = speed;
- }
- mDirty |= AnimDirtyStateFlag::Value;
- }
- void Animation::play(const HAnimationClip& clip)
- {
- AnimationClipInfo* clipInfo = addClip(clip, (UINT32)-1);
- if(clipInfo != nullptr)
- {
- clipInfo->state.time = 0.0f;
- clipInfo->state.speed = mDefaultSpeed;
- clipInfo->state.weight = 1.0f;
- clipInfo->state.wrapMode = mDefaultWrapMode;
- }
- mDirty |= AnimDirtyStateFlag::Value;
- }
- void Animation::blendAdditive(const HAnimationClip& clip, float weight, float fadeLength, UINT32 layer)
- {
- if(clip != nullptr && !clip->isAdditive())
- {
- LOGWRN("blendAdditive() called with a clip that doesn't contain additive animation. Ignoring.");
- // Stop any clips on this layer, even if invalid
- HAnimationClip nullClip;
- addClip(nullClip, layer);
- return;
- }
- AnimationClipInfo* clipInfo = addClip(clip, layer);
- if (clipInfo != nullptr)
- {
- clipInfo->state.time = 0.0f;
- clipInfo->state.speed = mDefaultSpeed;
- clipInfo->state.weight = weight;
- clipInfo->state.wrapMode = mDefaultWrapMode;
- if(fadeLength > 0.0f)
- {
- clipInfo->fadeDirection = 1.0f;
- clipInfo->fadeTime = 0.0f;
- clipInfo->fadeLength = fadeLength;
- }
- mDirty |= AnimDirtyStateFlag::Value;
- }
- }
- void Animation::blend1D(const Blend1DInfo& info, float t)
- {
- if (info.numClips == 0)
- return;
- // Find valid range
- float startPos = 0.0f;
- float endPos = 0.0f;
- for (UINT32 i = 0; i < info.numClips; i++)
- {
- startPos = std::min(startPos, info.clips[i].position);
- endPos = std::min(endPos, info.clips[i].position);
- }
- float length = endPos - startPos;
- if(Math::approxEquals(length, 0.0f) || info.numClips < 2)
- {
- play(info.clips[0].clip);
- return;
- }
- // Clamp or loop time
- bool loop = mDefaultWrapMode == AnimWrapMode::Loop;
- if (t < startPos)
- {
- if (loop)
- t = t - std::floor(t / length) * length;
- else // Clamping
- t = startPos;
- }
- if (t > endPos)
- {
- if (loop)
- t = t - std::floor(t / length) * length;
- else // Clamping
- t = endPos;
- }
- // Find keys to blend between
- UINT32 leftKey = 0;
- UINT32 rightKey = 0;
- INT32 start = 0;
- INT32 searchLength = (INT32)info.numClips;
- while (searchLength > 0)
- {
- INT32 half = searchLength >> 1;
- INT32 mid = start + half;
- if (t < info.clips[mid].position)
- {
- searchLength = half;
- }
- else
- {
- start = mid + 1;
- searchLength -= (half + 1);
- }
- }
- leftKey = std::max(0, start - 1);
- rightKey = std::min(start, (INT32)info.numClips - 1);
- float interpLength = info.clips[rightKey].position - info.clips[leftKey].position;
- t = (t - info.clips[leftKey].position) / interpLength;
- // Add clips and set weights
- for(UINT32 i = 0; i < info.numClips; i++)
- {
- AnimationClipInfo* clipInfo = addClip(info.clips[i].clip, (UINT32)-1, i == 0);
- if (clipInfo != nullptr)
- {
- clipInfo->state.time = 0.0f;
- clipInfo->state.stopped = true;
- clipInfo->state.speed = 0.0f;
- clipInfo->state.wrapMode = AnimWrapMode::Clamp;
- if (i == leftKey)
- clipInfo->state.weight = 1.0f - t;
- else if (i == rightKey)
- clipInfo->state.weight = t;
- else
- clipInfo->state.weight = 0.0f;
- }
- }
- mDirty |= AnimDirtyStateFlag::Value;
- }
- void Animation::blend2D(const Blend2DInfo& info, const Vector2& t)
- {
- AnimationClipInfo* topLeftClipInfo = addClip(info.topLeftClip, (UINT32)-1, true);
- if (topLeftClipInfo != nullptr)
- {
- topLeftClipInfo->state.time = 0.0f;
- topLeftClipInfo->state.stopped = true;
- topLeftClipInfo->state.speed = 0.0f;
- topLeftClipInfo->state.weight = (1.0f - t.x) * (1.0f - t.y);
- topLeftClipInfo->state.wrapMode = AnimWrapMode::Clamp;
- }
- AnimationClipInfo* topRightClipInfo = addClip(info.topRightClip, (UINT32)-1, false);
- if (topRightClipInfo != nullptr)
- {
- topRightClipInfo->state.time = 0.0f;
- topRightClipInfo->state.stopped = true;
- topLeftClipInfo->state.speed = 0.0f;
- topRightClipInfo->state.weight = t.x * (1.0f - t.y);
- topRightClipInfo->state.wrapMode = AnimWrapMode::Clamp;
- }
- AnimationClipInfo* botLeftClipInfo = addClip(info.botLeftClip, (UINT32)-1, false);
- if (botLeftClipInfo != nullptr)
- {
- botLeftClipInfo->state.time = 0.0f;
- botLeftClipInfo->state.stopped = true;
- topLeftClipInfo->state.speed = 0.0f;
- botLeftClipInfo->state.weight = (1.0f - t.x) * t.y;
- botLeftClipInfo->state.wrapMode = AnimWrapMode::Clamp;
- }
- AnimationClipInfo* botRightClipInfo = addClip(info.botRightClip, (UINT32)-1, false);
- if (botRightClipInfo != nullptr)
- {
- botRightClipInfo->state.time = 0.0f;
- botRightClipInfo->state.stopped = true;
- botRightClipInfo->state.speed = 0.0f;
- botRightClipInfo->state.weight = t.x * t.y;
- botRightClipInfo->state.wrapMode = AnimWrapMode::Clamp;
- }
- mDirty |= AnimDirtyStateFlag::Value;
- }
- void Animation::crossFade(const HAnimationClip& clip, float fadeLength)
- {
- bool isFading = fadeLength > 0.0f;
- if(!isFading)
- {
- play(clip);
- return;
- }
- AnimationClipInfo* clipInfo = addClip(clip, (UINT32)-1, false);
- if (clipInfo != nullptr)
- {
- clipInfo->state.time = 0.0f;
- clipInfo->state.speed = mDefaultSpeed;
- clipInfo->state.weight = 1.0f;
- clipInfo->state.wrapMode = mDefaultWrapMode;
- // Set up fade lengths
- clipInfo->fadeDirection = 1.0f;
- clipInfo->fadeTime = 0.0f;
- clipInfo->fadeLength = fadeLength;
- for (auto& entry : mClipInfos)
- {
- if (entry.state.layer == (UINT32)-1 && entry.clip != clip)
- {
- // If other clips are already cross-fading, we need to persist their current weight before starting
- // a new crossfade. We do that by adjusting the fade times.
- if(clipInfo->fadeDirection != 0 && clipInfo->fadeTime < clipInfo->fadeLength)
- {
- float t = clipInfo->fadeTime / clipInfo->fadeLength;
- if (clipInfo->fadeDirection < 0.0f)
- t = (1.0f - t);
- clipInfo->state.weight *= t;
- }
- clipInfo->fadeDirection = -1.0f;
- clipInfo->fadeTime = 0.0f;
- clipInfo->fadeLength = fadeLength;
- }
- }
- }
- mDirty |= AnimDirtyStateFlag::Value;
- }
- void Animation::stop(UINT32 layer)
- {
- bs_frame_mark();
- {
- FrameVector<AnimationClipInfo> newClips;
- for (auto& clipInfo : mClipInfos)
- {
- if (clipInfo.state.layer != layer)
- newClips.push_back(clipInfo);
- else
- mDirty |= AnimDirtyStateFlag::Layout;
- }
- mClipInfos.resize(newClips.size());
- memcpy(mClipInfos.data(), newClips.data(), sizeof(AnimationClipInfo) * newClips.size());
- }
- bs_frame_clear();
- }
- void Animation::stopAll()
- {
- mClipInfos.clear();
- mDirty |= AnimDirtyStateFlag::Layout;
- }
- AnimationClipInfo* Animation::addClip(const HAnimationClip& clip, UINT32 layer, bool stopExisting)
- {
- AnimationClipInfo* output = nullptr;
- bool hasExisting = false;
- // Search for existing
- for (auto& clipInfo : mClipInfos)
- {
- if (clipInfo.state.layer == layer)
- {
- if (clipInfo.clip == clip)
- output = &clipInfo;
- else if (stopExisting)
- hasExisting = true;
- }
- }
- // Doesn't exist or found extra animations, rebuild
- if (output == nullptr || hasExisting)
- {
- bs_frame_mark();
- {
- FrameVector<AnimationClipInfo> newClips;
- for (auto& clipInfo : mClipInfos)
- {
- if (!stopExisting || clipInfo.state.layer != layer || clipInfo.clip == clip)
- newClips.push_back(clipInfo);
- }
- if (output == nullptr && clip != nullptr)
- newClips.push_back(AnimationClipInfo());
- mClipInfos.resize(newClips.size());
- memcpy(mClipInfos.data(), newClips.data(), sizeof(AnimationClipInfo) * newClips.size());
- mDirty |= AnimDirtyStateFlag::Layout;
- }
- bs_frame_clear();
- }
- // If new clip was added, get its address
- if (output == nullptr && clip != nullptr)
- {
- AnimationClipInfo& newInfo = mClipInfos.back();
- newInfo.clip = clip;
- newInfo.layerIdx = layer;
- output = &newInfo;
- }
-
- return output;
- }
- bool Animation::isPlaying() const
- {
- for(auto& clipInfo : mClipInfos)
- {
- if (clipInfo.clip.isLoaded())
- return true;
- }
- return false;
- }
- bool Animation::getState(const HAnimationClip& clip, AnimationClipState& state)
- {
- if (clip == nullptr)
- return false;
- for (auto& clipInfo : mClipInfos)
- {
- if (clipInfo.clip == clip)
- {
- state = clipInfo.state;
- return true;
- }
- }
-
- return false;
- }
- void Animation::setState(const HAnimationClip& clip, AnimationClipState state)
- {
- AnimationClipInfo* clipInfo = addClip(clip, state.layer, false);
- if (clipInfo == nullptr)
- return;
- clipInfo->state = state;
- mDirty |= AnimDirtyStateFlag::Value;
- }
- UINT32 Animation::getNumClips() const
- {
- return (UINT32)mClipInfos.size();
- }
- HAnimationClip Animation::getClip(UINT32 idx) const
- {
- if (idx >= (UINT32)mClipInfos.size())
- return HAnimationClip();
- return mClipInfos[idx].clip;
- }
- void Animation::triggerEvents(float lastFrameTime, float delta)
- {
- for (auto& clipInfo : mClipInfos)
- {
- if (!clipInfo.clip.isLoaded())
- continue;
- const Vector<AnimationEvent>& events = clipInfo.clip->getEvents();
- bool loop = clipInfo.state.wrapMode == AnimWrapMode::Loop;
- float start = lastFrameTime;
- float end = start + delta;
- float clipLength = clipInfo.clip->getLength();
- AnimationUtility::wrapTime(start, 0.0f, clipLength, loop);
- AnimationUtility::wrapTime(end, 0.0f, clipLength, false);
- for (auto& event : events)
- {
- if (event.time > start && event.time <= end)
- onEventTriggered(clipInfo.clip, event.name);
- }
- // Check the looped portion
- if(loop && end >= clipLength)
- {
- start = 0.0f;
- end = end - clipLength;
- for (auto& event : events)
- {
- if (event.time > start && event.time <= end)
- onEventTriggered(clipInfo.clip, event.name);
- }
- }
- }
- }
- void Animation::mapCurveToSceneObject(const String& curve, const HSceneObject& so)
- {
- AnimatedSceneObject animSo = { so, curve };
- mSceneObjects[so.getInstanceId()] = animSo;
- mDirty |= AnimDirtyStateFlag::Skeleton;
- }
- void Animation::unmapSceneObject(const HSceneObject& so)
- {
- mSceneObjects.erase(so.getInstanceId());
- mDirty |= AnimDirtyStateFlag::Skeleton;
- }
- bool Animation::getGenericCurveValue(UINT32 curveIdx, float& value)
- {
- if (!mGenericCurveValuesValid || curveIdx >= (UINT32)mGenericCurveOutputs.size())
- return false;
- value = mGenericCurveOutputs[curveIdx];
- return true;
- }
- SPtr<Animation> Animation::create()
- {
- Animation* anim = new (bs_alloc<Animation>()) Animation();
- SPtr<Animation> animPtr = bs_core_ptr(anim);
- animPtr->_setThisPtr(animPtr);
- animPtr->initialize();
- return animPtr;
- }
- void Animation::updateAnimProxy(float timeDelta)
- {
- // Check if any of the clip curves are dirty and advance time, perform fading
- for (auto& clipInfo : mClipInfos)
- {
- float scaledTimeDelta = timeDelta * clipInfo.state.speed;
- clipInfo.state.time += scaledTimeDelta;
- if (clipInfo.clip.isLoaded() && clipInfo.curveVersion != clipInfo.clip->getVersion())
- mDirty |= AnimDirtyStateFlag::Layout;
- float fadeTime = clipInfo.fadeTime + scaledTimeDelta;
- clipInfo.fadeTime = Math::clamp(fadeTime, 0.0f, clipInfo.fadeLength);
- }
- if((UINT32)mDirty == 0) // Clean
- {
- mAnimProxy->updateTime(mClipInfos);
- }
- else
- {
- auto getAnimatedSOList = [&]()
- {
- Vector<AnimatedSceneObject> animatedSO(mSceneObjects.size());
- UINT32 idx = 0;
- for (auto& entry : mSceneObjects)
- animatedSO[idx++] = entry.second;
- return animatedSO;
- };
- bool didFullRebuild = false;
- if (mDirty.isSet(AnimDirtyStateFlag::Skeleton))
- {
- Vector<AnimatedSceneObject> animatedSOs = getAnimatedSOList();
- mAnimProxy->rebuild(mSkeleton, mSkeletonMask, mClipInfos, animatedSOs);
- didFullRebuild = true;
- }
- else if (mDirty.isSet(AnimDirtyStateFlag::Layout))
- {
- Vector<AnimatedSceneObject> animatedSOs = getAnimatedSOList();
- mAnimProxy->rebuild(mClipInfos, animatedSOs);
- didFullRebuild = true;
- }
- else if (mDirty.isSet(AnimDirtyStateFlag::Value))
- mAnimProxy->updateValues(mClipInfos);
- // Check if there are dirty transforms
- if(!didFullRebuild)
- {
- UINT32 numSceneObjects = (UINT32)mSceneObjects.size();
- for (UINT32 i = 0; i < numSceneObjects; i++)
- {
- UINT32 hash;
- HSceneObject so = mSceneObjects[i].so;
- if (so.isDestroyed(true))
- hash = 0;
- else
- hash = so->getTransformHash();
- if(hash != mAnimProxy->sceneObjectInfos[i].hash)
- {
- Vector<AnimatedSceneObject> animatedSOs = getAnimatedSOList();
- mAnimProxy->updateTransforms(animatedSOs);
- break;
- }
- }
- }
- }
- mDirty = AnimDirtyState();
- }
- void Animation::updateFromProxy()
- {
- // Write TRS animation results to relevant SceneObjects
- for(UINT32 i = 0; i < mAnimProxy->numSceneObjects; i++)
- {
- const AnimatedSceneObjectInfo& soInfo = mAnimProxy->sceneObjectInfos[i];
- auto iterFind = mSceneObjects.find(soInfo.id);
- if (iterFind == mSceneObjects.end())
- continue;
- HSceneObject so = iterFind->second.so;
- if (so.isDestroyed(true))
- continue;
- if(soInfo.boneIdx != -1)
- {
- so->setPosition(mAnimProxy->skeletonPose.positions[soInfo.boneIdx]);
- so->setRotation(mAnimProxy->skeletonPose.rotations[soInfo.boneIdx]);
- so->setScale(mAnimProxy->skeletonPose.scales[soInfo.boneIdx]);
- }
- else
- {
- so->setPosition(mAnimProxy->sceneObjectPose.positions[i]);
- so->setRotation(mAnimProxy->sceneObjectPose.rotations[i]);
- so->setScale(mAnimProxy->sceneObjectPose.scales[i]);
- }
- }
- // Must ensure that clip in the proxy and current primary clip are the same
- mGenericCurveValuesValid = false;
- if(mAnimProxy->numLayers > 0 || mAnimProxy->layers[0].numStates > 0)
- {
- const AnimationState& state = mAnimProxy->layers[0].states[0];
- if(!state.disabled && mClipInfos.size() > 0)
- {
- const AnimationClipInfo& clipInfo = mClipInfos[0];
- if (clipInfo.stateIdx == 0 && clipInfo.layerIdx == 0)
- {
- if (clipInfo.clip.isLoaded() && clipInfo.curveVersion == clipInfo.clip->getVersion())
- {
- UINT32 numGenericCurves = (UINT32)clipInfo.clip->getCurves()->generic.size();
- mGenericCurveValuesValid = numGenericCurves == mAnimProxy->numGenericCurves;
- }
- }
- }
- }
- if(mGenericCurveValuesValid)
- {
- mGenericCurveOutputs.resize(mAnimProxy->numGenericCurves);
- memcpy(mGenericCurveOutputs.data(), mAnimProxy->genericCurveOutputs, mAnimProxy->numGenericCurves * sizeof(float));
- }
- }
- }
|