Jelajahi Sumber

Use one transform.

Aster@中国上海 11 tahun lalu
induk
melakukan
4d6392c182

+ 22 - 21
Source/Engine/Urho2D/AnimatedSprite2D.cpp

@@ -88,8 +88,11 @@ void AnimatedSprite2D::SetLayer(int layer)
 
 
     for (unsigned i = 0; i < timelineNodes_.Size(); ++i)
     for (unsigned i = 0; i < timelineNodes_.Size(); ++i)
     {
     {
-        StaticSprite2D* objectSprite = timelineNodes_[i]->GetComponent<StaticSprite2D>();
-        objectSprite->SetLayer(layer_);
+        if (!timelineNodes_[i])
+            continue;
+
+        StaticSprite2D* staticSprite = timelineNodes_[i]->GetComponent<StaticSprite2D>();
+        staticSprite->SetLayer(layer_);
     }
     }
 }
 }
 
 
@@ -107,8 +110,11 @@ void AnimatedSprite2D::SetBlendMode(BlendMode blendMode)
 
 
     for (unsigned i = 0; i < timelineNodes_.Size(); ++i)
     for (unsigned i = 0; i < timelineNodes_.Size(); ++i)
     {
     {
-        StaticSprite2D* objectSprite = timelineNodes_[i]->GetComponent<StaticSprite2D>();
-        objectSprite->SetBlendMode(blendMode_);
+        if (!timelineNodes_[i])
+            continue;
+
+        StaticSprite2D* staticSprite = timelineNodes_[i]->GetComponent<StaticSprite2D>();
+        staticSprite->SetBlendMode(blendMode_);
     }
     }
 }
 }
 
 
@@ -283,8 +289,8 @@ void AnimatedSprite2D::UpdateAnimation(float timeStep)
                 const TimelineKey2D& nextKey = objectKeys[j + 1];
                 const TimelineKey2D& nextKey = objectKeys[j + 1];
                 float t = (time - currKey.time_)  / (nextKey.time_ - currKey.time_);
                 float t = (time - currKey.time_)  / (nextKey.time_ - currKey.time_);
 
 
-                timelineTransformInfos_[i].localTransform_ = currKey.transform_.Lerp(nextKey.transform_, t, currKey.spin_);
-                timelineTransformInfos_[i].worldTransformUpdated_ = false;
+                timelineTransformInfos_[i].worldSpace_ = false;
+                timelineTransformInfos_[i].transform_ = currKey.transform_.Lerp(nextKey.transform_, t, currKey.spin_);
 
 
                 // Update sprite's sprite and hot spot and color
                 // Update sprite's sprite and hot spot and color
                 Node* timelineNode = timelineNodes_[i];
                 Node* timelineNode = timelineNodes_[i];
@@ -302,9 +308,9 @@ void AnimatedSprite2D::UpdateAnimation(float timeStep)
         }
         }
     }
     }
 
 
-    // Update timeline's world transform
+    // Calculate timeline world transform.
     for (unsigned i = 0; i < timelineTransformInfos_.Size(); ++i)
     for (unsigned i = 0; i < timelineTransformInfos_.Size(); ++i)
-        UpateTimelineWorldTransform(i);
+        CalculateTimelineWorldTransform(i);
 
 
     // Get mainline key
     // Get mainline key
     const Vector<MainlineKey2D>& mainlineKeys = animation_->GetMainlineKeys();
     const Vector<MainlineKey2D>& mainlineKeys = animation_->GetMainlineKeys();
@@ -342,7 +348,7 @@ void AnimatedSprite2D::UpdateAnimation(float timeStep)
                 timelineNode->SetEnabled(true);
                 timelineNode->SetEnabled(true);
 
 
             // Update node's transform
             // Update node's transform
-            const Transform2D& transform = timelineTransformInfos_[i].worldTransform_;
+            const Transform2D& transform = timelineTransformInfos_[i].transform_;
             timelineNode->SetScale(transform.scale_);
             timelineNode->SetScale(transform.scale_);
             timelineNode->SetRotation(transform.angle_);
             timelineNode->SetRotation(transform.angle_);
             timelineNode->SetPosition(transform.position_);
             timelineNode->SetPosition(transform.position_);
@@ -356,23 +362,18 @@ void AnimatedSprite2D::UpdateAnimation(float timeStep)
     MarkForUpdate();
     MarkForUpdate();
 }
 }
 
 
-void AnimatedSprite2D::UpateTimelineWorldTransform(unsigned index)
+void AnimatedSprite2D::CalculateTimelineWorldTransform(unsigned index)
 {
 {
     TransformInfo& info = timelineTransformInfos_[index];
     TransformInfo& info = timelineTransformInfos_[index];
-    if (info.worldTransformUpdated_)
+    if (info.worldSpace_)
         return;
         return;
 
 
-    if (info.parent_ == -1)
-    {
-        info.worldTransform_ = info.localTransform_;
-        info.worldTransformUpdated_ = true;
-    }
-    else
-    {
-        UpateTimelineWorldTransform(info.parent_);
+    info.worldSpace_ = true;
 
 
-        info.worldTransform_ = timelineTransformInfos_[info.parent_].worldTransform_ * info.localTransform_;
-        info.worldTransformUpdated_ = true;
+    if (info.parent_ != -1)
+    {
+        CalculateTimelineWorldTransform(info.parent_);
+        info.transform_ = timelineTransformInfos_[info.parent_].transform_ * info.transform_;
     }
     }
 }
 }
 
 

+ 14 - 8
Source/Engine/Urho2D/AnimatedSprite2D.h

@@ -88,8 +88,8 @@ protected:
     void SetAnimation(Animation2D* animation);
     void SetAnimation(Animation2D* animation);
     /// Update animation.
     /// Update animation.
     void UpdateAnimation(float timeStep);
     void UpdateAnimation(float timeStep);
-    /// Update timeline world transform.
-    void UpateTimelineWorldTransform(unsigned index);
+    /// Calculate timeline world world transform.
+    void CalculateTimelineWorldTransform(unsigned index);
     /// Handle scene post update.
     /// Handle scene post update.
     void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
     void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
 
 
@@ -111,17 +111,23 @@ protected:
     float currentTime_;
     float currentTime_;
     /// Timeline nodes.
     /// Timeline nodes.
     Vector<SharedPtr<Node> > timelineNodes_;
     Vector<SharedPtr<Node> > timelineNodes_;
+
     /// Transform info.
     /// Transform info.
     struct TransformInfo
     struct TransformInfo
     {
     {
+        /// Construct.
+        TransformInfo() :
+            parent_(-1),
+            worldSpace_(false)
+        {
+        }
+
         /// Parent.
         /// Parent.
         int parent_;
         int parent_;
-        /// Local transform.
-        Transform2D localTransform_;
-        /// World transform updated.
-        bool worldTransformUpdated_;
-        /// World transform.
-        Transform2D worldTransform_;
+        /// World space.
+        bool worldSpace_;
+        /// Transform.
+        Transform2D transform_;
     };
     };
     /// Timeline transform infos.
     /// Timeline transform infos.
     Vector<TransformInfo> timelineTransformInfos_;
     Vector<TransformInfo> timelineTransformInfos_;

+ 63 - 67
Source/Engine/Urho2D/Animation2D.cpp

@@ -42,6 +42,69 @@ MainlineKey2D::MainlineKey2D() :
 {
 {
 }
 }
 
 
+Transform2D::Transform2D() :
+    position_(Vector2::ZERO),
+    angle_(0.0f),
+    scale_(Vector2::ONE)
+{
+}
+
+Transform2D::Transform2D(const Vector2 position, float angle, const Vector2& scale) :
+position_(position), 
+    angle_(angle), 
+    scale_(scale)
+{
+}
+
+Transform2D::Transform2D(const Transform2D& other) :
+position_(other.position_), 
+    angle_(other.angle_), 
+    scale_(other.scale_)
+{
+}
+
+Transform2D& Transform2D::operator = (const Transform2D& other)
+{
+    position_ = other.position_; 
+    angle_ = other.angle_;
+    scale_ = other.scale_;
+    return *this;
+}
+
+Transform2D Transform2D::operator * (const Transform2D& other) const
+{
+    float x = scale_.x_ * other.position_.x_;
+    float y = scale_.y_ * other.position_.y_;
+    float s = Sin(angle_);
+    float c = Cos(angle_);
+
+    Vector2 position;
+    position.x_ = (x * c) - (y * s);
+    position.y_ = (x * s) + (y * c);
+    position = position_ + position;
+
+    float angle = angle_ + other.angle_;
+    Vector2 scale = scale_ * other.scale_;
+
+    return Transform2D(position, angle, scale);
+}
+
+Transform2D Transform2D::Lerp(const Transform2D& other, float t, int spin) const
+{
+    Transform2D ret;
+    ret.position_ = position_.Lerp(other.position_, t);
+
+    if (spin > 0 && angle_ > other.angle_)
+        ret.angle_ = Urho3D::Lerp(angle_, other.angle_ + 360.0f, t);
+    else if (spin < 0 && angle_ < other.angle_)
+        ret.angle_= Urho3D::Lerp(angle_, other.angle_ - 360.0f, t);
+    else
+        ret.angle_= Urho3D::Lerp(angle_, other.angle_, t);
+
+    ret.scale_ = scale_.Lerp(other.scale_, t);
+    return ret;
+}
+
 const Reference2D* MainlineKey2D::GetReference(int timeline) const
 const Reference2D* MainlineKey2D::GetReference(int timeline) const
 {
 {
     for (unsigned i = 0; i < references_.Size(); ++i)
     for (unsigned i = 0; i < references_.Size(); ++i)
@@ -114,71 +177,4 @@ AnimationSet2D* Animation2D::GetAnimationSet() const
     return animationSet_;
     return animationSet_;
 }
 }
 
 
-
-Transform2D::Transform2D() :
-position_(Vector2::ZERO),
-    angle_(0.0f),
-    scale_(Vector2::ONE)
-{
-
-}
-
-Transform2D::Transform2D(const Vector2 position, float angle, const Vector2& scale) :
-position_(position), 
-    angle_(angle), 
-    scale_(scale)
-{
-
-}
-
-Transform2D::Transform2D(const Transform2D& other) :
-position_(other.position_), 
-    angle_(other.angle_), 
-    scale_(other.scale_)
-{
-
-}
-
-Transform2D& Transform2D::operator = (const Transform2D& other)
-{
-    position_ = other.position_; 
-    angle_ = other.angle_;
-    scale_ = other.scale_;
-    return *this;
-}
-
-Transform2D Transform2D::operator * (const Transform2D& other) const
-{
-    float x = scale_.x_ * other.position_.x_;
-    float y = scale_.y_ * other.position_.y_;
-    float s = Sin(angle_);
-    float c = Cos(angle_);
-
-    Vector2 position;
-    position.x_ = (x * c) - (y * s);
-    position.y_ = (x * s) + (y * c);
-    position = position_ + position;
-
-    float angle = angle_ + other.angle_;
-    Vector2 scale = scale_ * other.scale_;
-
-    return Transform2D(position, angle, scale);
-}
-
-Urho3D::Transform2D Transform2D::Lerp(const Transform2D& other, float t, int spin) const
-{
-    Transform2D ret;
-    ret.position_ = position_.Lerp(other.position_, t);
-
-    if (spin > 0 && angle_ > other.angle_)
-        ret.angle_ = Urho3D::Lerp(angle_, other.angle_ + 360.0f, t);
-    else if (spin < 0 && angle_ < other.angle_)
-        ret.angle_= Urho3D::Lerp(angle_, other.angle_ - 360.0f, t);
-    else
-        ret.angle_= Urho3D::Lerp(angle_, other.angle_, t);
-
-    ret.scale_ = scale_.Lerp(other.scale_, t);
-    return ret;
-}
-
 }
 }

+ 1 - 2
Source/Engine/Urho2D/Animation2D.h

@@ -22,7 +22,6 @@
 
 
 #pragma once
 #pragma once
 
 
-#include "HashMap.h"
 #include "Ptr.h"
 #include "Ptr.h"
 #include "RefCounted.h"
 #include "RefCounted.h"
 #include "Vector2.h"
 #include "Vector2.h"
@@ -151,7 +150,7 @@ public:
     void AddMainlineKey(const MainlineKey2D& mainlineKey);
     void AddMainlineKey(const MainlineKey2D& mainlineKey);
     /// Add timeline.
     /// Add timeline.
     void AddTimeline(const Timeline2D& timeline);
     void AddTimeline(const Timeline2D& timeline);
-    /// Set time line parent.
+    /// Set timeline parent.
     void SetTimelineParent(int timeline, int timelineParent);
     void SetTimelineParent(int timeline, int timelineParent);
 
 
     /// Return animation set.
     /// Return animation set.

+ 1 - 1
Source/Engine/Urho2D/AnimationSet2D.cpp

@@ -167,7 +167,7 @@ bool AnimationSet2D::LoadAnimation(const XMLElement& animationElem)
         looped = animationElem.GetBool("looping");
         looped = animationElem.GetBool("looping");
     animation->SetLooped(looped);
     animation->SetLooped(looped);
 
 
-    // Load time lines
+    // Load timelines
     for (XMLElement timelineElem = animationElem.GetChild("timeline"); timelineElem; timelineElem = timelineElem.GetNext("timeline"))
     for (XMLElement timelineElem = animationElem.GetChild("timeline"); timelineElem; timelineElem = timelineElem.GetNext("timeline"))
     {
     {
         Timeline2D timeline;
         Timeline2D timeline;

+ 2 - 2
Source/Engine/Urho2D/AnimationSet2D.h

@@ -27,9 +27,9 @@
 namespace Urho3D
 namespace Urho3D
 {
 {
 
 
-class XMLElement;
-class Sprite2D;
 class Animation2D;
 class Animation2D;
+class Sprite2D;
+class XMLElement;
 
 
 /// Spriter animation set, it includes one or more animations, for more information please refer to http://www.brashmonkey.com/spriter.htm.
 /// Spriter animation set, it includes one or more animations, for more information please refer to http://www.brashmonkey.com/spriter.htm.
 class URHO3D_API AnimationSet2D : public Resource
 class URHO3D_API AnimationSet2D : public Resource