| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- //
- // Copyright (c) 2008-2013 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "AnimatedModel.h"
- #include "Animation.h"
- #include "AnimationState.h"
- #include "Deserializer.h"
- #include "DrawableEvents.h"
- #include "Serializer.h"
- #include "DebugNew.h"
- namespace Urho3D
- {
- AnimationState::AnimationState(AnimatedModel* model, Animation* animation) :
- model_(model),
- animation_(animation),
- startBone_(0),
- looped_(false),
- weight_(0.0f),
- time_(0.0f),
- layer_(0)
- {
- SetStartBone(0);
-
- if (animation_)
- {
- // Setup a cache for last keyframe of each track
- lastKeyFrame_.Resize(animation_->GetNumTracks());
- }
-
- for (unsigned i = 0; i < lastKeyFrame_.Size(); ++i)
- lastKeyFrame_[i] = 0;
- }
- AnimationState::~AnimationState()
- {
- }
- void AnimationState::SetStartBone(Bone* startBone)
- {
- if (!model_ || !animation_)
- return;
-
- Skeleton& skeleton = model_->GetSkeleton();
- if (!startBone)
- {
- Bone* rootBone = skeleton.GetRootBone();
- if (!rootBone)
- return;
- startBone = rootBone;
- }
-
- // Do not reassign if the start bone did not actually change, and we already have valid bone nodes
- if (startBone == startBone_ && !trackToBoneMap_.Empty())
- return;
-
- startBone_ = startBone;
-
- trackToBoneMap_.Clear();
- if (!startBone->node_)
- return;
-
- const Vector<AnimationTrack>& tracks = animation_->GetTracks();
-
- for (unsigned i = 0; i < tracks.Size(); ++i)
- {
- // Include those tracks that are either the start bone itself, or its children
- Bone* trackBone = 0;
- const StringHash& nameHash = tracks[i].nameHash_;
-
- if (nameHash == startBone->nameHash_)
- trackBone = startBone;
- else
- {
- Node* trackBoneNode = startBone->node_->GetChild(nameHash, true);
- if (trackBoneNode)
- trackBone = skeleton.GetBone(nameHash);
- }
-
- if (trackBone)
- trackToBoneMap_[i] = trackBone;
- }
-
- model_->MarkAnimationDirty();
- }
- void AnimationState::SetLooped(bool looped)
- {
- looped_ = looped;
- }
- void AnimationState::SetWeight(float weight)
- {
- weight = Clamp(weight, 0.0f, 1.0f);
- if (weight != weight_)
- {
- weight_ = weight;
- if (model_)
- model_->MarkAnimationDirty();
- }
- }
- void AnimationState::SetTime(float time)
- {
- if (!animation_)
- return;
-
- time = Clamp(time, 0.0f, animation_->GetLength());
- if (time != time_)
- {
- time_ = time;
- if (model_)
- model_->MarkAnimationDirty();
- }
- }
- void AnimationState::AddWeight(float delta)
- {
- if (delta == 0.0f)
- return;
-
- SetWeight(GetWeight() + delta);
- }
- void AnimationState::AddTime(float delta)
- {
- if (!animation_)
- return;
-
- float length = animation_->GetLength();
- if (delta == 0.0f || length == 0.0f)
- return;
-
- float oldTime = GetTime();
- float time = oldTime + delta;
- if (looped_)
- {
- while (time >= length)
- time -= length;
- while (time < 0.0f)
- time += length;
- }
-
- SetTime(time);
-
- // Process animation triggers
- if (model_ && animation_->GetNumTriggers())
- {
- if (delta > 0.0f)
- {
- if (oldTime > time)
- oldTime -= length;
- }
- if (delta < 0.0f)
- {
- if (time > oldTime)
- time -= length;
- }
- if (oldTime > time)
- Swap(oldTime, time);
-
- const Vector<AnimationTriggerPoint>& triggers = animation_->GetTriggers();
- for (Vector<AnimationTriggerPoint>::ConstIterator i = triggers.Begin(); i != triggers.End(); ++i)
- {
- if (oldTime <= i->time_ && time > i->time_)
- {
- using namespace AnimationTrigger;
-
- VariantMap eventData;
- eventData[P_NODE] = (void*)model_->GetNode();
- eventData[P_NAME] = animation_->GetAnimationName();
- eventData[P_TIME] = i->time_;
- eventData[P_DATA] = i->data_;
- model_->GetNode()->SendEvent(E_ANIMATIONTRIGGER, eventData);
- }
- }
- }
- }
- void AnimationState::SetLayer(unsigned char layer)
- {
- if (layer != layer_)
- {
- layer_ = layer;
- if (model_)
- model_->MarkAnimationOrderDirty();
- }
- }
- Bone* AnimationState::GetStartBone() const
- {
- return model_ ? startBone_ : 0;
- }
- float AnimationState::GetLength() const
- {
- return animation_ ? animation_->GetLength() : 0.0f;
- }
- void AnimationState::Apply()
- {
- if (!animation_ || !IsEnabled())
- return;
-
- // Check first if full weight or blending
- if (Equals(weight_, 1.0f))
- {
- for (HashMap<unsigned, Bone*>::ConstIterator i = trackToBoneMap_.Begin(); i != trackToBoneMap_.End(); ++i)
- {
- const AnimationTrack* track = animation_->GetTrack(i->first_);
- Bone* bone = i->second_;
- Node* boneNode = bone->node_;
- if (!boneNode || !bone->animated_ || !track->keyFrames_.Size())
- continue;
-
- unsigned& frame = lastKeyFrame_[i->first_];
- track->GetKeyFrameIndex(time_, frame);
-
- // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
- unsigned nextFrame = frame + 1;
- bool interpolate = true;
- if (nextFrame >= track->keyFrames_.Size())
- {
- if (!looped_)
- {
- nextFrame = frame;
- interpolate = false;
- }
- else
- nextFrame = 0;
- }
-
- const AnimationKeyFrame* keyFrame = &track->keyFrames_[frame];
- unsigned char channelMask = track->channelMask_;
-
- if (!interpolate)
- {
- // No interpolation, full weight
- if (channelMask & CHANNEL_POSITION)
- boneNode->SetPosition(keyFrame->position_);
- if (channelMask & CHANNEL_ROTATION)
- boneNode->SetRotation(keyFrame->rotation_);
- if (channelMask & CHANNEL_SCALE)
- boneNode->SetScale(keyFrame->scale_);
- }
- else
- {
- const AnimationKeyFrame* nextKeyFrame = &track->keyFrames_[nextFrame];
- float timeInterval = nextKeyFrame->time_ - keyFrame->time_;
- if (timeInterval < 0.0f)
- timeInterval += animation_->GetLength();
- float t = timeInterval > 0.0f ? (time_ - keyFrame->time_) / timeInterval : 1.0f;
-
- // Interpolation, full weight
- if (channelMask & CHANNEL_POSITION)
- boneNode->SetPosition(keyFrame->position_.Lerp(nextKeyFrame->position_, t));
- if (channelMask & CHANNEL_ROTATION)
- boneNode->SetRotation(keyFrame->rotation_.Slerp(nextKeyFrame->rotation_, t));
- if (channelMask & CHANNEL_SCALE)
- boneNode->SetScale(keyFrame->scale_.Lerp(nextKeyFrame->scale_, t));
- }
- }
- }
- else
- {
- for (HashMap<unsigned, Bone*>::ConstIterator i = trackToBoneMap_.Begin(); i != trackToBoneMap_.End(); ++i)
- {
- const AnimationTrack* track = animation_->GetTrack(i->first_);
- Bone* bone = i->second_;
- Node* boneNode = bone->node_;
- if (!boneNode || !bone->animated_ || !track->keyFrames_.Size())
- continue;
-
- unsigned& frame = lastKeyFrame_[i->first_];
- track->GetKeyFrameIndex(time_, frame);
-
- // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
- unsigned nextFrame = frame + 1;
- bool interpolate = true;
- if (nextFrame >= track->keyFrames_.Size())
- {
- if (!looped_)
- {
- nextFrame = frame;
- interpolate = false;
- }
- else
- nextFrame = 0;
- }
-
- const AnimationKeyFrame* keyFrame = &track->keyFrames_[frame];
- unsigned char channelMask = track->channelMask_;
-
- if (!interpolate)
- {
- // No interpolation, blend between old transform & animation
- if (channelMask & CHANNEL_POSITION)
- boneNode->SetPosition(boneNode->GetPosition().Lerp(keyFrame->position_, weight_));
- if (channelMask & CHANNEL_ROTATION)
- boneNode->SetRotation(boneNode->GetRotation().Slerp(keyFrame->rotation_, weight_));
- if (channelMask & CHANNEL_SCALE)
- boneNode->SetScale(boneNode->GetScale().Lerp(keyFrame->scale_, weight_));
- }
- else
- {
- const AnimationKeyFrame* nextKeyFrame = &track->keyFrames_[nextFrame];
- float timeInterval = nextKeyFrame->time_ - keyFrame->time_;
- if (timeInterval < 0.0f)
- timeInterval += animation_->GetLength();
- float t = timeInterval > 0.0f ? (time_ - keyFrame->time_) / timeInterval : 1.0f;
-
- // Interpolation, blend between old transform & animation
- if (channelMask & CHANNEL_POSITION)
- {
- boneNode->SetPosition(boneNode->GetPosition().Lerp(
- keyFrame->position_.Lerp(nextKeyFrame->position_, t), weight_));
- }
- if (channelMask & CHANNEL_ROTATION)
- {
- boneNode->SetRotation(boneNode->GetRotation().Slerp(
- keyFrame->rotation_.Slerp(nextKeyFrame->rotation_, t), weight_));
- }
- if (channelMask & CHANNEL_SCALE)
- {
- boneNode->SetScale(boneNode->GetScale().Lerp(
- keyFrame->scale_.Lerp(nextKeyFrame->scale_, t), weight_));
- }
- }
- }
- }
- }
- }
|