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
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
#include "3rdparty/json/json.hpp"
|
2024-04-05 00:18:07 +02:00
|
|
|
#include "AnimGraphNodes.h"
|
2024-03-17 22:06:27 +01:00
|
|
|
|
|
|
|
struct AnimGraphBlendTree;
|
|
|
|
struct AnimGraphStateMachine;
|
2022-03-25 11:46:44 +01:00
|
|
|
|
|
|
|
struct AnimNodeResource {
|
2024-03-24 21:50:22 +01:00
|
|
|
virtual ~AnimNodeResource() = default;
|
|
|
|
|
2022-03-25 11:46:44 +01:00
|
|
|
std::string m_name;
|
2024-03-24 21:50:22 +01:00
|
|
|
std::string m_node_type_name;
|
2022-03-25 11:46:44 +01:00
|
|
|
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};
|
|
|
|
};
|
|
|
|
|
2024-04-05 00:18:07 +02:00
|
|
|
static inline AnimNodeResource* AnimNodeResourceFactory(
|
|
|
|
const std::string& node_type_name);
|
2022-03-25 11:46:44 +01:00
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
struct BlendTreeConnectionResource {
|
2024-04-21 12:42:49 +02:00
|
|
|
int source_node_index = -1;
|
2023-04-15 22:07:22 +02:00
|
|
|
std::string source_socket_name;
|
2024-04-21 12:42:49 +02:00
|
|
|
int target_node_index = -1;
|
2023-04-15 22:07:22 +02:00
|
|
|
std::string target_socket_name;
|
2024-04-21 12:42:49 +02:00
|
|
|
|
|
|
|
bool operator==(const BlendTreeConnectionResource& other) const {
|
|
|
|
return (
|
|
|
|
source_node_index == other.source_node_index
|
|
|
|
&& target_node_index == other.target_node_index
|
|
|
|
&& source_socket_name == other.source_socket_name
|
|
|
|
|
|
|
|
&& target_socket_name == other.target_socket_name);
|
|
|
|
}
|
2022-03-25 11:46:44 +01:00
|
|
|
};
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
struct BlendTreeResource {
|
2024-04-16 22:11:59 +02:00
|
|
|
std::vector<std::vector<size_t> > m_node_input_connection_indices;
|
2024-04-21 12:42:49 +02:00
|
|
|
std::vector<std::vector<size_t> > m_node_inputs_subtree;
|
2022-03-25 11:46:44 +01:00
|
|
|
|
2024-04-05 00:18:07 +02:00
|
|
|
~BlendTreeResource() { CleanupNodes(); }
|
2024-03-22 12:25:42 +01:00
|
|
|
|
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();
|
2024-04-16 22:11:59 +02:00
|
|
|
|
|
|
|
m_node_input_connection_indices.clear();
|
2024-04-21 12:42:49 +02:00
|
|
|
m_node_inputs_subtree.clear();
|
2022-03-25 11:46:44 +01:00
|
|
|
}
|
|
|
|
|
2024-03-22 12:25:42 +01:00
|
|
|
void CleanupNodes() {
|
2024-03-24 21:50:22 +01:00
|
|
|
for (AnimNodeResource* node_resource : m_nodes) {
|
|
|
|
delete node_resource->m_anim_node;
|
|
|
|
delete node_resource->m_socket_accessor;
|
|
|
|
delete node_resource;
|
2024-03-22 12:25:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_nodes.clear();
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
void InitGraphConnectors() {
|
2024-04-16 22:11:59 +02:00
|
|
|
AddNode(AnimNodeResourceFactory("BlendTreeSockets"));
|
|
|
|
AnimNodeResource* output_node = GetGraphOutputNode();
|
|
|
|
output_node->m_name = "Outputs";
|
|
|
|
|
|
|
|
AddNode(AnimNodeResourceFactory("BlendTreeSockets"));
|
|
|
|
AnimNodeResource* input_node = GetGraphInputNode();
|
|
|
|
output_node->m_name = "Inputs";
|
2024-03-17 22:06:27 +01:00
|
|
|
}
|
2022-03-25 11:46:44 +01:00
|
|
|
|
2024-04-05 00:18:07 +02:00
|
|
|
[[nodiscard]] AnimNodeResource* GetGraphOutputNode() const {
|
|
|
|
return m_nodes[0];
|
|
|
|
}
|
|
|
|
[[nodiscard]] AnimNodeResource* GetGraphInputNode() const {
|
|
|
|
return m_nodes[1];
|
|
|
|
}
|
2022-03-25 11:46:44 +01:00
|
|
|
|
2024-04-21 12:42:49 +02:00
|
|
|
int 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++) {
|
2024-03-24 21:50:22 +01:00
|
|
|
if (m_nodes[i] == node_resource) {
|
2022-03-25 11:46:44 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-21 12:42:49 +02:00
|
|
|
std::cerr << "Error: could not find node index for node resource "
|
|
|
|
<< node_resource << std::endl;
|
2022-03-25 11:46:44 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-04-16 22:11:59 +02:00
|
|
|
[[maybe_unused]] size_t AddNode(AnimNodeResource* node_resource) {
|
|
|
|
m_nodes.push_back(node_resource);
|
|
|
|
m_node_input_connection_indices.emplace_back();
|
2024-04-21 12:42:49 +02:00
|
|
|
m_node_inputs_subtree.emplace_back();
|
2024-04-16 22:11:59 +02:00
|
|
|
return m_nodes.size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] size_t GetNumNodes() const { return m_nodes.size(); }
|
|
|
|
[[nodiscard]] AnimNodeResource* GetNode(size_t i) { return m_nodes[i]; }
|
|
|
|
[[nodiscard]] const AnimNodeResource* GetNode(size_t i) const {
|
|
|
|
return m_nodes[i];
|
|
|
|
}
|
|
|
|
[[nodiscard]] const std::vector<AnimNodeResource*>& GetNodes() const {
|
|
|
|
return m_nodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] size_t GetNumConnections() const {
|
|
|
|
return m_connections.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] BlendTreeConnectionResource* GetConnection(size_t i) {
|
|
|
|
return &m_connections[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const BlendTreeConnectionResource* GetConnection(
|
|
|
|
size_t i) const {
|
|
|
|
return &m_connections[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const std::vector<BlendTreeConnectionResource>& GetConnections()
|
|
|
|
const {
|
|
|
|
return m_connections;
|
|
|
|
}
|
|
|
|
|
2024-04-24 21:38:11 +02:00
|
|
|
Socket* GetNodeOutputSocket(
|
|
|
|
const AnimNodeResource* node,
|
|
|
|
const std::string& output_socket_name) const;
|
|
|
|
|
|
|
|
Socket* GetNodeInputSocket(
|
|
|
|
const AnimNodeResource* node,
|
|
|
|
const std::string& input_socket_name) const;
|
|
|
|
|
2024-04-05 00:18:07 +02:00
|
|
|
bool ConnectSockets(
|
2024-03-24 21:50:22 +01:00
|
|
|
const AnimNodeResource* source_node,
|
2022-03-25 11:46:44 +01:00
|
|
|
const std::string& source_socket_name,
|
2024-03-24 21:50:22 +01:00
|
|
|
const AnimNodeResource* target_node,
|
|
|
|
const std::string& target_socket_name);
|
2022-03-25 11:46:44 +01:00
|
|
|
|
2024-04-21 12:42:49 +02:00
|
|
|
bool DisconnectSockets(
|
|
|
|
const AnimNodeResource* source_node,
|
|
|
|
const std::string& source_socket_name,
|
|
|
|
const AnimNodeResource* target_node,
|
|
|
|
const std::string& target_socket_name);
|
|
|
|
|
|
|
|
bool IsConnectionValid(
|
|
|
|
const AnimNodeResource* source_node,
|
|
|
|
const std::string& source_socket_name,
|
|
|
|
const AnimNodeResource* target_node,
|
|
|
|
const std::string& target_socket_name) const;
|
|
|
|
|
2024-04-25 21:12:08 +02:00
|
|
|
const BlendTreeConnectionResource* FindConnectionForSocket(
|
|
|
|
const AnimNodeResource* node,
|
|
|
|
const std::string& socket_name) const;
|
|
|
|
|
2024-04-21 12:42:49 +02:00
|
|
|
bool IsSocketConnected(
|
2024-04-25 21:12:08 +02:00
|
|
|
const AnimNodeResource* node,
|
|
|
|
const std::string& socket_name) const {
|
|
|
|
const BlendTreeConnectionResource* connection =
|
|
|
|
FindConnectionForSocket(node, socket_name);
|
|
|
|
return connection != nullptr;
|
2024-04-21 12:42:49 +02:00
|
|
|
}
|
|
|
|
|
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++) {
|
2024-04-05 00:18:07 +02:00
|
|
|
for (size_t j = 0,
|
|
|
|
num_inputs = instance_node_descriptors[i]->m_inputs.size();
|
2024-03-17 22:06:27 +01:00
|
|
|
j < num_inputs;
|
|
|
|
j++) {
|
|
|
|
Socket& input = instance_node_descriptors[i]->m_inputs[j];
|
|
|
|
|
|
|
|
if (*input.m_reference.ptr_ptr == nullptr) {
|
2024-04-05 00:18:07 +02:00
|
|
|
memcpy(
|
|
|
|
&input.m_value,
|
|
|
|
&m_nodes[i]->m_socket_accessor->m_inputs[j].m_value,
|
|
|
|
sizeof(Socket::SocketValue));
|
2024-03-17 22:06:27 +01:00
|
|
|
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-04-01 12:33:23 +02:00
|
|
|
|
|
|
|
size_t GetNodeIndexForOutputSocket(const std::string& socket_name) const {
|
|
|
|
for (size_t i = 0; i < m_connections.size(); i++) {
|
|
|
|
const BlendTreeConnectionResource& connection = m_connections[i];
|
2024-04-05 00:18:07 +02:00
|
|
|
if (connection.target_node_index == 0
|
|
|
|
&& connection.target_socket_name == socket_name) {
|
2024-04-01 12:33:23 +02:00
|
|
|
return connection.source_node_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 00:18:07 +02:00
|
|
|
std::cerr << "Error: could not find a node connected to output '"
|
|
|
|
<< socket_name << "'." << std::endl;
|
2024-04-01 12:33:23 +02:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetNodeIndexForInputSocket(const std::string& socket_name) const {
|
|
|
|
for (size_t i = 0; i < m_connections.size(); i++) {
|
|
|
|
const BlendTreeConnectionResource& connection = m_connections[i];
|
2024-04-05 00:18:07 +02:00
|
|
|
if (connection.source_node_index == 1
|
|
|
|
&& connection.source_socket_name == socket_name) {
|
2024-04-01 12:33:23 +02:00
|
|
|
return connection.target_node_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 00:18:07 +02:00
|
|
|
std::cerr << "Error: could not find a node connected to input '"
|
|
|
|
<< socket_name << "'." << std::endl;
|
2024-04-01 12:33:23 +02:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2024-04-16 22:11:59 +02:00
|
|
|
|
2024-04-21 12:42:49 +02:00
|
|
|
void UpdateTreeTopologyInfo();
|
|
|
|
|
|
|
|
[[nodiscard]] const std::vector<size_t>& GetNodeEvalOrder() const {
|
|
|
|
return m_node_eval_order;
|
|
|
|
}
|
|
|
|
|
2024-04-16 22:11:59 +02:00
|
|
|
private:
|
2024-04-21 12:42:49 +02:00
|
|
|
void UpdateNodeEvalOrder() {
|
|
|
|
m_node_eval_order.clear();
|
|
|
|
UpdateNodeEvalOrderRecursive(0);
|
|
|
|
}
|
|
|
|
void UpdateNodeEvalOrderRecursive(size_t node_index);
|
|
|
|
void UpdateNodeSubtrees();
|
|
|
|
|
2024-04-16 22:11:59 +02:00
|
|
|
std::vector<AnimNodeResource*> m_nodes;
|
|
|
|
std::vector<BlendTreeConnectionResource> m_connections;
|
2024-04-21 12:42:49 +02:00
|
|
|
std::vector<size_t> m_node_eval_order;
|
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;
|
|
|
|
};
|
|
|
|
|
2024-04-05 00:18:07 +02:00
|
|
|
struct AnimGraphResource : AnimNodeResource {
|
2024-03-24 21:50:22 +01:00
|
|
|
std::string m_graph_type_name;
|
2024-03-17 22:06:27 +01:00
|
|
|
|
|
|
|
BlendTreeResource m_blend_tree_resource;
|
2024-04-08 21:52:03 +02:00
|
|
|
typedef std::pair<const AnimNodeResource*, std::string> NodeSocketPair;
|
|
|
|
typedef std::map<NodeSocketPair, int> NodeSocketDataOffsetMap;
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
StateMachineResource m_state_machine_resource;
|
|
|
|
|
2024-04-05 00:44:37 +02:00
|
|
|
void Clear() { m_blend_tree_resource.Reset(); }
|
2024-03-17 22:06:27 +01:00
|
|
|
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;
|
|
|
|
void CreateBlendTreeRuntimeNodeInstances(AnimGraphBlendTree& result) const;
|
2024-04-08 21:52:03 +02:00
|
|
|
void PrepareBlendTreeIOData(
|
|
|
|
AnimGraphBlendTree& instance,
|
|
|
|
NodeSocketDataOffsetMap& node_offset_map) const;
|
|
|
|
void CreateBlendTreeConnectionInstances(
|
|
|
|
AnimGraphBlendTree& instance,
|
|
|
|
NodeSocketDataOffsetMap& node_offset_map) const;
|
2024-03-17 22:06:27 +01:00
|
|
|
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
|
|
|
|
2024-03-24 21:50:22 +01:00
|
|
|
static inline AnimNodeResource* AnimNodeResourceFactory(
|
|
|
|
const std::string& node_type_name) {
|
|
|
|
AnimNodeResource* result;
|
|
|
|
|
|
|
|
if (node_type_name == "BlendTree") {
|
|
|
|
AnimGraphResource* blend_tree_resource = new AnimGraphResource();
|
|
|
|
blend_tree_resource->m_blend_tree_resource.InitGraphConnectors();
|
|
|
|
result = blend_tree_resource;
|
|
|
|
} else {
|
|
|
|
result = new AnimNodeResource();
|
|
|
|
}
|
|
|
|
|
|
|
|
result->m_node_type_name = node_type_name;
|
|
|
|
|
|
|
|
if (node_type_name == "BlendTreeSockets") {
|
|
|
|
result->m_anim_node = AnimNodeFactory("BlendTree");
|
|
|
|
result->m_socket_accessor = new NodeDescriptorBase();
|
|
|
|
} else {
|
|
|
|
result->m_anim_node = AnimNodeFactory(node_type_name);
|
|
|
|
result->m_socket_accessor =
|
|
|
|
AnimNodeDescriptorFactory(node_type_name, result->m_anim_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-03-25 11:46:44 +01:00
|
|
|
#endif //ANIMTESTBED_ANIMGRAPHRESOURCE_H
|