Skip to content

Commit

Permalink
Enable changing blackboard component values while simulating in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
jankrassnigg committed Mar 3, 2021
1 parent 1c3a6ef commit 86d3eaa
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <Core/WorldSerializer/WorldWriter.h>
#include <Foundation/Strings/HashedString.h>
#include <GameEngine/Animation/Skeletal/AnimationControllerComponent.h>
#include <GameEngine/Gameplay/BlackboardComponent.h>
#include <Physics/CharacterControllerComponent.h>
#include <RendererCore/AnimationSystem/AnimGraph/AnimGraphResource.h>
#include <RendererCore/AnimationSystem/SkeletonResource.h>
Expand Down Expand Up @@ -87,27 +88,29 @@ void ezAnimationControllerComponent::OnSimulationStarted()

pAnimController->DeserializeAnimGraphState(m_AnimationGraph);

m_AnimationGraph.SetExternalBlackboard(ezBlackboardComponent::FindBlackboard(GetOwner()));

m_AnimationGraph.m_hSkeleton = msg.m_hSkeleton;

ezHashedString hs;
hs.Assign("Idle");
m_AnimationGraph.m_Blackboard.RegisterEntry(hs, 0.0f);
m_AnimationGraph.GetBlackboard().RegisterEntry(hs, 0.0f);
hs.Assign("Left");
m_AnimationGraph.m_Blackboard.RegisterEntry(hs, 0.0f);
m_AnimationGraph.GetBlackboard().RegisterEntry(hs, 0.0f);
hs.Assign("Right");
m_AnimationGraph.m_Blackboard.RegisterEntry(hs, 0.0f);
m_AnimationGraph.GetBlackboard().RegisterEntry(hs, 0.0f);
hs.Assign("Forwards");
m_AnimationGraph.m_Blackboard.RegisterEntry(hs, 0.0f);
m_AnimationGraph.GetBlackboard().RegisterEntry(hs, 0.0f);
hs.Assign("Backwards");
m_AnimationGraph.m_Blackboard.RegisterEntry(hs, 0.0f);
m_AnimationGraph.GetBlackboard().RegisterEntry(hs, 0.0f);
hs.Assign("A");
m_AnimationGraph.m_Blackboard.RegisterEntry(hs, 0.0f);
m_AnimationGraph.GetBlackboard().RegisterEntry(hs, 0.0f);
hs.Assign("B");
m_AnimationGraph.m_Blackboard.RegisterEntry(hs, 0.0f);
m_AnimationGraph.GetBlackboard().RegisterEntry(hs, 0.0f);
hs.Assign("X");
m_AnimationGraph.m_Blackboard.RegisterEntry(hs, 0.0f);
m_AnimationGraph.GetBlackboard().RegisterEntry(hs, 0.0f);
hs.Assign("Y");
m_AnimationGraph.m_Blackboard.RegisterEntry(hs, 0.0f);
m_AnimationGraph.GetBlackboard().RegisterEntry(hs, 0.0f);
}

void ezAnimationControllerComponent::Update()
Expand All @@ -116,38 +119,38 @@ void ezAnimationControllerComponent::Update()
bool bActive = false;

ezInputManager::GetInputSlotState(ezInputSlot_Controller0_LeftStick_NegX, &fValue);
m_AnimationGraph.m_Blackboard.SetEntryValue("Left", fValue);
m_AnimationGraph.GetBlackboard().SetEntryValue("Left", fValue);
bActive |= fValue != 0;

ezInputManager::GetInputSlotState(ezInputSlot_Controller0_LeftStick_PosX, &fValue);
m_AnimationGraph.m_Blackboard.SetEntryValue("Right", fValue);
m_AnimationGraph.GetBlackboard().SetEntryValue("Right", fValue);
bActive |= fValue != 0;

ezInputManager::GetInputSlotState(ezInputSlot_Controller0_LeftStick_NegY, &fValue);
m_AnimationGraph.m_Blackboard.SetEntryValue("Backwards", fValue);
m_AnimationGraph.GetBlackboard().SetEntryValue("Backwards", fValue);
bActive |= fValue != 0;

ezInputManager::GetInputSlotState(ezInputSlot_Controller0_LeftStick_PosY, &fValue);
m_AnimationGraph.m_Blackboard.SetEntryValue("Forwards", fValue);
m_AnimationGraph.GetBlackboard().SetEntryValue("Forwards", fValue);
bActive |= fValue != 0;

ezInputManager::GetInputSlotState(ezInputSlot_Controller0_ButtonA, &fValue);
m_AnimationGraph.m_Blackboard.SetEntryValue("A", fValue);
m_AnimationGraph.GetBlackboard().SetEntryValue("A", fValue);
bActive |= fValue != 0;

ezInputManager::GetInputSlotState(ezInputSlot_Controller0_ButtonB, &fValue);
m_AnimationGraph.m_Blackboard.SetEntryValue("B", fValue);
m_AnimationGraph.GetBlackboard().SetEntryValue("B", fValue);
bActive |= fValue != 0;

ezInputManager::GetInputSlotState(ezInputSlot_Controller0_ButtonX, &fValue);
m_AnimationGraph.m_Blackboard.SetEntryValue("X", fValue);
m_AnimationGraph.GetBlackboard().SetEntryValue("X", fValue);
// bActive |= fValue != 0;

ezInputManager::GetInputSlotState(ezInputSlot_Controller0_ButtonY, &fValue);
m_AnimationGraph.m_Blackboard.SetEntryValue("Y", fValue);
m_AnimationGraph.GetBlackboard().SetEntryValue("Y", fValue);
bActive |= fValue != 0;

m_AnimationGraph.m_Blackboard.SetEntryValue("Idle", bActive ? 0.0f : 1.0f);
m_AnimationGraph.GetBlackboard().SetEntryValue("Idle", bActive ? 0.0f : 1.0f);

m_AnimationGraph.Update(GetWorld()->GetClock().GetTimeDiff());
m_AnimationGraph.SendResultTo(GetOwner());
Expand Down
3 changes: 1 addition & 2 deletions Code/Engine/GameEngine/Gameplay/BlackboardComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,11 @@ class EZ_GAMEENGINE_DLL ezBlackboardComponent : public ezComponent
void Entries_Insert(ezUInt32 uiIndex, const ezBlackboardEntry& entry);
void Entries_Remove(ezUInt32 uiIndex);

void RegisterEntries(ezArrayPtr<ezBlackboardEntry> entries);

void OnUpdateLocalBounds(ezMsgUpdateLocalBounds& msg) const;
void OnExtractRenderData(ezMsgExtractRenderData& msg) const;

ezUniquePtr<ezBlackboard> m_pBoard;

// this array is not held during runtime, it is only needed during editor time until the component is serialized out
ezDynamicArray<ezBlackboardEntry> m_InitialEntries;
};
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,14 @@ void ezBlackboardComponent::DeserializeComponent(ezWorldReader& stream)
s >> sb;
m_pBoard->SetName(sb);

// we don't write the data to m_InitialEntries, because that is never needed anymore at runtime
ezDynamicArray<ezBlackboardEntry> initialEntries;
if (s.ReadArray(initialEntries).Succeeded())
{
RegisterEntries(initialEntries);
for (auto& entry : initialEntries)
{
m_pBoard->RegisterEntry(entry.m_sName, entry.m_InitialValue, entry.m_Flags);
}
}
}

Expand Down Expand Up @@ -194,33 +198,27 @@ const ezBlackboardEntry& ezBlackboardComponent::Entries_GetValue(ezUInt32 uiInde
void ezBlackboardComponent::Entries_SetValue(ezUInt32 uiIndex, const ezBlackboardEntry& entry)
{
m_InitialEntries.EnsureCount(uiIndex + 1);

m_pBoard->UnregisterEntry(m_InitialEntries[uiIndex].m_sName);

m_InitialEntries[uiIndex] = entry;

RegisterEntries(m_InitialEntries);
m_pBoard->RegisterEntry(entry.m_sName, entry.m_InitialValue, entry.m_Flags);
}

void ezBlackboardComponent::Entries_Insert(ezUInt32 uiIndex, const ezBlackboardEntry& entry)
{
m_InitialEntries.Insert(entry, uiIndex);

RegisterEntries(m_InitialEntries);
m_pBoard->RegisterEntry(entry.m_sName, entry.m_InitialValue, entry.m_Flags);
}

void ezBlackboardComponent::Entries_Remove(ezUInt32 uiIndex)
{
m_InitialEntries.RemoveAtAndCopy(uiIndex);

RegisterEntries(m_InitialEntries);
}

void ezBlackboardComponent::RegisterEntries(ezArrayPtr<ezBlackboardEntry> entries)
{
m_pBoard->UnregisterAllEntries();
auto& entry = m_InitialEntries[uiIndex];
m_pBoard->UnregisterEntry(entry.m_sName);

for (auto& entry : entries)
{
m_pBoard->RegisterEntry(entry.m_sName, entry.m_InitialValue, entry.m_Flags);
}
m_InitialEntries.RemoveAtAndCopy(uiIndex);
}

void ezBlackboardComponent::OnUpdateLocalBounds(ezMsgUpdateLocalBounds& msg) const
Expand Down
35 changes: 21 additions & 14 deletions Code/Engine/RendererCore/AnimationSystem/AnimGraph/AnimGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@
#include <Foundation/Types/UniquePtr.h>
#include <RendererCore/AnimationSystem/AnimGraph/AnimGraphNode.h>
#include <ozz/animation/runtime/blending_job.h>
#include <ozz/animation/runtime/sampling_job.h>
#include <ozz/base/containers/vector.h>
#include <ozz/base/maths/soa_transform.h>
#include <ozz/animation/runtime/sampling_job.h>

class ezGameObject;
class ezStreamWriter;
class ezStreamReader;

using ezSkeletonResourceHandle = ezTypedResourceHandle<class ezSkeletonResource>;

using ezSkeletonResourceHandle = ezTypedResourceHandle<class ezSkeletonResource>;

struct ezAnimGraphBlendWeights
{
ozz::vector<ozz::math::SimdFloat4> m_ozzBlendWeights;
};


struct ezAnimGraphLocalTransforms
{
ozz::vector<ozz::math::SoaTransform> m_ozzLocalTransforms;
};

};

struct ezAnimGraphSamplingCache
{
ozz::animation::SamplingCache m_ozzSamplingCache;
Expand All @@ -50,7 +50,11 @@ class EZ_RENDERERCORE_DLL ezAnimGraph

ezSkeletonResourceHandle m_hSkeleton;

ezBlackboard m_Blackboard;
void SetExternalBlackboard(ezBlackboard* pBlackboard);
ezBlackboard& GetBlackboard()
{
return *m_pBlackboard;
}

ezResult Serialize(ezStreamWriter& stream) const;
ezResult Deserialize(ezStreamReader& stream);
Expand All @@ -62,28 +66,31 @@ class EZ_RENDERERCORE_DLL ezAnimGraph
void AddFrameBlendLayer(const ozz::animation::BlendingJob::Layer& layer);

/// \brief To be called by ezAnimGraphNode classes every frame that they want to affect the root motion
void AddFrameRootMotion(const ezVec3& motion);

void AddFrameRootMotion(const ezVec3& motion);

ezAnimGraphBlendWeights* AllocateBlendWeights(const ezSkeletonResource& skeleton);
void FreeBlendWeights(ezAnimGraphBlendWeights*& pWeights);
void FreeBlendWeights(ezAnimGraphBlendWeights*& pWeights);

ezAnimGraphLocalTransforms* AllocateLocalTransforms(const ezSkeletonResource& skeleton);
void FreeLocalTransforms(ezAnimGraphLocalTransforms*& pTransforms);


ezAnimGraphSamplingCache* AllocateSamplingCache(const ozz::animation::Animation& animclip);
void FreeSamplingCache(ezAnimGraphSamplingCache*& pTransforms);

private:
ezBlackboard* m_pBlackboard = nullptr;
ezBlackboard m_Blackboard;

ezDynamicArray<ozz::animation::BlendingJob::Layer> m_ozzBlendLayers;
ozz::vector<ozz::math::SoaTransform> m_ozzLocalTransforms;
ezDynamicArray<ezMat4, ezAlignedAllocatorWrapper> m_ModelSpaceTransforms;
ezDynamicArray<ezMat4, ezAlignedAllocatorWrapper> m_ModelSpaceTransforms;

ezDeque<ezAnimGraphBlendWeights> m_BlendWeights;
ezHybridArray<ezAnimGraphBlendWeights*, 8> m_BlendWeightsFreeList;
ezHybridArray<ezAnimGraphBlendWeights*, 8> m_BlendWeightsFreeList;

ezDeque<ezAnimGraphLocalTransforms> m_LocalTransforms;
ezHybridArray<ezAnimGraphLocalTransforms*, 8> m_LocalTransformsFreeList;


ezDeque<ezAnimGraphSamplingCache> m_SamplingCaches;
ezHybridArray<ezAnimGraphSamplingCache*, 8> m_SamplingCachesFreeList;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,20 @@ void ezSetBlackboardValueAnimNode::Step(ezAnimGraph* pOwner, ezTime tDiff, const
m_bLastActiveState = bIsActiveNow;

// TODO: register only once
pOwner->m_Blackboard.RegisterEntry(m_sBlackboardEntry, 0.0f);
pOwner->GetBlackboard().RegisterEntry(m_sBlackboardEntry, 0.0f);

if (bIsActiveNow)
{
if (m_bSetOnActivation)
{
pOwner->m_Blackboard.SetEntryValue(m_sBlackboardEntry, m_fOnActivatedValue);
pOwner->GetBlackboard().SetEntryValue(m_sBlackboardEntry, m_fOnActivatedValue);
}
}
else
{
if (m_bSetOnDeactivation)
{
pOwner->m_Blackboard.SetEntryValue(m_sBlackboardEntry, m_fOnDeactivatedValue);
pOwner->GetBlackboard().SetEntryValue(m_sBlackboardEntry, m_fOnDeactivatedValue);
}
}
}
Expand Down Expand Up @@ -183,10 +183,10 @@ static bool Compare(ezComparisonOperator::Enum cmp, float f1, float f2)

void ezCheckBlackboardValueAnimNode::Step(ezAnimGraph* pOwner, ezTime tDiff, const ezSkeletonResource* pSkeleton)
{
ezVariant value = pOwner->m_Blackboard.GetEntryValue(m_sBlackboardEntry);
ezVariant value = pOwner->GetBlackboard().GetEntryValue(m_sBlackboardEntry);
float fValue = 0.0f;

if (value.IsValid() && value.IsFloatingPoint())
if (value.IsValid() && value.IsNumber())
{
fValue = value.ConvertTo<float>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
#include <ozz/animation/runtime/local_to_model_job.h>
#include <ozz/animation/runtime/skeleton.h>

ezAnimGraph::ezAnimGraph() = default;
ezAnimGraph::ezAnimGraph()
{
m_pBlackboard = &m_Blackboard;
}

ezAnimGraph::~ezAnimGraph() = default;

void ezAnimGraph::Update(ezTime tDiff)
Expand Down Expand Up @@ -86,6 +90,18 @@ void ezAnimGraph::SendResultTo(ezGameObject* pObject)
pObject->SendMessageRecursive(msg);
}

void ezAnimGraph::SetExternalBlackboard(ezBlackboard* pBlackboard)
{
if (pBlackboard)
{
m_pBlackboard = pBlackboard;
}
else
{
m_pBlackboard = &m_Blackboard;
}
}

ezResult ezAnimGraph::Serialize(ezStreamWriter& stream) const
{
stream.WriteVersion(2);
Expand Down

0 comments on commit 86d3eaa

Please sign in to comment.