diff --git a/shared/sdk/Math.hpp b/shared/sdk/Math.hpp index 8101cedf..70cb74b9 100644 --- a/shared/sdk/Math.hpp +++ b/shared/sdk/Math.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #define GLM_ENABLE_EXPERIMENTAL @@ -195,4 +196,78 @@ static glm::vec3 ue_euler_from_rotation_matrix(const glm::mat4& m) { return rotator; } + +static nlohmann::json to_json(const glm::vec3& v) { + return nlohmann::json{ { "x", v.x }, { "y", v.y }, { "z", v.z } }; +} + +static nlohmann::json to_json(const glm::vec4& v) { + return nlohmann::json{ { "x", v.x }, { "y", v.y }, { "z", v.z }, { "w", v.w } }; +} + +static nlohmann::json to_json(const glm::quat& q) { + return nlohmann::json{ { "x", q.x }, { "y", q.y }, { "z", q.z }, { "w", q.w } }; +} + +static glm::vec3 from_json_vec3(const nlohmann::json& j) { + glm::vec3 result{}; + + if (j.contains("x")) { + result.x = j["x"].get(); + } + + if (j.contains("y")) { + result.y = j["y"].get(); + } + + if (j.contains("z")) { + result.z = j["z"].get(); + } + + return result; +} + +static glm::vec4 from_json_vec4(const nlohmann::json& j) { + glm::vec4 result{}; + + if (j.contains("x")) { + result.x = j["x"].get(); + } + + if (j.contains("y")) { + result.y = j["y"].get(); + } + + if (j.contains("z")) { + result.z = j["z"].get(); + } + + if (j.contains("w")) { + result.w = j["w"].get(); + } + + return result; +} + +static glm::quat from_json_quat(const nlohmann::json& j) { + glm::quat result{}; + + if (j.contains("x")) { + result.x = j["x"].get(); + } + + if (j.contains("y")) { + result.y = j["y"].get(); + } + + if (j.contains("z")) { + result.z = j["z"].get(); + } + + if (j.contains("w")) { + result.w = j["w"].get(); + } + + return result; +} } \ No newline at end of file