AnimTestbed/tests/AnimGraphResourceTests.cc

769 lines
28 KiB
C++

//
// Created by martin on 04.02.22.
//
#include "AnimGraph/AnimGraph.h"
#include "AnimGraph/AnimGraphBlendTree.h"
#include "AnimGraph/AnimGraphEditor.h"
#include "AnimGraph/AnimGraphNodes.h"
#include "AnimGraph/AnimGraphResource.h"
#include "catch.hpp"
#include "ozz/base/io/archive.h"
#include "ozz/base/io/stream.h"
#include "ozz/base/log.h"
bool load_skeleton(ozz::animation::Skeleton& skeleton, const char* filename) {
assert(filename);
ozz::io::File file(filename, "rb");
if (!file.opened()) {
ozz::log::Err() << "Failed to open skeleton file " << filename << "."
<< std::endl;
return false;
}
ozz::io::IArchive archive(&file);
if (!archive.TestTag<ozz::animation::Skeleton>()) {
ozz::log::Err() << "Failed to load skeleton instance from file " << filename
<< "." << std::endl;
return false;
}
// Once the tag is validated, reading cannot fail.
archive >> skeleton;
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]") {
AnimGraphResource graph_resource;
graph_resource.m_name = "AnimSamplerBlendTree";
graph_resource.m_graph_type_name = "BlendTree";
BlendTreeResource& blend_tree_resource = graph_resource.m_blend_tree_resource;
blend_tree_resource.InitGraphConnectors();
// Prepare graph inputs and outputs
blend_tree_resource.m_nodes.push_back(AnimNodeResourceFactory("AnimSampler"));
size_t walk_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_socket_accessor->SetPropertyValue(
"Filename",
std::string("media/Walking-loop.ozz"));
AnimNodeResource* graph_node = blend_tree_resource.m_nodes[0];
graph_node->m_socket_accessor->RegisterInput<AnimData>("GraphOutput", nullptr);
blend_tree_resource.ConnectSockets(
walk_node,
"Output",
blend_tree_resource.GetGraphOutputNode(),
"GraphOutput");
graph_resource.SaveToFile("TestGraphAnimSamplerBlendTree.json");
AnimGraphResource graph_resource_loaded;
graph_resource_loaded.LoadFromFile("TestGraphAnimSamplerBlendTree.json");
AnimGraphBlendTree anim_graph_blend_tree;
graph_resource_loaded.CreateBlendTreeInstance(anim_graph_blend_tree);
AnimGraphContext graph_context;
ozz::animation::Skeleton skeleton;
REQUIRE(load_skeleton(skeleton, "media/skeleton.ozz"));
graph_context.m_skeleton = &skeleton;
REQUIRE(anim_graph_blend_tree.Init(graph_context));
REQUIRE(anim_graph_blend_tree.m_nodes.size() == 3);
REQUIRE(anim_graph_blend_tree.m_nodes[0]->m_node_type_name == "BlendTreeSockets");
REQUIRE(anim_graph_blend_tree.m_nodes[1]->m_node_type_name == "BlendTreeSockets");
REQUIRE(anim_graph_blend_tree.m_nodes[2]->m_node_type_name == "AnimSampler");
// connections within the graph
AnimSamplerNode* anim_sampler_walk =
dynamic_cast<AnimSamplerNode*>(anim_graph_blend_tree.m_nodes[2]);
BlendTreeNode* graph_output_node =
dynamic_cast<BlendTreeNode*>(anim_graph_blend_tree.m_nodes[0]);
// check node input dependencies
size_t anim_sampler_index = anim_graph_blend_tree.GetAnimNodeIndex(anim_sampler_walk);
REQUIRE(anim_graph_blend_tree.m_node_output_connections[anim_sampler_index].size() == 1);
CHECK(
anim_graph_blend_tree.m_node_output_connections[anim_sampler_index][0].m_target_node
== graph_output_node);
// Ensure animation sampler nodes use the correct files
REQUIRE(anim_sampler_walk->m_filename == "media/Walking-loop.ozz");
REQUIRE(anim_sampler_walk->m_animation != nullptr);
// Ensure that outputs are properly propagated.
AnimData output;
output.m_local_matrices.resize(skeleton.num_soa_joints());
anim_graph_blend_tree.SetOutput("GraphOutput", &output);
REQUIRE(anim_sampler_walk->o_output == &output);
WHEN("Emulating Graph Evaluation") {
CHECK(anim_graph_blend_tree.m_anim_data_allocator.size() == 0);
anim_sampler_walk->Evaluate(graph_context);
}
graph_context.freeAnimations();
}
//
// Checks that node const inputs are properly set.
//
TEST_CASE("AnimSamplerSpeedScaleGraph", "[AnimGraphResource]") {
AnimGraphResource graph_resource;
graph_resource.m_name = "AnimSamplerSpeedScaleGraph";
graph_resource.m_graph_type_name = "BlendTree";
BlendTreeResource& blend_tree_resource = graph_resource.m_blend_tree_resource;
blend_tree_resource.Reset();
blend_tree_resource.InitGraphConnectors();
// Prepare graph inputs and outputs
blend_tree_resource.m_nodes.push_back(AnimNodeResourceFactory("AnimSampler"));
size_t walk_node_index = blend_tree_resource.m_nodes.size() - 1;
blend_tree_resource.m_nodes.push_back(AnimNodeResourceFactory("SpeedScale"));
size_t speed_scale_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_socket_accessor->SetPropertyValue(
"Filename",
std::string("media/Walking-loop.ozz"));
AnimNodeResource* speed_scale_node =
blend_tree_resource.m_nodes[speed_scale_node_index];
speed_scale_node->m_name = "SpeedScale";
float speed_scale_value = 1.35f;
speed_scale_node->m_socket_accessor->SetInputValue(
"SpeedScale",
speed_scale_value);
AnimNodeResource* graph_node = blend_tree_resource.GetGraphOutputNode();
graph_node->m_socket_accessor->RegisterInput<AnimData>("GraphOutput", nullptr);
blend_tree_resource.ConnectSockets(walk_node, "Output", speed_scale_node, "Input");
blend_tree_resource.ConnectSockets(
speed_scale_node,
"Output",
blend_tree_resource.GetGraphOutputNode(),
"GraphOutput");
graph_resource.SaveToFile("TestGraphAnimSamplerSpeedScaleGraph.animgraph.json");
AnimGraphResource graph_resource_loaded;
graph_resource_loaded.LoadFromFile(
"TestGraphAnimSamplerSpeedScaleGraph.animgraph.json");
BlendTreeResource& blend_tree_resource_loaded = graph_resource_loaded.m_blend_tree_resource;
Socket* speed_scale_resource_loaded_input =
blend_tree_resource_loaded.m_nodes[speed_scale_node_index]->m_socket_accessor->GetInputSocket("SpeedScale");
REQUIRE(speed_scale_resource_loaded_input != nullptr);
REQUIRE_THAT(
speed_scale_resource_loaded_input->m_value.float_value,
Catch::Matchers::WithinAbs(speed_scale_value, 0.1));
AnimGraphBlendTree blend_tree;
graph_resource_loaded.CreateBlendTreeInstance(blend_tree);
REQUIRE_THAT(*dynamic_cast<SpeedScaleNode*>(blend_tree.m_nodes[speed_scale_node_index])->i_speed_scale,
Catch::Matchers::WithinAbs(speed_scale_value, 0.1));
}
TEST_CASE("Blend2Graph", "[AnimGraphResource]") {
AnimGraphResource graph_resource;
graph_resource.m_name = "WalkRunBlendGraph";
graph_resource.m_graph_type_name = "BlendTree";
BlendTreeResource& blend_tree_resource = graph_resource.m_blend_tree_resource;
blend_tree_resource.Reset();
blend_tree_resource.InitGraphConnectors();
// Prepare graph inputs and outputs
blend_tree_resource.m_nodes.push_back(AnimNodeResourceFactory("AnimSampler"));
size_t walk_node_index = blend_tree_resource.m_nodes.size() - 1;
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_socket_accessor->SetPropertyValue(
"Filename",
std::string("media/Walking-loop.ozz"));
AnimNodeResource* run_node = blend_tree_resource.m_nodes[run_node_index];
run_node->m_socket_accessor->SetPropertyValue(
"Filename",
std::string("media/Running0-loop.ozz"));
run_node->m_name = "RunAnim";
AnimNodeResource* blend_node = blend_tree_resource.m_nodes[blend_node_index];
blend_node->m_name = "BlendWalkRun";
AnimNodeResource* graph_node = blend_tree_resource.GetGraphOutputNode();
graph_node->m_socket_accessor->RegisterInput<AnimData>("GraphOutput", 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);
blend_tree_resource.ConnectSockets(walk_node, "Output", blend_node, "Input0");
blend_tree_resource.ConnectSockets(run_node, "Output", blend_node, "Input1");
blend_tree_resource.ConnectSockets(
blend_node,
"Output",
blend_tree_resource.GetGraphOutputNode(),
"GraphOutput");
graph_resource.SaveToFile("TestGraphBlend2Graph.animgraph.json");
AnimGraphResource graph_resource_loaded;
graph_resource_loaded.LoadFromFile("TestGraphBlend2Graph.animgraph.json");
AnimGraphBlendTree blend_tree_graph;
graph_resource_loaded.CreateBlendTreeInstance(blend_tree_graph);
AnimGraphContext graph_context;
ozz::animation::Skeleton skeleton;
REQUIRE(load_skeleton(skeleton, "media/skeleton.ozz"));
graph_context.m_skeleton = &skeleton;
REQUIRE(blend_tree_graph.Init(graph_context));
REQUIRE(blend_tree_graph.m_nodes.size() == 5);
REQUIRE(blend_tree_graph.m_nodes[0]->m_node_type_name == "BlendTreeSockets");
REQUIRE(blend_tree_graph.m_nodes[1]->m_node_type_name == "BlendTreeSockets");
REQUIRE(blend_tree_graph.m_nodes[2]->m_node_type_name == "AnimSampler");
REQUIRE(blend_tree_graph.m_nodes[3]->m_node_type_name == "AnimSampler");
REQUIRE(blend_tree_graph.m_nodes[4]->m_node_type_name == "Blend2");
// connections within the graph
AnimSamplerNode* anim_sampler_walk =
dynamic_cast<AnimSamplerNode*>(blend_tree_graph.m_nodes[2]);
AnimSamplerNode* anim_sampler_run =
dynamic_cast<AnimSamplerNode*>(blend_tree_graph.m_nodes[3]);
Blend2Node* blend2_instance = dynamic_cast<Blend2Node*>(blend_tree_graph.m_nodes[4]);
// check node input dependencies
size_t anim_sampler_index0 = blend_tree_graph.GetAnimNodeIndex(anim_sampler_walk);
size_t anim_sampler_index1 = blend_tree_graph.GetAnimNodeIndex(anim_sampler_run);
size_t blend_index = blend_tree_graph.GetAnimNodeIndex(blend2_instance);
REQUIRE(blend_tree_graph.m_node_input_connections[blend_index].size() == 2);
CHECK(
blend_tree_graph.m_node_input_connections[blend_index][0].m_source_node
== anim_sampler_walk);
CHECK(
blend_tree_graph.m_node_input_connections[blend_index][1].m_source_node
== anim_sampler_run);
REQUIRE(
blend_tree_graph.m_node_output_connections[anim_sampler_index0].size() == 1);
CHECK(
blend_tree_graph.m_node_output_connections[anim_sampler_index0][0].m_target_node
== blend2_instance);
REQUIRE(
blend_tree_graph.m_node_output_connections[anim_sampler_index1].size() == 1);
CHECK(
blend_tree_graph.m_node_output_connections[anim_sampler_index1][0].m_target_node
== blend2_instance);
// Ensure animation sampler nodes use the correct files
REQUIRE(anim_sampler_walk->m_filename == "media/Walking-loop.ozz");
REQUIRE(anim_sampler_walk->m_animation != nullptr);
REQUIRE(anim_sampler_run->m_filename == "media/Running0-loop.ozz");
REQUIRE(anim_sampler_run->m_animation != nullptr);
WHEN("Emulating Graph Evaluation") {
CHECK(blend_tree_graph.m_anim_data_allocator.size() == 0);
CHECK(blend2_instance->i_input0 == anim_sampler_walk->o_output);
CHECK(blend2_instance->i_input1 == anim_sampler_run->o_output);
const Socket* graph_output_socket =
blend_tree_graph.GetOutputSocket("GraphOutput");
AnimData* graph_output =
static_cast<AnimData*>(*graph_output_socket->m_reference.ptr_ptr);
CHECK(
graph_output->m_local_matrices.size()
== graph_context.m_skeleton->num_soa_joints());
CHECK(
blend2_instance->o_output == *graph_output_socket->m_reference.ptr_ptr);
}
graph_context.freeAnimations();
}
//
// Graph:
// Outputs:
// - GraphFloatOutput (float)
// - GraphVec3Output (Vec3)
//
// Inputs:
// - GraphFloatInput (float)
//
// Nodes:
// - MathFloatToVec3Node
//
// Connectivity:
// GraphFloatInput -+----------------------> GraphFloatOutput
// \-> MathFloatToVec3 --> GraphVec3Output
//
//
TEST_CASE("ResourceSaveLoadMathGraphInputs", "[AnimGraphResource]") {
AnimGraphResource graph_resource_origin;
graph_resource_origin.m_name = "TestInputOutputGraph";
graph_resource_origin.m_graph_type_name = "BlendTree";
BlendTreeResource& blend_tree_resource = graph_resource_origin.m_blend_tree_resource;
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 =
blend_tree_resource.GetGraphOutputNode();
graph_output_node->m_socket_accessor->RegisterInput<float>(
"GraphFloatOutput",
nullptr);
graph_output_node->m_socket_accessor->RegisterInput<Vec3>(
"GraphVec3Output",
nullptr);
AnimNodeResource* graph_input_node =
blend_tree_resource.GetGraphInputNode();
graph_input_node->m_socket_accessor->RegisterOutput<float>(
"GraphFloatInput",
nullptr);
// Prepare graph inputs and outputs
AnimNodeResource* float_to_vec3_node =
blend_tree_resource.m_nodes[float_to_vec3_node_index];
REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node,
"GraphFloatInput",
graph_output_node,
"GraphFloatOutput"));
REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node,
"GraphFloatInput",
float_to_vec3_node,
"Input0"));
REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node,
"GraphFloatInput",
float_to_vec3_node,
"Input1"));
REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node,
"GraphFloatInput",
float_to_vec3_node,
"Input2"));
REQUIRE(blend_tree_resource.ConnectSockets(
float_to_vec3_node,
"Output",
graph_output_node,
"GraphVec3Output"));
WHEN("Saving and loading graph resource") {
const char* filename = "TestGraphResourceSaveLoadGraphInputs.json";
graph_resource_origin.SaveToFile(filename);
AnimGraphResource graph_resource_loaded;
graph_resource_loaded.LoadFromFile(filename);
BlendTreeResource& graph_blend_tree_loaded = graph_resource_loaded.m_blend_tree_resource;
const AnimNodeResource* graph_loaded_output_node =
graph_blend_tree_loaded.m_nodes[0];
const AnimNodeResource* graph_loaded_input_node =
graph_blend_tree_loaded.m_nodes[1];
THEN("Graph inputs and outputs must be in loaded resource as well.") {
REQUIRE(
graph_output_node->m_socket_accessor->m_inputs.size()
== graph_loaded_output_node->m_socket_accessor->m_inputs.size());
REQUIRE(
graph_input_node->m_socket_accessor->m_outputs.size()
== graph_loaded_input_node->m_socket_accessor->m_outputs.size());
REQUIRE(
graph_loaded_input_node->m_socket_accessor->GetOutputSocket(
"GraphFloatInput")
!= nullptr);
REQUIRE(
graph_loaded_output_node->m_socket_accessor->GetInputSocket(
"GraphFloatOutput")
!= nullptr);
REQUIRE(
graph_loaded_output_node->m_socket_accessor->GetInputSocket(
"GraphVec3Output")
!= nullptr);
WHEN("Instantiating an AnimGraph") {
AnimGraphBlendTree blend_tree_node;
graph_resource_loaded.CreateBlendTreeInstance(blend_tree_node);
REQUIRE(blend_tree_node.GetInputSocket("GraphFloatInput") != nullptr);
REQUIRE(
blend_tree_node.GetInputPtr("GraphFloatInput")
== blend_tree_node.m_input_buffer);
float graph_float_input = 123.456f;
blend_tree_node.SetInput("GraphFloatInput", &graph_float_input);
AND_WHEN("Evaluating Graph") {
AnimGraphContext context;
context.m_graph = nullptr;
blend_tree_node.Init(context);
// GraphFloatOutput is directly connected to GraphFloatInput therefore
// we need to get the pointer here.
float* graph_float_ptr = blend_tree_node.GetOutputPtr<float>("GraphFloatOutput");
Vec3 graph_vec3_output;
blend_tree_node.SetOutput("GraphVec3Output", &graph_vec3_output);
blend_tree_node.UpdateTime(0.f, 0.f);
blend_tree_node.Evaluate(context);
THEN("output vector components equal the graph input values") {
CHECK(graph_float_ptr == &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[2] == graph_float_input);
}
context.freeAnimations();
}
}
}
}
}
TEST_CASE("SimpleMathEvaluations", "[AnimGraphResource]") {
AnimGraphResource graph_resource_origin;
graph_resource_origin.m_name = "TestSimpleMathGraph";
graph_resource_origin.m_graph_type_name = "BlendTree";
BlendTreeResource& blend_tree_resource = graph_resource_origin.m_blend_tree_resource;
blend_tree_resource.Reset();
blend_tree_resource.InitGraphConnectors();
// Prepare graph inputs and outputs
blend_tree_resource.m_nodes.push_back(AnimNodeResourceFactory("MathAddNode"));
size_t math_add0_node_index = blend_tree_resource.m_nodes.size() - 1;
blend_tree_resource.m_nodes.push_back(AnimNodeResourceFactory("MathAddNode"));
size_t math_add1_node_index = blend_tree_resource.m_nodes.size() - 1;
AnimNodeResource* graph_output_node =
blend_tree_resource.GetGraphOutputNode();
graph_output_node->m_socket_accessor->RegisterInput<float>(
"GraphFloat0Output",
nullptr);
graph_output_node->m_socket_accessor->RegisterInput<float>(
"GraphFloat1Output",
nullptr);
graph_output_node->m_socket_accessor->RegisterInput<float>(
"GraphFloat2Output",
nullptr);
AnimNodeResource* graph_input_node =
blend_tree_resource.GetGraphInputNode();
graph_input_node->m_socket_accessor->RegisterOutput<float>(
"GraphFloatInput",
nullptr);
// Prepare graph inputs and outputs
AnimNodeResource* math_add0_node =
blend_tree_resource.m_nodes[math_add0_node_index];
AnimNodeResource* math_add1_node =
blend_tree_resource.m_nodes[math_add1_node_index];
// direct output
REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node,
"GraphFloatInput",
graph_output_node,
"GraphFloat0Output"));
// add0 node
REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node,
"GraphFloatInput",
math_add0_node,
"Input0"));
REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node,
"GraphFloatInput",
math_add0_node,
"Input1"));
REQUIRE(blend_tree_resource.ConnectSockets(
math_add0_node,
"Output",
graph_output_node,
"GraphFloat1Output"));
// add1 node
REQUIRE(blend_tree_resource.ConnectSockets(
math_add0_node,
"Output",
math_add1_node,
"Input0"));
REQUIRE(blend_tree_resource.ConnectSockets(
graph_input_node,
"GraphFloatInput",
math_add1_node,
"Input1"));
REQUIRE(blend_tree_resource.ConnectSockets(
math_add1_node,
"Output",
graph_output_node,
"GraphFloat2Output"));
WHEN("Saving and loading graph resource") {
const char* filename = "TestGraphResourceSaveLoadGraphInputs.json";
graph_resource_origin.SaveToFile(filename);
AnimGraphResource graph_resource_loaded;
graph_resource_loaded.LoadFromFile(filename);
WHEN("Instantiating an AnimGraph") {
AnimGraphBlendTree blend_tree;
graph_resource_loaded.CreateBlendTreeInstance(blend_tree);
REQUIRE(blend_tree.GetInputSocket("GraphFloatInput") != nullptr);
REQUIRE(
blend_tree.GetInputPtr("GraphFloatInput")
== blend_tree.m_input_buffer);
float graph_float_input = 123.456f;
blend_tree.SetInput("GraphFloatInput", &graph_float_input);
AND_WHEN("Evaluating Graph") {
AnimGraphContext context;
context.m_graph = nullptr;
// float0 output is directly connected to the graph input, therefore
// we have to get a ptr to the input data here.
float* float0_output_ptr = blend_tree.GetOutputPtr<float>("GraphFloat0Output");
float float1_output = -1.f;
float float2_output = -1.f;
blend_tree.SetOutput("GraphFloat1Output", &float1_output);
blend_tree.SetOutput("GraphFloat2Output", &float2_output);
blend_tree.UpdateTime(0.f, 0.f);
blend_tree.Evaluate(context);
THEN("output vector components equal the graph input vaulues") {
CHECK(*float0_output_ptr == Approx(graph_float_input));
CHECK(float1_output == Approx(graph_float_input * 2.f));
REQUIRE_THAT(
float2_output,
Catch::Matchers::WithinAbs(graph_float_input * 3.f, 10));
}
context.freeAnimations();
}
}
}
}
//
// Tests whether BlendTree within a BlendTree can be saved and loaded
//
// +-----------Parent BlendTree-------------+
// | |
// | +-----Embd Btree---+ |
// | AnmSmpl--+-\ | |
// | | \-SpdScale------+--Out |
// | |------------------+ |
// | |
// +----------------------------------------+
//
TEST_CASE("EmbeddedBlendTreeSaveLoad", "[AnimGraphResource]") {
const char* parent_graph_filename = "TestGraphEmbeddedBlendTree.json";
AnimGraphResource parent_graph_resource;
parent_graph_resource.m_name = "ParentBlendTree";
parent_graph_resource.m_graph_type_name = "BlendTree";
parent_graph_resource.m_node_type_name = "BlendTree";
BlendTreeResource& parent_blend_tree_resource =
parent_graph_resource.m_blend_tree_resource;
parent_blend_tree_resource.Reset();
parent_blend_tree_resource.InitGraphConnectors();
// Setup parent outputs
AnimNodeResource* parent_blend_tree_outputs = parent_blend_tree_resource.GetGraphOutputNode();
parent_blend_tree_outputs->m_socket_accessor->RegisterInput<AnimData>("Output", nullptr);
// Parent AnimSampler
parent_blend_tree_resource.m_nodes.push_back(
AnimNodeResourceFactory("AnimSampler"));
AnimNodeResource* walk_node =
parent_blend_tree_resource.m_nodes.back();
walk_node->m_name = "WalkAnim";
walk_node->m_socket_accessor->SetPropertyValue(
"Filename",
std::string("media/Walking-loop.ozz"));
//
// Embedded Tree
//
parent_blend_tree_resource.m_nodes.push_back(
AnimNodeResourceFactory("BlendTree"));
size_t embedded_blend_tree_node_index =
parent_blend_tree_resource.m_nodes.size() - 1;
AnimGraphResource* embedded_graph =
dynamic_cast<AnimGraphResource*>(parent_blend_tree_resource.m_nodes.back());
embedded_graph->m_name = "EmbeddedBlendTree";
embedded_graph->m_node_type_name = "BlendTree";
BlendTreeResource* embedded_blend_tree_resource = &embedded_graph->m_blend_tree_resource;
// Embedded: outputs
AnimNodeResource* embedded_outputs =
embedded_blend_tree_resource->GetGraphOutputNode();
embedded_outputs->m_socket_accessor->RegisterInput<AnimData>(
"AnimOutput",
nullptr);
// Embedded: inputs
AnimNodeResource* embedded_inputs =
embedded_blend_tree_resource->GetGraphInputNode();
embedded_inputs->m_socket_accessor->RegisterOutput<AnimData>(
"AnimInput",
nullptr);
// Embedded: SpeedScale node
embedded_blend_tree_resource->m_nodes.push_back(
AnimNodeResourceFactory("SpeedScale"));
AnimNodeResource* embedded_speed_scale_resource = embedded_blend_tree_resource->m_nodes.back();
embedded_blend_tree_resource->ConnectSockets(embedded_inputs, "AnimInput", embedded_speed_scale_resource, "Input");
embedded_blend_tree_resource->ConnectSockets(embedded_speed_scale_resource, "Output", embedded_outputs, "AnimOutput");
//
// Parent: setup connections
//
REQUIRE(parent_blend_tree_resource.ConnectSockets(walk_node, "Output", embedded_graph, "AnimInput"));
REQUIRE(parent_blend_tree_resource.ConnectSockets(embedded_graph, "AnimOutput", parent_blend_tree_outputs, "Output"));
parent_graph_resource.SaveToFile(parent_graph_filename);
//
// Load the graph
//
AnimGraphResource parent_graph_resource_loaded;
parent_graph_resource_loaded.LoadFromFile(parent_graph_filename);
//
// Check the loaded parent graph
//
CHECK(parent_graph_resource.m_name == parent_graph_resource_loaded.m_name);
CHECK(parent_graph_resource.m_graph_type_name == parent_graph_resource_loaded.m_graph_type_name);
CHECK(parent_graph_resource.m_node_type_name
== parent_graph_resource_loaded.m_node_type_name);
const BlendTreeResource& parent_blend_tree_resource_loaded = parent_graph_resource_loaded.m_blend_tree_resource;
CHECK(parent_blend_tree_resource.m_nodes.size() == parent_blend_tree_resource_loaded.m_nodes.size());
for (size_t i = 0; i < parent_blend_tree_resource.m_nodes.size(); i++) {
const AnimNodeResource* parent_node = parent_blend_tree_resource.m_nodes[i];
const AnimNodeResource* parent_node_loaded = parent_blend_tree_resource_loaded.m_nodes[i];
CHECK(parent_node->m_name == parent_node_loaded->m_name);
CHECK(parent_node->m_node_type_name == parent_node_loaded->m_node_type_name);
}
CHECK(parent_blend_tree_resource.m_connections.size() == parent_blend_tree_resource_loaded.m_connections.size());
for (size_t i = 0; i < parent_blend_tree_resource.m_connections.size(); i++) {
const BlendTreeConnectionResource& parent_connection = parent_blend_tree_resource.m_connections[i];
const BlendTreeConnectionResource& parent_connection_loaded = parent_blend_tree_resource_loaded.m_connections[i];
CHECK(parent_connection.source_node_index == parent_connection_loaded.source_node_index);
CHECK(parent_connection.source_socket_name == parent_connection_loaded.source_socket_name);
CHECK(parent_connection.target_node_index == parent_connection_loaded.target_node_index);
CHECK(parent_connection.target_socket_name == parent_connection_loaded.target_socket_name);
}
//
// Check the loaded embedded graph
//
REQUIRE(parent_blend_tree_resource_loaded.m_nodes[3]->m_node_type_name == "BlendTree");
const AnimGraphResource* embedded_graph_loaded = dynamic_cast<AnimGraphResource*>(parent_blend_tree_resource_loaded.m_nodes[3]);
const BlendTreeResource& embedded_blend_tree_resource_loaded = embedded_graph_loaded->m_blend_tree_resource;
CHECK(embedded_blend_tree_resource->m_nodes.size() == embedded_blend_tree_resource_loaded.m_nodes.size());
CHECK(embedded_blend_tree_resource->m_connections.size() == embedded_blend_tree_resource_loaded.m_connections.size());
for (size_t i = 0; i < embedded_blend_tree_resource->m_nodes.size(); i++) {
const AnimNodeResource* parent_node = embedded_blend_tree_resource->m_nodes[i];
const AnimNodeResource* parent_node_loaded = embedded_blend_tree_resource_loaded.m_nodes[i];
CHECK(parent_node->m_name == parent_node_loaded->m_name);
CHECK(parent_node->m_node_type_name == parent_node_loaded->m_node_type_name);
}
CHECK(embedded_blend_tree_resource->m_connections.size() == embedded_blend_tree_resource_loaded.m_connections.size());
for (size_t i = 0; i < embedded_blend_tree_resource->m_connections.size(); i++) {
const BlendTreeConnectionResource& embedded_connection = embedded_blend_tree_resource->m_connections[i];
const BlendTreeConnectionResource& embedded_connection_loaded = embedded_blend_tree_resource_loaded.m_connections[i];
CHECK(embedded_connection.source_node_index == embedded_connection_loaded.source_node_index);
CHECK(embedded_connection.source_socket_name == embedded_connection_loaded.source_socket_name);
CHECK(embedded_connection.target_node_index == embedded_connection_loaded.target_node_index);
CHECK(embedded_connection.target_socket_name == embedded_connection_loaded.target_socket_name);
}
}