| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136 |
- //********************************** 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++)
- {
- 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]);
- }
- soInfo.hash = so->getTransformHash();
- }
- // 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));
- }
- }
- }
|