Browse Source

Merge pull request #2231 from eugeneko/ribbon-trail-update

Add RibbonTrail base velocity.
Eugene Kozlov 7 years ago
parent
commit
4ee6b14aac

+ 2 - 0
Source/Urho3D/AngelScript/GraphicsAPI.cpp

@@ -1715,6 +1715,8 @@ static void RegisterRibbonTrail(asIScriptEngine* engine)
     engine->RegisterObjectMethod("RibbonTrail", "float get_endScale() const", asMETHOD(RibbonTrail, GetEndScale), asCALL_THISCALL);
     engine->RegisterObjectMethod("RibbonTrail", "void set_trailType(TrailType)", asMETHOD(RibbonTrail, SetTrailType), asCALL_THISCALL);
     engine->RegisterObjectMethod("RibbonTrail", "TrailType get_trailType() const", asMETHOD(RibbonTrail, GetTrailType), asCALL_THISCALL);
+    engine->RegisterObjectMethod("RibbonTrail", "void set_baseVelocity(const Vector3&in)", asMETHOD(RibbonTrail, SetBaseVelocity), asCALL_THISCALL);
+    engine->RegisterObjectMethod("RibbonTrail", "const Vector3& get_baseVelocity() const", asMETHOD(RibbonTrail, GetBaseVelocity), asCALL_THISCALL);
     engine->RegisterObjectMethod("RibbonTrail", "void set_sorted(bool)", asMETHOD(RibbonTrail, SetSorted), asCALL_THISCALL);
     engine->RegisterObjectMethod("RibbonTrail", "bool get_sorted() const", asMETHOD(RibbonTrail, IsSorted), asCALL_THISCALL);
     engine->RegisterObjectMethod("RibbonTrail", "void set_lifetime(float)", asMETHOD(RibbonTrail, SetLifetime), asCALL_THISCALL);

+ 21 - 7
Source/Urho3D/Graphics/RibbonTrail.cpp

@@ -112,6 +112,7 @@ void RibbonTrail::RegisterObject(Context* context)
     URHO3D_ACCESSOR_ATTRIBUTE("Emitting", IsEmitting, SetEmitting, bool, true, AM_DEFAULT);
     URHO3D_ACCESSOR_ATTRIBUTE("Update Invisible", GetUpdateInvisible, SetUpdateInvisible, bool, false, AM_DEFAULT);
     URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Trail Type", GetTrailType, SetTrailType, TrailType, trailTypeNames, TT_FACE_CAMERA, AM_DEFAULT);
+    URHO3D_ACCESSOR_ATTRIBUTE("Base Velocity", GetBaseVelocity, SetBaseVelocity, Vector3, Vector3::ZERO, AM_DEFAULT);
     URHO3D_ACCESSOR_ATTRIBUTE("Tail Lifetime", GetLifetime, SetLifetime, float, 1.0f, AM_DEFAULT);
     URHO3D_ACCESSOR_ATTRIBUTE("Tail Column", GetTailColumn, SetTailColumn, unsigned, 0, AM_DEFAULT);
     URHO3D_ACCESSOR_ATTRIBUTE("Vertex Distance", GetVertexDistance, SetVertexDistance, float, 0.1f, AM_DEFAULT);
@@ -204,15 +205,23 @@ void RibbonTrail::Update(const FrameInfo &frame)
     if (!needUpdate_)
         return;
 
-    UpdateTail();
+    UpdateTail(frame.timeStep_);
     OnMarkedDirty(node_);
     needUpdate_ = false;
 }
 
-void RibbonTrail::UpdateTail()
+void RibbonTrail::UpdateTail(float timeStep)
 {
-    Vector3 worldPosition = node_->GetWorldPosition();
-    float path = (previousPosition_ - worldPosition).Length();
+    // Apply base velocity to all cached positions
+    if (baseVelocity_ != Vector3::ZERO)
+    {
+        for (TrailPoint& point : points_)
+            point.position_ += baseVelocity_ * timeStep;
+        previousPosition_ += baseVelocity_ * timeStep;
+    }
+
+    const Vector3 worldPosition = node_->GetWorldPosition();
+    const float path = (previousPosition_ - worldPosition).Length();
 
     // Update tails lifetime
     int expiredIndex = -1;
@@ -256,7 +265,7 @@ void RibbonTrail::UpdateTail()
     // Update end of trail position using endTail linear interpolation
     else if (points_.Size() > 1 && points_[0].lifetime_ < lifetime_)
     {
-        float step = SmoothStep(startEndTailTime_, lifetime_, points_[0].lifetime_);
+        const float step = SmoothStep(startEndTailTime_, lifetime_, points_[0].lifetime_);
         points_[0].position_ = Lerp(endTail_.position_, points_[1].position_, step);
         bufferDirty_ = true;
     }
@@ -264,7 +273,7 @@ void RibbonTrail::UpdateTail()
     // Add starting points
     if (points_.Size() == 0 && path > M_LARGE_EPSILON && emitting_)
     {
-        Vector3 forwardMotion = (previousPosition_ - worldPosition).Normalized();
+        const Vector3 forwardMotion = (previousPosition_ - worldPosition).Normalized();
 
         TrailPoint startPoint{previousPosition_, forwardMotion};
         TrailPoint nextPoint{worldPosition, forwardMotion};
@@ -286,7 +295,7 @@ void RibbonTrail::UpdateTail()
     // Add more points
     if (points_.Size() > 1 && emitting_)
     {
-        Vector3 forwardMotion = (previousPosition_ - worldPosition).Normalized();
+        const Vector3 forwardMotion = (previousPosition_ - worldPosition).Normalized();
 
         // Add more points if path exceeded tail length
         if (path > vertexDistance_)
@@ -847,6 +856,11 @@ void RibbonTrail::SetTrailType(TrailType type)
     MarkNetworkUpdate();
 }
 
+void RibbonTrail::SetBaseVelocity(const Vector3& baseVelocity)
+{
+    baseVelocity_ = baseVelocity;
+}
+
 void RibbonTrail::SetMaterialAttr(const ResourceRef& value)
 {
     auto* cache = GetSubsystem<ResourceCache>();

+ 11 - 4
Source/Urho3D/Graphics/RibbonTrail.h

@@ -102,13 +102,15 @@ public:
     void SetEndScale(float endScale);
     /// Set how the trail behave.
     void SetTrailType(TrailType type);
+    /// Set base velocity applied to the trail.
+    void SetBaseVelocity(const Vector3& baseVelocity);
     /// Set whether tails are sorted by distance. Default false.
     void SetSorted(bool enable);
     /// Set tail time to live.
     void SetLifetime(float time);
     /// Set whether trail should be emitting.
     void SetEmitting(bool emitting);
-    /// Set whether to update when trail emiiter are not visible.
+    /// Set whether to update when trail emitter are not visible.
     void SetUpdateInvisible(bool enable);
     /// Set number of column for every tails. Can be useful for fixing distortion at high angle.
     void SetTailColumn(unsigned tailColumn);
@@ -153,7 +155,10 @@ public:
     /// Return how the trail behave.
     TrailType GetTrailType() const { return trailType_; }
 
-    /// Get number of column for tails.
+    /// Return base trail velocity.
+    const Vector3& GetBaseVelocity() const { return baseVelocity_; }
+
+    /// Return number of column for tails.
     unsigned GetTailColumn() const { return tailColumn_; }
 
     /// Return whether is currently emitting.
@@ -179,6 +184,8 @@ protected:
     float animationLodTimer_;
     /// Trail type.
     TrailType trailType_;
+    /// Base velocity applied to the trail.
+    Vector3 baseVelocity_;
 
 private:
     /// Handle scene post-update event.
@@ -189,7 +196,7 @@ private:
     /// Rewrite RibbonTrail vertex buffer.
     void UpdateVertexBuffer(const FrameInfo& frame);
     /// Update/Rebuild tail mesh only if position changed (called by UpdateBatches())
-    void UpdateTail();
+    void UpdateTail(float timeStep);
     /// Geometry.
     SharedPtr<Geometry> geometry_;
     /// Vertex buffer.
@@ -220,7 +227,7 @@ private:
     float endScale_;
     /// Last scene timestep.
     float lastTimeStep_;
-    // Lifetime
+    /// Lifetime
     float lifetime_;
     /// Number of columns for every tails.
     unsigned tailColumn_;

+ 3 - 0
Source/Urho3D/LuaScript/pkgs/Graphics/RibbonTrail.pkg

@@ -16,6 +16,7 @@ class RibbonTrail : public Drawable
     void SetStartScale(float startScale);
     void SetEndScale(float endScale);
     void SetTrailType(TrailType type);
+    void SetBaseVelocity(const Vector3& baseVelocity);
     void SetSorted(bool enable);
     void SetLifetime(float time);
     void SetEmitting(bool emitting);
@@ -33,6 +34,7 @@ class RibbonTrail : public Drawable
     float GetStartScale() const;
     float GetEndScale() const;
     TrailType GetTrailType() const;
+    const Vector3& GetBaseVelocity() const;
     bool IsSorted() const;
     float GetLifetime() const;
     unsigned GetTailColumn() const;
@@ -48,6 +50,7 @@ class RibbonTrail : public Drawable
     tolua_property__get_set float startScale;
     tolua_property__get_set float endScale;
     tolua_property__get_set TrailType trailType;
+    tolua_property__get_set Vector3 baseVelocity;
     tolua_property__is_set bool sorted;
     tolua_property__get_set float lifetime;
     tolua_property__get_set unsigned tailColumn;