2022-02-11 16:51:18 +01:00
|
|
|
//
|
2024-03-17 22:06:27 +01:00
|
|
|
// Created by martin on 17.03.24.
|
2022-02-11 16:51:18 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "AnimGraphResource.h"
|
|
|
|
|
2023-04-01 22:53:53 +02:00
|
|
|
#include <cstring>
|
2023-04-02 16:26:24 +02:00
|
|
|
#include <fstream>
|
2022-02-14 22:37:19 +01:00
|
|
|
|
2022-02-11 16:51:18 +01:00
|
|
|
#include "3rdparty/json/json.hpp"
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
#include "AnimGraphBlendTree.h"
|
|
|
|
#include "AnimGraphNodes.h"
|
|
|
|
|
2022-02-11 16:51:18 +01:00
|
|
|
using json = nlohmann::json;
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
//
|
|
|
|
// Socket <-> json
|
|
|
|
//
|
|
|
|
std::string sSocketTypeToStr(SocketType pin_type) {
|
2022-03-25 10:59:12 +01:00
|
|
|
if (pin_type < SocketType::SocketTypeUndefined
|
|
|
|
|| pin_type >= SocketType::SocketTypeLast) {
|
|
|
|
return "Unknown";
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 10:59:12 +01:00
|
|
|
return SocketTypeNames[static_cast<int>(pin_type)];
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
json sSocketToJson(const Socket& socket) {
|
2022-02-11 16:51:18 +01:00
|
|
|
json result;
|
2022-02-14 22:37:19 +01:00
|
|
|
result["name"] = socket.m_name;
|
|
|
|
result["type"] = sSocketTypeToStr(socket.m_type);
|
2022-02-18 23:33:30 +01:00
|
|
|
|
2023-04-02 16:26:24 +02:00
|
|
|
if (socket.m_type == SocketType::SocketTypeString
|
2023-04-15 22:07:22 +02:00
|
|
|
&& !socket.m_value_string.empty()) {
|
2023-04-01 22:53:53 +02:00
|
|
|
result["value"] = socket.m_value_string;
|
|
|
|
} else if (socket.m_value.flag) {
|
2022-02-18 23:33:30 +01:00
|
|
|
if (socket.m_type == SocketType::SocketTypeBool) {
|
2022-04-11 16:46:09 +02:00
|
|
|
result["value"] = socket.m_value.flag;
|
2022-02-18 23:33:30 +01:00
|
|
|
} else if (socket.m_type == SocketType::SocketTypeAnimation) {
|
2023-04-02 21:40:49 +02:00
|
|
|
} else if (socket.m_type == SocketType::SocketTypeInt) {
|
|
|
|
result["value"] = socket.m_value.int_value;
|
2022-02-18 23:33:30 +01:00
|
|
|
} else if (socket.m_type == SocketType::SocketTypeFloat) {
|
2022-04-11 16:46:09 +02:00
|
|
|
result["value"] = socket.m_value.float_value;
|
2022-02-18 23:33:30 +01:00
|
|
|
} else if (socket.m_type == SocketType::SocketTypeVec3) {
|
2023-03-30 23:50:07 +02:00
|
|
|
result["value"][0] = socket.m_value.vec3.v[0];
|
|
|
|
result["value"][1] = socket.m_value.vec3.v[1];
|
|
|
|
result["value"][2] = socket.m_value.vec3.v[2];
|
2022-02-18 23:33:30 +01:00
|
|
|
} else if (socket.m_type == SocketType::SocketTypeQuat) {
|
2023-03-30 23:50:07 +02:00
|
|
|
result["value"][0] = socket.m_value.quat.v[0];
|
|
|
|
result["value"][1] = socket.m_value.quat.v[1];
|
|
|
|
result["value"][2] = socket.m_value.quat.v[2];
|
|
|
|
result["value"][3] = socket.m_value.quat.v[3];
|
2022-02-18 23:33:30 +01:00
|
|
|
} else {
|
|
|
|
std::cerr << "Invalid socket type '" << static_cast<int>(socket.m_type)
|
|
|
|
<< "'." << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2022-02-14 22:37:19 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket sJsonToSocket(const json& json_data) {
|
|
|
|
Socket result;
|
|
|
|
result.m_type = SocketType::SocketTypeUndefined;
|
2023-04-15 21:52:01 +02:00
|
|
|
result.m_reference.ptr = &result.m_value.int_value;
|
2022-02-14 22:37:19 +01:00
|
|
|
result.m_name = json_data["name"];
|
|
|
|
|
|
|
|
std::string type_string = json_data["type"];
|
2023-04-01 22:53:53 +02:00
|
|
|
bool have_value = json_data.contains("value");
|
2022-02-14 22:37:19 +01:00
|
|
|
|
|
|
|
if (type_string == "Bool") {
|
|
|
|
result.m_type = SocketType::SocketTypeBool;
|
|
|
|
result.m_type_size = sizeof(bool);
|
2023-04-15 21:52:01 +02:00
|
|
|
result.m_reference.ptr = &result.m_value.int_value;
|
|
|
|
|
2023-04-01 22:53:53 +02:00
|
|
|
if (have_value) {
|
|
|
|
result.m_value.flag = json_data["value"];
|
|
|
|
}
|
2022-02-14 22:37:19 +01:00
|
|
|
} else if (type_string == "Animation") {
|
|
|
|
result.m_type = SocketType::SocketTypeAnimation;
|
|
|
|
result.m_type_size = sizeof(AnimData);
|
2023-04-02 21:40:49 +02:00
|
|
|
} else if (type_string == "Int") {
|
|
|
|
result.m_type = SocketType::SocketTypeInt;
|
|
|
|
result.m_type_size = sizeof(int);
|
|
|
|
if (have_value) {
|
|
|
|
result.m_value.int_value = json_data["value"];
|
|
|
|
}
|
2022-02-14 22:37:19 +01:00
|
|
|
} else if (type_string == "Float") {
|
|
|
|
result.m_type = SocketType::SocketTypeFloat;
|
|
|
|
result.m_type_size = sizeof(float);
|
2023-04-01 22:53:53 +02:00
|
|
|
if (have_value) {
|
|
|
|
result.m_value.float_value = json_data["value"];
|
|
|
|
}
|
2022-02-14 22:37:19 +01:00
|
|
|
} else if (type_string == "Vec3") {
|
|
|
|
result.m_type = SocketType::SocketTypeVec3;
|
|
|
|
result.m_type_size = sizeof(Vec3);
|
2023-04-01 22:53:53 +02:00
|
|
|
if (have_value) {
|
|
|
|
result.m_value.vec3.x = json_data["value"][0];
|
|
|
|
result.m_value.vec3.y = json_data["value"][1];
|
|
|
|
result.m_value.vec3.z = json_data["value"][2];
|
|
|
|
}
|
2022-02-14 22:37:19 +01:00
|
|
|
} else if (type_string == "Quat") {
|
|
|
|
result.m_type = SocketType::SocketTypeQuat;
|
|
|
|
result.m_type_size = sizeof(Quat);
|
2023-04-01 22:53:53 +02:00
|
|
|
if (have_value) {
|
|
|
|
result.m_value.quat.x = json_data["value"][0];
|
|
|
|
result.m_value.quat.y = json_data["value"][1];
|
|
|
|
result.m_value.quat.z = json_data["value"][2];
|
|
|
|
result.m_value.quat.w = json_data["value"][3];
|
|
|
|
}
|
2022-02-14 22:37:19 +01:00
|
|
|
} else if (type_string == "String") {
|
|
|
|
result.m_type = SocketType::SocketTypeString;
|
|
|
|
result.m_type_size = sizeof(std::string);
|
2023-04-01 22:53:53 +02:00
|
|
|
if (have_value) {
|
|
|
|
result.m_value_string = json_data["value"];
|
|
|
|
}
|
2022-02-14 22:37:19 +01:00
|
|
|
} else {
|
|
|
|
std::cerr << "Invalid socket type '" << type_string << "'." << std::endl;
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:51:18 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
//
|
|
|
|
// AnimGraphNode <-> json
|
|
|
|
//
|
2023-04-01 22:53:53 +02:00
|
|
|
json sAnimGraphNodeToJson(
|
|
|
|
const AnimNodeResource& node,
|
2023-04-15 22:07:22 +02:00
|
|
|
size_t node_index,
|
2024-03-17 22:06:27 +01:00
|
|
|
const std::vector<BlendTreeConnectionResource>& connections) {
|
2022-02-11 16:51:18 +01:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
for (const auto& socket : node.m_socket_accessor->m_inputs) {
|
2023-04-01 22:53:53 +02:00
|
|
|
if (socket.m_type == SocketType::SocketTypeAnimation) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool socket_connected = false;
|
2024-03-17 22:06:27 +01:00
|
|
|
for (const auto& connection : connections) {
|
2023-04-15 22:07:22 +02:00
|
|
|
if (connection.source_node_index == node_index
|
|
|
|
&& connection.source_socket_name == socket.m_name) {
|
2023-04-01 22:53:53 +02:00
|
|
|
socket_connected = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!socket_connected) {
|
2023-04-02 16:26:24 +02:00
|
|
|
result["inputs"].push_back(sSocketToJson(socket));
|
2023-04-01 22:53:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
for (auto& property : node.m_socket_accessor->m_properties) {
|
2022-02-18 23:33:30 +01:00
|
|
|
result["properties"][property.m_name] = sSocketToJson(property);
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:51:18 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
AnimNodeResource sAnimGraphNodeFromJson(
|
|
|
|
const json& json_node,
|
|
|
|
size_t node_index) {
|
2022-02-11 16:51:18 +01:00
|
|
|
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);
|
2022-02-14 22:37:19 +01:00
|
|
|
result.m_socket_accessor =
|
2023-03-30 23:50:07 +02:00
|
|
|
AnimNodeDescriptorFactory(result.m_type_name, result.m_anim_node);
|
2022-02-11 16:51:18 +01:00
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
for (auto& property : result.m_socket_accessor->m_properties) {
|
2023-04-01 22:53:53 +02:00
|
|
|
property = sJsonToSocket(json_node["properties"][property.m_name]);
|
|
|
|
}
|
|
|
|
|
2023-04-02 16:26:24 +02:00
|
|
|
if (node_index != 0 && node_index != 1 && json_node.contains("inputs")) {
|
|
|
|
for (size_t j = 0, n = json_node["inputs"].size(); j < n; j++) {
|
|
|
|
assert(json_node["inputs"][j].contains("name"));
|
|
|
|
std::string input_name = json_node["inputs"][j]["name"];
|
|
|
|
Socket* input_socket =
|
|
|
|
result.m_socket_accessor->GetInputSocket(input_name.c_str());
|
|
|
|
if (input_socket == nullptr) {
|
|
|
|
std::cerr << "Could not find input socket with name " << input_name
|
|
|
|
<< " for node type " << result.m_type_name << std::endl;
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
*input_socket = sJsonToSocket(json_node["inputs"][j]);
|
2022-02-18 23:33:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:51:18 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:37:19 +01:00
|
|
|
//
|
2022-04-01 13:19:54 +02:00
|
|
|
// AnimGraphConnectionResource <-> Json
|
2022-02-14 22:37:19 +01:00
|
|
|
//
|
2024-03-17 22:06:27 +01:00
|
|
|
json sAnimGraphConnectionToJson(const BlendTreeConnectionResource& connection) {
|
2022-02-11 16:51:18 +01:00
|
|
|
json result;
|
|
|
|
|
2022-04-01 13:19:54 +02:00
|
|
|
result["type"] = "AnimGraphConnectionResource";
|
2022-02-11 16:51:18 +01:00
|
|
|
|
2022-04-01 13:19:54 +02:00
|
|
|
result["source_node_index"] = connection.source_node_index;
|
|
|
|
result["source_socket_name"] = connection.source_socket_name;
|
2022-02-11 16:51:18 +01:00
|
|
|
|
2022-04-01 13:19:54 +02:00
|
|
|
result["target_node_index"] = connection.target_node_index;
|
|
|
|
result["target_socket_name"] = connection.target_socket_name;
|
2022-02-11 16:51:18 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
BlendTreeConnectionResource sAnimGraphConnectionFromJson(
|
2022-04-01 13:19:54 +02:00
|
|
|
const json& json_node) {
|
2024-03-17 22:06:27 +01:00
|
|
|
BlendTreeConnectionResource connection;
|
2022-02-11 16:51:18 +01:00
|
|
|
|
2022-04-01 13:19:54 +02:00
|
|
|
connection.source_node_index = json_node["source_node_index"];
|
|
|
|
connection.source_socket_name = json_node["source_socket_name"];
|
2022-02-11 16:51:18 +01:00
|
|
|
|
2022-04-01 13:19:54 +02:00
|
|
|
connection.target_node_index = json_node["target_node_index"];
|
|
|
|
connection.target_socket_name = json_node["target_socket_name"];
|
2022-02-11 16:51:18 +01:00
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
bool AnimGraphResource::LoadFromFile(const char* filename) {
|
2022-02-11 16:51:18 +01:00
|
|
|
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()) {
|
2022-02-14 22:37:19 +01:00
|
|
|
std::cerr << "Error parsing json of file '" << filename << "'."
|
|
|
|
<< std::endl;
|
2024-03-17 22:06:27 +01:00
|
|
|
|
|
|
|
return false;
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
if (json_data["type"] != "AnimNodeResource") {
|
2022-02-14 22:37:19 +01:00
|
|
|
std::cerr
|
2024-03-17 22:06:27 +01:00
|
|
|
<< "Invalid json object. Expected type 'AnimNodeResource' but got '"
|
2022-02-14 22:37:19 +01:00
|
|
|
<< json_data["type"] << "'." << std::endl;
|
2024-03-17 22:06:27 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_data["node_type"] == "BlendTree") {
|
|
|
|
return LoadBlendTreeResourceFromJson(json_data);
|
|
|
|
} else if (json_data["node_type"] == "StateMachine") {
|
|
|
|
return LoadStateMachineResourceFromJson(json_data);
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
std::cerr << "Invalid node_type. Expected type 'BlendTree' or "
|
|
|
|
"'StateMachine' but got '"
|
|
|
|
<< json_data["node_type"] << "'." << std::endl;
|
2022-02-11 16:51:18 +01:00
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AnimGraphResource::LoadBlendTreeResourceFromJson(nlohmann::json const& json_data) {
|
|
|
|
m_blend_tree_resource.Reset();
|
|
|
|
m_type = "BlendTree";
|
2022-02-11 16:51:18 +01:00
|
|
|
m_name = json_data["name"];
|
2022-04-01 13:19:54 +02:00
|
|
|
|
2022-03-25 11:23:03 +01:00
|
|
|
// Load nodes
|
2023-04-02 16:26:24 +02:00
|
|
|
for (size_t i = 0, n = json_data["nodes"].size(); i < n; i++) {
|
2022-02-11 16:51:18 +01:00
|
|
|
const json& json_node = json_data["nodes"][i];
|
|
|
|
if (json_node["type"] != "AnimNodeResource") {
|
2022-02-14 22:37:19 +01:00
|
|
|
std::cerr
|
|
|
|
<< "Invalid json object. Expected type 'AnimNodeResource' but got '"
|
|
|
|
<< json_node["type"] << "'." << std::endl;
|
2022-02-11 16:51:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-02 16:26:24 +02:00
|
|
|
AnimNodeResource node = sAnimGraphNodeFromJson(json_node, i);
|
2024-03-17 22:06:27 +01:00
|
|
|
m_blend_tree_resource.m_nodes.push_back(node);
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
2022-04-01 13:19:54 +02:00
|
|
|
|
2024-03-20 22:33:31 +01:00
|
|
|
// Graph outputs
|
2022-03-25 11:23:03 +01:00
|
|
|
const json& graph_outputs = json_data["nodes"][0]["inputs"];
|
2024-03-17 22:06:27 +01:00
|
|
|
for (const auto& graph_output : graph_outputs) {
|
|
|
|
AnimNodeResource& graph_node = m_blend_tree_resource.m_nodes[0];
|
2022-03-25 11:23:03 +01:00
|
|
|
graph_node.m_socket_accessor->m_inputs.push_back(
|
2023-04-15 22:07:22 +02:00
|
|
|
sJsonToSocket(graph_output));
|
2022-03-25 11:23:03 +01:00
|
|
|
}
|
2022-04-01 13:19:54 +02:00
|
|
|
|
2024-03-20 22:33:31 +01:00
|
|
|
// Graph inputs (optional)
|
|
|
|
if (json_data["nodes"][1].contains("outputs")) {
|
|
|
|
const json& graph_inputs = json_data["nodes"][1]["outputs"];
|
|
|
|
for (const auto& graph_input : graph_inputs) {
|
|
|
|
AnimNodeResource& graph_node = m_blend_tree_resource.m_nodes[1];
|
|
|
|
graph_node.m_socket_accessor->m_outputs.push_back(
|
|
|
|
sJsonToSocket(graph_input));
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 16:51:18 +01:00
|
|
|
|
2022-03-25 11:23:03 +01:00
|
|
|
// Load connections
|
2024-03-17 22:06:27 +01:00
|
|
|
for (const auto& json_connection : json_data["connections"]) {
|
2022-04-01 13:19:54 +02:00
|
|
|
if (json_connection["type"] != "AnimGraphConnectionResource") {
|
2024-03-17 22:06:27 +01:00
|
|
|
std::cerr << "Invalid json object. Expected type "
|
|
|
|
"'AnimGraphConnectionResource' "
|
|
|
|
"but got '"
|
|
|
|
<< json_connection["type"] << "'." << std::endl;
|
2022-02-11 16:51:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
BlendTreeConnectionResource connection =
|
2023-04-15 22:07:22 +02:00
|
|
|
sAnimGraphConnectionFromJson(json_connection);
|
2024-03-17 22:06:27 +01:00
|
|
|
m_blend_tree_resource.m_connections.push_back(connection);
|
2022-02-11 16:51:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:33:30 +01:00
|
|
|
return true;
|
2022-04-01 13:19:54 +02:00
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
bool AnimGraphResource::SaveToFile(const char* filename) const {
|
|
|
|
if (m_type == "BlendTree") {
|
|
|
|
return SaveBlendTreeResourceToFile(filename);
|
|
|
|
} else if (m_type == "StateMachine") {
|
|
|
|
return SaveStateMachineResourceToFile(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "Invalid AnimGraphResource type: " << m_type << "." << std::endl;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AnimGraphResource::SaveBlendTreeResourceToFile(
|
|
|
|
const char* filename) const {
|
|
|
|
json result;
|
|
|
|
|
|
|
|
result["name"] = m_name;
|
|
|
|
result["type"] = "AnimNodeResource";
|
|
|
|
result["node_type"] = "BlendTree";
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_blend_tree_resource.m_nodes.size(); i++) {
|
|
|
|
const AnimNodeResource& node = m_blend_tree_resource.m_nodes[i];
|
|
|
|
result["nodes"][i] =
|
|
|
|
sAnimGraphNodeToJson(node, i, m_blend_tree_resource.m_connections);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_blend_tree_resource.m_connections.size(); i++) {
|
|
|
|
const BlendTreeConnectionResource& connection =
|
|
|
|
m_blend_tree_resource.m_connections[i];
|
|
|
|
result["connections"][i] = sAnimGraphConnectionToJson(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Graph inputs and outputs
|
|
|
|
{
|
|
|
|
const AnimNodeResource& graph_output_node =
|
|
|
|
m_blend_tree_resource.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_blend_tree_resource.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 << result.dump(4, ' ') << std::endl;
|
|
|
|
output_file.close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimGraphResource::CreateBlendTreeInstance(
|
|
|
|
AnimGraphBlendTree& result) const {
|
|
|
|
if (m_type != "BlendTree") {
|
|
|
|
std::cerr << "Invalid AnimGraphResource. Expected type 'BlendTree' but got '"
|
|
|
|
<< m_type << "'." << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
2022-04-01 13:19:54 +02:00
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
CreateBlendTreeRuntimeNodeInstances(result);
|
|
|
|
PrepareBlendTreeIOData(result);
|
|
|
|
SetRuntimeNodeProperties(result);
|
|
|
|
|
|
|
|
result.UpdateOrderedNodes();
|
|
|
|
result.ResetNodeStates();
|
2022-04-01 13:19:54 +02:00
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
void AnimGraphResource::CreateBlendTreeRuntimeNodeInstances(
|
|
|
|
AnimGraphBlendTree& result) const {
|
|
|
|
for (int i = 0; i < m_blend_tree_resource.m_nodes.size(); i++) {
|
|
|
|
const AnimNodeResource& node_resource = m_blend_tree_resource.m_nodes[i];
|
2023-04-15 22:07:22 +02:00
|
|
|
AnimNode* node = AnimNodeFactory(node_resource.m_type_name);
|
2022-04-01 13:19:54 +02:00
|
|
|
node->m_name = node_resource.m_name;
|
|
|
|
node->m_node_type_name = node_resource.m_type_name;
|
2024-03-17 22:06:27 +01:00
|
|
|
result.m_nodes.push_back(node);
|
2022-04-01 13:19:54 +02:00
|
|
|
|
|
|
|
// runtime node connections
|
2024-03-17 22:06:27 +01:00
|
|
|
result.m_node_input_connections.emplace_back();
|
|
|
|
result.m_node_output_connections.emplace_back();
|
2022-04-01 13:19:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
void AnimGraphResource::PrepareBlendTreeIOData(
|
|
|
|
AnimGraphBlendTree& instance) const {
|
2023-04-01 14:16:20 +02:00
|
|
|
instance.m_node_descriptor =
|
2023-03-30 23:50:07 +02:00
|
|
|
AnimNodeDescriptorFactory("BlendTree", instance.m_nodes[0]);
|
2023-04-01 14:16:20 +02:00
|
|
|
instance.m_node_descriptor->m_outputs =
|
2024-03-17 22:06:27 +01:00
|
|
|
m_blend_tree_resource.m_nodes[1].m_socket_accessor->m_outputs;
|
|
|
|
instance.m_node_descriptor->m_inputs =
|
|
|
|
m_blend_tree_resource.m_nodes[0].m_socket_accessor->m_inputs;
|
2022-04-01 13:19:54 +02:00
|
|
|
|
2023-04-02 16:26:24 +02:00
|
|
|
//
|
|
|
|
// graph inputs
|
|
|
|
//
|
2022-04-01 13:19:54 +02:00
|
|
|
int input_block_size = 0;
|
|
|
|
std::vector<Socket>& graph_inputs = instance.getGraphInputs();
|
|
|
|
for (int i = 0; i < graph_inputs.size(); i++) {
|
|
|
|
input_block_size += sizeof(void*);
|
|
|
|
}
|
2023-03-26 23:39:11 +02:00
|
|
|
|
|
|
|
if (input_block_size > 0) {
|
|
|
|
instance.m_input_buffer = new char[input_block_size];
|
|
|
|
memset(instance.m_input_buffer, 0, input_block_size);
|
|
|
|
}
|
2022-04-01 13:19:54 +02:00
|
|
|
|
|
|
|
int input_block_offset = 0;
|
|
|
|
for (int i = 0; i < graph_inputs.size(); i++) {
|
2022-04-11 16:46:09 +02:00
|
|
|
graph_inputs[i].m_reference.ptr =
|
2022-04-01 13:19:54 +02:00
|
|
|
(void*)&instance.m_input_buffer[input_block_offset];
|
2023-04-01 14:16:20 +02:00
|
|
|
instance.m_node_descriptor->m_outputs[i].m_reference.ptr =
|
|
|
|
&instance.m_input_buffer[input_block_offset];
|
2022-04-01 13:19:54 +02:00
|
|
|
input_block_offset += sizeof(void*);
|
|
|
|
}
|
|
|
|
|
2023-04-02 16:26:24 +02:00
|
|
|
//
|
|
|
|
// graph outputs
|
|
|
|
//
|
2022-04-01 13:19:54 +02:00
|
|
|
int output_block_size = 0;
|
|
|
|
std::vector<Socket>& graph_outputs = instance.getGraphOutputs();
|
|
|
|
for (int i = 0; i < graph_outputs.size(); i++) {
|
2023-04-01 14:16:20 +02:00
|
|
|
output_block_size += sizeof(void*);
|
2022-04-01 13:19:54 +02:00
|
|
|
}
|
2023-03-26 23:39:11 +02:00
|
|
|
|
|
|
|
if (output_block_size > 0) {
|
|
|
|
instance.m_output_buffer = new char[output_block_size];
|
|
|
|
memset(instance.m_output_buffer, 0, output_block_size);
|
|
|
|
}
|
2022-04-01 13:19:54 +02:00
|
|
|
|
|
|
|
int output_block_offset = 0;
|
|
|
|
for (int i = 0; i < graph_outputs.size(); i++) {
|
2023-04-01 14:16:20 +02:00
|
|
|
instance.m_node_descriptor->m_inputs[i].m_reference.ptr =
|
2022-04-14 18:03:36 +02:00
|
|
|
&instance.m_output_buffer[output_block_offset];
|
2023-04-01 14:16:20 +02:00
|
|
|
output_block_offset += sizeof(void*);
|
2022-04-01 13:19:54 +02:00
|
|
|
}
|
2023-03-30 23:50:07 +02:00
|
|
|
|
|
|
|
// connections: make source and target sockets point to the same address in the connection data storage.
|
2023-04-01 14:16:20 +02:00
|
|
|
// TODO: instead of every connection, only create data blocks for the source sockets and make sure every source socket gets allocated once.
|
2023-04-15 22:07:22 +02:00
|
|
|
size_t connection_data_storage_size = 0;
|
2024-03-17 22:06:27 +01:00
|
|
|
for (const auto& connection : m_blend_tree_resource.m_connections) {
|
|
|
|
const AnimNodeResource& source_node =
|
|
|
|
m_blend_tree_resource.m_nodes[connection.source_node_index];
|
2023-03-30 23:50:07 +02:00
|
|
|
Socket* source_socket = source_node.m_socket_accessor->GetOutputSocket(
|
|
|
|
connection.source_socket_name.c_str());
|
|
|
|
connection_data_storage_size += source_socket->m_type_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connection_data_storage_size > 0) {
|
|
|
|
instance.m_connection_data_storage = new char[connection_data_storage_size];
|
|
|
|
memset(instance.m_connection_data_storage, 0, connection_data_storage_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<NodeDescriptorBase*> instance_node_descriptors(
|
2024-03-17 22:06:27 +01:00
|
|
|
m_blend_tree_resource.m_nodes.size(),
|
2023-03-30 23:50:07 +02:00
|
|
|
nullptr);
|
2024-03-17 22:06:27 +01:00
|
|
|
for (int i = 0; i < m_blend_tree_resource.m_nodes.size(); i++) {
|
2023-03-30 23:50:07 +02:00
|
|
|
instance_node_descriptors[i] = AnimNodeDescriptorFactory(
|
2024-03-17 22:06:27 +01:00
|
|
|
m_blend_tree_resource.m_nodes[i].m_type_name,
|
2023-03-30 23:50:07 +02:00
|
|
|
instance.m_nodes[i]);
|
|
|
|
}
|
|
|
|
|
2023-04-01 14:16:20 +02:00
|
|
|
instance_node_descriptors[0]->m_inputs = instance.m_node_descriptor->m_inputs;
|
|
|
|
instance_node_descriptors[1]->m_outputs =
|
|
|
|
instance.m_node_descriptor->m_outputs;
|
2023-03-30 23:50:07 +02:00
|
|
|
|
2023-04-15 22:07:22 +02:00
|
|
|
size_t connection_data_offset = 0;
|
2024-03-17 22:06:27 +01:00
|
|
|
for (const auto& connection : m_blend_tree_resource.m_connections) {
|
2023-03-30 23:50:07 +02:00
|
|
|
NodeDescriptorBase* source_node_descriptor =
|
|
|
|
instance_node_descriptors[connection.source_node_index];
|
|
|
|
NodeDescriptorBase* target_node_descriptor =
|
|
|
|
instance_node_descriptors[connection.target_node_index];
|
|
|
|
|
|
|
|
AnimNode* source_node = instance.m_nodes[connection.source_node_index];
|
|
|
|
AnimNode* target_node = instance.m_nodes[connection.target_node_index];
|
|
|
|
|
|
|
|
Socket* source_socket = source_node_descriptor->GetOutputSocket(
|
|
|
|
connection.source_socket_name.c_str());
|
|
|
|
Socket* target_socket = target_node_descriptor->GetInputSocket(
|
|
|
|
connection.target_socket_name.c_str());
|
|
|
|
|
|
|
|
AnimGraphConnection instance_connection;
|
|
|
|
instance_connection.m_source_node = source_node;
|
|
|
|
instance_connection.m_source_socket = *source_socket;
|
|
|
|
instance_connection.m_target_node = target_node;
|
|
|
|
instance_connection.m_target_socket = *target_socket;
|
|
|
|
instance.m_node_input_connections[connection.target_node_index].push_back(
|
|
|
|
instance_connection);
|
|
|
|
instance.m_node_output_connections[connection.source_node_index].push_back(
|
|
|
|
instance_connection);
|
|
|
|
|
|
|
|
source_node_descriptor->SetOutputUnchecked(
|
|
|
|
connection.source_socket_name.c_str(),
|
|
|
|
&instance.m_connection_data_storage[connection_data_offset]);
|
|
|
|
|
|
|
|
target_node_descriptor->SetInputUnchecked(
|
|
|
|
connection.target_socket_name.c_str(),
|
|
|
|
&instance.m_connection_data_storage[connection_data_offset]);
|
|
|
|
|
|
|
|
if (source_socket->m_type == SocketType::SocketTypeAnimation) {
|
2023-04-01 14:16:20 +02:00
|
|
|
instance.m_animdata_blocks.push_back(
|
|
|
|
(AnimData*)(&instance
|
|
|
|
.m_connection_data_storage[connection_data_offset]));
|
2023-03-30 23:50:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
connection_data_offset += source_socket->m_type_size;
|
|
|
|
}
|
|
|
|
|
2023-04-02 16:26:24 +02:00
|
|
|
//
|
|
|
|
// const node inputs
|
|
|
|
//
|
|
|
|
std::vector<Socket*> const_inputs =
|
2024-03-17 22:06:27 +01:00
|
|
|
m_blend_tree_resource.GetConstantNodeInputs(instance_node_descriptors);
|
2023-04-15 22:07:22 +02:00
|
|
|
size_t const_node_inputs_buffer_size = 0;
|
2024-03-17 22:06:27 +01:00
|
|
|
for (auto& const_input : const_inputs) {
|
2023-04-15 22:07:22 +02:00
|
|
|
if (const_input->m_type == SocketType::SocketTypeString) {
|
2023-04-02 16:26:24 +02:00
|
|
|
// TODO: implement string const node input support
|
|
|
|
std::cerr << "Error: const inputs for strings not yet implemented!"
|
|
|
|
<< std::endl;
|
|
|
|
abort();
|
|
|
|
}
|
2023-04-15 22:07:22 +02:00
|
|
|
const_node_inputs_buffer_size += const_input->m_type_size;
|
2023-04-02 16:26:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (const_node_inputs_buffer_size > 0) {
|
|
|
|
instance.m_const_node_inputs = new char[const_node_inputs_buffer_size];
|
|
|
|
memset(instance.m_const_node_inputs, '\0', const_node_inputs_buffer_size);
|
|
|
|
}
|
|
|
|
|
2023-04-15 22:07:22 +02:00
|
|
|
size_t const_input_buffer_offset = 0;
|
2024-03-17 22:06:27 +01:00
|
|
|
for (auto& i : const_inputs) {
|
2023-04-15 22:07:22 +02:00
|
|
|
Socket* const_input = i;
|
2023-04-02 16:26:24 +02:00
|
|
|
|
|
|
|
// TODO: implement string const node input support
|
|
|
|
assert(const_input->m_type != SocketType::SocketTypeString);
|
|
|
|
|
|
|
|
*const_input->m_reference.ptr_ptr =
|
|
|
|
&instance.m_const_node_inputs[const_input_buffer_offset];
|
2024-03-17 22:06:27 +01:00
|
|
|
memcpy(
|
|
|
|
*const_input->m_reference.ptr_ptr,
|
|
|
|
&const_input->m_value,
|
|
|
|
i->m_type_size);
|
2023-04-02 16:26:24 +02:00
|
|
|
|
2023-04-15 22:07:22 +02:00
|
|
|
const_input_buffer_offset += i->m_type_size;
|
2023-04-02 16:26:24 +02:00
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
for (int i = 0; i < m_blend_tree_resource.m_nodes.size(); i++) {
|
2023-03-30 23:50:07 +02:00
|
|
|
delete instance_node_descriptors[i];
|
|
|
|
}
|
2022-04-01 13:19:54 +02:00
|
|
|
}
|
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
void AnimGraphResource::SetRuntimeNodeProperties(
|
|
|
|
AnimGraphBlendTree& result) const {
|
|
|
|
for (int i = 2; i < m_blend_tree_resource.m_nodes.size(); i++) {
|
|
|
|
const AnimNodeResource& node_resource = m_blend_tree_resource.m_nodes[i];
|
2022-04-11 16:46:09 +02:00
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
NodeDescriptorBase* node_instance_accessor =
|
|
|
|
AnimNodeDescriptorFactory(node_resource.m_type_name, result.m_nodes[i]);
|
2022-04-11 16:46:09 +02:00
|
|
|
|
2022-04-14 18:03:36 +02:00
|
|
|
std::vector<Socket>& resource_properties =
|
|
|
|
node_resource.m_socket_accessor->m_properties;
|
2024-03-17 22:06:27 +01:00
|
|
|
for (const auto& property : resource_properties) {
|
2022-04-11 16:46:09 +02:00
|
|
|
const std::string& name = property.m_name;
|
|
|
|
|
|
|
|
switch (property.m_type) {
|
|
|
|
case SocketType::SocketTypeBool:
|
2023-03-30 23:50:07 +02:00
|
|
|
node_instance_accessor->SetProperty(
|
|
|
|
name.c_str(),
|
2022-04-14 18:03:36 +02:00
|
|
|
property.m_value.flag);
|
2022-04-11 16:46:09 +02:00
|
|
|
break;
|
2023-04-02 21:40:49 +02:00
|
|
|
case SocketType::SocketTypeInt:
|
|
|
|
node_instance_accessor->SetProperty(
|
|
|
|
name.c_str(),
|
|
|
|
property.m_value.int_value);
|
|
|
|
break;
|
2022-04-11 16:46:09 +02:00
|
|
|
case SocketType::SocketTypeFloat:
|
2023-03-30 23:50:07 +02:00
|
|
|
node_instance_accessor->SetProperty(
|
|
|
|
name.c_str(),
|
2022-04-14 18:03:36 +02:00
|
|
|
property.m_value.float_value);
|
2022-04-11 16:46:09 +02:00
|
|
|
break;
|
|
|
|
case SocketType::SocketTypeVec3:
|
2023-03-30 23:50:07 +02:00
|
|
|
node_instance_accessor->SetProperty<Vec3>(
|
|
|
|
name.c_str(),
|
2022-04-14 18:03:36 +02:00
|
|
|
property.m_value.vec3);
|
2022-04-11 16:46:09 +02:00
|
|
|
break;
|
|
|
|
case SocketType::SocketTypeQuat:
|
2023-03-30 23:50:07 +02:00
|
|
|
node_instance_accessor->SetProperty(
|
|
|
|
name.c_str(),
|
2022-04-14 18:03:36 +02:00
|
|
|
property.m_value.quat);
|
2022-04-11 16:46:09 +02:00
|
|
|
break;
|
|
|
|
case SocketType::SocketTypeString:
|
2023-03-30 23:50:07 +02:00
|
|
|
node_instance_accessor->SetProperty(
|
|
|
|
name.c_str(),
|
|
|
|
property.m_value_string);
|
2022-04-11 16:46:09 +02:00
|
|
|
break;
|
|
|
|
default:
|
2022-04-14 18:03:36 +02:00
|
|
|
std::cerr << "Invalid socket type "
|
|
|
|
<< static_cast<int>(property.m_type) << std::endl;
|
2022-04-11 16:46:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete node_instance_accessor;
|
|
|
|
}
|
|
|
|
}
|
2023-04-02 16:26:24 +02:00
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
bool AnimGraphResource::SaveStateMachineResourceToFile(
|
|
|
|
const char* filename) const {
|
|
|
|
assert(false && "Not yet implemented");
|
2023-04-02 16:26:24 +02:00
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
return false;
|
|
|
|
}
|
2023-04-02 16:26:24 +02:00
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
bool AnimGraphResource::LoadStateMachineResourceFromJson(nlohmann::json const& json_data) {
|
|
|
|
assert(false && "Not yet implemented");
|
2023-04-02 16:26:24 +02:00
|
|
|
|
2024-03-17 22:06:27 +01:00
|
|
|
return false;
|
|
|
|
}
|