Skip to content

Commit

Permalink
(Skeleton) Оптимизация статики
Browse files Browse the repository at this point in the history
  • Loading branch information
Monika0000 committed Jul 28, 2023
1 parent 449e88c commit e3a82d6
Show file tree
Hide file tree
Showing 31 changed files with 345 additions and 136 deletions.
2 changes: 2 additions & 0 deletions Engine/Core/inc/Core/GUI/ComponentDrawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace SR_SCRIPTING_NS {

namespace SR_ANIMATIONS_NS {
class Animator;
class BoneComponent;
class Skeleton;
}

Expand Down Expand Up @@ -62,6 +63,7 @@ namespace SR_CORE_NS::GUI::ComponentDrawer {
void DrawComponent(SR_PTYPES_NS::Rigidbody3D*& pComponent, EditorGUI* context, int32_t index);
void DrawComponent(SR_ANIMATIONS_NS::Animator*& pComponent, EditorGUI* context, int32_t index);
void DrawComponent(SR_ANIMATIONS_NS::Skeleton*& pComponent, EditorGUI* context, int32_t index);
void DrawComponent(SR_ANIMATIONS_NS::BoneComponent*& pComponent, EditorGUI* context, int32_t index) { }
void DrawComponent(SR_UTILS_NS::LookAtComponent*& pComponent, EditorGUI* context, int32_t index);

void DrawComponent(SR_PTYPES_NS::Rigidbody*& pComponent, EditorGUI* context, int32_t index);
Expand Down
1 change: 1 addition & 0 deletions Engine/Core/libs/Graphics/Graphics.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "../Graphics/src/Graphics/Animations/AnimationStateTransition.cpp"
#include "../Graphics/src/Graphics/Animations/AnimationStateMachine.cpp"
#include "../Graphics/src/Graphics/Animations/AnimationState.cpp"
#include "../Graphics/src/Graphics/Animations/BoneComponent.cpp"

#include "../Graphics/src/Graphics/Pipeline/Vulkan/VulkanImGUI.cpp"
#include "../Graphics/src/Graphics/Pipeline/Vulkan/VulkanMemory.cpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ namespace SR_ANIMATIONS_NS {
class AnimationPose;

class AnimationChannel final : public SR_UTILS_NS::NonCopyable {
using Keys = std::vector<std::pair<double_t, AnimationKey*>>;
using Keys = std::vector<std::pair<float_t, AnimationKey*>>;
public:
~AnimationChannel() override;

public:
static void Load(aiNodeAnim* pChannel, double_t ticksPerSecond, std::vector<AnimationChannel*>& channels);
static void Load(aiNodeAnim* pChannel, float_t ticksPerSecond, std::vector<AnimationChannel*>& channels);

SR_NODISCARD AnimationChannel* Copy() const noexcept {
auto&& pChannel = new AnimationChannel();
Expand All @@ -36,7 +36,7 @@ namespace SR_ANIMATIONS_NS {
}

void SetName(const std::string_view& name);
void AddKey(double_t timePoint, AnimationKey* pKey);
void AddKey(float_t timePoint, AnimationKey* pKey);

uint32_t UpdateChannel(uint32_t keyIndex,
float_t time,
Expand Down
32 changes: 21 additions & 11 deletions Engine/Core/libs/Graphics/inc/Graphics/Animations/AnimationKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,33 @@ namespace SR_UTILS_NS {
namespace SR_ANIMATIONS_NS {
class AnimationChannel;

class TranslationKey;
class RotationKey;
class ScalingKey;

/// Задача ключа обеспечить необходимый переход из предыдущего ключа в этот в зависимости от интервала времени.
/// Интервал времени задается от 0.f до 1.f в зависимости от положения перехода в момент времени.
/// Переход должен работать и в обратную сторону (от 1.f до 0.f)
class AnimationKey : public SR_UTILS_NS::NonCopyable {
public:
explicit AnimationKey(AnimationChannel* pChannel)
: m_channel(pChannel)
{ }
explicit AnimationKey(AnimationChannel* pChannel);

public:
virtual void Update(double_t progress, float_t weight, AnimationKey* pPreviousKey, AnimationData* pData, AnimationData* pStaticData) = 0;
virtual void Set(float_t weight, AnimationData* pData) = 0;
virtual void SR_FASTCALL Update(double_t progress, float_t weight, AnimationKey* pPreviousKey, AnimationData* pData, AnimationData* pStaticData) noexcept = 0;
virtual void SR_FASTCALL Set(float_t weight, AnimationData* pData) noexcept = 0;
virtual AnimationKey* Copy(AnimationChannel* pChannel) const noexcept = 0;

SR_NODISCARD SR_FORCE_INLINE TranslationKey* SR_FASTCALL GetTranslation() const noexcept { return m_translation; }
SR_NODISCARD SR_FORCE_INLINE RotationKey* SR_FASTCALL GetRotation() const noexcept { return m_rotation; }
SR_NODISCARD SR_FORCE_INLINE ScalingKey* SR_FASTCALL GetScaling() const noexcept { return m_scaling; }

protected:
AnimationChannel* m_channel = nullptr;

TranslationKey* m_translation = nullptr;
RotationKey* m_rotation = nullptr;
ScalingKey* m_scaling = nullptr;

};

/// ----------------------------------------------------------------------------------------------------------------
Expand All @@ -46,8 +56,8 @@ namespace SR_ANIMATIONS_NS {
{ }

public:
void Update(double_t progress, float_t weight, AnimationKey* pPreviousKey, AnimationData* pData, AnimationData* pStaticData) override;
void Set(float_t weight, AnimationData* pData) override;
void SR_FASTCALL Update(double_t progress, float_t weight, AnimationKey* pPreviousKey, AnimationData* pData, AnimationData* pStaticData) noexcept override;
void SR_FASTCALL Set(float_t weight, AnimationData* pData) noexcept override;

SR_NODISCARD AnimationKey* Copy(AnimationChannel* pChannel) const noexcept override {
return new TranslationKey(pChannel, m_translation, m_delta);
Expand All @@ -71,8 +81,8 @@ namespace SR_ANIMATIONS_NS {
{ }

public:
void Update(double_t progress, float_t weight, AnimationKey* pPreviousKey, AnimationData* pData, AnimationData* pStaticData) override;
void Set(float_t weight, AnimationData* pData) override;
void SR_FASTCALL Update(double_t progress, float_t weight, AnimationKey* pPreviousKey, AnimationData* pData, AnimationData* pStaticData) noexcept override;
void SR_FASTCALL Set(float_t weight, AnimationData* pData) noexcept override;

SR_NODISCARD AnimationKey* Copy(AnimationChannel* pChannel) const noexcept override {
return new RotationKey(pChannel, m_rotation, m_delta);
Expand All @@ -96,8 +106,8 @@ namespace SR_ANIMATIONS_NS {
{ }

public:
void Update(double_t progress, float_t weight, AnimationKey* pPreviousKey, AnimationData* pData, AnimationData* pStaticData) override;
void Set(float_t weight, AnimationData* pData) override;
void SR_FASTCALL Update(double_t progress, float_t weight, AnimationKey* pPreviousKey, AnimationData* pData, AnimationData* pStaticData) noexcept override;
void SR_FASTCALL Set(float_t weight, AnimationData* pData) noexcept override;

SR_NODISCARD AnimationKey* Copy(AnimationChannel* pChannel) const noexcept override {
return new ScalingKey(pChannel, m_scaling, m_delta);
Expand Down
40 changes: 3 additions & 37 deletions Engine/Core/libs/Graphics/inc/Graphics/Animations/Bone.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <Graphics/Types/Mesh.h>

namespace SR_ANIMATIONS_NS {
class BoneComponent;

struct Bone : public SR_UTILS_NS::NonCopyable {
public:
~Bone() override {
Expand Down Expand Up @@ -38,43 +40,7 @@ namespace SR_ANIMATIONS_NS {
return pRootBone;
}

bool Initialize() {
if (!pRoot->gameObject && !pRoot->pScene) {
SRHalt0();
hasError = true;
return false;
}

std::vector<uint64_t> names = { hashName };

Bone* pParentBone = pParent;
/// рутовую ноду в расчет не берем
while (pParentBone && pParentBone->pParent) {
names.emplace_back(pParentBone->hashName);
pParentBone = pParentBone->pParent;
}

if (pRoot->gameObject) {
gameObject = pRoot->gameObject;
}

for (int32_t i = names.size() - 1; i >= 0; i--) {
if (gameObject) {
if (!(gameObject = gameObject->Find(names[i]))) {
break;
}
}
else {
if (!(gameObject = pRoot->pScene->Find(names[i]))) {
break;
}
}
}

hasError = !gameObject.Valid();

return !hasError;
}
bool Initialize();

private:
SR_NODISCARD Bone* Clone(Bone* pParentBone) const noexcept {
Expand Down
42 changes: 42 additions & 0 deletions Engine/Core/libs/Graphics/inc/Graphics/Animations/BoneComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Created by Monika on 28.07.2023.
//

#ifndef SRENGINE_BONECOMPONENT_H
#define SRENGINE_BONECOMPONENT_H

#include <Utils/ECS/EntityRef.h>
#include <Utils/ECS/Component.h>

namespace SR_ANIMATIONS_NS {
class Skeleton;

class BoneComponent : public SR_UTILS_NS::Component {
SR_ENTITY_SET_VERSION(1000);
SR_INITIALIZE_COMPONENT(BoneComponent);
using Super = SR_UTILS_NS::Component;
public:
using RenderScenePtr = SR_HTYPES_NS::SafePtr<RenderScene>;
public:
BoneComponent();

static Component* LoadComponent(SR_HTYPES_NS::Marshal& marshal, const SR_HTYPES_NS::DataStorage* pDataStorage);

public:
SR_NODISCARD Component* CopyComponent() const override;
SR_NODISCARD SR_HTYPES_NS::Marshal::Ptr Save(SR_HTYPES_NS::Marshal::Ptr pMarshal, SR_UTILS_NS::SavableFlags flags) const override;

void OnMatrixDirty() override;

void Initialize(Skeleton* pSkeleton, uint16_t boneIndex);

SR_NODISCARD bool ExecuteInEditMode() const override { return true; }

private:
uint16_t m_boneIndex = 0;
SR_UTILS_NS::EntityRef m_skeleton;

};
}

#endif //SRENGINE_BONECOMPONENT_H
10 changes: 8 additions & 2 deletions Engine/Core/libs/Graphics/inc/Graphics/Animations/Skeleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ namespace SR_ANIMATIONS_NS {
void OnDestroy() override;

bool ReCalculateSkeleton();
void CalculateMatrices();

Bone* AddBone(Bone* pParent, const std::string& name, bool recalculate);
SR_NODISCARD Bone* GetRootBone() const noexcept { return m_rootBone; }

const SR_MATH_NS::Matrix4x4& GetMatrixByIndex(uint16_t index) noexcept;
SR_UTILS_NS::Transform* GetTransformByIndex(uint16_t index) noexcept;
SR_NODISCARD const std::vector<Bone*>& GetBones() const noexcept { return m_bonesByIndex; };
SR_NODISCARD Bone* TryGetBone(uint64_t hashName);
SR_NODISCARD Bone* GetBone(uint64_t hashName);
SR_NODISCARD Bone* GetBoneByIndex(uint64_t index);
SR_NODISCARD uint64_t GetBoneIndex(uint64_t hashName);
SR_NODISCARD bool IsDebugEnabled() const noexcept { return m_debugEnabled; }
void SetDebugEnabled(bool enabled) { m_debugEnabled = enabled; }
Expand All @@ -53,11 +55,15 @@ namespace SR_ANIMATIONS_NS {
ska::flat_hash_map<Bone*, uint64_t> m_debugLines;

ska::flat_hash_map<uint64_t, Bone*> m_bonesByName;

std::vector<Bone*> m_bonesByIndex;

std::vector<SR_MATH_NS::Matrix4x4> m_matrices;
bool m_dirtyMatrices = false;

Bone* m_rootBone = nullptr;
};

};
}

#endif //SRENGINE_SKELETON_H
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ namespace SR_ANIMATIONS_NS {
}

uint32_t AnimationChannel::UpdateChannel(uint32_t keyIndex,
float_t time,
float_t weight,
const AnimationPose* pStaticPose,
AnimationPose* pWorkingPose
float_t time,
float_t weight,
const AnimationPose* pStaticPose,
AnimationPose* pWorkingPose
) const {
SR_TRACY_ZONE;

auto&& pWorkingData = pWorkingPose->GetData(GetGameObjectHashName());
auto&& pStaticData = pStaticPose->GetData(GetGameObjectHashName());

Expand Down Expand Up @@ -50,17 +52,19 @@ namespace SR_ANIMATIONS_NS {
else {
auto&& [prevTime, prevKey] = m_keys.at(keyIndex - 1);

const double_t currentTime = time - prevTime;
const double_t keyCurrTime = keyTime - prevTime;
const double_t progress = currentTime / keyCurrTime;
const float_t currentTime = time - prevTime;
const float_t keyCurrTime = keyTime - prevTime;
const float_t progress = currentTime / keyCurrTime;

pKey->Update(progress, weight, prevKey, pWorkingData, pStaticData);
}

return keyIndex;
}

void AnimationChannel::Load(aiNodeAnim *pChannel, double_t ticksPerSecond, std::vector<AnimationChannel*>& channels) {
void AnimationChannel::Load(aiNodeAnim* pChannel, float_t ticksPerSecond, std::vector<AnimationChannel*>& channels) {
SR_TRACY_ZONE;

if (pChannel->mNumPositionKeys > 0) {
static constexpr float_t mul = 0.01;

Expand Down Expand Up @@ -122,7 +126,7 @@ namespace SR_ANIMATIONS_NS {

auto&& scale = AiV3ToFV3(pScalingKey.mValue, 1.f);

pScalingChannel->AddKey(pScalingKey.mTime /ticksPerSecond,
pScalingChannel->AddKey(pScalingKey.mTime / ticksPerSecond,
new ScalingKey(
pScalingChannel,
scale,
Expand All @@ -142,7 +146,7 @@ namespace SR_ANIMATIONS_NS {
}
}

void AnimationChannel::AddKey(double_t timePoint, AnimationKey* pKey) {
void AnimationChannel::AddKey(float_t timePoint, AnimationKey* pKey) {
m_keys.emplace_back(std::make_pair(timePoint, pKey));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace SR_ANIMATIONS_NS {
}

void AnimationGraph::Update(const UpdateContext& context) {
SR_TRACY_ZONE;
GetFinal()->Update(context, AnimationLink(SR_ID_INVALID, SR_ID_INVALID));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace SR_ANIMATIONS_NS {
}

void AnimationGraphNodeFinal::Update(const UpdateContext& context, const AnimationLink& from) {
SR_TRACY_ZONE;

if (m_inputPins.front().has_value()) {
auto&& pNode = m_graph->GetNode(m_inputPins.front().value().m_targetNodeIndex);
if (pNode) {
Expand All @@ -46,6 +48,8 @@ namespace SR_ANIMATIONS_NS {
}

void AnimationGraphNodeStateMachine::Update(const UpdateContext& context, const AnimationLink& from) {
SR_TRACY_ZONE;

if (m_stateMachine) {
m_stateMachine->Update(context);
}
Expand Down
Loading

0 comments on commit e3a82d6

Please sign in to comment.