Restored AnimGraphResourceTests.

RefactorUnifiedBlendTreeStateMachineHandling
Martin Felis 2024-03-20 22:33:31 +01:00
parent ccb9bc4e9b
commit e687c9b613
5 changed files with 213 additions and 181 deletions

View File

@ -15,7 +15,7 @@ struct AnimGraph {
~AnimGraph() {} ~AnimGraph() {}
bool Init(AnimGraphContext& context) { bool Init(AnimGraphContext& context) {
m_root_node->Init(context); return m_root_node->Init(context);
} }
void UpdateTime(float dt) { void UpdateTime(float dt) {
m_time_last = m_time_now; m_time_last = m_time_now;

View File

@ -156,7 +156,7 @@ void AnimGraphBlendTree::Evaluate(AnimGraphContext& context) {
} }
} }
Socket* AnimGraphBlendTree::getInputSocket(const std::string& name) { Socket* AnimGraphBlendTree::GetInputSocket(const std::string& name) {
for (size_t i = 0, n = m_node_output_connections[1].size(); i < n; i++) { for (size_t i = 0, n = m_node_output_connections[1].size(); i < n; i++) {
AnimGraphConnection& connection = m_node_output_connections[1][i]; AnimGraphConnection& connection = m_node_output_connections[1][i];
if (connection.m_source_socket.m_name == name) { if (connection.m_source_socket.m_name == name) {
@ -167,7 +167,7 @@ Socket* AnimGraphBlendTree::getInputSocket(const std::string& name) {
return nullptr; return nullptr;
} }
Socket* AnimGraphBlendTree::getOutputSocket(const std::string& name) { Socket* AnimGraphBlendTree::GetOutputSocket(const std::string& name) {
for (size_t i = 0, n = m_node_input_connections[0].size(); i < n; i++) { for (size_t i = 0, n = m_node_input_connections[0].size(); i < n; i++) {
AnimGraphConnection& connection = m_node_input_connections[0][i]; AnimGraphConnection& connection = m_node_input_connections[0][i];
if (connection.m_target_socket.m_name == name) { if (connection.m_target_socket.m_name == name) {
@ -178,7 +178,7 @@ Socket* AnimGraphBlendTree::getOutputSocket(const std::string& name) {
return nullptr; return nullptr;
} }
const Socket* AnimGraphBlendTree::getInputSocket(const std::string& name) const { const Socket* AnimGraphBlendTree::GetInputSocket(const std::string& name) const {
for (size_t i = 0, n = m_node_output_connections[1].size(); i < n; i++) { for (size_t i = 0, n = m_node_output_connections[1].size(); i < n; i++) {
const AnimGraphConnection& connection = m_node_output_connections[1][i]; const AnimGraphConnection& connection = m_node_output_connections[1][i];
if (connection.m_source_socket.m_name == name) { if (connection.m_source_socket.m_name == name) {
@ -189,7 +189,7 @@ const Socket* AnimGraphBlendTree::getInputSocket(const std::string& name) const
return nullptr; return nullptr;
} }
const Socket* AnimGraphBlendTree::getOutputSocket(const std::string& name) const { const Socket* AnimGraphBlendTree::GetOutputSocket(const std::string& name) const {
for (size_t i = 0, n = m_node_input_connections[0].size(); i < n; i++) { for (size_t i = 0, n = m_node_input_connections[0].size(); i < n; i++) {
const AnimGraphConnection& connection = m_node_input_connections[0][i]; const AnimGraphConnection& connection = m_node_input_connections[0][i];
if (connection.m_target_socket.m_name == name) { if (connection.m_target_socket.m_name == name) {

View File

@ -74,11 +74,11 @@ struct AnimGraphBlendTree : public AnimNode {
} }
} }
Socket* getInputSocket(const std::string& name); Socket* GetInputSocket(const std::string& name);
Socket* getOutputSocket(const std::string& name); Socket* GetOutputSocket(const std::string& name);
const Socket* getInputSocket(const std::string& name) const; const Socket* GetInputSocket(const std::string& name) const;
const Socket* getOutputSocket(const std::string& name) const; const Socket* GetOutputSocket(const std::string& name) const;
/** Sets the address that is used for the specified AnimGraph input Socket. /** Sets the address that is used for the specified AnimGraph input Socket.
* *
@ -166,8 +166,8 @@ struct AnimGraphBlendTree : public AnimNode {
return nullptr; return nullptr;
} }
void* getInputPtr(const std::string& name) const { void* GetInputPtr(const std::string& name) const {
const Socket* input_socket = getInputSocket(name); const Socket* input_socket = GetInputSocket(name);
if (input_socket != nullptr) { if (input_socket != nullptr) {
return input_socket->m_reference.ptr; return input_socket->m_reference.ptr;
} }
@ -175,8 +175,8 @@ struct AnimGraphBlendTree : public AnimNode {
return nullptr; return nullptr;
} }
void* getOutputPtr(const std::string& name) const { void* GetOutputPtr(const std::string& name) const {
const Socket* input_socket = getOutputSocket(name); const Socket* input_socket = GetOutputSocket(name);
if (input_socket != nullptr) { if (input_socket != nullptr) {
return input_socket->m_reference.ptr; return input_socket->m_reference.ptr;
} }

View File

@ -284,7 +284,7 @@ bool AnimGraphResource::LoadBlendTreeResourceFromJson(nlohmann::json const& json
m_blend_tree_resource.m_nodes.push_back(node); m_blend_tree_resource.m_nodes.push_back(node);
} }
// Setup graph inputs and outputs // Graph outputs
const json& graph_outputs = json_data["nodes"][0]["inputs"]; const json& graph_outputs = json_data["nodes"][0]["inputs"];
for (const auto& graph_output : graph_outputs) { for (const auto& graph_output : graph_outputs) {
AnimNodeResource& graph_node = m_blend_tree_resource.m_nodes[0]; AnimNodeResource& graph_node = m_blend_tree_resource.m_nodes[0];
@ -292,12 +292,15 @@ bool AnimGraphResource::LoadBlendTreeResourceFromJson(nlohmann::json const& json
sJsonToSocket(graph_output)); sJsonToSocket(graph_output));
} }
const json& graph_inputs = json_data["nodes"][1]["outputs"]; // Graph inputs (optional)
for (const auto& graph_input : graph_inputs) { if (json_data["nodes"][1].contains("outputs")) {
AnimNodeResource& graph_node = m_blend_tree_resource.m_nodes[1]; const json& graph_inputs = json_data["nodes"][1]["outputs"];
graph_node.m_socket_accessor->m_outputs.push_back( for (const auto& graph_input : graph_inputs) {
sJsonToSocket(graph_input)); AnimNodeResource& graph_node = m_blend_tree_resource.m_nodes[1];
} graph_node.m_socket_accessor->m_outputs.push_back(
sJsonToSocket(graph_input));
}
}
// Load connections // Load connections
for (const auto& json_connection : json_data["connections"]) { for (const auto& json_connection : json_data["connections"]) {

View File

@ -33,6 +33,25 @@ bool load_skeleton(ozz::animation::Skeleton& skeleton, const char* filename) {
return true; return true;
} }
TEST_CASE("InputAttributeConversion", "[AnimGraphResource]") {
int node_id = 3321;
int input_index = 221;
int output_index = 125;
int parsed_node_id;
int parsed_input_index;
int parsed_output_index;
int attribute_id = GenerateInputAttributeId(node_id, input_index);
SplitInputAttributeId(attribute_id, &parsed_node_id, &parsed_input_index);
CHECK(node_id == parsed_node_id);
CHECK(input_index == parsed_input_index);
attribute_id = GenerateOutputAttributeId(node_id, output_index);
SplitOutputAttributeId(attribute_id, &parsed_node_id, &parsed_output_index);
CHECK(node_id == parsed_node_id);
CHECK(output_index == parsed_output_index);
}
TEST_CASE("AnimSamplerGraph", "[AnimGraphResource]") { TEST_CASE("AnimSamplerGraph", "[AnimGraphResource]") {
AnimGraphResource graph_resource; AnimGraphResource graph_resource;
graph_resource.m_name = "AnimSamplerBlendTree"; graph_resource.m_name = "AnimSamplerBlendTree";
@ -114,56 +133,59 @@ TEST_CASE("AnimSamplerGraph", "[AnimGraphResource]") {
graph_context.freeAnimations(); graph_context.freeAnimations();
} }
/*
// //
// Checks that node const inputs are properly set. // Checks that node const inputs are properly set.
// //
TEST_CASE("AnimSamplerSpeedScaleGraph", "[AnimGraphResource]") { TEST_CASE("AnimSamplerSpeedScaleGraph", "[AnimGraphResource]") {
AnimGraphBlendTreeResource graph_resource; AnimGraphResource graph_resource;
graph_resource.clear();
graph_resource.m_name = "AnimSamplerSpeedScaleGraph"; graph_resource.m_name = "AnimSamplerSpeedScaleGraph";
graph_resource.m_type = "BlendTree";
BlendTreeResource& blend_tree_resource = graph_resource.m_blend_tree_resource;
blend_tree_resource.Reset();
blend_tree_resource.InitGraphConnectors();
// Prepare graph inputs and outputs // Prepare graph inputs and outputs
size_t walk_node_index = blend_tree_resource.m_nodes.push_back(AnimNodeResourceFactory("AnimSampler"));
graph_resource.addNode(AnimNodeResourceFactory("AnimSampler")); size_t walk_node_index = blend_tree_resource.m_nodes.size() - 1;
size_t speed_scale_node_index = blend_tree_resource.m_nodes.push_back(AnimNodeResourceFactory("SpeedScale"));
graph_resource.addNode(AnimNodeResourceFactory("SpeedScale")); size_t speed_scale_node_index = blend_tree_resource.m_nodes.size() - 1;
AnimNodeResource& walk_node = graph_resource.m_nodes[walk_node_index]; AnimNodeResource& walk_node = blend_tree_resource.m_nodes[walk_node_index];
walk_node.m_name = "WalkAnim"; walk_node.m_name = "WalkAnim";
walk_node.m_socket_accessor->SetPropertyValue( walk_node.m_socket_accessor->SetPropertyValue(
"Filename", "Filename",
std::string("media/Walking-loop.ozz")); std::string("media/Walking-loop.ozz"));
AnimNodeResource& speed_scale_node = AnimNodeResource& speed_scale_node =
graph_resource.m_nodes[speed_scale_node_index]; blend_tree_resource.m_nodes[speed_scale_node_index];
speed_scale_node.m_name = "SpeedScale"; speed_scale_node.m_name = "SpeedScale";
float speed_scale_value = 1.35f; float speed_scale_value = 1.35f;
speed_scale_node.m_socket_accessor->SetInputValue( speed_scale_node.m_socket_accessor->SetInputValue(
"SpeedScale", "SpeedScale",
speed_scale_value); speed_scale_value);
AnimNodeResource& graph_node = graph_resource.m_nodes[0]; AnimNodeResource& graph_node = blend_tree_resource.m_nodes[0];
graph_node.m_socket_accessor->RegisterInput<AnimData>("GraphOutput", nullptr); graph_node.m_socket_accessor->RegisterInput<AnimData>("GraphOutput", nullptr);
graph_resource.connectSockets(walk_node, "Output", speed_scale_node, "Input"); blend_tree_resource.ConnectSockets(walk_node, "Output", speed_scale_node, "Input");
graph_resource.connectSockets( blend_tree_resource.ConnectSockets(
speed_scale_node, speed_scale_node,
"Output", "Output",
graph_resource.getGraphOutputNode(), blend_tree_resource.GetGraphOutputNode(),
"GraphOutput"); "GraphOutput");
graph_resource.saveToFile("AnimSamplerSpeedScaleGraph.animgraph.json"); graph_resource.SaveToFile("AnimSamplerSpeedScaleGraph.animgraph.json");
AnimGraphBlendTreeResource graph_resource_loaded; AnimGraphResource graph_resource_loaded;
graph_resource_loaded.loadFromFile( graph_resource_loaded.LoadFromFile(
"AnimSamplerSpeedScaleGraph.animgraph.json"); "AnimSamplerSpeedScaleGraph.animgraph.json");
BlendTreeResource& blend_tree_resource_loaded = graph_resource_loaded.m_blend_tree_resource;
Socket* speed_scale_resource_loaded_input = Socket* speed_scale_resource_loaded_input =
graph_resource_loaded.m_nodes[speed_scale_node_index] blend_tree_resource_loaded.m_nodes[speed_scale_node_index]
.m_socket_accessor->GetInputSocket("SpeedScale"); .m_socket_accessor->GetInputSocket("SpeedScale");
REQUIRE(speed_scale_resource_loaded_input != nullptr); REQUIRE(speed_scale_resource_loaded_input != nullptr);
@ -171,105 +193,112 @@ TEST_CASE("AnimSamplerSpeedScaleGraph", "[AnimGraphResource]") {
speed_scale_resource_loaded_input->m_value.float_value, speed_scale_resource_loaded_input->m_value.float_value,
Catch::Matchers::WithinAbs(speed_scale_value, 0.1)); Catch::Matchers::WithinAbs(speed_scale_value, 0.1));
AnimGraph graph; AnimGraphBlendTree blend_tree;
graph_resource_loaded.createInstance(graph); graph_resource_loaded.CreateBlendTreeInstance(blend_tree);
REQUIRE_THAT(*dynamic_cast<SpeedScaleNode*>(graph.m_nodes[speed_scale_node_index])->i_speed_scale, REQUIRE_THAT(*dynamic_cast<SpeedScaleNode*>(blend_tree.m_nodes[speed_scale_node_index])->i_speed_scale,
Catch::Matchers::WithinAbs(speed_scale_value, 0.1)); Catch::Matchers::WithinAbs(speed_scale_value, 0.1));
} }
TEST_CASE("Blend2Graph", "[AnimGraphResource]") { TEST_CASE("Blend2Graph", "[AnimGraphResource]") {
AnimGraphBlendTreeResource graph_resource; AnimGraphResource graph_resource;
graph_resource.clear();
graph_resource.m_name = "WalkRunBlendGraph"; graph_resource.m_name = "WalkRunBlendGraph";
graph_resource.m_type = "BlendTree";
BlendTreeResource& blend_tree_resource = graph_resource.m_blend_tree_resource;
blend_tree_resource.Reset();
blend_tree_resource.InitGraphConnectors();
// Prepare graph inputs and outputs // Prepare graph inputs and outputs
size_t walk_node_index = blend_tree_resource.m_nodes.push_back(AnimNodeResourceFactory("AnimSampler"));
graph_resource.addNode(AnimNodeResourceFactory("AnimSampler")); size_t walk_node_index = blend_tree_resource.m_nodes.size() - 1;
size_t run_node_index =
graph_resource.addNode(AnimNodeResourceFactory("AnimSampler"));
size_t blend_node_index =
graph_resource.addNode(AnimNodeResourceFactory("Blend2"));
AnimNodeResource& walk_node = graph_resource.m_nodes[walk_node_index]; blend_tree_resource.m_nodes.push_back(AnimNodeResourceFactory("AnimSampler"));
size_t run_node_index = blend_tree_resource.m_nodes.size() - 1;
blend_tree_resource.m_nodes.push_back(AnimNodeResourceFactory("Blend2"));
size_t blend_node_index = blend_tree_resource.m_nodes.size() - 1;
AnimNodeResource& walk_node = blend_tree_resource.m_nodes[walk_node_index];
walk_node.m_name = "WalkAnim"; walk_node.m_name = "WalkAnim";
walk_node.m_socket_accessor->SetPropertyValue( walk_node.m_socket_accessor->SetPropertyValue(
"Filename", "Filename",
std::string("media/Walking-loop.ozz")); std::string("media/Walking-loop.ozz"));
AnimNodeResource& run_node = graph_resource.m_nodes[run_node_index]; AnimNodeResource& run_node = blend_tree_resource.m_nodes[run_node_index];
run_node.m_socket_accessor->SetPropertyValue( run_node.m_socket_accessor->SetPropertyValue(
"Filename", "Filename",
std::string("media/Running0-loop.ozz")); std::string("media/Running0-loop.ozz"));
run_node.m_name = "RunAnim"; run_node.m_name = "RunAnim";
AnimNodeResource& blend_node = graph_resource.m_nodes[blend_node_index]; AnimNodeResource& blend_node = blend_tree_resource.m_nodes[blend_node_index];
blend_node.m_name = "BlendWalkRun"; blend_node.m_name = "BlendWalkRun";
AnimNodeResource& graph_node = graph_resource.m_nodes[0]; AnimNodeResource& graph_node = blend_tree_resource.m_nodes[0];
graph_node.m_socket_accessor->RegisterInput<AnimData>("GraphOutput", nullptr); graph_node.m_socket_accessor->RegisterInput<AnimData>("GraphOutput", nullptr);
REQUIRE(graph_node.m_socket_accessor->m_inputs.size() == 1); REQUIRE(graph_node.m_socket_accessor->m_inputs.size() == 1);
REQUIRE(blend_node.m_socket_accessor->GetInputIndex("Input0") == 0); REQUIRE(blend_node.m_socket_accessor->GetInputIndex("Input0") == 0);
REQUIRE(blend_node.m_socket_accessor->GetInputIndex("Input1") == 1); REQUIRE(blend_node.m_socket_accessor->GetInputIndex("Input1") == 1);
graph_resource.connectSockets(walk_node, "Output", blend_node, "Input0"); blend_tree_resource.ConnectSockets(walk_node, "Output", blend_node, "Input0");
graph_resource.connectSockets(run_node, "Output", blend_node, "Input1"); blend_tree_resource.ConnectSockets(run_node, "Output", blend_node, "Input1");
graph_resource.connectSockets( blend_tree_resource.ConnectSockets(
blend_node, blend_node,
"Output", "Output",
graph_resource.getGraphOutputNode(), blend_tree_resource.GetGraphOutputNode(),
"GraphOutput"); "GraphOutput");
graph_resource.saveToFile("Blend2Graph.animgraph.json"); graph_resource.SaveToFile("Blend2Graph.animgraph.json");
AnimGraphBlendTreeResource graph_resource_loaded;
graph_resource_loaded.loadFromFile("Blend2Graph.animgraph.json");
AnimGraph graph; AnimGraphResource graph_resource_loaded;
graph_resource_loaded.createInstance(graph); graph_resource_loaded.LoadFromFile("Blend2Graph.animgraph.json");
AnimGraphBlendTree blend_tree_graph;
graph_resource_loaded.CreateBlendTreeInstance(blend_tree_graph);
AnimGraphContext graph_context; AnimGraphContext graph_context;
ozz::animation::Skeleton skeleton; ozz::animation::Skeleton skeleton;
REQUIRE(load_skeleton(skeleton, "media/skeleton.ozz")); REQUIRE(load_skeleton(skeleton, "media/skeleton.ozz"));
graph_context.m_skeleton = &skeleton; graph_context.m_skeleton = &skeleton;
REQUIRE(graph.init(graph_context)); REQUIRE(blend_tree_graph.Init(graph_context));
REQUIRE(graph.m_nodes.size() == 5); REQUIRE(blend_tree_graph.m_nodes.size() == 5);
REQUIRE(graph.m_nodes[0]->m_node_type_name == "BlendTree"); REQUIRE(blend_tree_graph.m_nodes[0]->m_node_type_name == "BlendTree");
REQUIRE(graph.m_nodes[1]->m_node_type_name == "BlendTree"); REQUIRE(blend_tree_graph.m_nodes[1]->m_node_type_name == "BlendTree");
REQUIRE(graph.m_nodes[2]->m_node_type_name == "AnimSampler"); REQUIRE(blend_tree_graph.m_nodes[2]->m_node_type_name == "AnimSampler");
REQUIRE(graph.m_nodes[3]->m_node_type_name == "AnimSampler"); REQUIRE(blend_tree_graph.m_nodes[3]->m_node_type_name == "AnimSampler");
REQUIRE(graph.m_nodes[4]->m_node_type_name == "Blend2"); REQUIRE(blend_tree_graph.m_nodes[4]->m_node_type_name == "Blend2");
// connections within the graph // connections within the graph
AnimSamplerNode* anim_sampler_walk = AnimSamplerNode* anim_sampler_walk =
dynamic_cast<AnimSamplerNode*>(graph.m_nodes[2]); dynamic_cast<AnimSamplerNode*>(blend_tree_graph.m_nodes[2]);
AnimSamplerNode* anim_sampler_run = AnimSamplerNode* anim_sampler_run =
dynamic_cast<AnimSamplerNode*>(graph.m_nodes[3]); dynamic_cast<AnimSamplerNode*>(blend_tree_graph.m_nodes[3]);
Blend2Node* blend2_instance = dynamic_cast<Blend2Node*>(graph.m_nodes[4]); Blend2Node* blend2_instance = dynamic_cast<Blend2Node*>(blend_tree_graph.m_nodes[4]);
// check node input dependencies // check node input dependencies
size_t anim_sampler_index0 = anim_sampler_walk->m_index; size_t anim_sampler_index0 = blend_tree_graph.GetAnimNodeIndex(anim_sampler_walk);
size_t anim_sampler_index1 = anim_sampler_run->m_index; size_t anim_sampler_index1 = blend_tree_graph.GetAnimNodeIndex(anim_sampler_run);
size_t blend_index = blend2_instance->m_index; size_t blend_index = blend_tree_graph.GetAnimNodeIndex(blend2_instance);
REQUIRE(graph.m_node_input_connections[blend_index].size() == 2); REQUIRE(blend_tree_graph.m_node_input_connections[blend_index].size() == 2);
CHECK( CHECK(
graph.m_node_input_connections[blend_index][0].m_source_node blend_tree_graph.m_node_input_connections[blend_index][0].m_source_node
== anim_sampler_walk); == anim_sampler_walk);
CHECK( CHECK(
graph.m_node_input_connections[blend_index][1].m_source_node blend_tree_graph.m_node_input_connections[blend_index][1].m_source_node
== anim_sampler_run); == anim_sampler_run);
REQUIRE(graph.m_node_output_connections[anim_sampler_index0].size() == 1); REQUIRE(
blend_tree_graph.m_node_output_connections[anim_sampler_index0].size() == 1);
CHECK( CHECK(
graph.m_node_output_connections[anim_sampler_index0][0].m_target_node blend_tree_graph.m_node_output_connections[anim_sampler_index0][0].m_target_node
== blend2_instance); == blend2_instance);
REQUIRE(graph.m_node_output_connections[anim_sampler_index1].size() == 1); REQUIRE(
blend_tree_graph.m_node_output_connections[anim_sampler_index1].size() == 1);
CHECK( CHECK(
graph.m_node_output_connections[anim_sampler_index1][0].m_target_node blend_tree_graph.m_node_output_connections[anim_sampler_index1][0].m_target_node
== blend2_instance); == blend2_instance);
// Ensure animation sampler nodes use the correct files // Ensure animation sampler nodes use the correct files
@ -280,11 +309,12 @@ TEST_CASE("Blend2Graph", "[AnimGraphResource]") {
REQUIRE(anim_sampler_run->m_animation != nullptr); REQUIRE(anim_sampler_run->m_animation != nullptr);
WHEN("Emulating Graph Evaluation") { WHEN("Emulating Graph Evaluation") {
CHECK(graph.m_anim_data_allocator.size() == 0); CHECK(blend_tree_graph.m_anim_data_allocator.size() == 0);
CHECK(blend2_instance->i_input0 == anim_sampler_walk->o_output); CHECK(blend2_instance->i_input0 == anim_sampler_walk->o_output);
CHECK(blend2_instance->i_input1 == anim_sampler_run->o_output); CHECK(blend2_instance->i_input1 == anim_sampler_run->o_output);
const Socket* graph_output_socket = graph.getOutputSocket("GraphOutput"); const Socket* graph_output_socket =
blend_tree_graph.GetOutputSocket("GraphOutput");
AnimData* graph_output = AnimData* graph_output =
static_cast<AnimData*>(*graph_output_socket->m_reference.ptr_ptr); static_cast<AnimData*>(*graph_output_socket->m_reference.ptr_ptr);
@ -299,36 +329,38 @@ TEST_CASE("Blend2Graph", "[AnimGraphResource]") {
graph_context.freeAnimations(); graph_context.freeAnimations();
} }
TEST_CASE("InputAttributeConversion", "[AnimGraphResource]") { //
int node_id = 3321; // Graph:
int input_index = 221; // Outputs:
int output_index = 125; // - GraphFloatOutput (float)
int parsed_node_id; // - GraphVec3Output (Vec3)
int parsed_input_index; //
int parsed_output_index; // Inputs:
// - GraphFloatInput (float)
int attribute_id = GenerateInputAttributeId(node_id, input_index); //
SplitInputAttributeId(attribute_id, &parsed_node_id, &parsed_input_index); // Nodes:
CHECK(node_id == parsed_node_id); // - MathFloatToVec3Node
CHECK(input_index == parsed_input_index); //
// Connectivity:
attribute_id = GenerateOutputAttributeId(node_id, output_index); // GraphFloatInput -+----------------------> GraphFloatOutput
SplitOutputAttributeId(attribute_id, &parsed_node_id, &parsed_output_index); // \-> MathFloatToVec3 --> GraphVec3Output
CHECK(node_id == parsed_node_id); //
CHECK(output_index == parsed_output_index); //
}
TEST_CASE("ResourceSaveLoadMathGraphInputs", "[AnimGraphResource]") { TEST_CASE("ResourceSaveLoadMathGraphInputs", "[AnimGraphResource]") {
AnimGraphBlendTreeResource graph_resource_origin; AnimGraphResource graph_resource_origin;
graph_resource_origin.clear();
graph_resource_origin.m_name = "TestInputOutputGraph"; graph_resource_origin.m_name = "TestInputOutputGraph";
graph_resource_origin.m_type = "BlendTree";
size_t float_to_vec3_node_index = graph_resource_origin.addNode( BlendTreeResource& blend_tree_resource = graph_resource_origin.m_blend_tree_resource;
AnimNodeResourceFactory("MathFloatToVec3Node")); blend_tree_resource.Reset();
blend_tree_resource.InitGraphConnectors();
// Prepare graph inputs and outputs
blend_tree_resource.m_nodes.push_back(AnimNodeResourceFactory("MathFloatToVec3Node"));
size_t float_to_vec3_node_index = blend_tree_resource.m_nodes.size() - 1;
AnimNodeResource& graph_output_node = AnimNodeResource& graph_output_node =
graph_resource_origin.getGraphOutputNode(); blend_tree_resource.GetGraphOutputNode();
graph_output_node.m_socket_accessor->RegisterInput<float>( graph_output_node.m_socket_accessor->RegisterInput<float>(
"GraphFloatOutput", "GraphFloatOutput",
nullptr); nullptr);
@ -337,38 +369,38 @@ TEST_CASE("ResourceSaveLoadMathGraphInputs", "[AnimGraphResource]") {
nullptr); nullptr);
AnimNodeResource& graph_input_node = AnimNodeResource& graph_input_node =
graph_resource_origin.getGraphInputNode(); blend_tree_resource.GetGraphInputNode();
graph_input_node.m_socket_accessor->RegisterOutput<float>( graph_input_node.m_socket_accessor->RegisterOutput<float>(
"GraphFloatInput", "GraphFloatInput",
nullptr); nullptr);
// Prepare graph inputs and outputs // Prepare graph inputs and outputs
AnimNodeResource& float_to_vec3_node = AnimNodeResource& float_to_vec3_node =
graph_resource_origin.m_nodes[float_to_vec3_node_index]; blend_tree_resource.m_nodes[float_to_vec3_node_index];
REQUIRE(graph_resource_origin.connectSockets( REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node, graph_input_node,
"GraphFloatInput", "GraphFloatInput",
graph_output_node, graph_output_node,
"GraphFloatOutput")); "GraphFloatOutput"));
REQUIRE(graph_resource_origin.connectSockets( REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node, graph_input_node,
"GraphFloatInput", "GraphFloatInput",
float_to_vec3_node, float_to_vec3_node,
"Input0")); "Input0"));
REQUIRE(graph_resource_origin.connectSockets( REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node, graph_input_node,
"GraphFloatInput", "GraphFloatInput",
float_to_vec3_node, float_to_vec3_node,
"Input1")); "Input1"));
REQUIRE(graph_resource_origin.connectSockets( REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node, graph_input_node,
"GraphFloatInput", "GraphFloatInput",
float_to_vec3_node, float_to_vec3_node,
"Input2")); "Input2"));
REQUIRE(graph_resource_origin.connectSockets( REQUIRE(blend_tree_resource.ConnectSockets(
float_to_vec3_node, float_to_vec3_node,
"Output", "Output",
graph_output_node, graph_output_node,
@ -376,15 +408,17 @@ TEST_CASE("ResourceSaveLoadMathGraphInputs", "[AnimGraphResource]") {
WHEN("Saving and loading graph resource") { WHEN("Saving and loading graph resource") {
const char* filename = "ResourceSaveLoadGraphInputs.json"; const char* filename = "ResourceSaveLoadGraphInputs.json";
graph_resource_origin.saveToFile(filename); graph_resource_origin.SaveToFile(filename);
AnimGraphBlendTreeResource graph_resource_loaded; AnimGraphResource graph_resource_loaded;
graph_resource_loaded.loadFromFile(filename); graph_resource_loaded.LoadFromFile(filename);
BlendTreeResource graph_blend_tree_loaded = graph_resource_loaded.m_blend_tree_resource;
const AnimNodeResource& graph_loaded_output_node = const AnimNodeResource& graph_loaded_output_node =
graph_resource_loaded.m_nodes[0]; graph_blend_tree_loaded.m_nodes[0];
const AnimNodeResource& graph_loaded_input_node = const AnimNodeResource& graph_loaded_input_node =
graph_resource_loaded.m_nodes[1]; graph_blend_tree_loaded.m_nodes[1];
THEN("Graph inputs and outputs must be in loaded resource as well.") { THEN("Graph inputs and outputs must be in loaded resource as well.") {
REQUIRE( REQUIRE(
@ -410,34 +444,33 @@ TEST_CASE("ResourceSaveLoadMathGraphInputs", "[AnimGraphResource]") {
!= nullptr); != nullptr);
WHEN("Instantiating an AnimGraph") { WHEN("Instantiating an AnimGraph") {
AnimGraph anim_graph; AnimGraphBlendTree blend_tree_node;
graph_resource_loaded.createInstance(anim_graph); graph_resource_loaded.CreateBlendTreeInstance(blend_tree_node);
REQUIRE(anim_graph.getInputSocket("GraphFloatInput") != nullptr); REQUIRE(blend_tree_node.GetInputSocket("GraphFloatInput") != nullptr);
REQUIRE( REQUIRE(
anim_graph.getInputPtr("GraphFloatInput") blend_tree_node.GetInputPtr("GraphFloatInput")
== anim_graph.m_input_buffer); == blend_tree_node.m_input_buffer);
float graph_float_input = 123.456f; float graph_float_input = 123.456f;
anim_graph.SetInput("GraphFloatInput", &graph_float_input); blend_tree_node.SetInput("GraphFloatInput", &graph_float_input);
AND_WHEN("Evaluating Graph") { AND_WHEN("Evaluating Graph") {
AnimGraphContext context; AnimGraphContext context;
context.m_graph = &anim_graph; context.m_graph = nullptr;
anim_graph.init(context); blend_tree_node.Init(context);
// GraphFloatOutput is directly connected to GraphFloatInput therefore // GraphFloatOutput is directly connected to GraphFloatInput therefore
// we need to get the pointer here. // we need to get the pointer here.
float* graph_float_ptr = nullptr; float* graph_float_ptr = blend_tree_node.GetOutputPtr<float>("GraphFloatOutput");
graph_float_ptr = anim_graph.GetOutputPtr<float>("GraphFloatOutput");
Vec3 graph_vec3_output; Vec3 graph_vec3_output;
anim_graph.SetOutput("GraphVec3Output", &graph_vec3_output); blend_tree_node.SetOutput("GraphVec3Output", &graph_vec3_output);
anim_graph.updateTime(0.f); blend_tree_node.UpdateTime(0.f, 0.f);
anim_graph.evaluate(context); blend_tree_node.Evaluate(context);
THEN("output vector components equal the graph input vaulues") { THEN("output vector components equal the graph input values") {
CHECK(graph_float_ptr == &graph_float_input); CHECK(graph_float_ptr == &graph_float_input);
CHECK(graph_vec3_output.v[0] == graph_float_input); CHECK(graph_vec3_output.v[0] == graph_float_input);
CHECK(graph_vec3_output.v[1] == graph_float_input); CHECK(graph_vec3_output.v[1] == graph_float_input);
@ -451,20 +484,25 @@ TEST_CASE("ResourceSaveLoadMathGraphInputs", "[AnimGraphResource]") {
} }
} }
TEST_CASE("SimpleMathEvaluations", "[AnimGraphResource]") { TEST_CASE("SimpleMathEvaluations", "[AnimGraphResource]") {
AnimGraphBlendTreeResource graph_resource_origin; AnimGraphResource graph_resource_origin;
graph_resource_origin.m_name = "TestSimpleMathGraph";
graph_resource_origin.m_type = "BlendTree";
graph_resource_origin.clear(); BlendTreeResource& blend_tree_resource = graph_resource_origin.m_blend_tree_resource;
graph_resource_origin.m_name = "TestInputOutputGraph"; blend_tree_resource.Reset();
blend_tree_resource.InitGraphConnectors();
size_t math_add0_node_index = // Prepare graph inputs and outputs
graph_resource_origin.addNode(AnimNodeResourceFactory("MathAddNode")); blend_tree_resource.m_nodes.push_back(AnimNodeResourceFactory("MathAddNode"));
size_t math_add0_node_index = blend_tree_resource.m_nodes.size() - 1;
size_t math_add1_node_index = blend_tree_resource.m_nodes.push_back(AnimNodeResourceFactory("MathAddNode"));
graph_resource_origin.addNode(AnimNodeResourceFactory("MathAddNode")); size_t math_add1_node_index = blend_tree_resource.m_nodes.size() - 1;
AnimNodeResource& graph_output_node = AnimNodeResource& graph_output_node =
graph_resource_origin.getGraphOutputNode(); blend_tree_resource.GetGraphOutputNode();
graph_output_node.m_socket_accessor->RegisterInput<float>( graph_output_node.m_socket_accessor->RegisterInput<float>(
"GraphFloat0Output", "GraphFloat0Output",
@ -477,57 +515,57 @@ TEST_CASE("SimpleMathEvaluations", "[AnimGraphResource]") {
nullptr); nullptr);
AnimNodeResource& graph_input_node = AnimNodeResource& graph_input_node =
graph_resource_origin.getGraphInputNode(); blend_tree_resource.GetGraphInputNode();
graph_input_node.m_socket_accessor->RegisterOutput<float>( graph_input_node.m_socket_accessor->RegisterOutput<float>(
"GraphFloatInput", "GraphFloatInput",
nullptr); nullptr);
// Prepare graph inputs and outputs // Prepare graph inputs and outputs
AnimNodeResource& math_add0_node = AnimNodeResource& math_add0_node =
graph_resource_origin.m_nodes[math_add0_node_index]; blend_tree_resource.m_nodes[math_add0_node_index];
AnimNodeResource& math_add1_node = AnimNodeResource& math_add1_node =
graph_resource_origin.m_nodes[math_add1_node_index]; blend_tree_resource.m_nodes[math_add1_node_index];
// direct output // direct output
REQUIRE(graph_resource_origin.connectSockets( REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node, graph_input_node,
"GraphFloatInput", "GraphFloatInput",
graph_output_node, graph_output_node,
"GraphFloat0Output")); "GraphFloat0Output"));
// add0 node // add0 node
REQUIRE(graph_resource_origin.connectSockets( REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node, graph_input_node,
"GraphFloatInput", "GraphFloatInput",
math_add0_node, math_add0_node,
"Input0")); "Input0"));
REQUIRE(graph_resource_origin.connectSockets( REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node, graph_input_node,
"GraphFloatInput", "GraphFloatInput",
math_add0_node, math_add0_node,
"Input1")); "Input1"));
REQUIRE(graph_resource_origin.connectSockets( REQUIRE(blend_tree_resource.ConnectSockets(
math_add0_node, math_add0_node,
"Output", "Output",
graph_output_node, graph_output_node,
"GraphFloat1Output")); "GraphFloat1Output"));
// add1 node // add1 node
REQUIRE(graph_resource_origin.connectSockets( REQUIRE(blend_tree_resource.ConnectSockets(
math_add0_node, math_add0_node,
"Output", "Output",
math_add1_node, math_add1_node,
"Input0")); "Input0"));
REQUIRE(graph_resource_origin.connectSockets( REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node, graph_input_node,
"GraphFloatInput", "GraphFloatInput",
math_add1_node, math_add1_node,
"Input1")); "Input1"));
REQUIRE(graph_resource_origin.connectSockets( REQUIRE(blend_tree_resource.ConnectSockets(
math_add1_node, math_add1_node,
"Output", "Output",
graph_output_node, graph_output_node,
@ -535,45 +573,38 @@ TEST_CASE("SimpleMathEvaluations", "[AnimGraphResource]") {
WHEN("Saving and loading graph resource") { WHEN("Saving and loading graph resource") {
const char* filename = "ResourceSaveLoadGraphInputs.json"; const char* filename = "ResourceSaveLoadGraphInputs.json";
graph_resource_origin.saveToFile(filename); graph_resource_origin.SaveToFile(filename);
AnimGraphBlendTreeResource graph_resource_loaded; AnimGraphResource graph_resource_loaded;
graph_resource_loaded.loadFromFile(filename); graph_resource_loaded.LoadFromFile(filename);
const AnimNodeResource& graph_loaded_output_node =
graph_resource_loaded.m_nodes[0];
const AnimNodeResource& graph_loaded_input_node =
graph_resource_loaded.m_nodes[1];
WHEN("Instantiating an AnimGraph") { WHEN("Instantiating an AnimGraph") {
AnimGraph anim_graph; AnimGraphBlendTree blend_tree;
graph_resource_loaded.createInstance(anim_graph); graph_resource_loaded.CreateBlendTreeInstance(blend_tree);
REQUIRE(anim_graph.getInputSocket("GraphFloatInput") != nullptr); REQUIRE(blend_tree.GetInputSocket("GraphFloatInput") != nullptr);
REQUIRE( REQUIRE(
anim_graph.getInputPtr("GraphFloatInput") blend_tree.GetInputPtr("GraphFloatInput")
== anim_graph.m_input_buffer); == blend_tree.m_input_buffer);
float graph_float_input = 123.456f; float graph_float_input = 123.456f;
anim_graph.SetInput("GraphFloatInput", &graph_float_input); blend_tree.SetInput("GraphFloatInput", &graph_float_input);
AND_WHEN("Evaluating Graph") { AND_WHEN("Evaluating Graph") {
AnimGraphContext context; AnimGraphContext context;
context.m_graph = &anim_graph; context.m_graph = nullptr;
// float0 output is directly connected to the graph input, therefore // float0 output is directly connected to the graph input, therefore
// we have to get a ptr to the input data here. // we have to get a ptr to the input data here.
float* float0_output_ptr = nullptr; float* float0_output_ptr = blend_tree.GetOutputPtr<float>("GraphFloat0Output");
float float1_output = -1.f; float float1_output = -1.f;
float float2_output = -1.f; float float2_output = -1.f;
float0_output_ptr = anim_graph.GetOutputPtr<float>("GraphFloat0Output"); blend_tree.SetOutput("GraphFloat1Output", &float1_output);
blend_tree.SetOutput("GraphFloat2Output", &float2_output);
anim_graph.SetOutput("GraphFloat1Output", &float1_output); blend_tree.UpdateTime(0.f, 0.f);
anim_graph.SetOutput("GraphFloat2Output", &float2_output); blend_tree.Evaluate(context);
anim_graph.updateTime(0.f);
anim_graph.evaluate(context);
THEN("output vector components equal the graph input vaulues") { THEN("output vector components equal the graph input vaulues") {
CHECK(*float0_output_ptr == Approx(graph_float_input)); CHECK(*float0_output_ptr == Approx(graph_float_input));
@ -588,5 +619,3 @@ TEST_CASE("SimpleMathEvaluations", "[AnimGraphResource]") {
} }
} }
} }
*/