axmol/core/3d/CCAnimate3D.cpp

539 lines
17 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
https://axis-project.github.io/
2019-11-23 20:27:39 +08:00
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 "3d/CCAnimate3D.h"
#include "3d/CCMeshRenderer.h"
2019-11-23 20:27:39 +08:00
#include "3d/CCSkeleton3D.h"
#include "platform/CCFileUtils.h"
#include "base/CCConfiguration.h"
#include "base/CCEventCustom.h"
#include "base/CCDirector.h"
#include "base/CCEventDispatcher.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
std::unordered_map<Node*, Animate3D*> Animate3D::s_fadeInAnimates;
std::unordered_map<Node*, Animate3D*> Animate3D::s_fadeOutAnimates;
std::unordered_map<Node*, Animate3D*> Animate3D::s_runningAnimates;
2021-12-25 10:04:45 +08:00
float Animate3D::_transTime = 0.1f;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// create Animate3D using Animation.
2019-11-23 20:27:39 +08:00
Animate3D* Animate3D::create(Animation3D* animation)
{
2021-12-08 00:11:53 +08:00
auto animate = new Animate3D();
2019-11-23 20:27:39 +08:00
animate->init(animation);
animate->autorelease();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return animate;
}
Animate3D* Animate3D::create(Animation3D* animation, float fromTime, float duration)
{
2021-12-08 00:11:53 +08:00
auto animate = new Animate3D();
2019-11-23 20:27:39 +08:00
animate->init(animation, fromTime, duration);
animate->autorelease();
2021-12-25 10:04:45 +08:00
return animate;
2019-11-23 20:27:39 +08:00
}
Animate3D* Animate3D::createWithFrames(Animation3D* animation, int startFrame, int endFrame, float frameRate)
{
2021-12-08 00:11:53 +08:00
auto animate = new Animate3D();
2019-11-23 20:27:39 +08:00
animate->initWithFrames(animation, startFrame, endFrame, frameRate);
animate->autorelease();
2021-12-25 10:04:45 +08:00
return animate;
2019-11-23 20:27:39 +08:00
}
bool Animate3D::init(Animation3D* animation)
{
_animation = animation;
animation->retain();
setDuration(animation->getDuration());
setOriginInterval(animation->getDuration());
setQuality(Configuration::getInstance()->getAnimate3DQuality());
return true;
}
bool Animate3D::init(Animation3D* animation, float fromTime, float duration)
{
float fullDuration = animation->getDuration();
if (duration > fullDuration - fromTime)
duration = fullDuration - fromTime;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_start = fromTime / fullDuration;
2021-12-25 10:04:45 +08:00
_last = duration / fullDuration;
2019-11-23 20:27:39 +08:00
setDuration(duration);
setOriginInterval(duration);
_animation = animation;
animation->retain();
setQuality(Configuration::getInstance()->getAnimate3DQuality());
return true;
}
bool Animate3D::initWithFrames(Animation3D* animation, int startFrame, int endFrame, float frameRate)
{
float perFrameTime = 1.f / frameRate;
2021-12-25 10:04:45 +08:00
float fromTime = startFrame * perFrameTime;
float duration = (endFrame - startFrame) * perFrameTime;
_frameRate = frameRate;
2019-11-23 20:27:39 +08:00
init(animation, fromTime, duration);
return true;
}
/** returns a clone of action */
Animate3D* Animate3D::clone() const
{
auto animate = const_cast<Animate3D*>(this);
2021-12-25 10:04:45 +08:00
auto copy = Animate3D::create(animate->_animation);
copy->_absSpeed = _absSpeed;
copy->_weight = _weight;
copy->_elapsed = _elapsed;
copy->_start = _start;
copy->_last = _last;
2019-11-23 20:27:39 +08:00
copy->_playReverse = _playReverse;
copy->setDuration(animate->getDuration());
copy->setOriginInterval(animate->getOriginInterval());
return copy;
}
/** returns a new action that performs the exactly the reverse action */
Animate3D* Animate3D::reverse() const
{
2021-12-25 10:04:45 +08:00
auto animate = clone();
2019-11-23 20:27:39 +08:00
animate->_playReverse = !animate->_playReverse;
return animate;
}
Node* findChildByNameRecursively(Node* node, std::string_view childName)
2019-11-23 20:27:39 +08:00
{
std::string_view name = node->getName();
2019-11-23 20:27:39 +08:00
if (name == childName)
return node;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
const Vector<Node*>& children = node->getChildren();
for (const auto& child : children)
{
Node* findNode = findChildByNameRecursively(child, childName);
if (findNode)
return findNode;
}
return nullptr;
}
//! called before the action start. It will also set the target.
2021-12-25 10:04:45 +08:00
void Animate3D::startWithTarget(Node* target)
2019-11-23 20:27:39 +08:00
{
bool needReMap = (_target != target);
ActionInterval::startWithTarget(target);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (needReMap)
{
_boneCurves.clear();
_nodeCurves.clear();
2021-12-25 10:04:45 +08:00
bool hasCurve = false;
MeshRenderer* mesh = dynamic_cast<MeshRenderer*>(target);
2021-12-25 10:04:45 +08:00
if (mesh)
2019-11-23 20:27:39 +08:00
{
if (_animation)
{
auto& boneCurves = _animation->getBoneCurves();
2021-12-25 10:04:45 +08:00
for (const auto& iter : boneCurves)
2019-11-23 20:27:39 +08:00
{
std::string_view boneName = iter.first;
auto skin = mesh->getSkeleton();
2021-12-25 10:04:45 +08:00
if (skin)
2019-11-23 20:27:39 +08:00
{
auto bone = skin->getBoneByName(boneName);
if (bone)
{
2021-12-25 10:04:45 +08:00
auto curve = _animation->getBoneCurveByName(boneName);
2019-11-23 20:27:39 +08:00
_boneCurves[bone] = curve;
2021-12-25 10:04:45 +08:00
hasCurve = true;
2019-11-23 20:27:39 +08:00
}
else
{
Node* node = nullptr;
if (target->getName() == boneName)
node = target;
else
node = findChildByNameRecursively(target, boneName);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (node)
{
auto curve = _animation->getBoneCurveByName(boneName);
if (curve)
{
_nodeCurves[node] = curve;
2021-12-25 10:04:45 +08:00
hasCurve = true;
2019-11-23 20:27:39 +08:00
}
}
}
}
}
}
}
else
{
auto& boneCurves = _animation->getBoneCurves();
2021-12-25 10:04:45 +08:00
for (const auto& iter : boneCurves)
2019-11-23 20:27:39 +08:00
{
std::string_view boneName = iter.first;
Node* node = nullptr;
2019-11-23 20:27:39 +08:00
if (target->getName() == boneName)
node = target;
else
node = findChildByNameRecursively(target, boneName);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (node)
{
auto curve = _animation->getBoneCurveByName(boneName);
if (curve)
{
_nodeCurves[node] = curve;
2021-12-25 10:04:45 +08:00
hasCurve = true;
2019-11-23 20:27:39 +08:00
}
}
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (!hasCurve)
{
CCLOG("warning: no animation found for the skeleton");
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
auto runningAction = s_runningAnimates.find(target);
if (runningAction != s_runningAnimates.end())
{
2021-12-25 10:04:45 +08:00
// make the running action fade out
2019-11-23 20:27:39 +08:00
auto action = (*runningAction).second;
if (action != this)
{
if (_transTime < 0.001f)
{
s_runningAnimates[target] = this;
2021-12-25 10:04:45 +08:00
_state = Animate3D::Animate3DState::Running;
_weight = 1.0f;
2019-11-23 20:27:39 +08:00
}
else
{
s_fadeOutAnimates[target] = action;
2021-12-25 10:04:45 +08:00
action->_state = Animate3D::Animate3DState::FadeOut;
action->_accTransTime = 0.0f;
action->_weight = 1.0f;
action->_lastTime = 0.f;
2019-11-23 20:27:39 +08:00
s_runningAnimates.erase(target);
s_fadeInAnimates[target] = this;
2021-12-25 10:04:45 +08:00
_accTransTime = 0.0f;
_state = Animate3D::Animate3DState::FadeIn;
_weight = 0.f;
_lastTime = 0.f;
2019-11-23 20:27:39 +08:00
}
}
}
else
{
auto it = s_fadeInAnimates.find(target);
if (it != s_fadeInAnimates.end())
{
s_fadeInAnimates.erase(it);
}
s_runningAnimates[target] = this;
2021-12-25 10:04:45 +08:00
_state = Animate3D::Animate3DState::Running;
_weight = 1.0f;
2019-11-23 20:27:39 +08:00
}
}
void Animate3D::stop()
{
removeFromMap();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
ActionInterval::stop();
}
//! called every frame with it's delta time. DON'T override unless you know what you are doing.
void Animate3D::step(float dt)
{
ActionInterval::step(dt);
}
bool cmpEventInfoAsc(Animate3D::Animate3DDisplayedEventInfo* info1, Animate3D::Animate3DDisplayedEventInfo* info2)
{
return info1->frame < info2->frame;
}
bool cmpEventInfoDes(Animate3D::Animate3DDisplayedEventInfo* info1, Animate3D::Animate3DDisplayedEventInfo* info2)
{
return info1->frame > info2->frame;
}
void Animate3D::update(float t)
{
if (_target)
{
if (_state == Animate3D::Animate3DState::FadeIn && _lastTime > 0.f)
{
_accTransTime += (t - _lastTime) * getDuration();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_weight = _accTransTime / _transTime;
if (_weight >= 1.0f)
{
_accTransTime = _transTime;
2021-12-25 10:04:45 +08:00
_weight = 1.0f;
_state = Animate3D::Animate3DState::Running;
2019-11-23 20:27:39 +08:00
s_fadeInAnimates.erase(_target);
s_runningAnimates[_target] = this;
}
}
else if (_state == Animate3D::Animate3DState::FadeOut && _lastTime > 0.f)
{
_accTransTime += (t - _lastTime) * getDuration();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_weight = 1 - _accTransTime / _transTime;
if (_weight <= 0.0f)
{
_accTransTime = _transTime;
2021-12-25 10:04:45 +08:00
_weight = 0.0f;
2019-11-23 20:27:39 +08:00
s_fadeOutAnimates.erase(_target);
_target->stopAction(this);
return;
}
}
float lastTime = _lastTime;
2021-12-25 10:04:45 +08:00
_lastTime = t;
2019-11-23 20:27:39 +08:00
if (_quality != Animate3DQuality::QUALITY_NONE)
{
if (_weight > 0.0f)
{
float transDst[3], rotDst[4], scaleDst[3];
2021-12-25 10:04:45 +08:00
float *trans = nullptr, *rot = nullptr, *scale = nullptr;
if (_playReverse)
{
t = 1 - t;
2019-11-23 20:27:39 +08:00
lastTime = 1.0f - lastTime;
}
2021-12-25 10:04:45 +08:00
t = _start + t * _last;
2019-11-23 20:27:39 +08:00
lastTime = _start + lastTime * _last;
2021-12-25 10:04:45 +08:00
for (const auto& it : _boneCurves)
{
auto bone = it.first;
2019-11-23 20:27:39 +08:00
auto curve = it.second;
if (curve->translateCurve)
{
curve->translateCurve->evaluate(t, transDst, _translateEvaluate);
trans = &transDst[0];
}
if (curve->rotCurve)
{
curve->rotCurve->evaluate(t, rotDst, _roteEvaluate);
rot = &rotDst[0];
}
if (curve->scaleCurve)
{
curve->scaleCurve->evaluate(t, scaleDst, _scaleEvaluate);
scale = &scaleDst[0];
}
bone->setAnimationValue(trans, rot, scale, this, _weight);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
for (const auto& it : _nodeCurves)
{
2021-12-25 10:04:45 +08:00
auto node = it.first;
2019-11-23 20:27:39 +08:00
auto curve = it.second;
Mat4 transform;
if (curve->translateCurve)
{
curve->translateCurve->evaluate(t, transDst, _translateEvaluate);
transform.translate(transDst[0], transDst[1], transDst[2]);
}
if (curve->rotCurve)
{
curve->rotCurve->evaluate(t, rotDst, _roteEvaluate);
Quaternion qua(rotDst[0], rotDst[1], rotDst[2], rotDst[3]);
transform.rotate(qua);
}
if (curve->scaleCurve)
{
curve->scaleCurve->evaluate(t, scaleDst, _scaleEvaluate);
transform.scale(scaleDst[0], scaleDst[1], scaleDst[2]);
}
node->setAdditionalTransform(&transform);
}
2021-12-25 10:04:45 +08:00
if (!_keyFrameUserInfos.empty())
{
2019-11-23 20:27:39 +08:00
float prekeyTime = lastTime * getDuration() * _frameRate;
2021-12-25 10:04:45 +08:00
float keyTime = t * getDuration() * _frameRate;
2019-11-23 20:27:39 +08:00
std::vector<Animate3DDisplayedEventInfo*> eventInfos;
for (const auto& keyFrame : _keyFrameUserInfos)
{
2021-12-25 10:04:45 +08:00
if ((!_playReverse && keyFrame.first >= prekeyTime && keyFrame.first < keyTime) ||
(_playReverse && keyFrame.first >= keyTime && keyFrame.first < prekeyTime))
{
auto& frameEvent = _keyFrameEvent[keyFrame.first];
if (frameEvent == nullptr)
frameEvent = new EventCustom(Animate3DDisplayedNotification);
auto eventInfo = &_displayedEventInfo[keyFrame.first];
eventInfo->target = _target;
eventInfo->frame = keyFrame.first;
eventInfo->userInfo = &_keyFrameUserInfos[keyFrame.first];
eventInfos.push_back(eventInfo);
frameEvent->setUserData((void*)eventInfo);
}
2019-11-23 20:27:39 +08:00
}
std::sort(eventInfos.begin(), eventInfos.end(), _playReverse ? cmpEventInfoDes : cmpEventInfoAsc);
2021-12-25 10:04:45 +08:00
for (auto eventInfo : eventInfos)
{
2019-11-23 20:27:39 +08:00
Director::getInstance()->getEventDispatcher()->dispatchEvent(_keyFrameEvent[eventInfo->frame]);
}
}
}
}
}
}
float Animate3D::getSpeed() const
{
return _playReverse ? -_absSpeed : _absSpeed;
}
void Animate3D::setSpeed(float speed)
{
2021-12-25 10:04:45 +08:00
_absSpeed = fabsf(speed);
2019-11-23 20:27:39 +08:00
_playReverse = speed < 0;
2021-12-25 10:04:45 +08:00
_duration = _originInterval / _absSpeed;
2019-11-23 20:27:39 +08:00
}
void Animate3D::setWeight(float weight)
{
CCASSERT(weight >= 0.0f, "invalid weight");
_weight = fabsf(weight);
}
void Animate3D::setOriginInterval(float interval)
{
_originInterval = interval;
}
void Animate3D::setQuality(Animate3DQuality quality)
{
if (quality == Animate3DQuality::QUALITY_HIGH)
{
_translateEvaluate = EvaluateType::INT_LINEAR;
2021-12-25 10:04:45 +08:00
_roteEvaluate = EvaluateType::INT_QUAT_SLERP;
_scaleEvaluate = EvaluateType::INT_LINEAR;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (quality == Animate3DQuality::QUALITY_LOW)
2019-11-23 20:27:39 +08:00
{
_translateEvaluate = EvaluateType::INT_NEAR;
2021-12-25 10:04:45 +08:00
_roteEvaluate = EvaluateType::INT_NEAR;
_scaleEvaluate = EvaluateType::INT_NEAR;
2019-11-23 20:27:39 +08:00
}
_quality = quality;
}
Animate3DQuality Animate3D::getQuality() const
{
return _quality;
}
const ValueMap* Animate3D::getKeyFrameUserInfo(int keyFrame) const
{
auto iter = _keyFrameUserInfos.find(keyFrame);
if (iter != _keyFrameUserInfos.end())
return &iter->second;
return nullptr;
}
ValueMap* Animate3D::getKeyFrameUserInfo(int keyFrame)
{
auto iter = _keyFrameUserInfos.find(keyFrame);
if (iter != _keyFrameUserInfos.end())
return &iter->second;
return nullptr;
}
2021-12-25 10:04:45 +08:00
void Animate3D::setKeyFrameUserInfo(int keyFrame, const ValueMap& userInfo)
2019-11-23 20:27:39 +08:00
{
_keyFrameUserInfos[keyFrame] = userInfo;
}
Animate3D::Animate3D()
2021-12-25 10:04:45 +08:00
: _state(Animate3D::Animate3DState::Running)
, _animation(nullptr)
, _absSpeed(1.f)
, _weight(1.f)
, _start(0.f)
, _last(1.f)
, _playReverse(false)
, _accTransTime(0.0f)
, _lastTime(0.0f)
, _originInterval(0.0f)
, _frameRate(30.0f)
2019-11-23 20:27:39 +08:00
{
setQuality(Animate3DQuality::QUALITY_HIGH);
}
Animate3D::~Animate3D()
{
removeFromMap();
2021-12-25 10:04:45 +08:00
for (auto& it : _keyFrameEvent)
{
2019-11-23 20:27:39 +08:00
delete it.second;
}
_keyFrameEvent.clear();
2021-12-25 10:04:45 +08:00
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE(_animation);
2019-11-23 20:27:39 +08:00
}
void Animate3D::removeFromMap()
{
2021-12-25 10:04:45 +08:00
// remove this action from map
2019-11-23 20:27:39 +08:00
if (_target)
{
auto it = s_fadeInAnimates.find(_target);
if (it != s_fadeInAnimates.end() && it->second == this)
s_fadeInAnimates.erase(it);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
it = s_fadeOutAnimates.find(_target);
if (it != s_fadeOutAnimates.end() && it->second == this)
s_fadeOutAnimates.erase(it);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
it = s_runningAnimates.find(_target);
if (it != s_runningAnimates.end() && it->second == this)
s_runningAnimates.erase(it);
}
}
NS_AX_END