2022-03-25 11:46:44 +01:00
|
|
|
//
|
|
|
|
// Created by martin on 04.02.22.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef ANIMTESTBED_ANIMGRAPHRESOURCE_H
|
|
|
|
#define ANIMTESTBED_ANIMGRAPHRESOURCE_H
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "SyncTrack.h"
|
|
|
|
#include "AnimGraphData.h"
|
|
|
|
#include "AnimGraphNodes.h"
|
2022-03-25 12:05:56 +01:00
|
|
|
#include "AnimGraph.h"
|
2022-03-25 11:46:44 +01:00
|
|
|
|
|
|
|
struct AnimNode;
|
|
|
|
struct NodeSocketAccessorBase;
|
|
|
|
|
|
|
|
struct AnimNodeResource {
|
|
|
|
std::string m_name;
|
|
|
|
std::string m_type_name;
|
|
|
|
AnimNode* m_anim_node = nullptr;
|
|
|
|
NodeSocketAccessorBase* m_socket_accessor = nullptr;
|
|
|
|
float m_position[2] = {0.f, 0.f};
|
|
|
|
};
|
|
|
|
|
2022-03-25 12:05:56 +01:00
|
|
|
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;
|
|
|
|
}
|
2022-03-25 11:46:44 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// AnimGraphResource
|
|
|
|
//
|
|
|
|
struct AnimGraphConnection {
|
|
|
|
const AnimNodeResource* m_source_node = nullptr;
|
|
|
|
const Socket* m_source_socket = nullptr;
|
|
|
|
const AnimNodeResource* m_target_node = nullptr;
|
|
|
|
const Socket* m_target_socket = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AnimGraphResource {
|
|
|
|
std::string m_name;
|
|
|
|
std::vector<AnimNodeResource> m_nodes;
|
|
|
|
std::vector<AnimGraphConnection> m_connections;
|
|
|
|
|
|
|
|
~AnimGraphResource() {
|
|
|
|
for (size_t i = 0, n = m_nodes.size(); i < n; i++) {
|
|
|
|
delete m_nodes[i].m_anim_node;
|
|
|
|
delete m_nodes[i].m_socket_accessor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AnimGraphResource() { clear(); }
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
void clearNodes();
|
|
|
|
void initGraphConnectors();
|
|
|
|
bool saveToFile(const char* filename) const;
|
|
|
|
bool loadFromFile(const char* filename);
|
|
|
|
|
|
|
|
AnimNodeResource& getGraphOutputNode() { return m_nodes[0]; }
|
|
|
|
AnimNodeResource& getGraphInputNode() { return m_nodes[1]; }
|
|
|
|
|
|
|
|
const AnimNodeResource& getGraphOutputNode() const { return m_nodes[0]; }
|
|
|
|
const AnimNodeResource& getGraphInputNode() const { return m_nodes[1]; }
|
|
|
|
|
|
|
|
size_t getNodeIndex(const AnimNodeResource& node_resource) const {
|
|
|
|
for (size_t i = 0, n = m_nodes.size(); i < n; i++) {
|
|
|
|
if (&m_nodes[i] == &node_resource) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t addNode(AnimNodeResource node_resource) {
|
|
|
|
m_nodes.push_back(node_resource);
|
|
|
|
return m_nodes.size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool connectSockets(
|
|
|
|
const AnimNodeResource& source_node,
|
|
|
|
const std::string& source_socket_name,
|
|
|
|
const AnimNodeResource& target_node,
|
|
|
|
const std::string& target_socket_name) {
|
|
|
|
size_t source_index = -1;
|
|
|
|
size_t target_index = -1;
|
|
|
|
for (size_t i = 0, n = m_nodes.size(); i < n; i++) {
|
|
|
|
if (&source_node == &m_nodes[i]) {
|
|
|
|
source_index = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (&target_node == &m_nodes[i]) {
|
|
|
|
target_index = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source_index < m_nodes.size() && target_index < m_nodes.size()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source_index >= m_nodes.size() || target_index >= m_nodes.size()) {
|
|
|
|
std::cerr << "Cannot connect nodes: could not find nodes." << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket* source_socket =
|
|
|
|
source_node.m_socket_accessor->FindOutputSocket(source_socket_name);
|
|
|
|
Socket* target_socket =
|
|
|
|
target_node.m_socket_accessor->FindInputSocket(target_socket_name);
|
|
|
|
|
|
|
|
if (source_socket == nullptr || target_socket == nullptr) {
|
|
|
|
std::cerr << "Cannot connect nodes: could not find sockets." << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnimGraphConnection connection;
|
|
|
|
connection.m_source_node = &source_node;
|
|
|
|
connection.m_source_socket = source_socket;
|
|
|
|
connection.m_target_node = &target_node;
|
|
|
|
connection.m_target_socket = target_socket;
|
|
|
|
m_connections.push_back(connection);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-25 12:05:56 +01:00
|
|
|
AnimGraph createInstance() {
|
|
|
|
AnimGraph result;
|
2022-03-25 11:46:44 +01:00
|
|
|
|
2022-03-25 12:05:56 +01:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 11:46:44 +01:00
|
|
|
|
2022-03-25 12:05:56 +01:00
|
|
|
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;
|
2022-03-25 11:46:44 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 12:05:56 +01:00
|
|
|
};
|
2022-03-25 11:46:44 +01:00
|
|
|
|
|
|
|
#endif //ANIMTESTBED_ANIMGRAPHRESOURCE_H
|