AnimTestbed/tests/AnimGraphResourceTests.cc

122 lines
4.9 KiB
C++
Raw Normal View History

//
// Created by martin on 04.02.22.
//
#include "AnimGraphResource.h"
#include "catch.hpp"
TEST_CASE("BasicGraph", "[AnimGraphResource]") {
AnimGraphResource graph_resource;
2022-02-12 10:14:26 +01:00
graph_resource.clear();
graph_resource.m_name = "WalkRunBlendGraph";
2022-02-12 10:14:26 +01:00
// Prepare graph inputs and outputs
size_t walk_node_index =
graph_resource.addNode(AnimNodeResourceFactory("AnimSampler"));
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];
walk_node.m_name = "WalkAnim";
2022-02-12 10:14:26 +01:00
AnimNodeResource& run_node = graph_resource.m_nodes[run_node_index];
walk_node.m_name = "RunAnim";
AnimNodeResource& blend_node = graph_resource.m_nodes[blend_node_index];
walk_node.m_name = "BlendWalkRun";
2022-02-12 10:14:26 +01:00
AnimNodeResource& graph_node = graph_resource.m_nodes[0];
graph_node.m_socket_accessor->RegisterInput<AnimData>("GraphOutput", nullptr);
2022-02-12 10:14:26 +01:00
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("Input1") == 1);
AnimGraphConnection walk_to_blend;
2022-02-12 10:14:26 +01:00
walk_to_blend.m_source_node_index = walk_node_index;
walk_to_blend.m_source_socket_index =
walk_node.m_socket_accessor->GetOutputIndex("Output");
walk_to_blend.m_target_node_index = blend_node_index;
walk_to_blend.m_target_socket_index =
blend_node.m_socket_accessor->GetInputIndex("Input0");
graph_resource.m_connections.push_back(walk_to_blend);
AnimGraphConnection run_to_blend;
2022-02-12 10:14:26 +01:00
run_to_blend.m_source_node_index = run_node_index;
run_to_blend.m_source_socket_index =
run_node.m_socket_accessor->GetOutputIndex("Output");
run_to_blend.m_target_node_index = blend_node_index;
run_to_blend.m_target_socket_index =
blend_node.m_socket_accessor->GetInputIndex("Input1");
graph_resource.m_connections.push_back(run_to_blend);
AnimGraphConnection blend_to_output;
2022-02-12 10:14:26 +01:00
blend_to_output.m_source_node_index = blend_node_index;
blend_to_output.m_source_socket_index =
blend_node.m_socket_accessor->GetOutputIndex("Output");
blend_to_output.m_target_node_index = 0;
blend_to_output.m_target_socket_index =
graph_node.m_socket_accessor->GetInputIndex("GraphOutput");
graph_resource.m_connections.push_back(blend_to_output);
graph_resource.saveToFile("WalkGraph.animgraph.json");
AnimGraph graph = AnimGraph::createFromResource(graph_resource);
for (size_t i = 0; i < graph.m_nodes.size(); i++) {
const AnimNode* node = graph.m_nodes[i];
std::cout << node->m_name << " [" << node->m_node_type_name << "]"
<< std::endl;
}
REQUIRE(graph.m_nodes.size() == 4);
REQUIRE(graph.m_nodes[0]->m_node_type_name == "BlendTree");
REQUIRE(graph.m_nodes[1]->m_node_type_name == "AnimSampler");
REQUIRE(graph.m_nodes[2]->m_node_type_name == "AnimSampler");
REQUIRE(graph.m_nodes[3]->m_node_type_name == "Blend2");
// connections within the graph
AnimSamplerNode* anim_sampler_instance0 =
dynamic_cast<AnimSamplerNode*>(graph.m_nodes[1]);
2022-02-12 10:14:26 +01:00
AnimSamplerNode* anim_sampler_instance1 =
dynamic_cast<AnimSamplerNode*>(graph.m_nodes[2]);
Blend2Node* blend2_instance = dynamic_cast<Blend2Node*>(graph.m_nodes[3]);
CHECK(anim_sampler_instance0->m_output == &blend2_instance->m_input0);
CHECK(anim_sampler_instance1->m_output == &blend2_instance->m_input1);
// connections from graph to the graph node
CHECK(graph.m_socket_accessor->m_inputs.size() == 1);
CHECK(graph.m_socket_accessor->FindInputSocket("GraphOutput"));
CHECK(reinterpret_cast<char*>(blend2_instance->m_output) == graph.m_input_buffer);
// check node input dependencies
size_t anim_sampler_index0 = graph.getAnimNodeIndex(anim_sampler_instance0);
size_t anim_sampler_index1 = graph.getAnimNodeIndex(anim_sampler_instance1);
size_t blend_index = graph.getAnimNodeIndex(blend2_instance);
CHECK(graph.m_node_inputs[anim_sampler_index0].size() == 0);
CHECK(graph.m_node_inputs[anim_sampler_index1].size() == 0);
CHECK(graph.m_node_inputs[blend_index].size() == 2);
CHECK(graph.m_node_inputs[blend_index][0] == anim_sampler_instance0);
CHECK(graph.m_node_inputs[blend_index][1] == anim_sampler_instance1);
}
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);
}