2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "3d/CCAnimation3D.h"
|
|
|
|
#include "3d/CCBundle3D.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Animation3D* Animation3D::create(std::string_view fileName, std::string_view animationName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(fileName);
|
2021-12-26 23:26:34 +08:00
|
|
|
fullPath.append("#").append(animationName);
|
2021-12-28 11:00:34 +08:00
|
|
|
auto animation = Animation3DCache::getInstance()->getAnimation(fullPath);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (animation != nullptr)
|
|
|
|
return animation;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
animation = new Animation3D();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (animation->initWithFile(fileName, animationName))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
animation->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(animation);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return animation;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Animation3D::initWithFile(std::string_view filename, std::string_view animationName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// load animation here
|
2019-11-23 20:27:39 +08:00
|
|
|
auto bundle = Bundle3D::createBundle();
|
|
|
|
Animation3DData animationdata;
|
|
|
|
if (bundle->load(fullPath) && bundle->loadAnimationData(animationName, &animationdata) && init(animationdata))
|
|
|
|
{
|
2021-12-26 23:26:34 +08:00
|
|
|
fullPath.append("#").append(animationName);
|
|
|
|
Animation3DCache::getInstance()->addAnimation(fullPath, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
Bundle3D::destroyBundle(bundle);
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Bundle3D::destroyBundle(bundle);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Animation3D::Curve* Animation3D::getBoneCurveByName(std::string_view name) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto it = _boneCurves.find(name);
|
|
|
|
if (it != _boneCurves.end())
|
|
|
|
return it->second;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Animation3D::Animation3D() : _duration(0) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Animation3D::~Animation3D()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& itor : _boneCurves)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
Curve* curve = itor.second;
|
|
|
|
CC_SAFE_DELETE(curve);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Animation3D::Curve::Curve() : translateCurve(nullptr), rotCurve(nullptr), scaleCurve(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
Animation3D::Curve::~Curve()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(translateCurve);
|
|
|
|
CC_SAFE_RELEASE_NULL(rotCurve);
|
|
|
|
CC_SAFE_RELEASE_NULL(scaleCurve);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Animation3D::init(const Animation3DData& data)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_duration = data._totalTime;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& iter : data._translationKeys)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Curve* curve = _boneCurves[iter.first];
|
2021-12-25 10:04:45 +08:00
|
|
|
if (curve == nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
curve = new Curve();
|
2019-11-23 20:27:39 +08:00
|
|
|
_boneCurves[iter.first] = curve;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (iter.second.empty())
|
|
|
|
continue;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::vector<float> keys;
|
|
|
|
std::vector<float> values;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& keyIter : iter.second)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
keys.push_back(keyIter._time);
|
|
|
|
values.push_back(keyIter._key.x);
|
|
|
|
values.push_back(keyIter._key.y);
|
|
|
|
values.push_back(keyIter._key.z);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
curve->translateCurve = Curve::AnimationCurveVec3::create(&keys[0], &values[0], (int)keys.size());
|
2021-12-25 10:04:45 +08:00
|
|
|
if (curve->translateCurve)
|
|
|
|
curve->translateCurve->retain();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (const auto& iter : data._rotationKeys)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Curve* curve = _boneCurves[iter.first];
|
2021-12-25 10:04:45 +08:00
|
|
|
if (curve == nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
curve = new Curve();
|
2019-11-23 20:27:39 +08:00
|
|
|
_boneCurves[iter.first] = curve;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (iter.second.empty())
|
|
|
|
continue;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::vector<float> keys;
|
|
|
|
std::vector<float> values;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& keyIter : iter.second)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
keys.push_back(keyIter._time);
|
|
|
|
values.push_back(keyIter._key.x);
|
|
|
|
values.push_back(keyIter._key.y);
|
|
|
|
values.push_back(keyIter._key.z);
|
|
|
|
values.push_back(keyIter._key.w);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
curve->rotCurve = Curve::AnimationCurveQuat::create(&keys[0], &values[0], (int)keys.size());
|
2021-12-25 10:04:45 +08:00
|
|
|
if (curve->rotCurve)
|
|
|
|
curve->rotCurve->retain();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (const auto& iter : data._scaleKeys)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Curve* curve = _boneCurves[iter.first];
|
2021-12-25 10:04:45 +08:00
|
|
|
if (curve == nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
curve = new Curve();
|
2019-11-23 20:27:39 +08:00
|
|
|
_boneCurves[iter.first] = curve;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (iter.second.empty())
|
|
|
|
continue;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::vector<float> keys;
|
|
|
|
std::vector<float> values;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& keyIter : iter.second)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
keys.push_back(keyIter._time);
|
|
|
|
values.push_back(keyIter._key.x);
|
|
|
|
values.push_back(keyIter._key.y);
|
|
|
|
values.push_back(keyIter._key.z);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
curve->scaleCurve = Curve::AnimationCurveVec3::create(&keys[0], &values[0], (int)keys.size());
|
2021-12-25 10:04:45 +08:00
|
|
|
if (curve->scaleCurve)
|
|
|
|
curve->scaleCurve->retain();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
Animation3DCache* Animation3DCache::_cacheInstance = nullptr;
|
|
|
|
|
|
|
|
Animation3DCache* Animation3DCache::getInstance()
|
|
|
|
{
|
|
|
|
if (_cacheInstance == nullptr)
|
2021-12-08 00:11:53 +08:00
|
|
|
_cacheInstance = new Animation3DCache();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return _cacheInstance;
|
|
|
|
}
|
|
|
|
void Animation3DCache::destroyInstance()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_cacheInstance);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Animation3D* Animation3DCache::getAnimation(std::string_view key)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto it = _animations.find(key);
|
|
|
|
if (it != _animations.end())
|
|
|
|
return it->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void Animation3DCache::addAnimation(std::string_view key, Animation3D* animation)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
const auto& it = _animations.find(key);
|
|
|
|
if (it != _animations.end())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return; // already have this key
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-26 23:26:34 +08:00
|
|
|
_animations.emplace(key, animation); //_animations[key] = animation;
|
2019-11-23 20:27:39 +08:00
|
|
|
animation->retain();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animation3DCache::removeAllAnimations()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto itor : _animations)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
CC_SAFE_RELEASE(itor.second);
|
|
|
|
}
|
|
|
|
_animations.clear();
|
|
|
|
}
|
|
|
|
void Animation3DCache::removeUnusedAnimation()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto itor = _animations.begin(); itor != _animations.end();)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
if (itor->second->getReferenceCount() == 1)
|
|
|
|
{
|
|
|
|
itor->second->release();
|
|
|
|
itor = _animations.erase(itor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++itor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Animation3DCache::Animation3DCache() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
Animation3DCache::~Animation3DCache()
|
|
|
|
{
|
|
|
|
removeAllAnimations();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|