Browse Source

Store animation references with the model resource to avoid issues with updating nodes which reference them (was in AnimatonController component)

Josh Engebretson 10 years ago
parent
commit
34b5f07148

+ 16 - 19
Source/Atomic/Atomic3D/AnimationController.cpp

@@ -51,8 +51,7 @@ static const unsigned MAX_NODE_ANIMATION_STATES = 256;
 extern const char* LOGIC_CATEGORY;
 
 AnimationController::AnimationController(Context* context) :
-    Component(context),
-    animationsResourcesAttr_(Animation::GetTypeStatic())
+    Component(context)
 {
 }
 
@@ -72,8 +71,6 @@ void AnimationController::RegisterObject(Context* context)
     MIXED_ACCESSOR_ATTRIBUTE("Node Animation States", GetNodeAnimationStatesAttr, SetNodeAnimationStatesAttr, VariantVector,
         Variant::emptyVariantVector, AM_FILE | AM_NOEDIT);
 
-    ACCESSOR_ATTRIBUTE("Animation Resources", GetAnimationResourcesAttr, SetAnimationResourcesAttr, ResourceRefList, ResourceRefList(Animation::GetTypeStatic()), AM_FILE | AM_NOEDIT);
-
 }
 
 void AnimationController::OnSetEnabled()
@@ -894,25 +891,25 @@ void AnimationController::ClearAnimationResources()
     animationsResources_.Clear();
 }
 
-void AnimationController::SetAnimationResourcesAttr(const ResourceRefList& value)
+void AnimationController::ApplyAttributes()
 {
-    animationsResources_.Clear();
 
-    ResourceCache* cache = GetSubsystem<ResourceCache>();
-    for (unsigned i = 0; i < value.names_.Size(); ++i)
-    {
-        AddAnimationResource(cache->GetResource<Animation>(value.names_[i]));
-    }
-}
+    // This currently requires that the AnimationController is after the AnimatedModel
+    // component on the node, look into removing the requirement
 
-const ResourceRefList& AnimationController::GetAnimationResourcesAttr() const
-{
-    animationsResourcesAttr_.names_.Resize(animationsResources_.Size());
-    for (unsigned i = 0; i < animationsResources_.Size(); ++i)
-        animationsResourcesAttr_.names_[i] = GetResourceName(animationsResources_[i]);
+    AnimatedModel* animatedModel = GetComponent<AnimatedModel>();
 
-    return animationsResourcesAttr_;
-}
+    if (!animatedModel)
+        return;
+
+    Model* model = animatedModel->GetModel();
+
+    if (!model)
+        return;
+
+    animationsResources_ = model->GetAnimationResources();
 
 
 }
+
+}

+ 5 - 6
Source/Atomic/Atomic3D/AnimationController.h

@@ -178,12 +178,15 @@ public:
     void AddAnimationResource(Animation* animation);
     void RemoveAnimationResource(Animation* animation);
     void ClearAnimationResources();
-    void SetAnimationResourcesAttr(const ResourceRefList& value);
-    const ResourceRefList& GetAnimationResourcesAttr() const;
+
 protected:
     /// Handle scene being assigned.
     virtual void OnSceneSet(Scene* scene);
 
+    // ATOMIC BEGIN
+    void ApplyAttributes();
+    // ATOMIC END
+
 private:
     /// Add an animation state either to AnimatedModel or as a node animation.
     AnimationState* AddAnimationState(Animation* animation);
@@ -203,10 +206,6 @@ private:
 
     /// animation resources
     Vector<SharedPtr<Animation> > animationsResources_;
-
-    /// Material list attribute.
-    mutable ResourceRefList animationsResourcesAttr_;
-    ;
 };
 
 }

+ 74 - 2
Source/Atomic/Atomic3D/Model.cpp

@@ -33,6 +33,9 @@
 
 #include "../DebugNew.h"
 
+#include "Animation.h"
+#include "../Resource/ResourceCache.h"
+
 namespace Atomic
 {
 
@@ -73,7 +76,13 @@ void Model::RegisterObject(Context* context)
 bool Model::BeginLoad(Deserializer& source)
 {
     // Check ID
-    if (source.ReadFileID() != "UMDL")
+
+    String id = source.ReadFileID();
+    bool umdl = false;
+    if (id == "UMDL") // we only support UMDL for some current legacy mdl's (ToonTown)
+        umdl = true;
+
+    if (!umdl && id != "AMDL")
     {
         LOGERROR(source.GetName() + " is not a valid model file");
         return false;
@@ -284,6 +293,25 @@ bool Model::BeginLoad(Deserializer& source)
         geometryCenters_.Push(Vector3::ZERO);
     memoryUse += sizeof(Vector3) * geometries_.Size();
 
+    if (umdl)
+    {
+        SetMemoryUse(memoryUse);
+        return true;
+    }
+
+    // MODEL_VERSION
+    unsigned version = source.ReadUInt();
+
+    ResourceRefList animList = source.ReadResourceRefList();
+
+    animationsResources_.Clear();
+
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    for (unsigned i = 0; i < animList.names_.Size(); ++i)
+    {
+        AddAnimationResource(cache->GetResource<Animation>(animList.names_[i]));
+    }
+
     SetMemoryUse(memoryUse);
     return true;
 }
@@ -338,7 +366,7 @@ bool Model::EndLoad()
 bool Model::Save(Serializer& dest) const
 {
     // Write ID
-    if (!dest.WriteFileID("UMDL"))
+    if (!dest.WriteFileID("AMDL")) // atomic model specifier
         return false;
 
     // Write vertex buffers
@@ -423,6 +451,21 @@ bool Model::Save(Serializer& dest) const
     for (unsigned i = 0; i < geometryCenters_.Size(); ++i)
         dest.WriteVector3(geometryCenters_[i]);
 
+    // ATOMIC BEGIN
+
+    dest.WriteUInt(MODEL_VERSION);
+
+    // animation resources
+
+    ResourceRefList animList(Animation::GetTypeStatic());
+    animList.names_.Resize(animationsResources_.Size());
+    for (unsigned i = 0; i < animationsResources_.Size(); ++i)
+        animList.names_[i] = GetResourceName(animationsResources_[i]);
+    dest.WriteResourceRefList(animList);
+
+    // ATOMIC END
+
+
     return true;
 }
 
@@ -724,4 +767,33 @@ unsigned Model::GetMorphRangeCount(unsigned bufferIndex) const
     return bufferIndex < vertexBuffers_.Size() ? morphRangeCounts_[bufferIndex] : 0;
 }
 
+// ATOMIC BEGIN
+
+void Model::AddAnimationResource(Animation* animation)
+{
+    if (!animation)
+        return;
+
+    SharedPtr<Animation> anim(animation);
+
+    if (!animationsResources_.Contains(anim))
+        animationsResources_.Push(anim);
+}
+
+void Model::RemoveAnimationResource(Animation* animation)
+{
+    if (!animation)
+        return;
+
+    animationsResources_.Remove(SharedPtr<Animation>(animation));
+
+}
+
+void Model::ClearAnimationResources()
+{
+    animationsResources_.Clear();
+}
+
+// ATOMIC END
+
 }

+ 25 - 0
Source/Atomic/Atomic3D/Model.h

@@ -29,6 +29,8 @@
 #include "../Math/BoundingBox.h"
 #include "../Resource/Resource.h"
 
+#include "Animation.h"
+
 namespace Atomic
 {
 
@@ -104,6 +106,12 @@ struct GeometryDesc
     unsigned indexCount_;
 };
 
+// ATOMIC BEGIN
+
+static const unsigned MODEL_VERSION = 1;
+
+// ATOMIC END
+
 /// 3D model resource.
 class ATOMIC_API Model : public Resource
 {
@@ -201,6 +209,16 @@ public:
     /// Return vertex buffer morph range vertex count.
     unsigned GetMorphRangeCount(unsigned bufferIndex) const;
 
+    // ATOMIC BEGIN
+
+    void AddAnimationResource(Animation* animation);
+    void RemoveAnimationResource(Animation* animation);
+    void ClearAnimationResources();
+    unsigned GetAnimationCount() const { return animationsResources_.Size(); }
+    const Vector<SharedPtr<Animation>>& GetAnimationResources() { return animationsResources_; }
+
+    // ATOMIC END
+
 private:
     /// Bounding box.
     BoundingBox boundingBox_;
@@ -228,6 +246,13 @@ private:
     Vector<IndexBufferDesc> loadIBData_;
     /// Geometry definitions for asynchronous loading.
     Vector<PODVector<GeometryDesc> > loadGeometries_;
+
+    // ATOMIC BEGIN
+
+    /// animation resources
+    Vector<SharedPtr<Animation> > animationsResources_;
+
+    // ATOMIC END
 };
 
 }

+ 32 - 8
Source/ToolCore/Assets/ModelImporter.cpp

@@ -5,7 +5,7 @@
 #include <Atomic/IO/FileSystem.h>
 #include <Atomic/Scene/Node.h>
 
-#include <Atomic/Atomic3D/AnimationController.h>
+#include <Atomic/Atomic3D/AnimatedModel.h>
 #include <Atomic/Atomic3D/Animation.h>
 #include <Atomic/Atomic3D/StaticModel.h>
 #include <Atomic/Atomic3D/Model.h>
@@ -96,13 +96,19 @@ bool ModelImporter::ImportAnimation(const String& filename, const String& name,
 
             ResourceCache* cache = GetSubsystem<ResourceCache>();
 
-            AnimationController* controller = importNode_->GetComponent<AnimationController>();
+            AnimatedModel* animatedModel = importNode_->GetComponent<AnimatedModel>();
 
-            if (controller)
+            if (animatedModel)
             {
-                SharedPtr<Animation> animation = cache->GetTempResource<Animation>(fileName + extension);
-                if (animation)
-                    controller->AddAnimationResource(animation);
+                Model* model = animatedModel->GetModel();
+
+                if (model)
+                {
+                    SharedPtr<Animation> animation = cache->GetTempResource<Animation>(fileName + extension);
+                    if (animation)
+                        model->AddAnimationResource(animation);
+                }
+
             }
 
             LOGINFOF("Import Info: %s : %s", info.name_.CString(), fileName.CString());
@@ -224,17 +230,35 @@ bool ModelImporter::Import()
     {
         // skip external animations, they will be brought in when importing their
         // corresponding model
+
         if (!modelAssetFilename.Contains("@"))
         {
             ImportModel();
 
             if (importAnimations_)
             {
-                //ImportAnimations();
+                ImportAnimations();
             }
 
-        }
+            AnimatedModel* animatedModel = importNode_->GetComponent<AnimatedModel>();
+            if (animatedModel)
+            {
+                Model* model = animatedModel->GetModel();
+                if (model && model->GetAnimationCount())
+                {
+                    // resave with animation info
+
+                    File mdlFile(context_);
+                    if (!mdlFile.Open(asset_->GetCachePath() + ".mdl", FILE_WRITE))
+                    {
+                        ErrorExit("Could not open output file " + asset_->GetCachePath() + ".mdl");
+                        return false;
+                    }
 
+                    model->Save(mdlFile);
+                }
+            }
+        }
     }