Working on unified BlendTree and StateMachine handling.
This commit is contained in:
@@ -1,23 +1,17 @@
|
||||
//
|
||||
// Created by martin on 04.02.22.
|
||||
// Created by martin on 17.03.24.
|
||||
//
|
||||
|
||||
#ifndef ANIMTESTBED_ANIMGRAPHRESOURCE_H
|
||||
#define ANIMTESTBED_ANIMGRAPHRESOURCE_H
|
||||
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "AnimGraph.h"
|
||||
#include "AnimGraphData.h"
|
||||
#include "AnimGraphNodes.h"
|
||||
#include "SyncTrack.h"
|
||||
|
||||
struct AnimNode;
|
||||
#include "3rdparty/json/json.hpp"
|
||||
|
||||
struct AnimGraphBlendTree;
|
||||
struct AnimGraphStateMachine;
|
||||
|
||||
struct AnimNodeResource {
|
||||
std::string m_name;
|
||||
@@ -37,40 +31,33 @@ static inline AnimNodeResource AnimNodeResourceFactory(
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// AnimGraphResource
|
||||
//
|
||||
struct AnimGraphConnectionResource {
|
||||
struct BlendTreeConnectionResource {
|
||||
size_t source_node_index = -1;
|
||||
std::string source_socket_name;
|
||||
size_t target_node_index = -1;
|
||||
std::string target_socket_name;
|
||||
};
|
||||
|
||||
struct AnimGraphResource {
|
||||
std::string m_name;
|
||||
struct BlendTreeResource {
|
||||
std::vector<AnimNodeResource> m_nodes;
|
||||
std::vector<AnimGraphConnectionResource> m_connections;
|
||||
std::vector<BlendTreeConnectionResource> m_connections;
|
||||
|
||||
~AnimGraphResource() {
|
||||
for (auto & m_node : m_nodes) {
|
||||
delete m_node.m_anim_node;
|
||||
delete m_node.m_socket_accessor;
|
||||
}
|
||||
void Reset() {
|
||||
m_nodes.clear();
|
||||
m_connections.clear();
|
||||
}
|
||||
|
||||
AnimGraphResource() { clear(); }
|
||||
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";
|
||||
}
|
||||
|
||||
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]; }
|
||||
|
||||
AnimNodeResource& getGraphOutputNode() { return m_nodes[0]; }
|
||||
AnimNodeResource& getGraphInputNode() { return m_nodes[1]; }
|
||||
|
||||
size_t getNodeIndex(const AnimNodeResource& node_resource) const {
|
||||
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;
|
||||
@@ -80,18 +67,13 @@ struct AnimGraphResource {
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t addNode(const AnimNodeResource &node_resource) {
|
||||
m_nodes.push_back(node_resource);
|
||||
return m_nodes.size() - 1;
|
||||
}
|
||||
|
||||
bool connectSockets(
|
||||
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_node_index = getNodeIndex(source_node);
|
||||
size_t target_node_index = getNodeIndex(target_node);
|
||||
size_t source_node_index = GetNodeIndex(source_node);
|
||||
size_t target_node_index = GetNodeIndex(target_node);
|
||||
|
||||
if (source_node_index >= m_nodes.size()
|
||||
|| target_node_index >= m_nodes.size()) {
|
||||
@@ -109,7 +91,7 @@ struct AnimGraphResource {
|
||||
return false;
|
||||
}
|
||||
|
||||
AnimGraphConnectionResource connection;
|
||||
BlendTreeConnectionResource connection;
|
||||
connection.source_node_index = source_node_index;
|
||||
connection.source_socket_name = source_socket_name;
|
||||
connection.target_node_index = target_node_index;
|
||||
@@ -119,28 +101,62 @@ struct AnimGraphResource {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isSocketConnected(
|
||||
const AnimNodeResource& node,
|
||||
const std::string& socket_name) {
|
||||
size_t node_index = getNodeIndex(node);
|
||||
for (const auto & connection : m_connections) {
|
||||
if ((connection.source_node_index == node_index
|
||||
&& connection.source_socket_name == socket_name)
|
||||
|| ((connection.target_node_index == node_index)
|
||||
&& connection.target_socket_name == socket_name)) {
|
||||
return true;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
void createInstance(AnimGraph& result) const;
|
||||
struct StateMachineTransitionResources {
|
||||
size_t source_state_index = -1;
|
||||
size_t target_state_index = -1;
|
||||
float blend_time = 0.f;
|
||||
bool sync_blend = false;
|
||||
};
|
||||
|
||||
void createRuntimeNodeInstances(AnimGraph& instance) const;
|
||||
void prepareGraphIOData(AnimGraph& instance) const;
|
||||
void setRuntimeNodeProperties(AnimGraph& instance) const;
|
||||
std::vector<Socket*> getConstNodeInputs(std::vector<NodeDescriptorBase*>& instance_node_descriptors) const;
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
bool SaveStateMachineResourceToFile(const char* filename) const;
|
||||
bool LoadStateMachineResourceFromJson(nlohmann::json const& json_data);
|
||||
};
|
||||
|
||||
#endif //ANIMTESTBED_ANIMGRAPHRESOURCE_H
|
||||
|
||||
Reference in New Issue
Block a user