Removed old animation graph code.
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
//
|
||||
// Created by martin on 25.03.22.
|
||||
//
|
||||
|
||||
#include "AnimGraph.h"
|
||||
|
||||
void AnimGraph::updateOrderedNodes() {
|
||||
std::vector<int> node_index_stack;
|
||||
node_index_stack.push_back(0);
|
||||
|
||||
m_eval_ordered_nodes.clear();
|
||||
|
||||
while (node_index_stack.size() > 0) {
|
||||
std::vector<NodeInput>& node_inputs =
|
||||
m_node_inputs[node_index_stack.back()];
|
||||
node_index_stack.pop_back();
|
||||
|
||||
for (size_t i = 0, n = node_inputs.size(); i < n; i++) {
|
||||
AnimNode* input_node = node_inputs[i].m_node;
|
||||
if (input_node == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int input_node_index = input_node->m_index;
|
||||
bool is_node_processed = false;
|
||||
for (size_t j = 0, m = m_eval_ordered_nodes.size(); j < m; j++) {
|
||||
if (m_eval_ordered_nodes[j] == input_node) {
|
||||
is_node_processed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_node_processed) {
|
||||
continue;
|
||||
}
|
||||
|
||||
m_eval_ordered_nodes.push_back(input_node);
|
||||
node_index_stack.push_back(input_node_index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AnimGraph::markActiveNodes() {
|
||||
for (size_t i = 0, n = m_nodes.size(); i < n; i++) {
|
||||
m_nodes[i]->m_state = AnimNodeEvalState::Deactivated;
|
||||
}
|
||||
|
||||
const std::vector<NodeInput> graph_output_inputs = m_node_inputs[0];
|
||||
for (size_t i = 0, n = graph_output_inputs.size(); i < n; i++) {
|
||||
const NodeInput& graph_input = graph_output_inputs[i];
|
||||
AnimNode* node = graph_input.m_node;
|
||||
if (node != nullptr) {
|
||||
node->m_state = AnimNodeEvalState::Activated;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0, n = m_eval_ordered_nodes.size(); i < n; i++) {
|
||||
AnimNode* node = m_eval_ordered_nodes[i];
|
||||
if (checkIsNodeActive(node)) {
|
||||
int node_index = node->m_index;
|
||||
node->MarkActiveInputs(m_node_inputs[node_index]);
|
||||
|
||||
// Non-animation data inputs are always active.
|
||||
for (size_t j = 0, nj = m_node_inputs[node_index].size(); j < nj; j++) {
|
||||
const NodeInput& input = m_node_inputs[node_index][j];
|
||||
if (input.m_node != nullptr && input.m_type != SocketType::SocketTypeAnimation) {
|
||||
input.m_node->m_state = AnimNodeEvalState::Activated;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AnimGraph::evalSyncTracks() {
|
||||
for (size_t i = m_eval_ordered_nodes.size() - 1; i >= 0; i--) {
|
||||
AnimNode* node = m_eval_ordered_nodes[i];
|
||||
int node_index = node->m_index;
|
||||
if (node->m_state == AnimNodeEvalState::Deactivated) {
|
||||
continue;
|
||||
}
|
||||
|
||||
node->CalcSyncTrack(m_node_inputs[node_index]);
|
||||
}
|
||||
}
|
||||
|
||||
void AnimGraph::updateTime(float dt) {
|
||||
const std::vector<NodeInput> graph_output_inputs = m_node_inputs[0];
|
||||
for (size_t i = 0, n = graph_output_inputs.size(); i < n; i++) {
|
||||
AnimNode* node = m_eval_ordered_nodes[i];
|
||||
if (node != nullptr) {
|
||||
node->UpdateTime(node->m_time_now, node->m_time_now + dt);
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0, n = m_eval_ordered_nodes.size(); i < n; i++) {
|
||||
AnimNode* node = m_eval_ordered_nodes[i];
|
||||
if (node->m_state != AnimNodeEvalState::TimeUpdated) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int node_index = node->m_index;
|
||||
const std::vector<NodeInput> node_inputs =
|
||||
m_node_inputs[node_index];
|
||||
float node_time_now = node->m_time_now;
|
||||
float node_time_last = node->m_time_last;
|
||||
|
||||
for (size_t i = 0, n = node_inputs.size(); i < n; i++) {
|
||||
AnimNode* input_node = node_inputs[i].m_node;
|
||||
|
||||
// Only propagate time updates via animation sockets.
|
||||
if (input_node != nullptr
|
||||
&& node_inputs[i].m_type == SocketType::SocketTypeAnimation
|
||||
&& input_node->m_state == AnimNodeEvalState::Activated) {
|
||||
input_node->UpdateTime(node_time_last, node_time_now);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AnimGraph::evaluate() {
|
||||
for (size_t i = m_eval_ordered_nodes.size() - 1; i >= 0; i--) {
|
||||
AnimNode* node = m_eval_ordered_nodes[i];
|
||||
if (node->m_state == AnimNodeEvalState::Deactivated) {
|
||||
continue;
|
||||
}
|
||||
|
||||
node->Evaluate();
|
||||
}
|
||||
}
|
||||
|
||||
void* AnimGraph::getOutput(const std::string& name) const {
|
||||
Socket* socket = m_socket_accessor->FindInputSocket(name);
|
||||
if (socket == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return socket->m_value.ptr;
|
||||
}
|
||||
|
||||
void* AnimGraph::getInput(const std::string& name) const {
|
||||
Socket* socket = m_socket_accessor->FindOutputSocket(name);
|
||||
if (socket == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return *(socket->m_value.ptr_ptr);
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
//
|
||||
// Created by martin on 25.03.22.
|
||||
//
|
||||
|
||||
#ifndef ANIMTESTBED_ANIMGRAPH_H
|
||||
#define ANIMTESTBED_ANIMGRAPH_H
|
||||
|
||||
#include "AnimGraphResource.h"
|
||||
|
||||
//
|
||||
// AnimGraph (Runtime)
|
||||
//
|
||||
struct AnimGraph {
|
||||
AnimData m_local_transforms;
|
||||
|
||||
std::vector<AnimNode*> m_nodes;
|
||||
std::vector<AnimNode*> m_eval_ordered_nodes;
|
||||
std::vector<std::vector<NodeInput> > m_node_inputs;
|
||||
NodeSocketAccessorBase* m_socket_accessor;
|
||||
char* m_input_buffer = nullptr;
|
||||
char* m_output_buffer = nullptr;
|
||||
|
||||
std::vector<Socket>& getGraphOutputs() { return m_socket_accessor->m_inputs; }
|
||||
std::vector<Socket>& getGraphInputs() { return m_socket_accessor->m_outputs; }
|
||||
|
||||
~AnimGraph() {
|
||||
delete[] m_input_buffer;
|
||||
delete[] m_output_buffer;
|
||||
|
||||
for (int i = 0; i < m_nodes.size(); i++) {
|
||||
delete m_nodes[i];
|
||||
}
|
||||
|
||||
delete m_socket_accessor;
|
||||
}
|
||||
|
||||
void updateOrderedNodes();
|
||||
void markActiveNodes();
|
||||
bool checkIsNodeActive(AnimNode* node) {
|
||||
return node->m_state != AnimNodeEvalState::Deactivated;
|
||||
}
|
||||
void evalSyncTracks();
|
||||
void updateTime(float dt);
|
||||
void evaluate();
|
||||
void reset() {
|
||||
for (size_t i = 0, n = m_nodes.size(); i < n; i++) {
|
||||
m_nodes[i]->m_time_now = 0.f;
|
||||
m_nodes[i]->m_time_last = 0.f;
|
||||
m_nodes[i]->m_state = AnimNodeEvalState::Undefined;
|
||||
}
|
||||
}
|
||||
|
||||
void* getOutput(const std::string& name) const;
|
||||
void* getInput(const std::string& name) const;
|
||||
|
||||
int getNodeEvalOrderIndex(const AnimNode* node) {
|
||||
for (size_t i = 0, n = m_eval_ordered_nodes.size(); i < n; i++) {
|
||||
if (m_eval_ordered_nodes[i] == node) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
AnimNode* getAnimNodeForInput(
|
||||
size_t node_index,
|
||||
const std::string& input_name) {
|
||||
assert(node_index < m_nodes.size());
|
||||
assert(node_index < m_node_inputs.size());
|
||||
|
||||
std::vector<NodeInput>& node_inputs = m_node_inputs[node_index];
|
||||
for (size_t i = 0, n = node_inputs.size(); i < n; i++) {
|
||||
if (node_inputs[i].m_input_name == input_name) {
|
||||
return node_inputs[i].m_node;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
AnimNode* getAnimNode(const char* name) {
|
||||
for (size_t i = 0; i < m_nodes.size(); i++) {
|
||||
if (m_nodes[i]->m_name == name) {
|
||||
return m_nodes[i];
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by martin on 25.03.22.
|
||||
//
|
||||
|
||||
#include "AnimGraphData.h"
|
||||
@@ -0,0 +1,244 @@
|
||||
//
|
||||
// Created by martin on 25.03.22.
|
||||
//
|
||||
|
||||
#ifndef ANIMTESTBED_ANIMGRAPHDATA_H
|
||||
#define ANIMTESTBED_ANIMGRAPHDATA_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#include "SyncTrack.h"
|
||||
|
||||
//
|
||||
// Data types
|
||||
//
|
||||
|
||||
struct AnimData {
|
||||
float m_bone_transforms[16];
|
||||
};
|
||||
|
||||
typedef float Vec3[3];
|
||||
typedef float Quat[4];
|
||||
|
||||
enum class SocketType {
|
||||
SocketTypeUndefined = 0,
|
||||
SocketTypeBool,
|
||||
SocketTypeAnimation,
|
||||
SocketTypeFloat,
|
||||
SocketTypeVec3,
|
||||
SocketTypeQuat,
|
||||
SocketTypeString,
|
||||
SocketTypeLast
|
||||
};
|
||||
|
||||
static const char* SocketTypeNames[] =
|
||||
{"", "Bool", "Animation", "Float", "Vec3", "Quat", "String"};
|
||||
|
||||
enum SocketFlags { SocketFlagAffectsTime = 1 };
|
||||
|
||||
struct Socket {
|
||||
std::string m_name;
|
||||
SocketType m_type = SocketType::SocketTypeUndefined;
|
||||
union SocketValue {
|
||||
void* ptr;
|
||||
void** ptr_ptr;
|
||||
};
|
||||
SocketValue m_value = {nullptr};
|
||||
int m_flags = 0;
|
||||
size_t m_type_size = 0;
|
||||
};
|
||||
|
||||
struct NodeSocketAccessorBase {
|
||||
std::vector<Socket> m_properties;
|
||||
std::vector<Socket> m_inputs;
|
||||
std::vector<Socket> m_outputs;
|
||||
|
||||
NodeSocketAccessorBase() {}
|
||||
virtual ~NodeSocketAccessorBase() {}
|
||||
|
||||
virtual void UpdateFlags(){};
|
||||
|
||||
Socket* FindSocket(std::vector<Socket>& sockets, const std::string& name) {
|
||||
Socket* result = nullptr;
|
||||
for (size_t i = 0, n = sockets.size(); i < n; i++) {
|
||||
if (sockets[i].m_name == name) {
|
||||
result = &sockets[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const Socket* FindSocket(
|
||||
const std::vector<Socket>& sockets,
|
||||
const std::string& name) const {
|
||||
const Socket* result = nullptr;
|
||||
for (size_t i = 0, n = sockets.size(); i < n; i++) {
|
||||
if (sockets[i].m_name == name) {
|
||||
result = &sockets[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
SocketType GetSocketType(
|
||||
const std::vector<Socket>& sockets,
|
||||
const std::string& name) {
|
||||
const Socket* socket = FindSocket(sockets, name);
|
||||
if (socket == nullptr) {
|
||||
return SocketType::SocketTypeUndefined;
|
||||
}
|
||||
return socket->m_type;
|
||||
}
|
||||
|
||||
size_t GetSocketIndex(
|
||||
const std::vector<Socket>& sockets,
|
||||
const std::string& name) const {
|
||||
for (size_t i = 0, n = sockets.size(); i < n; i++) {
|
||||
if (sockets[i].m_name == name) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T GetSocketValue(
|
||||
const std::vector<Socket>& sockets,
|
||||
const std::string& name,
|
||||
T default_value) {
|
||||
const Socket* socket = FindSocket(sockets, name);
|
||||
if (socket == nullptr) {
|
||||
return default_value;
|
||||
}
|
||||
|
||||
return *static_cast<T*>(socket->m_value.ptr);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void SetSocketValue(
|
||||
const std::vector<Socket>& sockets,
|
||||
const std::string& name,
|
||||
const T& value) {
|
||||
const Socket* socket = FindSocket(sockets, name);
|
||||
if (socket == nullptr) {
|
||||
std::cerr << "Error: could not set value of socket with name " << name
|
||||
<< ": no socket found." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
*static_cast<T*>(socket->m_value.ptr) = value;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool RegisterSocket(
|
||||
std::vector<Socket>& sockets,
|
||||
const std::string& name,
|
||||
T* value_ptr,
|
||||
int flags = 0) {
|
||||
Socket* socket = FindSocket(sockets, name);
|
||||
if (socket != nullptr) {
|
||||
std::cerr << "Socket " << name << " already registered." << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
sockets.push_back(Socket());
|
||||
socket = &sockets[sockets.size() - 1];
|
||||
socket->m_name = name;
|
||||
socket->m_type_size = sizeof(T);
|
||||
socket->m_flags = flags;
|
||||
|
||||
if constexpr (std::is_same<T, float>::value) {
|
||||
socket->m_type = SocketType::SocketTypeFloat;
|
||||
} else if constexpr (std::is_same<T, bool>::value) {
|
||||
socket->m_type = SocketType::SocketTypeBool;
|
||||
} else if constexpr (std::is_same<T, Vec3>::value) {
|
||||
socket->m_type = SocketType::SocketTypeVec3;
|
||||
} else if constexpr (std::is_same<T, Quat>::value) {
|
||||
socket->m_type = SocketType::SocketTypeQuat;
|
||||
} else if constexpr (std::is_same<T, AnimData>::value) {
|
||||
socket->m_type = SocketType::SocketTypeAnimation;
|
||||
} else if constexpr (std::is_same<T, std::string>::value) {
|
||||
socket->m_type = SocketType::SocketTypeString;
|
||||
} else if constexpr (std::is_same<T, float*>::value) {
|
||||
socket->m_type = SocketType::SocketTypeFloat;
|
||||
} else if constexpr (std::is_same<T, bool*>::value) {
|
||||
socket->m_type = SocketType::SocketTypeBool;
|
||||
} else if constexpr (std::is_same<T, Vec3*>::value) {
|
||||
socket->m_type = SocketType::SocketTypeVec3;
|
||||
} else if constexpr (std::is_same<T, Quat*>::value) {
|
||||
socket->m_type = SocketType::SocketTypeQuat;
|
||||
} else if constexpr (std::is_same<T, AnimData*>::value) {
|
||||
socket->m_type = SocketType::SocketTypeAnimation;
|
||||
} else if constexpr (std::is_same<T, std::string*>::value) {
|
||||
socket->m_type = SocketType::SocketTypeString;
|
||||
} else {
|
||||
std::cerr << "Cannot register socket, invalid type." << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
socket->m_value.ptr = value_ptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool RegisterProperty(const std::string& name, T* value) {
|
||||
return RegisterSocket(m_properties, name, value);
|
||||
}
|
||||
template <typename T>
|
||||
void SetProperty(const std::string& name, const T& value) {
|
||||
SetSocketValue(m_properties, name, value);
|
||||
}
|
||||
template <typename T>
|
||||
T GetProperty(const std::string& name, T default_value) {
|
||||
return GetSocketValue(m_properties, name, default_value);
|
||||
}
|
||||
SocketType GetPropertyType(const std::string& name) {
|
||||
return GetSocketType(m_properties, name);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool RegisterInput(const std::string& name, T* value, int flags = 0) {
|
||||
return RegisterSocket(m_inputs, name, value, flags);
|
||||
}
|
||||
template <typename T>
|
||||
T* GetInput(const std::string& name, T* value) {
|
||||
return GetSocketValue(m_inputs, name, value);
|
||||
}
|
||||
Socket* FindInputSocket(const std::string& name) {
|
||||
return FindSocket(m_inputs, name);
|
||||
}
|
||||
SocketType GetInputType(const std::string& name) {
|
||||
return GetSocketType(m_inputs, name);
|
||||
}
|
||||
size_t GetInputIndex(const std::string& name) {
|
||||
return GetSocketIndex(m_inputs, name);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool RegisterOutput(const std::string& name, T** value, int flags = 0) {
|
||||
return RegisterSocket(m_outputs, name, value, flags);
|
||||
}
|
||||
SocketType GetOutputType(const std::string& name) {
|
||||
return GetSocketType(m_outputs, name);
|
||||
}
|
||||
Socket* FindOutputSocket(const std::string& name) {
|
||||
return FindSocket(m_outputs, name);
|
||||
}
|
||||
size_t GetOutputIndex(const std::string& name) {
|
||||
return GetSocketIndex(m_outputs, name);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct NodeSocketAccessor : public NodeSocketAccessorBase {
|
||||
virtual ~NodeSocketAccessor() {}
|
||||
};
|
||||
|
||||
#endif //ANIMTESTBED_ANIMGRAPHDATA_H
|
||||
@@ -0,0 +1,394 @@
|
||||
//
|
||||
// Created by martin on 11.02.22.
|
||||
//
|
||||
|
||||
#include "AnimGraphEditor.h"
|
||||
|
||||
#include "AnimGraphResource.h"
|
||||
#include "imgui.h"
|
||||
#include "imnodes.h"
|
||||
#include "misc/cpp/imgui_stdlib.h"
|
||||
|
||||
ImNodesPinShape sGetSocketShapeFromSocketType(const SocketType& socket_type) {
|
||||
switch (socket_type) {
|
||||
case SocketType::SocketTypeAnimation:
|
||||
return ImNodesPinShape_QuadFilled;
|
||||
case SocketType::SocketTypeFloat:
|
||||
return ImNodesPinShape_CircleFilled;
|
||||
case SocketType::SocketTypeVec3:
|
||||
return ImNodesPinShape_TriangleFilled;
|
||||
case SocketType::SocketTypeQuat:
|
||||
return ImNodesPinShape_Triangle;
|
||||
case SocketType::SocketTypeBool:
|
||||
return ImNodesPinShape_Circle;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return ImNodesPinShape_Quad;
|
||||
}
|
||||
|
||||
void NodeSocketEditor(Socket& socket) {
|
||||
int mode_current = static_cast<int>(socket.m_type);
|
||||
ImGui::InputText("Name", &socket.m_name);
|
||||
if (ImGui::Combo(
|
||||
"Type",
|
||||
&mode_current,
|
||||
SocketTypeNames,
|
||||
sizeof(SocketTypeNames) / sizeof(char*))) {
|
||||
socket.m_type = static_cast<SocketType>(mode_current);
|
||||
}
|
||||
}
|
||||
|
||||
void RemoveConnectionsForSocket(
|
||||
AnimGraphResource& graph_resource,
|
||||
AnimNodeResource& node_resource,
|
||||
Socket& socket) {
|
||||
std::vector<AnimGraphConnection>::iterator iter =
|
||||
graph_resource.m_connections.begin();
|
||||
|
||||
while (iter != graph_resource.m_connections.end()) {
|
||||
AnimGraphConnection& connection = *iter;
|
||||
if (connection.m_source_node == &node_resource
|
||||
&& connection.m_source_socket == &socket) {
|
||||
iter = graph_resource.m_connections.erase(iter);
|
||||
} else {
|
||||
iter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AnimGraphEditorRenderSidebar(
|
||||
AnimGraphResource& graph_resource,
|
||||
AnimNodeResource& node_resource) {
|
||||
ImGui::Text("[%s]", node_resource.m_type_name.c_str());
|
||||
|
||||
char node_name_buffer[256];
|
||||
memset(node_name_buffer, 0, sizeof(node_name_buffer));
|
||||
strncpy(
|
||||
node_name_buffer,
|
||||
node_resource.m_name.c_str(),
|
||||
std::min(node_resource.m_name.size(), sizeof(node_name_buffer)));
|
||||
|
||||
if (ImGui::InputText("Name", node_name_buffer, sizeof(node_name_buffer))) {
|
||||
node_resource.m_name = node_name_buffer;
|
||||
}
|
||||
|
||||
int num_properties = node_resource.m_socket_accessor->m_properties.size();
|
||||
for (int i = 0; i < num_properties; i++) {
|
||||
Socket& property = node_resource.m_socket_accessor->m_properties[i];
|
||||
if (property.m_type == SocketType::SocketTypeFloat) {
|
||||
ImGui::SliderFloat(
|
||||
property.m_name.c_str(),
|
||||
reinterpret_cast<float*>(property.m_value.ptr),
|
||||
-100.f,
|
||||
100.f);
|
||||
} else if (property.m_type == SocketType::SocketTypeBool) {
|
||||
ImGui::Checkbox(
|
||||
property.m_name.c_str(),
|
||||
reinterpret_cast<bool*>(property.m_value.ptr));
|
||||
} else if (property.m_type == SocketType::SocketTypeString) {
|
||||
std::string* property_string =
|
||||
reinterpret_cast<std::string*>(property.m_value.ptr);
|
||||
char string_buf[256];
|
||||
memset(string_buf, 0, sizeof(string_buf));
|
||||
strncpy(
|
||||
string_buf,
|
||||
property_string->c_str(),
|
||||
std::min(property_string->size(), sizeof(string_buf)));
|
||||
if (ImGui::InputText(
|
||||
property.m_name.c_str(),
|
||||
string_buf,
|
||||
sizeof(string_buf))) {
|
||||
(*property_string) = string_buf;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (&node_resource == &graph_resource.getGraphOutputNode()) {
|
||||
ImGui::Text("Outputs");
|
||||
|
||||
// Graph outputs are the inputs of the output node!
|
||||
std::vector<Socket>& outputs = node_resource.m_socket_accessor->m_inputs;
|
||||
|
||||
std::vector<Socket>::iterator iter = outputs.begin();
|
||||
while (iter != outputs.end()) {
|
||||
Socket& output = *iter;
|
||||
ImGui::PushID(&output);
|
||||
NodeSocketEditor(output);
|
||||
if (ImGui::Button("X")) {
|
||||
RemoveConnectionsForSocket(graph_resource, node_resource, output);
|
||||
iter = outputs.erase(iter);
|
||||
} else {
|
||||
iter++;
|
||||
}
|
||||
ImGui::PopID();
|
||||
}
|
||||
}
|
||||
|
||||
if (&node_resource == &graph_resource.getGraphInputNode()) {
|
||||
ImGui::Text("Inputs");
|
||||
|
||||
// Graph inputs are the outputs of the input node!
|
||||
std::vector<Socket>& inputs = node_resource.m_socket_accessor->m_outputs;
|
||||
|
||||
std::vector<Socket>::iterator iter = inputs.begin();
|
||||
while (iter != inputs.end()) {
|
||||
Socket& input = *iter;
|
||||
ImGui::PushID(&input);
|
||||
NodeSocketEditor(input);
|
||||
if (ImGui::Button("X")) {
|
||||
RemoveConnectionsForSocket(graph_resource, node_resource, input);
|
||||
iter = inputs.erase(iter);
|
||||
} else {
|
||||
iter++;
|
||||
}
|
||||
ImGui::PopID();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AnimGraphEditorUpdate() {
|
||||
static AnimGraphResource graph_resource = AnimGraphResource();
|
||||
|
||||
ImGui::BeginMenuBar();
|
||||
if (ImGui::Button("Save")) {
|
||||
graph_resource.saveToFile("editor_graph.json");
|
||||
}
|
||||
if (ImGui::Button("Load")) {
|
||||
graph_resource.loadFromFile("editor_graph.json");
|
||||
|
||||
for (size_t i = 0, n = graph_resource.m_nodes.size(); i < n; i++) {
|
||||
const AnimNodeResource& node_resource = graph_resource.m_nodes[i];
|
||||
ImNodes::SetNodeGridSpacePos(
|
||||
i,
|
||||
ImVec2(node_resource.m_position[0], node_resource.m_position[1]));
|
||||
}
|
||||
}
|
||||
if (ImGui::Button("Clear")) {
|
||||
graph_resource.clear();
|
||||
}
|
||||
char graph_name_buffer[256];
|
||||
memset(graph_name_buffer, 0, sizeof(graph_name_buffer));
|
||||
strncpy(
|
||||
graph_name_buffer,
|
||||
graph_resource.m_name.c_str(),
|
||||
sizeof(graph_name_buffer));
|
||||
if (ImGui::InputText("Name", graph_name_buffer, sizeof(graph_name_buffer))) {
|
||||
graph_resource.m_name = graph_name_buffer;
|
||||
}
|
||||
|
||||
ImGui::EndMenuBar();
|
||||
|
||||
ImGui::Columns(2);
|
||||
|
||||
//
|
||||
// Node editor canvas
|
||||
//
|
||||
ImNodes::BeginNodeEditor();
|
||||
|
||||
// Popup menu
|
||||
{
|
||||
const bool open_popup =
|
||||
ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows)
|
||||
&& ImNodes::IsEditorHovered()
|
||||
&& ImGui::IsMouseReleased(ImGuiMouseButton_Right);
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(8.f, 8.f));
|
||||
if (!ImGui::IsAnyItemHovered() && open_popup) {
|
||||
ImGui::OpenPopup("add node");
|
||||
}
|
||||
|
||||
if (ImGui::BeginPopup("add node")) {
|
||||
const ImVec2 click_pos = ImGui::GetMousePosOnOpeningCurrentPopup();
|
||||
std::string node_type_name = "";
|
||||
if (ImGui::MenuItem("AnimSampler")) {
|
||||
node_type_name = "AnimSampler";
|
||||
}
|
||||
|
||||
if (ImGui::MenuItem("Blend2")) {
|
||||
node_type_name = "Blend2";
|
||||
}
|
||||
|
||||
if (ImGui::MenuItem("SpeedScale")) {
|
||||
node_type_name = "SpeedScale";
|
||||
}
|
||||
|
||||
if (node_type_name != "") {
|
||||
AnimNodeResource node_resource =
|
||||
AnimNodeResourceFactory(node_type_name);
|
||||
size_t node_id = graph_resource.m_nodes.size();
|
||||
ImNodes::SetNodeScreenSpacePos(node_id, ImGui::GetMousePos());
|
||||
graph_resource.m_nodes.push_back(node_resource);
|
||||
}
|
||||
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
|
||||
ImGui::PopStyleVar(ImGuiStyleVar_WindowPadding);
|
||||
}
|
||||
|
||||
for (size_t i = 0, n = graph_resource.m_nodes.size(); i < n; i++) {
|
||||
AnimNodeResource& node_resource = graph_resource.m_nodes[i];
|
||||
|
||||
ImNodes::BeginNode(i);
|
||||
|
||||
// Header
|
||||
ImNodes::BeginNodeTitleBar();
|
||||
if (&node_resource == &graph_resource.getGraphOutputNode()) {
|
||||
ImGui::TextUnformatted("Graph Outputs");
|
||||
} else if (&node_resource == &graph_resource.getGraphInputNode()) {
|
||||
ImGui::TextUnformatted("Graph Inputs");
|
||||
} else {
|
||||
ImGui::TextUnformatted(node_resource.m_type_name.c_str());
|
||||
}
|
||||
ImNodes::EndNodeTitleBar();
|
||||
|
||||
// Inputs
|
||||
const std::vector<Socket>& node_inputs =
|
||||
node_resource.m_socket_accessor->m_inputs;
|
||||
for (size_t j = 0, ni = node_inputs.size(); j < ni; j++) {
|
||||
const Socket& socket = node_inputs[j];
|
||||
|
||||
ImColor socket_color = ImColor(255, 255, 255, 255);
|
||||
if (socket.m_flags & SocketFlagAffectsTime) {
|
||||
socket_color = ImColor(255, 128, 128, 255);
|
||||
}
|
||||
|
||||
ImNodes::BeginInputAttribute(
|
||||
GenerateInputAttributeId(i, j),
|
||||
sGetSocketShapeFromSocketType(socket.m_type),
|
||||
socket_color);
|
||||
ImGui::TextUnformatted(socket.m_name.c_str());
|
||||
|
||||
ImNodes::PushAttributeFlag(
|
||||
ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
|
||||
ImNodes::EndInputAttribute();
|
||||
}
|
||||
|
||||
// Outputs
|
||||
const std::vector<Socket>& node_outputs =
|
||||
node_resource.m_socket_accessor->m_outputs;
|
||||
for (size_t j = 0, ni = node_outputs.size(); j < ni; j++) {
|
||||
const Socket& socket = node_outputs[j];
|
||||
ImNodes::BeginOutputAttribute(
|
||||
GenerateOutputAttributeId(i, j),
|
||||
sGetSocketShapeFromSocketType(socket.m_type),
|
||||
ImColor(255, 255, 255, 255));
|
||||
ImGui::TextUnformatted(socket.m_name.c_str());
|
||||
ImNodes::PushAttributeFlag(
|
||||
ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
|
||||
ImNodes::EndInputAttribute();
|
||||
}
|
||||
|
||||
// Graph output node
|
||||
if (i == 0) {
|
||||
if (ImGui::Button("+Output")) {
|
||||
AnimNodeResource& graph_output_node =
|
||||
graph_resource.getGraphOutputNode();
|
||||
|
||||
static float bla = 0.f;
|
||||
std::string socket_name = "Output";
|
||||
socket_name += std::to_string(
|
||||
graph_output_node.m_socket_accessor->m_inputs.size());
|
||||
graph_output_node.m_socket_accessor->RegisterInput<float>(
|
||||
socket_name,
|
||||
nullptr);
|
||||
}
|
||||
} else if (i == 1) {
|
||||
if (ImGui::Button("+Input")) {
|
||||
AnimNodeResource& graph_input_node = graph_resource.getGraphInputNode();
|
||||
|
||||
static float bla = 0.f;
|
||||
std::string socket_name = "Input";
|
||||
socket_name += std::to_string(
|
||||
graph_input_node.m_socket_accessor->m_outputs.size());
|
||||
graph_input_node.m_socket_accessor->RegisterOutput<float>(
|
||||
socket_name,
|
||||
nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
// Save state in node resource
|
||||
ImVec2 node_pos = ImNodes::GetNodeGridSpacePos(i);
|
||||
node_resource.m_position[0] = node_pos[0];
|
||||
node_resource.m_position[1] = node_pos[1];
|
||||
ImNodes::EndNode();
|
||||
|
||||
// Ensure flags such as SocketFlagAffectsTime are properly set.
|
||||
node_resource.m_socket_accessor->UpdateFlags();
|
||||
}
|
||||
|
||||
for (size_t i = 0, n = graph_resource.m_connections.size(); i < n; i++) {
|
||||
const AnimGraphConnection& connection = graph_resource.m_connections[i];
|
||||
int start_attr, end_attr;
|
||||
|
||||
int source_node_index =
|
||||
graph_resource.getNodeIndex(*connection.m_source_node);
|
||||
int source_socket_index =
|
||||
connection.m_source_node->m_socket_accessor->GetOutputIndex(
|
||||
connection.m_source_socket->m_name);
|
||||
start_attr =
|
||||
GenerateOutputAttributeId(source_node_index, source_socket_index);
|
||||
|
||||
int target_node_index =
|
||||
graph_resource.getNodeIndex(*connection.m_target_node);
|
||||
int target_socket_index =
|
||||
connection.m_target_node->m_socket_accessor->GetInputIndex(
|
||||
connection.m_target_socket->m_name);
|
||||
end_attr = GenerateInputAttributeId(target_node_index, target_socket_index);
|
||||
|
||||
ImNodes::Link(i, start_attr, end_attr);
|
||||
}
|
||||
|
||||
ImNodes::EndNodeEditor();
|
||||
|
||||
// Handle newly created links.
|
||||
int start_attr, end_attr;
|
||||
if (ImNodes::IsLinkCreated(&start_attr, &end_attr)) {
|
||||
int node_start_id;
|
||||
int node_start_output_index;
|
||||
SplitOutputAttributeId(
|
||||
start_attr,
|
||||
&node_start_id,
|
||||
&node_start_output_index);
|
||||
|
||||
int node_end_id;
|
||||
int node_end_input_index;
|
||||
SplitInputAttributeId(end_attr, &node_end_id, &node_end_input_index);
|
||||
|
||||
AnimGraphConnection connection;
|
||||
connection.m_source_node = &graph_resource.m_nodes[node_start_id];
|
||||
connection.m_source_socket = &connection.m_source_node->m_socket_accessor->m_outputs[node_start_output_index];
|
||||
|
||||
connection.m_target_node = &graph_resource.m_nodes[node_end_id];
|
||||
connection.m_target_socket = &connection.m_target_node->m_socket_accessor->m_inputs[node_end_input_index];
|
||||
|
||||
graph_resource.m_connections.push_back(connection);
|
||||
}
|
||||
|
||||
// Handle link detachements.
|
||||
int link_id = 0;
|
||||
if (ImNodes::IsLinkDestroyed(&link_id)) {
|
||||
graph_resource.m_connections.erase(
|
||||
graph_resource.m_connections.begin() + link_id);
|
||||
}
|
||||
|
||||
int selected_nodes[ImNodes::NumSelectedNodes()];
|
||||
ImNodes::GetSelectedNodes(selected_nodes);
|
||||
|
||||
//
|
||||
// Sidebar
|
||||
//
|
||||
ImGui::NextColumn();
|
||||
|
||||
if (ImNodes::NumSelectedNodes() == 1) {
|
||||
if (selected_nodes[0] < graph_resource.m_nodes.size()) {
|
||||
AnimNodeResource& selected_node =
|
||||
graph_resource.m_nodes[selected_nodes[0]];
|
||||
AnimGraphEditorRenderSidebar(graph_resource, selected_node);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::Columns(1);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by martin on 11.02.22.
|
||||
//
|
||||
|
||||
#ifndef ANIMTESTBED_ANIMGRAPHEDITOR_H
|
||||
#define ANIMTESTBED_ANIMGRAPHEDITOR_H
|
||||
|
||||
inline int GenerateInputAttributeId(int node_id, int input_index) {
|
||||
return ((input_index + 1) << 14) + node_id;
|
||||
}
|
||||
|
||||
inline void
|
||||
SplitInputAttributeId(int attribute_id, int* node_id, int* input_index) {
|
||||
*node_id = attribute_id & ((1 << 14) - 1);
|
||||
*input_index = (attribute_id >> 14) - 1;
|
||||
}
|
||||
|
||||
inline int GenerateOutputAttributeId(int node_id, int output_index) {
|
||||
return ((output_index + 1) << 23) + node_id;
|
||||
}
|
||||
|
||||
inline void
|
||||
SplitOutputAttributeId(int attribute_id, int* node_id, int* output_index) {
|
||||
*node_id = attribute_id & ((1 << 14) - 1);
|
||||
*output_index = (attribute_id >> 23) - 1;
|
||||
}
|
||||
|
||||
void AnimGraphEditorUpdate();
|
||||
|
||||
#endif //ANIMTESTBED_ANIMGRAPHEDITOR_H
|
||||
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by martin on 25.03.22.
|
||||
//
|
||||
|
||||
#include "AnimGraphNodes.h"
|
||||
@@ -0,0 +1,208 @@
|
||||
//
|
||||
// Created by martin on 25.03.22.
|
||||
//
|
||||
|
||||
#ifndef ANIMTESTBED_ANIMGRAPHNODES_H
|
||||
#define ANIMTESTBED_ANIMGRAPHNODES_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "AnimGraphData.h"
|
||||
#include "SyncTrack.h"
|
||||
|
||||
struct AnimNode;
|
||||
|
||||
struct NodeInput {
|
||||
AnimNode* m_node;
|
||||
SocketType m_type = SocketType::SocketTypeUndefined;
|
||||
std::string m_input_name;
|
||||
};
|
||||
|
||||
enum class AnimNodeEvalState {
|
||||
Undefined,
|
||||
Deactivated,
|
||||
Activated,
|
||||
SyncTrackUpdated,
|
||||
TimeUpdated,
|
||||
Evaluated
|
||||
};
|
||||
|
||||
struct AnimNode {
|
||||
std::string m_name;
|
||||
std::string m_node_type_name;
|
||||
float m_time_now = 0.f;
|
||||
float m_time_last = 0.f;
|
||||
size_t m_index = -1;
|
||||
AnimNodeEvalState m_state = AnimNodeEvalState::Undefined;
|
||||
SyncTrack m_sync_track;
|
||||
|
||||
virtual ~AnimNode(){};
|
||||
|
||||
virtual void MarkActiveInputs(const std::vector<NodeInput>& inputs) {
|
||||
for (size_t i = 0, n = inputs.size(); i < n; i++) {
|
||||
AnimNode* input_node = inputs[i].m_node;
|
||||
if (input_node != nullptr) {
|
||||
input_node->m_state = AnimNodeEvalState::Activated;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual void CalcSyncTrack(const std::vector<NodeInput>& inputs) {
|
||||
for (size_t i = 0, n = inputs.size(); i < n; i++) {
|
||||
AnimNode* input_node = inputs[i].m_node;
|
||||
if (input_node != nullptr
|
||||
&& inputs[i].m_type == SocketType::SocketTypeAnimation
|
||||
&& input_node->m_state != AnimNodeEvalState::Deactivated) {
|
||||
m_sync_track = input_node->m_sync_track;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual void UpdateTime(float time_last, float time_now) {
|
||||
m_time_last = time_last;
|
||||
m_time_now = time_now;
|
||||
m_state = AnimNodeEvalState::TimeUpdated;
|
||||
}
|
||||
|
||||
virtual void Evaluate(){};
|
||||
};
|
||||
|
||||
|
||||
//
|
||||
// BlendTreeNode
|
||||
//
|
||||
struct BlendTreeNode : public AnimNode {};
|
||||
|
||||
template <>
|
||||
struct NodeSocketAccessor<BlendTreeNode> : public NodeSocketAccessorBase {
|
||||
NodeSocketAccessor(AnimNode* node_) {}
|
||||
};
|
||||
|
||||
//
|
||||
// Blend2Node
|
||||
//
|
||||
struct Blend2Node : public AnimNode {
|
||||
AnimData m_input0;
|
||||
AnimData m_input1;
|
||||
AnimData* m_output = nullptr;
|
||||
float m_blend_weight = 0.f;
|
||||
bool m_sync_blend = false;
|
||||
|
||||
virtual void MarkActiveInputs(const std::vector<NodeInput>& inputs) override {
|
||||
for (size_t i = 0, n = inputs.size(); i < n; i++) {
|
||||
AnimNode* input_node = inputs[i].m_node;
|
||||
if (input_node == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (inputs[i].m_input_name == "Input0" && m_blend_weight < 0.999) {
|
||||
input_node->m_state = AnimNodeEvalState::Activated;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (inputs[i].m_input_name == "Input1" && m_blend_weight > 0.001) {
|
||||
input_node->m_state = AnimNodeEvalState::Activated;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual void UpdateTime(float dt, std::vector<NodeInput>& inputs) {
|
||||
if (!m_sync_blend) {
|
||||
m_time_now = m_time_now + dt;
|
||||
}
|
||||
|
||||
for (size_t i = 0, n = inputs.size(); i < n; i++) {
|
||||
AnimNode* input_node = inputs[i].m_node;
|
||||
if (input_node == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (input_node->m_state != AnimNodeEvalState::Deactivated) {
|
||||
if (!m_sync_blend) {
|
||||
input_node->m_time_now = m_time_now;
|
||||
}
|
||||
input_node->m_state = AnimNodeEvalState::TimeUpdated;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NodeSocketAccessor<Blend2Node> : public NodeSocketAccessorBase {
|
||||
NodeSocketAccessor(AnimNode* node_) {
|
||||
Blend2Node* node = dynamic_cast<Blend2Node*>(node_);
|
||||
RegisterInput("Input0", &node->m_input0);
|
||||
RegisterInput("Input1", &node->m_input1);
|
||||
RegisterInput(
|
||||
"Weight",
|
||||
&node->m_blend_weight,
|
||||
SocketFlags::SocketFlagAffectsTime);
|
||||
|
||||
RegisterOutput("Output", &node->m_output);
|
||||
|
||||
RegisterProperty("Sync", &node->m_sync_blend);
|
||||
}
|
||||
|
||||
virtual void UpdateFlags() override {
|
||||
Socket* weight_input_socket = FindSocket(m_inputs, "Weight");
|
||||
assert(weight_input_socket != nullptr);
|
||||
|
||||
if (GetProperty<bool>("Sync", false) == true) {
|
||||
weight_input_socket->m_flags = SocketFlags::SocketFlagAffectsTime;
|
||||
} else {
|
||||
weight_input_socket->m_flags = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// SpeedScaleNode
|
||||
//
|
||||
struct SpeedScaleNode : public AnimNode {
|
||||
AnimData m_input;
|
||||
AnimData* m_output = nullptr;
|
||||
float m_speed_scale = 0.f;
|
||||
|
||||
virtual void UpdateTime(float time_last, float time_now) {
|
||||
m_time_last = time_last;
|
||||
m_time_now = time_last + (time_now - time_last) * m_speed_scale;
|
||||
m_state = AnimNodeEvalState::TimeUpdated;
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NodeSocketAccessor<SpeedScaleNode> : public NodeSocketAccessorBase {
|
||||
NodeSocketAccessor(AnimNode* node_) {
|
||||
SpeedScaleNode* node = dynamic_cast<SpeedScaleNode*>(node_);
|
||||
RegisterInput(
|
||||
"SpeedScale",
|
||||
&node->m_speed_scale,
|
||||
SocketFlags::SocketFlagAffectsTime);
|
||||
RegisterInput("Input", &node->m_input);
|
||||
|
||||
RegisterOutput("Output", &node->m_output);
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// AnimSamplerNode
|
||||
//
|
||||
struct AnimSamplerNode : public AnimNode {
|
||||
AnimData* m_output = nullptr;
|
||||
std::string m_filename;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NodeSocketAccessor<AnimSamplerNode> : public NodeSocketAccessorBase {
|
||||
NodeSocketAccessor(AnimNode* node_) {
|
||||
AnimSamplerNode* node = dynamic_cast<AnimSamplerNode*>(node_);
|
||||
RegisterOutput("Output", &node->m_output);
|
||||
|
||||
RegisterProperty("Filename", &node->m_filename);
|
||||
}
|
||||
};
|
||||
|
||||
#endif //ANIMTESTBED_ANIMGRAPHNODES_H
|
||||
@@ -0,0 +1,343 @@
|
||||
//
|
||||
// Created by martin on 04.02.22.
|
||||
//
|
||||
|
||||
#include "AnimGraphResource.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "3rdparty/json/json.hpp"
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
//
|
||||
// Socket <-> json
|
||||
//
|
||||
std::string sSocketTypeToStr(SocketType pin_type) {
|
||||
if (pin_type < SocketType::SocketTypeUndefined
|
||||
|| pin_type >= SocketType::SocketTypeLast) {
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
return SocketTypeNames[static_cast<int>(pin_type)];
|
||||
}
|
||||
|
||||
json sSocketToJson(const Socket& socket) {
|
||||
json result;
|
||||
result["name"] = socket.m_name;
|
||||
result["type"] = sSocketTypeToStr(socket.m_type);
|
||||
|
||||
if (socket.m_value.ptr != nullptr) {
|
||||
if (socket.m_type == SocketType::SocketTypeBool) {
|
||||
result["value"] = *reinterpret_cast<bool*>(socket.m_value.ptr);
|
||||
} else if (socket.m_type == SocketType::SocketTypeAnimation) {
|
||||
} else if (socket.m_type == SocketType::SocketTypeFloat) {
|
||||
result["value"] = *reinterpret_cast<float*>(socket.m_value.ptr);
|
||||
} else if (socket.m_type == SocketType::SocketTypeVec3) {
|
||||
Vec3& vec3 = *reinterpret_cast<Vec3*>(socket.m_value.ptr);
|
||||
result["value"][0] = vec3[0];
|
||||
result["value"][1] = vec3[1];
|
||||
result["value"][2] = vec3[2];
|
||||
} else if (socket.m_type == SocketType::SocketTypeQuat) {
|
||||
Quat& quat = *reinterpret_cast<Quat*>(socket.m_value.ptr);
|
||||
result["value"][0] = quat[0];
|
||||
result["value"][1] = quat[1];
|
||||
result["value"][2] = quat[2];
|
||||
result["value"][3] = quat[3];
|
||||
} else if (socket.m_type == SocketType::SocketTypeString) {
|
||||
result["value"] = *reinterpret_cast<std::string*>(socket.m_value.ptr);
|
||||
} else {
|
||||
std::cerr << "Invalid socket type '" << static_cast<int>(socket.m_type)
|
||||
<< "'." << std::endl;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Socket sJsonToSocket(const json& json_data) {
|
||||
Socket result;
|
||||
result.m_type = SocketType::SocketTypeUndefined;
|
||||
result.m_value.ptr = nullptr;
|
||||
result.m_name = json_data["name"];
|
||||
|
||||
std::string type_string = json_data["type"];
|
||||
|
||||
if (type_string == "Bool") {
|
||||
result.m_type = SocketType::SocketTypeBool;
|
||||
result.m_type_size = sizeof(bool);
|
||||
} else if (type_string == "Animation") {
|
||||
result.m_type = SocketType::SocketTypeAnimation;
|
||||
result.m_type_size = sizeof(AnimData);
|
||||
} else if (type_string == "Float") {
|
||||
result.m_type = SocketType::SocketTypeFloat;
|
||||
result.m_type_size = sizeof(float);
|
||||
} else if (type_string == "Vec3") {
|
||||
result.m_type = SocketType::SocketTypeVec3;
|
||||
result.m_type_size = sizeof(Vec3);
|
||||
} else if (type_string == "Quat") {
|
||||
result.m_type = SocketType::SocketTypeQuat;
|
||||
result.m_type_size = sizeof(Quat);
|
||||
} else if (type_string == "String") {
|
||||
result.m_type = SocketType::SocketTypeString;
|
||||
result.m_type_size = sizeof(std::string);
|
||||
} else {
|
||||
std::cerr << "Invalid socket type '" << type_string << "'." << std::endl;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// AnimGraphNode <-> json
|
||||
//
|
||||
json sAnimGraphNodeToJson(const AnimNodeResource& node) {
|
||||
json result;
|
||||
|
||||
result["name"] = node.m_name;
|
||||
result["type"] = "AnimNodeResource";
|
||||
result["node_type"] = node.m_type_name;
|
||||
|
||||
for (size_t j = 0; j < 2; j++) {
|
||||
result["position"][j] = node.m_position[j];
|
||||
}
|
||||
|
||||
for (size_t j = 0, n = node.m_socket_accessor->m_properties.size(); j < n;
|
||||
j++) {
|
||||
Socket& property = node.m_socket_accessor->m_properties[j];
|
||||
result["properties"][property.m_name] = sSocketToJson(property);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
AnimNodeResource sAnimGraphNodeFromJson(const json& json_node) {
|
||||
AnimNodeResource result;
|
||||
|
||||
result.m_name = json_node["name"];
|
||||
result.m_type_name = json_node["node_type"];
|
||||
result.m_position[0] = json_node["position"][0];
|
||||
result.m_position[1] = json_node["position"][1];
|
||||
|
||||
result.m_anim_node = AnimNodeFactory(result.m_type_name);
|
||||
result.m_socket_accessor =
|
||||
AnimNodeAccessorFactory(result.m_type_name, result.m_anim_node);
|
||||
|
||||
for (size_t j = 0, n = result.m_socket_accessor->m_properties.size(); j < n;
|
||||
j++) {
|
||||
Socket& property = result.m_socket_accessor->m_properties[j];
|
||||
json json_property = json_node["properties"][property.m_name];
|
||||
|
||||
if (sSocketTypeToStr(property.m_type) == json_property["type"]) {
|
||||
if (property.m_type == SocketType::SocketTypeBool) {
|
||||
result.m_socket_accessor->SetProperty<bool>(
|
||||
property.m_name,
|
||||
json_property["value"]);
|
||||
} else if (property.m_type == SocketType::SocketTypeAnimation) {
|
||||
} else if (property.m_type == SocketType::SocketTypeFloat) {
|
||||
result.m_socket_accessor->SetProperty<float>(
|
||||
property.m_name,
|
||||
json_property["value"]);
|
||||
} else if (property.m_type == SocketType::SocketTypeVec3) {
|
||||
Vec3* property_vec3 = reinterpret_cast<Vec3*>(property.m_value.ptr);
|
||||
(*property_vec3)[0] = json_property["value"][0];
|
||||
(*property_vec3)[1] = json_property["value"][1];
|
||||
(*property_vec3)[2] = json_property["value"][2];
|
||||
} else if (property.m_type == SocketType::SocketTypeQuat) {
|
||||
Quat* property_quat = reinterpret_cast<Quat*>(property.m_value.ptr);
|
||||
(*property_quat)[0] = json_property["value"][0];
|
||||
(*property_quat)[1] = json_property["value"][1];
|
||||
(*property_quat)[2] = json_property["value"][2];
|
||||
(*property_quat)[3] = json_property["value"][3];
|
||||
} else if (property.m_type == SocketType::SocketTypeString) {
|
||||
result.m_socket_accessor->SetProperty<std::string>(
|
||||
property.m_name,
|
||||
json_property["value"]);
|
||||
} else {
|
||||
std::cerr << "Invalid type for property '" << property.m_name
|
||||
<< "'. Cannot parse json to type '"
|
||||
<< static_cast<int>(property.m_type) << std::endl;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
std::cerr << "Invalid type for property '" << property.m_name
|
||||
<< "': expected " << sSocketTypeToStr(property.m_type)
|
||||
<< " but got " << json_property["type"] << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// AnimGraphConnection <-> Json
|
||||
//
|
||||
json sAnimGraphConnectionToJson(const AnimGraphResource& graph_resource, const AnimGraphConnection& connection) {
|
||||
json result;
|
||||
|
||||
result["type"] = "AnimGraphConnection";
|
||||
|
||||
const AnimNodeResource* source_node = connection.m_source_node;
|
||||
result["source_node_index"] = graph_resource.getNodeIndex(*source_node);
|
||||
result["source_socket_index"] = source_node->m_socket_accessor->GetOutputIndex(connection.m_source_socket->m_name);
|
||||
|
||||
const AnimNodeResource* target_node = connection.m_source_node;
|
||||
result["source_node_index"] = graph_resource.getNodeIndex(*target_node);
|
||||
result["source_socket_index"] = target_node->m_socket_accessor->GetInputIndex(connection.m_target_node->m_name);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
AnimGraphConnection sAnimGraphConnectionFromJson(const AnimGraphResource& graph_resource, const json& json_node) {
|
||||
AnimGraphConnection connection;
|
||||
|
||||
int source_node_index = json_node["source_node_index"];
|
||||
connection.m_source_node = &graph_resource.m_nodes[source_node_index];
|
||||
int source_socket_index = json_node["source_socket_index"];
|
||||
connection.m_source_socket = &connection.m_source_node->m_socket_accessor->m_outputs[source_socket_index];
|
||||
|
||||
|
||||
int target_node_index = json_node["target_node_index"];
|
||||
connection.m_target_node = &graph_resource.m_nodes[target_node_index];
|
||||
|
||||
int target_socket_index = json_node["target_socket_index"];
|
||||
connection.m_target_socket = &connection.m_target_node->m_socket_accessor->m_outputs[target_socket_index];
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
void AnimGraphResource::clear() {
|
||||
m_name = "";
|
||||
|
||||
clearNodes();
|
||||
m_connections.clear();
|
||||
|
||||
initGraphConnectors();
|
||||
}
|
||||
|
||||
void AnimGraphResource::clearNodes() {
|
||||
for (size_t i = 0; i < m_nodes.size(); i++) {
|
||||
delete m_nodes[i].m_socket_accessor;
|
||||
m_nodes[i].m_socket_accessor = nullptr;
|
||||
delete m_nodes[i].m_anim_node;
|
||||
m_nodes[i].m_anim_node = nullptr;
|
||||
}
|
||||
m_nodes.clear();
|
||||
}
|
||||
|
||||
void AnimGraphResource::initGraphConnectors() {
|
||||
m_nodes.push_back(AnimNodeResourceFactory("BlendTree"));
|
||||
m_nodes[0].m_name = "Outputs";
|
||||
m_nodes.push_back(AnimNodeResourceFactory("BlendTree"));
|
||||
m_nodes[1].m_name = "Inputs";
|
||||
}
|
||||
|
||||
bool AnimGraphResource::saveToFile(const char* filename) const {
|
||||
json result;
|
||||
|
||||
result["name"] = m_name;
|
||||
result["type"] = "AnimGraphResource";
|
||||
|
||||
for (size_t i = 0; i < m_nodes.size(); i++) {
|
||||
const AnimNodeResource& node = m_nodes[i];
|
||||
result["nodes"][i] = sAnimGraphNodeToJson(node);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < m_connections.size(); i++) {
|
||||
const AnimGraphConnection& connection = m_connections[i];
|
||||
result["connections"][i] = sAnimGraphConnectionToJson(*this, connection);
|
||||
}
|
||||
|
||||
// Graph inputs and outputs
|
||||
{
|
||||
const AnimNodeResource& graph_output_node = m_nodes[0];
|
||||
const std::vector<Socket> graph_inputs =
|
||||
graph_output_node.m_socket_accessor->m_inputs;
|
||||
for (size_t i = 0; i < graph_inputs.size(); i++) {
|
||||
result["nodes"][0]["inputs"][i] = sSocketToJson(graph_inputs[i]);
|
||||
}
|
||||
|
||||
const AnimNodeResource& graph_input_node = m_nodes[1];
|
||||
const std::vector<Socket> graph_outputs =
|
||||
graph_input_node.m_socket_accessor->m_outputs;
|
||||
for (size_t i = 0; i < graph_outputs.size(); i++) {
|
||||
result["nodes"][1]["outputs"][i] = sSocketToJson(graph_outputs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
std::ofstream output_file;
|
||||
output_file.open(filename);
|
||||
output_file << to_string(result) << std::endl;
|
||||
output_file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AnimGraphResource::loadFromFile(const char* filename) {
|
||||
std::ifstream input_file;
|
||||
input_file.open(filename);
|
||||
std::stringstream buffer;
|
||||
buffer << input_file.rdbuf();
|
||||
|
||||
json json_data = json::parse(buffer.str(), nullptr, false);
|
||||
if (json_data.is_discarded()) {
|
||||
std::cerr << "Error parsing json of file '" << filename << "'."
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
if (json_data["type"] != "AnimGraphResource") {
|
||||
std::cerr
|
||||
<< "Invalid json object. Expected type 'AnimGraphResource' but got '"
|
||||
<< json_data["type"] << "'." << std::endl;
|
||||
}
|
||||
|
||||
clear();
|
||||
clearNodes();
|
||||
|
||||
m_name = json_data["name"];
|
||||
|
||||
// Load nodes
|
||||
for (size_t i = 0; i < json_data["nodes"].size(); i++) {
|
||||
const json& json_node = json_data["nodes"][i];
|
||||
if (json_node["type"] != "AnimNodeResource") {
|
||||
std::cerr
|
||||
<< "Invalid json object. Expected type 'AnimNodeResource' but got '"
|
||||
<< json_node["type"] << "'." << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
AnimNodeResource node = sAnimGraphNodeFromJson(json_node);
|
||||
m_nodes.push_back(node);
|
||||
}
|
||||
|
||||
// Setup graph inputs and outputs
|
||||
const json& graph_outputs = json_data["nodes"][0]["inputs"];
|
||||
for (size_t i = 0; i < graph_outputs.size(); i++) {
|
||||
AnimNodeResource& graph_node = m_nodes[0];
|
||||
graph_node.m_socket_accessor->m_inputs.push_back(
|
||||
sJsonToSocket(graph_outputs[i]));
|
||||
}
|
||||
|
||||
const json& graph_inputs = json_data["nodes"][1]["outputs"];
|
||||
for (size_t i = 0; i < graph_inputs.size(); i++) {
|
||||
AnimNodeResource& graph_node = m_nodes[1];
|
||||
graph_node.m_socket_accessor->m_outputs.push_back(
|
||||
sJsonToSocket(graph_inputs[i]));
|
||||
}
|
||||
|
||||
// Load connections
|
||||
for (size_t i = 0; i < json_data["connections"].size(); i++) {
|
||||
const json& json_connection = json_data["connections"][i];
|
||||
if (json_connection["type"] != "AnimGraphConnection") {
|
||||
std::cerr << "Invalid json object. Expected type 'AnimGraphConnection' "
|
||||
"but got '"
|
||||
<< json_connection["type"] << "'." << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
AnimGraphConnection connection =
|
||||
sAnimGraphConnectionFromJson(*this, json_connection);
|
||||
m_connections.push_back(connection);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
//
|
||||
// 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"
|
||||
|
||||
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};
|
||||
};
|
||||
|
||||
|
||||
|
||||
//
|
||||
// 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;
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user