2022-03-25 11:46:44 +01:00
|
|
|
//
|
2024-03-17 22:06:27 +01:00
|
|
|
// Created by martin on 17.03.24.
|
2022-03-25 11:46:44 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef ANIMTESTBED_ANIMGRAPHRESOURCE_H
|
|
|
|
#define ANIMTESTBED_ANIMGRAPHRESOURCE_H
|
|
|
|
|
2022-04-01 13:19:54 +02:00
|
|
|
#include "AnimGraph.h"
|
2022-03-25 11:46:44 +01:00
|
|
|
#include "AnimGraphNodes.h"
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
#include "3rdparty/json/json.hpp"
|
|
|
|
|
|
|
|
struct AnimGraphBlendTree;
|
|
|
|
struct AnimGraphStateMachine;
|
2022-03-25 11:46:44 +01:00
|
|
|
|
|
|
|
struct AnimNodeResource {
|
|
|
|
std::string m_name;
|
|
|
|
std::string m_type_name;
|
|
|
|
AnimNode* m_anim_node = nullptr;
|
2023-03-30 23:50:07 +02:00
|
|
|
NodeDescriptorBase* m_socket_accessor = nullptr;
|
2022-03-25 11:46:44 +01:00
|
|
|
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 =
|
2023-04-15 22:07:22 +02:00
|
|
|
AnimNodeDescriptorFactory(node_type_name, result.m_anim_node);
|
2022-03-25 12:05:56 +01:00
|
|
|
return result;
|
|
|
|
}
|
2022-03-25 11:46:44 +01:00
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
struct BlendTreeConnectionResource {
|
2022-04-01 13:19:54 +02:00
|
|
|
size_t source_node_index = -1;
|
2023-04-15 22:07:22 +02:00
|
|
|
std::string source_socket_name;
|
2022-04-01 13:19:54 +02:00
|
|
|
size_t target_node_index = -1;
|
2023-04-15 22:07:22 +02:00
|
|
|
std::string target_socket_name;
|
2022-03-25 11:46:44 +01:00
|
|
|
};
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
struct BlendTreeResource {
|
2022-03-25 11:46:44 +01:00
|
|
|
std::vector<AnimNodeResource> m_nodes;
|
2024-03-17 22:06:27 +01:00
|
|
|
std::vector<BlendTreeConnectionResource> m_connections;
|
2022-03-25 11:46:44 +01:00
|
|
|
|
2024-03-22 12:25:42 +01:00
|
|
|
~BlendTreeResource() { CleanupNodes();
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
void Reset() {
|
2024-03-22 12:25:42 +01:00
|
|
|
CleanupNodes();
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
m_connections.clear();
|
2022-03-25 11:46:44 +01:00
|
|
|
}
|
|
|
|
|
2024-03-22 12:25:42 +01:00
|
|
|
void CleanupNodes() {
|
|
|
|
for (AnimNodeResource& node_resource : m_nodes) {
|
|
|
|
delete node_resource.m_anim_node;
|
|
|
|
delete node_resource.m_socket_accessor;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_nodes.clear();
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
void 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";
|
|
|
|
}
|
2022-03-25 11:46:44 +01:00
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
AnimNodeResource& GetGraphOutputNode() { return m_nodes[0]; }
|
|
|
|
AnimNodeResource& GetGraphInputNode() { return m_nodes[1]; }
|
2022-03-25 11:46:44 +01:00
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
size_t GetNodeIndex(const AnimNodeResource& node_resource) const {
|
2022-03-25 11:46:44 +01:00
|
|
|
for (size_t i = 0, n = m_nodes.size(); i < n; i++) {
|
|
|
|
if (&m_nodes[i] == &node_resource) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
bool ConnectSockets (
|
2022-03-25 11:46:44 +01:00
|
|
|
const AnimNodeResource& source_node,
|
|
|
|
const std::string& source_socket_name,
|
|
|
|
const AnimNodeResource& target_node,
|
|
|
|
const std::string& target_socket_name) {
|
2024-03-17 22:06:27 +01:00
|
|
|
size_t source_node_index = GetNodeIndex(source_node);
|
|
|
|
size_t target_node_index = GetNodeIndex(target_node);
|
2022-03-25 11:46:44 +01:00
|
|
|
|
2022-04-01 13:19:54 +02:00
|
|
|
if (source_node_index >= m_nodes.size()
|
|
|
|
|| target_node_index >= m_nodes.size()) {
|
2022-03-25 11:46:44 +01:00
|
|
|
std::cerr << "Cannot connect nodes: could not find nodes." << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket* source_socket =
|
2023-03-30 23:50:07 +02:00
|
|
|
source_node.m_socket_accessor->GetOutputSocket(source_socket_name.c_str());
|
2022-03-25 11:46:44 +01:00
|
|
|
Socket* target_socket =
|
2023-03-30 23:50:07 +02:00
|
|
|
target_node.m_socket_accessor->GetInputSocket(target_socket_name.c_str());
|
2022-03-25 11:46:44 +01:00
|
|
|
|
|
|
|
if (source_socket == nullptr || target_socket == nullptr) {
|
|
|
|
std::cerr << "Cannot connect nodes: could not find sockets." << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
BlendTreeConnectionResource connection;
|
2022-04-01 13:19:54 +02:00
|
|
|
connection.source_node_index = source_node_index;
|
|
|
|
connection.source_socket_name = source_socket_name;
|
|
|
|
connection.target_node_index = target_node_index;
|
|
|
|
connection.target_socket_name = target_socket_name;
|
2022-03-25 11:46:44 +01:00
|
|
|
m_connections.push_back(connection);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
std::vector<Socket*> GetConstantNodeInputs(
|
|
|
|
std::vector<NodeDescriptorBase*>& instance_node_descriptors) const {
|
|
|
|
std::vector<Socket*> result;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_nodes.size(); i++) {
|
|
|
|
for (size_t j = 0, num_inputs = instance_node_descriptors[i]->m_inputs.size();
|
|
|
|
j < num_inputs;
|
|
|
|
j++) {
|
|
|
|
Socket& input = instance_node_descriptors[i]->m_inputs[j];
|
|
|
|
|
|
|
|
if (*input.m_reference.ptr_ptr == nullptr) {
|
|
|
|
memcpy(&input.m_value, &m_nodes[i].m_socket_accessor->m_inputs[j].m_value, sizeof(Socket::SocketValue));
|
|
|
|
result.push_back(&input);
|
|
|
|
}
|
2022-04-03 21:05:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
return result;
|
2022-04-03 21:05:11 +02:00
|
|
|
}
|
2024-03-17 22:06:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct StateMachineTransitionResources {
|
|
|
|
size_t source_state_index = -1;
|
|
|
|
size_t target_state_index = -1;
|
|
|
|
float blend_time = 0.f;
|
|
|
|
bool sync_blend = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct StateMachineResource {
|
|
|
|
std::vector<AnimNodeResource> m_states;
|
|
|
|
std::vector<StateMachineTransitionResources> m_transitions;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AnimGraphResource {
|
|
|
|
std::string m_type;
|
|
|
|
std::string m_name;
|
|
|
|
|
|
|
|
BlendTreeResource m_blend_tree_resource;
|
|
|
|
StateMachineResource m_state_machine_resource;
|
|
|
|
|
|
|
|
bool SaveToFile(const char* filename) const;
|
|
|
|
bool LoadFromFile(const char* filename);
|
|
|
|
|
|
|
|
void CreateBlendTreeInstance(AnimGraphBlendTree& result) const;
|
|
|
|
void CreateStateMachineInstance(AnimGraphStateMachine& result) const;
|
2022-04-03 21:05:11 +02:00
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
private:
|
|
|
|
// BlendTree
|
|
|
|
bool SaveBlendTreeResourceToFile(const char* filename) const;
|
|
|
|
bool LoadBlendTreeResourceFromJson(nlohmann::json const& json_data);
|
|
|
|
void CreateBlendTreeRuntimeNodeInstances(AnimGraphBlendTree& result) const;
|
|
|
|
void PrepareBlendTreeIOData(AnimGraphBlendTree& instance) const;
|
|
|
|
void SetRuntimeNodeProperties(AnimGraphBlendTree& result) const;
|
2022-04-11 16:46:09 +02:00
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
bool SaveStateMachineResourceToFile(const char* filename) const;
|
|
|
|
bool LoadStateMachineResourceFromJson(nlohmann::json const& json_data);
|
2022-03-25 12:05:56 +01:00
|
|
|
};
|
2022-03-25 11:46:44 +01:00
|
|
|
|
|
|
|
#endif //ANIMTESTBED_ANIMGRAPHRESOURCE_H
|