Initial works for actial animation sampling and graph evaluation.

This commit is contained in:
Martin Felis
2022-04-11 16:46:09 +02:00
parent bf0c7a7daa
commit eeef635c64
9 changed files with 430 additions and 158 deletions
+93 -54
View File
@@ -2,11 +2,36 @@
// Created by martin on 04.02.22.
//
#include "ozz/base/io/archive.h"
#include "ozz/base/io/stream.h"
#include "ozz/base/log.h"
#include "AnimGraph/AnimGraph.h"
#include "AnimGraph/AnimGraphEditor.h"
#include "AnimGraph/AnimGraphResource.h"
#include "catch.hpp"
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("BasicGraph", "[AnimGraphResource]") {
AnimGraphResource graph_resource;
@@ -23,7 +48,9 @@ TEST_CASE("BasicGraph", "[AnimGraphResource]") {
AnimNodeResource& walk_node = graph_resource.m_nodes[walk_node_index];
walk_node.m_name = "WalkAnim";
walk_node.m_socket_accessor->SetPropertyValue("Filename", "data/walk.anim.ozz");
AnimNodeResource& run_node = graph_resource.m_nodes[run_node_index];
run_node.m_socket_accessor->SetPropertyValue("Filename", "data/run.anim.ozz");
run_node.m_name = "RunAnim";
AnimNodeResource& blend_node = graph_resource.m_nodes[blend_node_index];
blend_node.m_name = "BlendWalkRun";
@@ -35,30 +62,27 @@ TEST_CASE("BasicGraph", "[AnimGraphResource]") {
REQUIRE(blend_node.m_socket_accessor->GetInputIndex("Input0") == 0);
REQUIRE(blend_node.m_socket_accessor->GetInputIndex("Input1") == 1);
AnimGraphConnectionResource walk_to_blend;
walk_to_blend.source_node_index = walk_node_index;
walk_to_blend.source_socket_name = "Output";
walk_to_blend.target_node_index = blend_node_index;
walk_to_blend.target_socket_name = "Input0";
graph_resource.m_connections.push_back(walk_to_blend);
AnimGraphConnectionResource run_to_blend;
run_to_blend.source_node_index = run_node_index;
run_to_blend.source_socket_name = "Output";
run_to_blend.target_node_index = blend_node_index;
run_to_blend.target_socket_name = "Input1";
graph_resource.m_connections.push_back(run_to_blend);
AnimGraphConnectionResource blend_to_output;
blend_to_output.source_node_index = blend_node_index;
blend_to_output.source_socket_name = "Output";
blend_to_output.target_node_index = 0;
blend_to_output.target_socket_name = "GraphOutput";
graph_resource.m_connections.push_back(blend_to_output);
graph_resource.connectSockets(walk_node, "Output", blend_node, "Input0");
graph_resource.connectSockets(run_node, "Output", blend_node, "Input1");
graph_resource.connectSockets(
blend_node,
"Output",
graph_resource.getGraphOutputNode(),
"GraphOutput");
graph_resource.saveToFile("WalkGraph.animgraph.json");
AnimGraphResource graph_resource_loaded;
graph_resource_loaded.loadFromFile("WalkGraph.animgraph.json");
AnimGraph graph = graph_resource.createInstance();
AnimGraph graph = graph_resource_loaded.createInstance();
AnimGraphContext graph_context;
graph_context.m_graph = &graph;
ozz::animation::Skeleton skeleton;
REQUIRE(load_skeleton(skeleton, "data/skeleton.ozz"));
graph_context.m_skeleton = &skeleton;
REQUIRE(graph.init(graph_context));
REQUIRE(graph.m_nodes.size() == 5);
REQUIRE(graph.m_nodes[0]->m_node_type_name == "BlendTree");
@@ -97,32 +121,40 @@ TEST_CASE("BasicGraph", "[AnimGraphResource]") {
graph.m_node_output_connections[anim_sampler_index1][0].m_target_node
== blend2_instance);
// Emulate evaluation
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 5);
graph.prepareNodeEval(walk_node_index);
graph.finishNodeEval(walk_node_index);
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 4);
graph.prepareNodeEval(run_node_index);
graph.finishNodeEval(run_node_index);
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 3);
graph.prepareNodeEval(blend_node_index);
CHECK(blend2_instance->i_input0 == anim_sampler_walk->o_output);
CHECK(blend2_instance->i_input1 == anim_sampler_run->o_output);
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 2);
graph.finishNodeEval(blend_node_index);
CHECK(anim_sampler_walk->o_output == nullptr);
CHECK(anim_sampler_run->o_output == nullptr);
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 4);
// Ensure animation sampler nodes use the correct files
REQUIRE(anim_sampler_walk->m_filename == "data/walk.anim.ozz");
REQUIRE(anim_sampler_walk->m_animation != nullptr);
graph.prepareNodeEval(0);
const Socket* graph_output_socket = graph.getOutputSocket("GraphOutput");
CHECK(blend2_instance->o_output == (*graph_output_socket->m_value.ptr_ptr));
AnimData* graph_output =
static_cast<AnimData*>(*graph_output_socket->m_value.ptr_ptr);
graph.finishNodeEval(0);
CHECK(blend2_instance->o_output == nullptr);
CHECK(graph_output == (*graph_output_socket->m_value.ptr_ptr));
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 5);
REQUIRE(anim_sampler_run->m_filename == "data/run.anim.ozz");
REQUIRE(anim_sampler_run->m_animation != nullptr);
WHEN("Emulating Graph Evaluation") {
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 5);
graph.prepareNodeEval(walk_node_index);
graph.finishNodeEval(walk_node_index);
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 4);
graph.prepareNodeEval(run_node_index);
graph.finishNodeEval(run_node_index);
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 3);
graph.prepareNodeEval(blend_node_index);
CHECK(blend2_instance->i_input0 == anim_sampler_walk->o_output);
CHECK(blend2_instance->i_input1 == anim_sampler_run->o_output);
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 2);
graph.finishNodeEval(blend_node_index);
CHECK(anim_sampler_walk->o_output == nullptr);
CHECK(anim_sampler_run->o_output == nullptr);
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 4);
graph.prepareNodeEval(0);
const Socket* graph_output_socket = graph.getOutputSocket("GraphOutput");
CHECK(blend2_instance->o_output == (*graph_output_socket->m_reference.ptr_ptr));
AnimData* graph_output =
static_cast<AnimData*>(*graph_output_socket->m_reference.ptr_ptr);
graph.finishNodeEval(0);
CHECK(blend2_instance->o_output == nullptr);
CHECK(graph_output == (*graph_output_socket->m_reference.ptr_ptr));
CHECK(graph.m_anim_data_work_buffer.m_available_data.size() == 5);
}
}
TEST_CASE("InputAttributeConversion", "[AnimGraphResource]") {
@@ -250,15 +282,17 @@ TEST_CASE("ResourceSaveLoadMathGraphInputs", "[AnimGraphResource]") {
*graph_float_input = 123.456f;
AND_WHEN("Evaluating Graph") {
AnimGraphContext context = {&anim_graph, nullptr};
anim_graph.updateTime(0.f);
anim_graph.evaluate();
anim_graph.evaluate(context);
Socket* float_output_socket =
anim_graph.getOutputSocket("GraphFloatOutput");
Socket* vec3_output_socket =
anim_graph.getOutputSocket("GraphVec3Output");
Vec3& vec3_output =
*static_cast<Vec3*>(vec3_output_socket->m_value.ptr);
*static_cast<Vec3*>(vec3_output_socket->m_reference.ptr);
THEN("output vector components equal the graph input vaulues") {
CHECK(vec3_output[0] == *graph_float_input);
@@ -380,8 +414,10 @@ TEST_CASE("SimpleMathEvaluations", "[AnimGraphResource]") {
*graph_float_input = 123.456f;
AND_WHEN("Evaluating Graph") {
AnimGraphContext context = {&anim_graph, nullptr};
anim_graph.updateTime(0.f);
anim_graph.evaluate();
anim_graph.evaluate(context);
Socket* float0_output_socket =
anim_graph.getOutputSocket("GraphFloat0Output");
@@ -394,9 +430,12 @@ TEST_CASE("SimpleMathEvaluations", "[AnimGraphResource]") {
REQUIRE(float1_output_socket != nullptr);
REQUIRE(float2_output_socket != nullptr);
float& float0_output = *static_cast<float*>(float0_output_socket->m_value.ptr);
float& float1_output = *static_cast<float*>(float1_output_socket->m_value.ptr);
float& float2_output = *static_cast<float*>(float2_output_socket->m_value.ptr);
float& float0_output =
*static_cast<float*>(float0_output_socket->m_reference.ptr);
float& float1_output =
*static_cast<float*>(float1_output_socket->m_reference.ptr);
float& float2_output =
*static_cast<float*>(float2_output_socket->m_reference.ptr);
THEN("output vector components equal the graph input vaulues") {
CHECK(float0_output == Approx(*graph_float_input));
@@ -502,7 +541,7 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") {
dynamic_cast<Blend2Node*>(anim_graph.m_nodes[blend2_node_index]);
REQUIRE(
*anim_graph.m_socket_accessor->m_outputs[0].m_value.ptr_ptr
*anim_graph.m_socket_accessor->m_outputs[0].m_reference.ptr_ptr
== blend2_node->i_blend_weight);
float* float_input_ptr = (float*)anim_graph.getInput("GraphFloatInput");
REQUIRE(float_input_ptr == blend2_node->i_blend_weight);
@@ -640,7 +679,7 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") {
sampler_node
== anim_graph.getAnimNodeForInput(speed_scale_node_index, "Input"));
REQUIRE(speed_scale_node->i_output == blend2_node->i_input1);
REQUIRE(speed_scale_node->o_output == blend2_node->i_input1);
REQUIRE(
speed_scale_node
== anim_graph.getAnimNodeForInput(blend2_node_index, "Input1"));