Jelajahi Sumber

Animation and assimp deltas

Josh Engebretson 9 tahun lalu
induk
melakukan
bc3ca75238

+ 8 - 0
Source/Atomic/Graphics/AnimatedModel.cpp

@@ -111,6 +111,14 @@ void AnimatedModel::RegisterObject(Context* context)
         Variant::emptyVariantVector, AM_FILE);
     ATOMIC_ACCESSOR_ATTRIBUTE("Morphs", GetMorphsAttr, SetMorphsAttr, PODVector<unsigned char>, Variant::emptyBuffer,
         AM_DEFAULT | AM_NOEDIT);
+
+    // ATOMIC BEGIN
+
+    ATOMIC_ACCESSOR_ATTRIBUTE("Geometry Enabled", GetGeometryEnabledAttr, SetGeometryEnabledAttr, VariantVector,
+        Variant::emptyVariantVector, AM_FILE | AM_NOEDIT);
+
+    // ATOMIC END
+
 }
 
 bool AnimatedModel::Load(Deserializer& source, bool setInstanceDefault)

+ 18 - 0
Source/Atomic/Graphics/Animation.cpp

@@ -395,4 +395,22 @@ AnimationTriggerPoint* Animation::GetTrigger(unsigned index)
     return index < triggers_.Size() ? &triggers_[index] : (AnimationTriggerPoint*)0;
 }
 
+// ATOMIC BEGIN
+
+/// Set all animation tracks.
+void Animation::SetTracks(const Vector<AnimationTrack>& tracks)
+{
+    tracks_.Clear();
+
+    for (Vector<AnimationTrack>::ConstIterator itr = tracks.Begin(); itr != tracks.End(); itr++)
+    {
+        tracks_[itr->name_] = *itr;
+    }
+
+
+}
+
+// ATOMIC END
+
+
 }

+ 7 - 0
Source/Atomic/Graphics/Animation.h

@@ -178,6 +178,13 @@ public:
     /// Return a trigger point by index.
     AnimationTriggerPoint* GetTrigger(unsigned index);
 
+    // ATOMIC BEGIN
+
+    /// Set all animation tracks.
+    void SetTracks(const Vector<AnimationTrack>& tracks);
+
+    // ATOMIC END
+
 private:
     /// Animation name.
     String animationName_;

+ 141 - 2
Source/Atomic/Graphics/AnimationController.cpp

@@ -53,7 +53,12 @@ static const unsigned MAX_NODE_ANIMATION_STATES = 256;
 extern const char* LOGIC_CATEGORY;
 
 AnimationController::AnimationController(Context* context) :
-    Component(context)
+    Component(context),
+// ATOMIC BEGIN
+    animationResourcesAttr_(Animation::GetTypeStatic()),
+    autoPlay_(true),
+    autoPlayed_(false)
+// ATOMIC END
 {
 }
 
@@ -72,6 +77,13 @@ void AnimationController::RegisterObject(Context* context)
         Variant::emptyBuffer, AM_NET | AM_LATESTDATA | AM_NOEDIT);
     ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Node Animation States", GetNodeAnimationStatesAttr, SetNodeAnimationStatesAttr, VariantVector,
         Variant::emptyVariantVector, AM_FILE | AM_NOEDIT);
+
+// ATOMIC BEGIN
+    ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Animation", GetAnimationAttr, SetAnimationAttr, ResourceRef, ResourceRef(Animation::GetTypeStatic()), AM_DEFAULT);
+    ATOMIC_ATTRIBUTE("Autoplay", bool, autoPlay_, true, AM_DEFAULT);
+    ATOMIC_ACCESSOR_ATTRIBUTE("AnimationResources", GetAnimationResourcesAttr, SetAnimationResourcesAttr, ResourceRefList, ResourceRefList(Animation::GetTypeStatic()), AM_DEFAULT);
+// ATOMIC END
+
 }
 
 void AnimationController::OnSetEnabled()
@@ -88,6 +100,15 @@ void AnimationController::OnSetEnabled()
 
 void AnimationController::Update(float timeStep)
 {
+
+// ATOMIC BEGIN
+    if (autoPlay_ && !autoPlayed_ && animation_.NotNull())
+    {
+        autoPlayed_ = true;
+        Play(animation_->GetAnimationName(), 0, true);
+    }
+// ATOMIC END
+
     // Loop through animations
     for (unsigned i = 0; i < animations_.Size();)
     {
@@ -159,12 +180,37 @@ void AnimationController::Update(float timeStep)
 
 bool AnimationController::Play(const String& name, unsigned char layer, bool looped, float fadeInTime)
 {
+// ATOMIC BEGIN
+
+    Animation* newAnimation = 0;
+
+    // Check if we're using attached animation resource
+    if (animation_.NotNull() && animation_->GetAnimationName() == name)
+    {
+        newAnimation = animation_;
+    }
+    else
+    {
+        for (unsigned i = 0; i < animationResources_.Size(); i++)
+        {
+            if (name == animationResources_[i]->GetAnimationName())
+            {
+                newAnimation = animationResources_[i];
+                break;
+            }
+        }
+    }
+
     // Get the animation resource first to be able to get the canonical resource name
     // (avoids potential adding of duplicate animations)
-    Animation* newAnimation = GetSubsystem<ResourceCache>()->GetResource<Animation>(name);
+    if (!newAnimation)
+        newAnimation = GetSubsystem<ResourceCache>()->GetResource<Animation>(name);
+
     if (!newAnimation)
         return false;
 
+// ATOMIC END
+
     // Check if already exists
     unsigned index;
     AnimationState* state;
@@ -868,6 +914,27 @@ void AnimationController::FindAnimation(const String& name, unsigned& index, Ani
 {
     StringHash nameHash(name);
 
+// ATOMIC BEGIN
+
+    // Check if we're using attached animation resource
+    if (animation_.NotNull() && animation_->GetAnimationName() == name)
+    {
+        nameHash = animation_->GetName();
+    }
+    else
+    {
+        for (unsigned i = 0; i < animationResources_.Size(); i++)
+        {
+            if (name == animationResources_[i]->GetAnimationName())
+            {
+                nameHash = animationResources_[i]->GetName();
+                break;
+            }
+        }
+    }
+
+// ATOMIC END
+
     // Find the AnimationState
     state = GetAnimationState(nameHash);
     if (state)
@@ -895,4 +962,76 @@ void AnimationController::HandleScenePostUpdate(StringHash eventType, VariantMap
     Update(eventData[P_TIMESTEP].GetFloat());
 }
 
+// ATOMIC BEGIN
+
+void AnimationController::SetAnimationAttr(const ResourceRef& value)
+{
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    animation_ = cache->GetResource<Animation>(value.name_);
+}
+
+ResourceRef AnimationController::GetAnimationAttr() const
+{
+    return GetResourceRef(animation_, Animation::GetTypeStatic());
+}
+
+void AnimationController::AddAnimationResource(Animation* animation)
+{
+    if (!animation)
+        return;
+
+    SharedPtr<Animation> anim(animation);
+
+    if (!animationResources_.Contains(anim))
+        animationResources_.Push(anim);
+}
+
+void AnimationController::RemoveAnimationResource(Animation* animation)
+{
+    if (!animation)
+        return;
+
+    animationResources_.Remove(SharedPtr<Animation>(animation));
+
+}
+
+void AnimationController::ClearAnimationResources()
+{
+    animationResources_.Clear();
+}
+
+void AnimationController::SetAnimationResourcesAttr(const ResourceRefList& value)
+{
+    animationResources_.Clear();
+
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    for (unsigned i = 0; i < value.names_.Size(); ++i)
+    {
+        Animation* animation = cache->GetResource<Animation>(value.names_[i]);
+        if (!animation)
+        {
+            //LOGERRORF("AnimationController::SetAnimationResourcesAttr - Unable to load animation: %s", value.names_[i].CString());
+            animationResources_.Push(SharedPtr<Animation>(nullptr));
+        }
+        else
+        {
+            animationResources_.Push(SharedPtr<Animation>(animation));
+        }
+
+    }
+
+}
+
+const ResourceRefList& AnimationController::GetAnimationResourcesAttr() const
+{
+    animationResourcesAttr_.names_.Resize(animationResources_.Size());
+    for (unsigned i = 0; i < animationResources_.Size(); ++i)
+        animationResourcesAttr_.names_[i] = GetResourceName(animationResources_[i]);
+
+    return animationResourcesAttr_;
+}
+
+// ATOMIC END
+
+
 }

+ 32 - 0
Source/Atomic/Graphics/AnimationController.h

@@ -184,6 +184,25 @@ public:
     /// Return node animation states attribute.
     VariantVector GetNodeAnimationStatesAttr() const;
 
+    // ATOMIC BEGIN
+
+    void AddAnimationResource(Animation* animation);
+    void RemoveAnimationResource(Animation* animation);
+    void ClearAnimationResources();
+    const Vector<SharedPtr<Animation>>& GetAnimationResources() { return animationResources_; }
+
+    /// Set animation resources attribute.
+    void SetAnimationResourcesAttr(const ResourceRefList& value);
+    /// Return animation resources attribute.
+    const ResourceRefList& GetAnimationResourcesAttr() const;
+
+    /// Set animation attribute.
+    void SetAnimationAttr(const ResourceRef& value);
+    /// Return animation attribute.
+    ResourceRef GetAnimationAttr() const;
+
+    // ATOMIC END
+
 protected:
     /// Handle scene being assigned.
     virtual void OnSceneSet(Scene* scene);
@@ -204,6 +223,19 @@ private:
     Vector<SharedPtr<AnimationState> > nodeAnimationStates_;
     /// Attribute buffer for network replication.
     mutable VectorBuffer attrBuffer_;
+
+    // ATOMIC BEGIN
+
+    SharedPtr<Animation> animation_;
+    bool autoPlay_;
+    bool autoPlayed_;
+
+    /// animation resources
+    Vector<SharedPtr<Animation>> animationResources_;
+    mutable ResourceRefList animationResourcesAttr_;
+
+    // ATOMIC END
+
 };
 
 }

+ 8 - 0
Source/Atomic/Scene/Scene.cpp

@@ -45,6 +45,10 @@
 
 #include "../DebugNew.h"
 
+// ATOMIC BEGIN
+#include "PrefabComponent.h"
+// ATOMIC END
+
 namespace Atomic
 {
 
@@ -1537,6 +1541,10 @@ void RegisterSceneLibrary(Context* context)
     SmoothedTransform::RegisterObject(context);
     UnknownComponent::RegisterObject(context);
     SplinePath::RegisterObject(context);
+
+    // ATOMIC BEGIN
+    PrefabComponent::RegisterObject(context);
+    // ATOMIC END
 }
 
 }

+ 12 - 1
Source/ThirdParty/Assimp/code/FBXConverter.cpp

@@ -725,7 +725,18 @@ private:
                 const TransformationComp comp = static_cast<TransformationComp>(i);
 
                 if (chain[i].IsIdentity() && (anim_chain_bitmask & bit) == 0) {
-                    continue;
+
+                    // ATOMIC BEGIN
+                     // Only optimize these out if not a TRS
+                     // As these may be animated in an external FBX
+                     // and should not be dropped
+                     if (comp != TransformationComp_Translation &&
+                         comp != TransformationComp_Rotation &&
+                         comp != TransformationComp_Scaling)
+                     {
+                        continue;
+                     }
+
                 }
 
                 aiNode* nd = new aiNode();

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

@@ -134,10 +134,8 @@ bool ModelImporter::ImportAnimation(const String& filename, const String& name,
             if (animatedModel && controller)
             {
                 SharedPtr<Animation> animation = cache->GetTempResource<Animation>(fileName + extension);
-// ATOMIC_UPDATE_FIX_BEGIN
-                //if (animation)
-                //    controller->AddAnimationResource(animation);
-// ATOMIC_UPDATE_FIX_END
+                if (animation)
+                    controller->AddAnimationResource(animation);
             }
 
             ATOMIC_LOGINFOF("Import Info: %s : %s", info.name_.CString(), fileName.CString());
@@ -326,8 +324,6 @@ void ModelImporter::GetAnimations(PODVector<Animation*>& animations)
     if (!controller)
         return;
 
-// ATOMIC_UPDATE_FIX_BEGIN
-    /*
     const Vector<SharedPtr<Animation>>& animresources = controller->GetAnimationResources();
 
     for (unsigned i = 0; i < animresources.Size(); i++)
@@ -337,8 +333,7 @@ void ModelImporter::GetAnimations(PODVector<Animation*>& animations)
             animations.Push(animresources[i]);
         }
     }
-    */
-// ATOMIC_UPDATE_FIX_END
+
 }
 
 bool ModelImporter::LoadSettingsInternal(JSONValue& jsonRoot)

+ 2 - 3
Source/ToolCore/Import/JSONSceneProcess.cpp

@@ -773,9 +773,8 @@ bool JSONSceneProcess::ProcessComponent(Node* node, const JSONAnimation* janim )
             tracks.Push(track);
 
         }
-// ATOMIC_UPDATE_FIX_BEGIN
-        //outAnim->SetTracks(tracks);
-// ATOMIC_UPDATE_FIX_END
+
+        outAnim->SetTracks(tracks);
 
         String filename = resourcePath_;
 

+ 1 - 3
Source/ToolCore/Import/OpenAssetImporter.cpp

@@ -1165,9 +1165,7 @@ bool OpenAssetImporter::BuildAndSaveAnimations(OutModel* model, const String &an
             tracks.Push(track);
         }
 
-// ATOMIC_UPDATE_FIX_BEGIN
-        // outAnim->SetTracks(tracks);
-// ATOMIC_UPDATE_FIX_END
+        outAnim->SetTracks(tracks);
 
         File outFile(context_);
         if (!outFile.Open(animOutName, FILE_WRITE))