// // Created by martin on 04.02.22. // #include "AnimGraphResource.h" #include "catch.hpp" TEST_CASE("BasicGraph", "[AnimGraphResource]") { AnimGraphResource graph_resource; graph_resource.clear(); graph_resource.m_name = "WalkRunBlendGraph"; // 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"; 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"; AnimNodeResource& graph_node = graph_resource.m_nodes[0]; graph_node.m_socket_accessor->RegisterInput("Output", nullptr); 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; 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; 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; 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("Output"); 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() == 4); CHECK(graph.m_nodes[0]->m_node_type_name == "Graph"); CHECK(graph.m_nodes[1]->m_node_type_name == "AnimSampler"); CHECK(graph.m_nodes[2]->m_node_type_name == "AnimSampler"); CHECK(graph.m_nodes[3]->m_node_type_name == "Blend2"); AnimSamplerNode* anim_sampler_instance0 = dynamic_cast(graph.m_nodes[1]); AnimSamplerNode* anim_sampler_instance1 = dynamic_cast(graph.m_nodes[2]); Blend2Node* blend2_instance = dynamic_cast(graph.m_nodes[3]); 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); }