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 {
|
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 {
|
2024-03-24 21:50:22 +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-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();
|
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-03-24 21:50:22 +01:00
|
|
|
m_nodes.push_back(AnimNodeResourceFactory("BlendTreeSockets"));
|
|
|
|
m_nodes[0]->m_name = "Outputs";
|
|
|
|
m_nodes.push_back(AnimNodeResourceFactory("BlendTreeSockets"));
|
|
|
|
m_nodes[1]->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-03-24 21:50:22 +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++) {
|
2024-03-24 21:50:22 +01:00
|
|
|
if (m_nodes[i] == node_resource) {
|
2022-03-25 11:46:44 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
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-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-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;
|
|
|
|
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;
|
|
|
|
void CreateBlendTreeRuntimeNodeInstances(AnimGraphBlendTree& result) const;
|
|
|
|
void PrepareBlendTreeIOData(AnimGraphBlendTree& instance) const;
|
2024-04-01 12:33:23 +02:00
|
|
|
void CreateBlendTreeConnectionInstances(AnimGraphBlendTree& instance) 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
|