Refactoring.

AnimGraphEditor
Martin Felis 2022-03-25 12:05:56 +01:00
parent 6c0c0599f8
commit abddbea62b
4 changed files with 260 additions and 255 deletions

View File

@ -5,7 +5,8 @@
#ifndef ANIMTESTBED_ANIMGRAPH_H
#define ANIMTESTBED_ANIMGRAPH_H
#include "AnimGraphResource.h"
#include "AnimGraphData.h"
#include "AnimGraphNodes.h"
//
// AnimGraph (Runtime)
@ -87,205 +88,6 @@ struct AnimGraph {
return nullptr;
}
static AnimGraph createFromResource(const AnimGraphResource& resource) {
AnimGraph result;
// create nodes
for (int i = 0; i < resource.m_nodes.size(); i++) {
const AnimNodeResource& node_resource = resource.m_nodes[i];
AnimNode* node = AnimNodeFactory(node_resource.m_type_name.c_str());
node->m_name = node_resource.m_name;
node->m_node_type_name = node_resource.m_type_name;
node->m_index = i;
result.m_nodes.push_back(node);
assert(node_resource.m_socket_accessor != nullptr);
result.m_node_inputs.push_back(std::vector<NodeInput>());
std::vector<NodeInput>& node_inputs = result.m_node_inputs.back();
for (int j = 0, n = node_resource.m_socket_accessor->m_inputs.size();
j < n;
j++) {
const Socket& input_socket =
node_resource.m_socket_accessor->m_inputs[j];
NodeInput input;
input.m_node = nullptr;
input.m_type = input_socket.m_type;
input.m_input_name = input_socket.m_name;
node_inputs.push_back(input);
}
}
// Prepare graph inputs
result.m_socket_accessor =
AnimNodeAccessorFactory("BlendTree", result.m_nodes[0]);
result.m_socket_accessor->m_outputs =
resource.m_nodes[1].m_socket_accessor->m_outputs;
result.m_socket_accessor->m_inputs =
resource.m_nodes[0].m_socket_accessor->m_inputs;
// inputs
int input_block_size = 0;
std::vector<Socket>& graph_inputs = result.getGraphInputs();
for (int i = 0; i < graph_inputs.size(); i++) {
input_block_size += sizeof(void*);
}
result.m_input_buffer = new char[input_block_size];
memset(result.m_input_buffer, 0, input_block_size);
int input_block_offset = 0;
for (int i = 0; i < graph_inputs.size(); i++) {
if (graph_inputs[i].m_type == SocketType::SocketTypeAnimation) {
}
graph_inputs[i].m_value.ptr =
(void*)&result.m_input_buffer[input_block_offset];
input_block_offset += sizeof(void*);
}
// outputs
int output_block_size = 0;
std::vector<Socket>& graph_outputs = result.getGraphOutputs();
for (int i = 0; i < graph_outputs.size(); i++) {
output_block_size += graph_outputs[i].m_type_size;
}
result.m_output_buffer = new char[output_block_size];
memset(result.m_output_buffer, 0, output_block_size);
int output_block_offset = 0;
for (int i = 0; i < graph_outputs.size(); i++) {
if (graph_outputs[i].m_type == SocketType::SocketTypeAnimation) {
}
graph_outputs[i].m_value.ptr =
(void*)&result.m_output_buffer[output_block_offset];
output_block_offset += graph_outputs[i].m_type_size;
}
// connect the nodes
for (int i = 0; i < resource.m_connections.size(); i++) {
const AnimGraphConnection& connection = resource.m_connections[i];
std::string source_node_type = "";
std::string target_node_type = "";
std::string source_node_name = "";
std::string target_node_name = "";
AnimNode* source_node = nullptr;
AnimNode* target_node = nullptr;
NodeSocketAccessorBase* source_node_accessor = nullptr;
NodeSocketAccessorBase* target_node_accessor = nullptr;
SocketType source_type;
SocketType target_type;
size_t source_socket_index = -1;
size_t target_socket_index = -1;
if (connection.m_source_node != nullptr) {
size_t node_index = resource.getNodeIndex(*connection.m_source_node);
if (node_index == -1) {
std::cerr << "Could not find source node index." << std::endl;
continue;
}
source_node = result.m_nodes[node_index];
source_node_name = source_node->m_name;
source_node_type = source_node->m_node_type_name;
if (node_index == 1) {
source_node_accessor = result.m_socket_accessor;
} else {
source_node_accessor =
AnimNodeAccessorFactory(source_node_type, source_node);
}
}
if (connection.m_target_node != nullptr) {
size_t node_index = resource.getNodeIndex(*connection.m_target_node);
if (node_index == -1) {
std::cerr << "Could not find source node index." << std::endl;
continue;
}
target_node = result.m_nodes[node_index];
target_node_name = target_node->m_name;
target_node_type = target_node->m_node_type_name;
if (node_index == 0) {
target_node_accessor = result.m_socket_accessor;
} else {
target_node_accessor =
AnimNodeAccessorFactory(target_node_type, target_node);
}
}
assert(source_node != nullptr);
assert(target_node != nullptr);
//
// Map resource node sockets to graph instance node sockets
//
if (connection.m_source_socket == nullptr) {
std::cerr << "Invalid source socket for connection " << i << "."
<< std::endl;
continue;
}
if (connection.m_target_socket == nullptr) {
std::cerr << "Invalid source socket for connection " << i << "."
<< std::endl;
continue;
}
source_socket_index = source_node_accessor->GetOutputIndex(
connection.m_source_socket->m_name);
if (source_socket_index == -1) {
std::cerr << "Invalid source socket "
<< connection.m_source_socket->m_name << " for node "
<< connection.m_source_node->m_name << "." << std::endl;
continue;
}
const Socket* source_socket =
&source_node_accessor->m_outputs[source_socket_index];
target_socket_index = target_node_accessor->GetInputIndex(
connection.m_target_socket->m_name);
if (target_socket_index == -1) {
std::cerr << "Invalid target socket "
<< connection.m_target_socket->m_name << " for node "
<< connection.m_target_node->m_name << "." << std::endl;
continue;
}
const Socket* target_socket =
&target_node_accessor->m_inputs[target_socket_index];
if (source_socket->m_type != target_socket->m_type) {
std::cerr << "Cannot connect sockets: invalid types!" << std::endl;
}
//
// Wire up outputs to inputs.
//
(*source_socket->m_value.ptr_ptr) = target_socket->m_value.ptr;
size_t target_node_index = target_node->m_index;
std::vector<NodeInput>& node_inputs =
result.m_node_inputs[target_node_index];
for (int j = 0, n = node_inputs.size(); j < n; j++) {
if (node_inputs[j].m_input_name == target_socket->m_name) {
node_inputs[j].m_node = source_node;
}
}
if (target_node_accessor != result.m_socket_accessor) {
delete target_node_accessor;
}
if (source_node_accessor != result.m_socket_accessor) {
delete source_node_accessor;
}
}
result.updateOrderedNodes();
result.reset();
return result;
}
};
#endif //ANIMTESTBED_ANIMGRAPH_H

View File

@ -205,4 +205,44 @@ struct NodeSocketAccessor<AnimSamplerNode> : public NodeSocketAccessorBase {
}
};
static inline AnimNode* AnimNodeFactory(const std::string& name) {
AnimNode* result;
if (name == "Blend2") {
result = new Blend2Node;
} else if (name == "SpeedScale") {
result = new SpeedScaleNode;
} else if (name == "AnimSampler") {
result = new AnimSamplerNode;
} else if (name == "BlendTree") {
result = new BlendTreeNode;
}
if (result != nullptr) {
result->m_node_type_name = name;
return result;
}
std::cerr << "Invalid node type: " << name << std::endl;
return nullptr;
}
static inline NodeSocketAccessorBase* AnimNodeAccessorFactory(
const std::string& node_type_name,
AnimNode* node) {
if (node_type_name == "Blend2") {
return new NodeSocketAccessor<Blend2Node>(node);
} else if (node_type_name == "SpeedScale") {
return new NodeSocketAccessor<SpeedScaleNode>(node);
} else if (node_type_name == "AnimSampler") {
return new NodeSocketAccessor<AnimSamplerNode>(node);
} else if (node_type_name == "BlendTree") {
return new NodeSocketAccessor<BlendTreeNode>(node);
} else {
std::cerr << "Invalid node type name " << node_type_name << "."
<< std::endl;
}
return nullptr;
}
#endif //ANIMTESTBED_ANIMGRAPHNODES_H

View File

@ -15,6 +15,7 @@
#include "SyncTrack.h"
#include "AnimGraphData.h"
#include "AnimGraphNodes.h"
#include "AnimGraph.h"
struct AnimNode;
struct NodeSocketAccessorBase;
@ -27,7 +28,15 @@ struct AnimNodeResource {
float m_position[2] = {0.f, 0.f};
};
static inline AnimNodeResource AnimNodeResourceFactory(
const std::string& node_type_name) {
AnimNodeResource result;
result.m_type_name = node_type_name;
result.m_anim_node = AnimNodeFactory(node_type_name);
result.m_socket_accessor =
AnimNodeAccessorFactory(node_type_name, result.m_anim_node);
return result;
}
//
// AnimGraphResource
@ -125,55 +134,207 @@ struct AnimGraphResource {
return true;
}
};
static inline AnimNode* AnimNodeFactory(const std::string& name) {
AnimNode* result;
if (name == "Blend2") {
result = new Blend2Node;
} else if (name == "SpeedScale") {
result = new SpeedScaleNode;
} else if (name == "AnimSampler") {
result = new AnimSamplerNode;
} else if (name == "BlendTree") {
result = new BlendTreeNode;
}
if (result != nullptr) {
result->m_node_type_name = name;
AnimGraph createInstance() {
AnimGraph result;
// create nodes
for (int i = 0; i < m_nodes.size(); i++) {
const AnimNodeResource& node_resource = m_nodes[i];
AnimNode* node = AnimNodeFactory(node_resource.m_type_name.c_str());
node->m_name = node_resource.m_name;
node->m_node_type_name = node_resource.m_type_name;
node->m_index = i;
result.m_nodes.push_back(node);
assert(node_resource.m_socket_accessor != nullptr);
result.m_node_inputs.push_back(std::vector<NodeInput>());
std::vector<NodeInput>& node_inputs = result.m_node_inputs.back();
for (int j = 0, n = node_resource.m_socket_accessor->m_inputs.size();
j < n;
j++) {
const Socket& input_socket =
node_resource.m_socket_accessor->m_inputs[j];
NodeInput input;
input.m_node = nullptr;
input.m_type = input_socket.m_type;
input.m_input_name = input_socket.m_name;
node_inputs.push_back(input);
}
}
// Prepare graph inputs
result.m_socket_accessor =
AnimNodeAccessorFactory("BlendTree", result.m_nodes[0]);
result.m_socket_accessor->m_outputs =
m_nodes[1].m_socket_accessor->m_outputs;
result.m_socket_accessor->m_inputs =
m_nodes[0].m_socket_accessor->m_inputs;
// inputs
int input_block_size = 0;
std::vector<Socket>& graph_inputs = result.getGraphInputs();
for (int i = 0; i < graph_inputs.size(); i++) {
input_block_size += sizeof(void*);
}
result.m_input_buffer = new char[input_block_size];
memset(result.m_input_buffer, 0, input_block_size);
int input_block_offset = 0;
for (int i = 0; i < graph_inputs.size(); i++) {
if (graph_inputs[i].m_type == SocketType::SocketTypeAnimation) {
}
graph_inputs[i].m_value.ptr =
(void*)&result.m_input_buffer[input_block_offset];
input_block_offset += sizeof(void*);
}
// outputs
int output_block_size = 0;
std::vector<Socket>& graph_outputs = result.getGraphOutputs();
for (int i = 0; i < graph_outputs.size(); i++) {
output_block_size += graph_outputs[i].m_type_size;
}
result.m_output_buffer = new char[output_block_size];
memset(result.m_output_buffer, 0, output_block_size);
int output_block_offset = 0;
for (int i = 0; i < graph_outputs.size(); i++) {
if (graph_outputs[i].m_type == SocketType::SocketTypeAnimation) {
}
graph_outputs[i].m_value.ptr =
(void*)&result.m_output_buffer[output_block_offset];
output_block_offset += graph_outputs[i].m_type_size;
}
// connect the nodes
for (int i = 0; i < m_connections.size(); i++) {
const AnimGraphConnection& connection = m_connections[i];
std::string source_node_type = "";
std::string target_node_type = "";
std::string source_node_name = "";
std::string target_node_name = "";
AnimNode* source_node = nullptr;
AnimNode* target_node = nullptr;
NodeSocketAccessorBase* source_node_accessor = nullptr;
NodeSocketAccessorBase* target_node_accessor = nullptr;
SocketType source_type;
SocketType target_type;
size_t source_socket_index = -1;
size_t target_socket_index = -1;
if (connection.m_source_node != nullptr) {
size_t node_index = getNodeIndex(*connection.m_source_node);
if (node_index == -1) {
std::cerr << "Could not find source node index." << std::endl;
continue;
}
source_node = result.m_nodes[node_index];
source_node_name = source_node->m_name;
source_node_type = source_node->m_node_type_name;
if (node_index == 1) {
source_node_accessor = result.m_socket_accessor;
} else {
source_node_accessor =
AnimNodeAccessorFactory(source_node_type, source_node);
}
}
if (connection.m_target_node != nullptr) {
size_t node_index = getNodeIndex(*connection.m_target_node);
if (node_index == -1) {
std::cerr << "Could not find source node index." << std::endl;
continue;
}
target_node = result.m_nodes[node_index];
target_node_name = target_node->m_name;
target_node_type = target_node->m_node_type_name;
if (node_index == 0) {
target_node_accessor = result.m_socket_accessor;
} else {
target_node_accessor =
AnimNodeAccessorFactory(target_node_type, target_node);
}
}
assert(source_node != nullptr);
assert(target_node != nullptr);
//
// Map resource node sockets to graph instance node sockets
//
if (connection.m_source_socket == nullptr) {
std::cerr << "Invalid source socket for connection " << i << "."
<< std::endl;
continue;
}
if (connection.m_target_socket == nullptr) {
std::cerr << "Invalid source socket for connection " << i << "."
<< std::endl;
continue;
}
source_socket_index = source_node_accessor->GetOutputIndex(
connection.m_source_socket->m_name);
if (source_socket_index == -1) {
std::cerr << "Invalid source socket "
<< connection.m_source_socket->m_name << " for node "
<< connection.m_source_node->m_name << "." << std::endl;
continue;
}
const Socket* source_socket =
&source_node_accessor->m_outputs[source_socket_index];
target_socket_index = target_node_accessor->GetInputIndex(
connection.m_target_socket->m_name);
if (target_socket_index == -1) {
std::cerr << "Invalid target socket "
<< connection.m_target_socket->m_name << " for node "
<< connection.m_target_node->m_name << "." << std::endl;
continue;
}
const Socket* target_socket =
&target_node_accessor->m_inputs[target_socket_index];
if (source_socket->m_type != target_socket->m_type) {
std::cerr << "Cannot connect sockets: invalid types!" << std::endl;
}
//
// Wire up outputs to inputs.
//
(*source_socket->m_value.ptr_ptr) = target_socket->m_value.ptr;
size_t target_node_index = target_node->m_index;
std::vector<NodeInput>& node_inputs =
result.m_node_inputs[target_node_index];
for (int j = 0, n = node_inputs.size(); j < n; j++) {
if (node_inputs[j].m_input_name == target_socket->m_name) {
node_inputs[j].m_node = source_node;
}
}
if (target_node_accessor != result.m_socket_accessor) {
delete target_node_accessor;
}
if (source_node_accessor != result.m_socket_accessor) {
delete source_node_accessor;
}
}
result.updateOrderedNodes();
result.reset();
return result;
}
std::cerr << "Invalid node type: " << name << std::endl;
return nullptr;
}
static inline NodeSocketAccessorBase* AnimNodeAccessorFactory(
const std::string& node_type_name,
AnimNode* node) {
if (node_type_name == "Blend2") {
return new NodeSocketAccessor<Blend2Node>(node);
} else if (node_type_name == "SpeedScale") {
return new NodeSocketAccessor<SpeedScaleNode>(node);
} else if (node_type_name == "AnimSampler") {
return new NodeSocketAccessor<AnimSamplerNode>(node);
} else if (node_type_name == "BlendTree") {
return new NodeSocketAccessor<BlendTreeNode>(node);
} else {
std::cerr << "Invalid node type name " << node_type_name << "."
<< std::endl;
}
return nullptr;
}
static inline AnimNodeResource AnimNodeResourceFactory(
const std::string& node_type_name) {
AnimNodeResource result;
result.m_type_name = node_type_name;
result.m_anim_node = AnimNodeFactory(node_type_name);
result.m_socket_accessor =
AnimNodeAccessorFactory(node_type_name, result.m_anim_node);
return result;
}
};
#endif //ANIMTESTBED_ANIMGRAPHRESOURCE_H

View File

@ -65,7 +65,7 @@ TEST_CASE("BasicGraph", "[AnimGraphResource]") {
graph_resource.saveToFile("WalkGraph.animgraph.json");
AnimGraph graph = AnimGraph::createFromResource(graph_resource);
AnimGraph graph = graph_resource.createInstance();
REQUIRE(graph.m_nodes.size() == 5);
REQUIRE(graph.m_nodes[0]->m_node_type_name == "BlendTree");
@ -192,8 +192,8 @@ TEST_CASE("ResourceSaveLoadGraphInputs", "[AnimGraphResource]") {
}
WHEN("Instantiating an AnimGraph") {
AnimGraph anim_graph =
AnimGraph::createFromResource(graph_resource_loaded);
AnimGraph anim_graph = graph_resource_loaded.createInstance();
REQUIRE(
anim_graph.getOutput("GraphOutput") == anim_graph.m_output_buffer);
REQUIRE(
@ -216,7 +216,7 @@ TEST_CASE("ResourceSaveLoadGraphInputs", "[AnimGraphResource]") {
graph_output_node,
"GraphOutput"));
AnimGraph anim_graph = AnimGraph::createFromResource(graph_resource_origin);
AnimGraph anim_graph = graph_resource_origin.createInstance();
void* graph_anim_input_ptr = anim_graph.getInput("GraphAnimInput");
void* graph_output_ptr = anim_graph.getOutput("GraphOutput");
@ -265,7 +265,7 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") {
graph_resource.getGraphOutputNode(),
"GraphFloatOutput"));
AnimGraph anim_graph = AnimGraph::createFromResource(graph_resource);
AnimGraph anim_graph = graph_resource.createInstance();
THEN("Writing to the input pointer changes the value of the output.") {
float* float_input_ptr = (float*)anim_graph.getInput("GraphFloatInput");
@ -292,7 +292,8 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") {
"Weight"));
THEN("Connected float input points to the blend weight.") {
AnimGraph anim_graph = AnimGraph::createFromResource(graph_resource);
AnimGraph anim_graph = graph_resource.createInstance();
Blend2Node* blend2_node =
dynamic_cast<Blend2Node*>(anim_graph.m_nodes[blend2_node_index]);
@ -327,7 +328,8 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") {
THEN(
"AnimData from output gets blended and result is written to "
"Output.") {
AnimGraph anim_graph = AnimGraph::createFromResource(graph_resource);
AnimGraph anim_graph = graph_resource.createInstance();
Blend2Node* blend2_node =
dynamic_cast<Blend2Node*>(anim_graph.m_nodes[blend2_node_index]);
@ -407,7 +409,7 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") {
"GraphAnimOutput"));
THEN("Data flow and node ordering must be correct.") {
AnimGraph anim_graph = AnimGraph::createFromResource(graph_resource);
AnimGraph anim_graph = graph_resource.createInstance();
Blend2Node* blend2_node =
dynamic_cast<Blend2Node*>(anim_graph.m_nodes[blend2_node_index]);
SpeedScaleNode* speed_scale_node = dynamic_cast<SpeedScaleNode*>(
@ -461,7 +463,7 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") {
}
WHEN("Instantiating graph") {
AnimGraph anim_graph = AnimGraph::createFromResource(graph_resource);
AnimGraph anim_graph = graph_resource.createInstance();
float* blend_weight_input =
reinterpret_cast<float*>(anim_graph.getInput("GraphFloatInput"));