Store the input values of nodes if they are non-zero.

This commit is contained in:
Martin Felis
2023-04-01 22:53:53 +02:00
parent 3d55b748e6
commit 42303d5f47
3 changed files with 87 additions and 38 deletions
+58 -34
View File
@@ -5,6 +5,7 @@
#include "AnimGraphResource.h"
#include <fstream>
#include <cstring>
#include "3rdparty/json/json.hpp"
@@ -27,7 +28,9 @@ json sSocketToJson(const Socket& socket) {
result["name"] = socket.m_name;
result["type"] = sSocketTypeToStr(socket.m_type);
if (socket.m_reference.ptr != nullptr) {
if (socket.m_type == SocketType::SocketTypeString && socket.m_value_string.size() > 0) {
result["value"] = socket.m_value_string;
} else if (socket.m_value.flag) {
if (socket.m_type == SocketType::SocketTypeBool) {
result["value"] = socket.m_value.flag;
} else if (socket.m_type == SocketType::SocketTypeAnimation) {
@@ -42,8 +45,6 @@ json sSocketToJson(const Socket& socket) {
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];
} else if (socket.m_type == SocketType::SocketTypeString) {
result["value"] = socket.m_value_string;
} else {
std::cerr << "Invalid socket type '" << static_cast<int>(socket.m_type)
<< "'." << std::endl;
@@ -59,25 +60,46 @@ Socket sJsonToSocket(const json& json_data) {
result.m_name = json_data["name"];
std::string type_string = json_data["type"];
bool have_value = json_data.contains("value");
if (type_string == "Bool") {
result.m_type = SocketType::SocketTypeBool;
result.m_type_size = sizeof(bool);
if (have_value) {
result.m_value.flag = json_data["value"];
}
} else if (type_string == "Animation") {
result.m_type = SocketType::SocketTypeAnimation;
result.m_type_size = sizeof(AnimData);
} else if (type_string == "Float") {
result.m_type = SocketType::SocketTypeFloat;
result.m_type_size = sizeof(float);
if (have_value) {
result.m_value.float_value = json_data["value"];
}
} else if (type_string == "Vec3") {
result.m_type = SocketType::SocketTypeVec3;
result.m_type_size = sizeof(Vec3);
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];
}
} else if (type_string == "Quat") {
result.m_type = SocketType::SocketTypeQuat;
result.m_type_size = sizeof(Quat);
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];
}
} else if (type_string == "String") {
result.m_type = SocketType::SocketTypeString;
result.m_type_size = sizeof(std::string);
if (have_value) {
result.m_value_string = json_data["value"];
}
} else {
std::cerr << "Invalid socket type '" << type_string << "'." << std::endl;
}
@@ -88,7 +110,10 @@ Socket sJsonToSocket(const json& json_data) {
//
// AnimGraphNode <-> json
//
json sAnimGraphNodeToJson(const AnimNodeResource& node) {
json sAnimGraphNodeToJson(
const AnimNodeResource& node,
int node_index,
const std::vector<AnimGraphConnectionResource>& connections) {
json result;
result["name"] = node.m_name;
@@ -99,6 +124,27 @@ json sAnimGraphNodeToJson(const AnimNodeResource& node) {
result["position"][j] = node.m_position[j];
}
for (size_t j = 0, n = node.m_socket_accessor->m_inputs.size(); j < n; j++) {
const Socket& socket = node.m_socket_accessor->m_inputs[j];
if (socket.m_type == SocketType::SocketTypeAnimation) {
continue;
}
bool socket_connected = false;
for (size_t k = 0, m = connections.size(); k < m; k++) {
if (connections[k].source_node_index == node_index
&& connections[k].source_socket_name == socket.m_name) {
socket_connected = true;
break;
}
}
if (!socket_connected) {
result["inputs"][socket.m_name] = sSocketToJson(socket);
}
}
for (size_t j = 0, n = node.m_socket_accessor->m_properties.size(); j < n;
j++) {
Socket& property = node.m_socket_accessor->m_properties[j];
@@ -123,36 +169,14 @@ AnimNodeResource sAnimGraphNodeFromJson(const json& json_node) {
for (size_t j = 0, n = result.m_socket_accessor->m_properties.size(); j < n;
j++) {
Socket& property = result.m_socket_accessor->m_properties[j];
json json_property = json_node["properties"][property.m_name];
property = sJsonToSocket(json_node["properties"][property.m_name]);
}
if (sSocketTypeToStr(property.m_type) == json_property["type"]) {
if (property.m_type == SocketType::SocketTypeBool) {
property.m_value.flag = json_property["value"];
} else if (property.m_type == SocketType::SocketTypeAnimation) {
} else if (property.m_type == SocketType::SocketTypeFloat) {
property.m_value.float_value = json_property["value"];
} else if (property.m_type == SocketType::SocketTypeVec3) {
property.m_value.vec3.v[0] = json_property["value"][0];
property.m_value.vec3.v[1] = json_property["value"][1];
property.m_value.vec3.v[2] = json_property["value"][2];
} else if (property.m_type == SocketType::SocketTypeQuat) {
Quat* property_quat = reinterpret_cast<Quat*>(property.m_reference.ptr);
property.m_value.quat.v[0] = json_property["value"][0];
property.m_value.quat.v[1] = json_property["value"][1];
property.m_value.quat.v[2] = json_property["value"][2];
property.m_value.quat.v[3] = json_property["value"][3];
} else if (property.m_type == SocketType::SocketTypeString) {
property.m_value_string = json_property["value"].get<std::string>();
} else {
std::cerr << "Invalid type for property '" << property.m_name
<< "'. Cannot parse json to type '"
<< static_cast<int>(property.m_type) << std::endl;
break;
}
} else {
std::cerr << "Invalid type for property '" << property.m_name
<< "': expected " << sSocketTypeToStr(property.m_type)
<< " but got " << json_property["type"] << std::endl;
for (size_t j = 0, n = result.m_socket_accessor->m_inputs.size(); j < n;
j++) {
Socket& input = result.m_socket_accessor->m_inputs[j];
if (json_node.contains("inputs") && json_node["inputs"].contains(input.m_name)) {
input = sJsonToSocket(json_node["inputs"][input.m_name]);
}
}
@@ -226,7 +250,7 @@ bool AnimGraphResource::saveToFile(const char* filename) const {
for (size_t i = 0; i < m_nodes.size(); i++) {
const AnimNodeResource& node = m_nodes[i];
result["nodes"][i] = sAnimGraphNodeToJson(node);
result["nodes"][i] = sAnimGraphNodeToJson(node, i, m_connections);
}
for (size_t i = 0; i < m_connections.size(); i++) {