2014-06-04 18:17:09 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-17 15:28:14 +08:00
|
|
|
#include "3d/CCAnimate3D.h"
|
|
|
|
#include "3d/CCAnimation3D.h"
|
|
|
|
#include "3d/CCSprite3D.h"
|
|
|
|
#include "3d/CCMeshSkin.h"
|
2014-06-04 18:17:09 +08:00
|
|
|
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
//create Animate3D using Animation.
|
|
|
|
Animate3D* Animate3D::create(Animation3D* animation)
|
|
|
|
{
|
|
|
|
auto animate = new Animate3D();
|
|
|
|
animate->_animation = animation;
|
|
|
|
animation->retain();
|
|
|
|
|
|
|
|
animate->autorelease();
|
2014-06-09 18:37:58 +08:00
|
|
|
animate->setDuration(animation->getDuration());
|
2014-06-04 18:17:09 +08:00
|
|
|
|
|
|
|
return animate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** returns a clone of action */
|
|
|
|
Animate3D* Animate3D::clone() const
|
|
|
|
{
|
|
|
|
|
|
|
|
auto animate = const_cast<Animate3D*>(this);
|
2014-06-17 19:18:56 +08:00
|
|
|
auto copy = Animate3D::create(animate->_animation);
|
2014-06-04 18:17:09 +08:00
|
|
|
|
|
|
|
copy->_speed = _speed;
|
|
|
|
copy->_elapsed = _elapsed;
|
|
|
|
copy->_playBack = _playBack;
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** returns a new action that performs the exactly the reverse action */
|
|
|
|
Animate3D* Animate3D::reverse() const
|
|
|
|
{
|
|
|
|
auto animate = clone();
|
|
|
|
animate->_playBack = !animate->_playBack;
|
|
|
|
return animate;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! called before the action start. It will also set the target.
|
|
|
|
void Animate3D::startWithTarget(Node *target)
|
|
|
|
{
|
|
|
|
Sprite3D* sprite = dynamic_cast<Sprite3D*>(target);
|
|
|
|
CCASSERT(sprite && sprite->getSkin() && _animation, "Animate3D apply to Sprite3D only");
|
|
|
|
|
|
|
|
ActionInterval::startWithTarget(target);
|
|
|
|
|
|
|
|
_boneCurves.clear();
|
|
|
|
auto skin = sprite->getSkin();
|
|
|
|
for (unsigned int i = 0; i < skin->getBoneCount(); i++) {
|
|
|
|
auto bone = skin->getBoneByIndex(i);
|
|
|
|
auto curve = _animation->getBoneCurveByName(bone->getName());
|
|
|
|
if (curve)
|
|
|
|
{
|
|
|
|
_boneCurves[bone] = curve;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("warning: bone %s not find in animation", bone->getName().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! 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 * _speed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animate3D::update(float t)
|
|
|
|
{
|
|
|
|
if (_target)
|
|
|
|
{
|
2014-06-13 19:20:19 +08:00
|
|
|
float transDst[3], rotDst[4], scaleDst[3];
|
|
|
|
float* trans = nullptr, *rot = nullptr, *scale = nullptr;
|
2014-06-04 18:17:09 +08:00
|
|
|
if (_playBack)
|
|
|
|
t = 1 - t;
|
|
|
|
|
|
|
|
for (const auto& it : _boneCurves) {
|
|
|
|
auto bone = it.first;
|
|
|
|
auto curve = it.second;
|
|
|
|
if (curve->translateCurve)
|
|
|
|
{
|
2014-06-17 19:18:56 +08:00
|
|
|
curve->translateCurve->evaluate(t, transDst, EvaluateType::LINEAR);
|
2014-06-13 19:20:19 +08:00
|
|
|
trans = &transDst[0];
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
|
|
|
if (curve->rotCurve)
|
|
|
|
{
|
2014-06-17 19:18:56 +08:00
|
|
|
curve->rotCurve->evaluate(t, rotDst, EvaluateType::QUAT_SLERP);
|
2014-06-13 19:20:19 +08:00
|
|
|
rot = &rotDst[0];
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
|
|
|
if (curve->scaleCurve)
|
|
|
|
{
|
2014-06-17 19:18:56 +08:00
|
|
|
curve->scaleCurve->evaluate(t, scaleDst, EvaluateType::LINEAR);
|
2014-06-13 19:20:19 +08:00
|
|
|
scale = &scaleDst[0];
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
2014-06-13 19:20:19 +08:00
|
|
|
bone->setAnimationValue(trans, rot, scale, _weight);
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Animate3D::Animate3D()
|
|
|
|
: _speed(1)
|
2014-06-13 19:20:19 +08:00
|
|
|
, _weight(1.f)
|
2014-06-04 18:17:09 +08:00
|
|
|
, _animation(nullptr)
|
|
|
|
, _playBack(false)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
Animate3D::~Animate3D()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_animation);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|