diff --git a/cocos/3d/CCAnimate3D.cpp b/cocos/3d/CCAnimate3D.cpp index 5f13d70cc7..da1e2c6a80 100644 --- a/cocos/3d/CCAnimate3D.cpp +++ b/cocos/3d/CCAnimate3D.cpp @@ -66,12 +66,12 @@ Animate3D* Animate3D::clone() const auto animate = const_cast(this); auto copy = Animate3D::create(animate->_animation); - copy->_speed = _speed; + copy->_absSpeed = _absSpeed; copy->_weight = _weight; copy->_elapsed = _elapsed; copy->_start = _start; copy->_last = _last; - copy->_playBack = _playBack; + copy->_playReverse = _playReverse; copy->setDuration(animate->getDuration()); return copy; @@ -81,7 +81,7 @@ Animate3D* Animate3D::clone() const Animate3D* Animate3D::reverse() const { auto animate = clone(); - animate->_playBack = !animate->_playBack; + animate->_playReverse = !animate->_playReverse; return animate; } @@ -112,16 +112,16 @@ void Animate3D::startWithTarget(Node *target) //! 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); + ActionInterval::step(dt * _absSpeed); } void Animate3D::update(float t) { - if (_target) + if (_target && _weight > 0.f) { float transDst[3], rotDst[4], scaleDst[3]; float* trans = nullptr, *rot = nullptr, *scale = nullptr; - if (_playBack) + if (_playReverse) t = 1 - t; t = _start + t * _last; @@ -149,13 +149,29 @@ void Animate3D::update(float t) } +float Animate3D::getSpeed() const +{ + return _playReverse ? -_absSpeed : _absSpeed; +} +void Animate3D::setSpeed(float speed) +{ + _absSpeed = fabsf(speed); + _playReverse = speed < 0; +} + +void Animate3D::setWeight(float weight) +{ + CCASSERT(weight >= 0.0f, "invalid weight"); + _weight = fabsf(weight); +} + Animate3D::Animate3D() -: _speed(1) +: _absSpeed(1.f) , _weight(1.f) , _start(0.f) , _last(1.f) , _animation(nullptr) -, _playBack(false) +, _playReverse(false) { } diff --git a/cocos/3d/CCAnimate3D.h b/cocos/3d/CCAnimate3D.h index fbda6328b5..67aff9a5c6 100644 --- a/cocos/3d/CCAnimate3D.h +++ b/cocos/3d/CCAnimate3D.h @@ -32,6 +32,7 @@ #include "base/ccMacros.h" #include "base/CCRef.h" #include "base/ccTypes.h" +#include "base/CCPlatformMacros.h" #include "2d/CCActionInterval.h" NS_CC_BEGIN @@ -66,17 +67,17 @@ public: virtual void update(float t) override; - /**get & set speed */ - float getSpeed() const { return _speed; } - void setSpeed(float speed) { _speed = speed; } + /**get & set speed, negative speed means playing reverse */ + float getSpeed() const; + void setSpeed(float speed); - /**get & set blend weight*/ + /**get & set blend weight, weight must positive*/ float getWeight() const { return _weight; } - void setWeight(float weight) { _weight = weight; } + void setWeight(float weight); - /**get & set play back*/ - bool getPlayBack() const { return _playBack; } - void setPlayBack(bool playBack) { _playBack = playBack; } + /**get & set play reverse*/ + CC_DEPRECATED_ATTRIBUTE bool getPlayBack() const { return _playReverse; } + CC_DEPRECATED_ATTRIBUTE void setPlayBack(bool reverse) { _playReverse = reverse; } CC_CONSTRUCTOR_ACCESS: @@ -86,11 +87,11 @@ CC_CONSTRUCTOR_ACCESS: protected: Animation3D* _animation; //animation data - float _speed; //playing speed + float _absSpeed; //playing speed float _weight; //blend weight float _start; //start time 0 - 1, used to generate sub Animate3D float _last; //last time 0 - 1, used to generate sub Animate3D - bool _playBack; // is playing back + bool _playReverse; // is playing reverse std::map _boneCurves; //weak ref }; diff --git a/cocos/3d/CCAnimation3D.cpp b/cocos/3d/CCAnimation3D.cpp index 9455a3e47e..611b130fe1 100644 --- a/cocos/3d/CCAnimation3D.cpp +++ b/cocos/3d/CCAnimation3D.cpp @@ -30,7 +30,7 @@ NS_CC_BEGIN -Animation3D* Animation3D::getOrCreate(const std::string& fileName, const std::string& animationName) +Animation3D* Animation3D::create(const std::string& fileName, const std::string& animationName) { std::string fullPath = FileUtils::getInstance()->fullPathForFilename(fileName); std::string key = fullPath + "#" + animationName; diff --git a/cocos/3d/CCAnimation3D.h b/cocos/3d/CCAnimation3D.h index a6f2a30a96..1d18cf6b0a 100644 --- a/cocos/3d/CCAnimation3D.h +++ b/cocos/3d/CCAnimation3D.h @@ -59,8 +59,10 @@ public: ~Curve(); }; - /**read all animation or only the animation with given animationName? animationName == "" read all.*/ - static Animation3D* getOrCreate(const std::string& filename, const std::string& animationName = ""); + /**read all animation or only the animation with given animationName? animationName == "" read the first.*/ + static Animation3D* create(const std::string& filename, const std::string& animationName = ""); + + CC_DEPRECATED_ATTRIBUTE static Animation3D* getOrCreate(const std::string& filename, const std::string& animationName = ""){ return create(filename, animationName); } /**get duration*/ float getDuration() const { return _duration; } diff --git a/cocos/3d/CCSprite3D.cpp b/cocos/3d/CCSprite3D.cpp index f92e4dc475..30cfca2552 100644 --- a/cocos/3d/CCSprite3D.cpp +++ b/cocos/3d/CCSprite3D.cpp @@ -342,7 +342,7 @@ void Sprite3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) _meshCommand.setDepthTestEnabled(true); if (_skin) { - _meshCommand.setMatrixPaletteSize(_skin->getMatrixPaletteSize()); + _meshCommand.setMatrixPaletteSize((int)_skin->getMatrixPaletteSize()); _meshCommand.setMatrixPalette(_skin->getMatrixPalette()); } //support tint and fade diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index ac8281b9f6..73b2784935 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -546,24 +546,23 @@ void Sprite3DWithSkinTest::addNewSpriteWithCoords(Vec2 p) addChild(sprite); sprite->setPosition( Vec2( p.x, p.y) ); - auto animation = Animation3D::getOrCreate(fileName); + auto animation = Animation3D::create(fileName); if (animation) { auto animate = Animate3D::create(animation); - if(std::rand() %3 == 0) - { - animate->setPlayBack(true); - } + bool inverse = (std::rand() % 3 == 0); int rand2 = std::rand(); + float speed = 1.0f; if(rand2 % 3 == 1) { - animate->setSpeed(animate->getSpeed() + CCRANDOM_0_1()); + speed = animate->getSpeed() + CCRANDOM_0_1(); } else if(rand2 % 3 == 2) { - animate->setSpeed(animate->getSpeed() - 0.5 * CCRANDOM_0_1()); + speed = animate->getSpeed() - 0.5 * CCRANDOM_0_1(); } + animate->setSpeed(inverse ? -speed : speed); sprite->runAction(RepeatForever::create(animate)); } @@ -652,7 +651,7 @@ void Animate3DTest::addSprite3D() sprite->setPosition(Vec2(s.width * 4.f / 5.f, s.height / 2.f)); addChild(sprite); _sprite = sprite; - auto animation = Animation3D::getOrCreate(fileName); + auto animation = Animation3D::create(fileName); if (animation) { auto animate = Animate3D::create(animation, 0.f, 1.933f);