AnimTestbed/tests/AnimGraphResourceTests.cc

99 lines
3.6 KiB
C++

//
// Created by martin on 04.02.22.
//
#include "AnimGraphResource.h"
#include "catch.hpp"
TEST_CASE("BasicGraph", "[AnimGraphResource]") {
AnimGraphResource graph_resource;
graph_resource.m_name = "WalkRunBlendGraph";
AnimNodeResource walk_node;
walk_node.m_name = "WalkAnim";
walk_node.m_type_name = "AnimSampler";
graph_resource.m_nodes.push_back(walk_node);
AnimNodeResource run_node;
run_node.m_name = "RunAnim";
run_node.m_type_name = "AnimSampler";
graph_resource.m_nodes.push_back(run_node);
AnimNodeResource blend_node;
blend_node.m_name = "BlendWalkRun";
blend_node.m_type_name = "Blend2";
graph_resource.m_nodes.push_back(blend_node);
AnimGraphConnection walk_to_blend;
walk_to_blend.m_source_node = &walk_node;
walk_to_blend.m_source_socket_name = "Output";
walk_to_blend.m_target_node = &blend_node;
walk_to_blend.m_target_socket_name = "Input0";
graph_resource.m_connections.push_back(walk_to_blend);
AnimGraphConnection run_to_blend;
run_to_blend.m_source_node = &run_node;
run_to_blend.m_source_socket_name = "Output";
run_to_blend.m_target_node = &blend_node;
run_to_blend.m_target_socket_name = "Input1";
graph_resource.m_connections.push_back(run_to_blend);
AnimGraphConnection blend_to_output;
blend_to_output.m_source_node = &blend_node;
blend_to_output.m_source_socket_name = "Output";
blend_to_output.m_target_socket_name = "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;
}
CHECK(graph.m_nodes.size() == 3);
CHECK(graph.m_nodes[0]->m_node_type_name == "AnimSampler");
CHECK(graph.m_nodes[1]->m_node_type_name == "AnimSampler");
CHECK(graph.m_nodes[2]->m_node_type_name == "Blend2");
AnimSamplerNode* anim_sampler_instance0 =
dynamic_cast<AnimSamplerNode*>(graph.m_nodes[0]);
AnimSamplerNode* anim_sampler_instance1 =
dynamic_cast<AnimSamplerNode*>(graph.m_nodes[1]);
Blend2Node* blend2_instance = dynamic_cast<Blend2Node*>(graph.m_nodes[2]);
CHECK(anim_sampler_instance0->m_output == &blend2_instance->m_input0);
CHECK(anim_sampler_instance1->m_output == &blend2_instance->m_input1);
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);
}