فهرست منبع

Serialize the node animation states of an AnimationController.
Merged DecalSet & ParticleEmitter components to the Geometry component category.

Lasse Öörni 12 سال پیش
والد
کامیت
6dcffc2f67

+ 53 - 0
Engine/Graphics/AnimationController.cpp

@@ -45,6 +45,7 @@ static const unsigned char CTRL_SETTIME = 0x08;
 static const unsigned char CTRL_SETWEIGHT = 0x10;
 static const float EXTRA_ANIM_FADEOUT_TIME = 0.1f;
 static const float COMMAND_STAY_TIME = 0.25f;
+static const unsigned MAX_NODE_ANIMATION_STATES = 256;
 
 extern const char* LOGIC_CATEGORY;
 
@@ -64,6 +65,7 @@ void AnimationController::RegisterObject(Context* context)
     ACCESSOR_ATTRIBUTE(AnimationController, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
     ACCESSOR_ATTRIBUTE(AnimationController, VAR_VARIANTVECTOR, "Animations", GetAnimationsAttr, SetAnimationsAttr, VariantVector, Variant::emptyVariantVector, AM_FILE | AM_NOEDIT);
     REF_ACCESSOR_ATTRIBUTE(AnimationController, VAR_BUFFER, "Network Animations", GetNetAnimationsAttr, SetNetAnimationsAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_NET | AM_LATESTDATA | AM_NOEDIT);
+    ACCESSOR_ATTRIBUTE(AnimationController, VAR_VARIANTVECTOR, "Node Animation States", GetNodeAnimationStatesAttr, SetNodeAnimationStatesAttr, VariantVector, Variant::emptyVariantVector, AM_FILE | AM_NOEDIT);
 }
 
 void AnimationController::OnSetEnabled()
@@ -595,6 +597,41 @@ void AnimationController::SetNetAnimationsAttr(const PODVector<unsigned char>& v
     }
 }
 
+
+void AnimationController::SetNodeAnimationStatesAttr(VariantVector value)
+{
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    nodeAnimationStates_.Clear();
+    unsigned index = 0;
+    unsigned numStates = index < value.Size() ? value[index++].GetUInt() : 0;
+    // Prevent negative or overly large value being assigned from the editor
+    if (numStates > M_MAX_INT)
+        numStates = 0;
+    if (numStates > MAX_NODE_ANIMATION_STATES)
+        numStates = MAX_NODE_ANIMATION_STATES;
+    
+    nodeAnimationStates_.Reserve(numStates);
+    while (numStates--)
+    {
+        if (index + 2 < value.Size())
+        {
+            // Note: null animation is allowed here for editing
+            const ResourceRef& animRef = value[index++].GetResourceRef();
+            SharedPtr<AnimationState> newState(new AnimationState(GetNode(), cache->GetResource<Animation>(animRef.id_)));
+            nodeAnimationStates_.Push(newState);
+
+            newState->SetLooped(value[index++].GetBool());
+            newState->SetTime(value[index++].GetFloat());
+        }
+        else
+        {
+            // If not enough data, just add an empty animation state
+            SharedPtr<AnimationState> newState(new AnimationState(GetNode(), 0));
+            nodeAnimationStates_.Push(newState);
+        }
+    }
+}
+
 VariantVector AnimationController::GetAnimationsAttr() const
 {
     VariantVector ret;
@@ -668,6 +705,22 @@ const PODVector<unsigned char>& AnimationController::GetNetAnimationsAttr() cons
     return attrBuffer_.GetBuffer();
 }
 
+VariantVector AnimationController::GetNodeAnimationStatesAttr() const
+{
+    VariantVector ret;
+    ret.Reserve(nodeAnimationStates_.Size() * 3 + 1);
+    ret.Push(nodeAnimationStates_.Size());
+    for (Vector<SharedPtr<AnimationState> >::ConstIterator i = nodeAnimationStates_.Begin(); i != nodeAnimationStates_.End(); ++i)
+    {
+        AnimationState* state = *i;
+        Animation* animation = state->GetAnimation();
+        ret.Push(ResourceRef(Animation::GetTypeStatic(), animation ? animation->GetNameHash() : StringHash()));
+        ret.Push(state->IsLooped());
+        ret.Push(state->GetTime());
+    }
+    return ret;
+}
+
 void AnimationController::OnNodeSet(Node* node)
 {
     if (node)

+ 7 - 3
Engine/Graphics/AnimationController.h

@@ -152,14 +152,18 @@ public:
     /// Return animation autofade time.
     float GetAutoFade(const String& name) const;
     
-    /// Set animations attribute.
+    /// Set animation control structures attribute.
     void SetAnimationsAttr(VariantVector value);
     /// Set animations attribute for network replication.
     void SetNetAnimationsAttr(const PODVector<unsigned char>& value);
-    /// Return animations attribute.
+    /// Set node animation states attribute.
+    void SetNodeAnimationStatesAttr(VariantVector value);
+    /// Return animation control structures attribute.
     VariantVector GetAnimationsAttr() const;
     /// Return animations attribute for network replication.
     const PODVector<unsigned char>& GetNetAnimationsAttr() const;
+    /// Return node animation states attribute.
+    VariantVector GetNodeAnimationStatesAttr() const;
     
 protected:
     /// Handle node being assigned.
@@ -177,7 +181,7 @@ private:
     /// Handle scene post-update event.
     void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
     
-    /// Controlled animations.
+    /// Animation control structures.
     Vector<AnimationControl> animations_;
     /// Node hierarchy mode animation states.
     Vector<SharedPtr<AnimationState> > nodeAnimationStates_;

+ 3 - 3
Engine/Graphics/DecalSet.cpp

@@ -46,6 +46,8 @@
 namespace Urho3D
 {
 
+extern const char* GEOMETRY_CATEGORY;
+
 static const unsigned MIN_VERTICES = 4;
 static const unsigned MIN_INDICES = 6;
 static const unsigned MAX_VERTICES = 65536;
@@ -55,8 +57,6 @@ static const unsigned STATIC_ELEMENT_MASK = MASK_POSITION | MASK_NORMAL | MASK_T
 static const unsigned SKINNED_ELEMENT_MASK = MASK_POSITION | MASK_NORMAL | MASK_TEXCOORD1 | MASK_TANGENT | MASK_BLENDWEIGHTS |
     MASK_BLENDINDICES;
 
-extern const char* EFFECT_CATEGORY;
-
 static DecalVertex ClipEdge(const DecalVertex& v0, const DecalVertex& v1, float d0, float d1, bool skinned)
 {
     DecalVertex ret;
@@ -178,7 +178,7 @@ DecalSet::~DecalSet()
 
 void DecalSet::RegisterObject(Context* context)
 {
-    context->RegisterFactory<DecalSet>(EFFECT_CATEGORY);
+    context->RegisterFactory<DecalSet>(GEOMETRY_CATEGORY);
     
     ACCESSOR_ATTRIBUTE(DecalSet, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
     ACCESSOR_ATTRIBUTE(DecalSet, VAR_RESOURCEREF, "Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);

+ 3 - 3
Engine/Graphics/ParticleEmitter.cpp

@@ -37,6 +37,8 @@
 namespace Urho3D
 {
 
+extern const char* GEOMETRY_CATEGORY;
+
 static const char* emitterTypeNames[] =
 {
     "Sphere",
@@ -44,8 +46,6 @@ static const char* emitterTypeNames[] =
     0
 };
 
-const char* EFFECT_CATEGORY = "Effect";
-
 static const unsigned MAX_PARTICLES_IN_FRAME = 100;
 static const unsigned DEFAULT_NUM_PARTICLES = 10;
 static const Vector2 DEFAULT_PARTICLE_SIZE(0.1f, 0.1f);
@@ -102,7 +102,7 @@ ParticleEmitter::~ParticleEmitter()
 
 void ParticleEmitter::RegisterObject(Context* context)
 {
-    context->RegisterFactory<ParticleEmitter>(EFFECT_CATEGORY);
+    context->RegisterFactory<ParticleEmitter>(GEOMETRY_CATEGORY);
     
     ACCESSOR_ATTRIBUTE(ParticleEmitter, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
     ACCESSOR_ATTRIBUTE(ParticleEmitter, VAR_RESOURCEREF, "Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);