feat: add vec3 type to nhollman json / fix: scene serialisation stuff

This commit is contained in:
2026-10-18 15:20:01 +02:00
parent 7223e9ce07
commit 94fa2bb4b2
18 changed files with 670 additions and 289 deletions
@@ -5,6 +5,7 @@
#include <destrum/ObjectModel/Component.h>
#include <destrum/Physics/PhysicsTypes.h>
#include <destrum/Serialization/GlmJson.h>
class Collider : public Component {
public:
@@ -31,23 +32,14 @@ public:
protected:
[[nodiscard]] nlohmann::json SerializeCollider() const {
return {
{"centerOffset", {m_CenterOffset.x, m_CenterOffset.y, m_CenterOffset.z}},
{"centerOffset", m_CenterOffset},
{"isTrigger", m_IsTrigger}
};
}
void DeserializeCollider(const nlohmann::json& data) {
if (data.contains("centerOffset")) {
const auto& offset = data.at("centerOffset");
m_CenterOffset = {
offset.at(0).get<float>(),
offset.at(1).get<float>(),
offset.at(2).get<float>()
};
}
if (data.contains("isTrigger")) {
m_IsTrigger = data.at("isTrigger").get<bool>();
}
m_CenterOffset = data.value("centerOffset", m_CenterOffset);
m_IsTrigger = data.value("isTrigger", m_IsTrigger);
}
bool m_IsTrigger{false};
@@ -1,12 +1,21 @@
#ifndef PRIMITIVEMESHES_H
#define PRIMITIVEMESHES_H
#include <optional>
#include <string>
#include <string_view>
#include <glm/glm.hpp>
#include <destrum/Graphics/Resources/CPUMesh.h>
namespace PrimitiveMeshes {
[[nodiscard]] CPUMesh MakeBox(const glm::vec3& size);
[[nodiscard]] std::string BoxKey(const glm::vec3& size);
[[nodiscard]] bool IsPrimitiveKey(std::string_view key);
[[nodiscard]] std::string_view StripDuplicateSuffix(std::string_view key);
[[nodiscard]] std::optional<CPUMesh> MakeFromKey(std::string_view key);
}
#endif // PRIMITIVEMESHES_H
@@ -52,7 +52,7 @@ public:
virtual std::string GetTypeName() const = 0;
virtual nlohmann::json Serialize() const {
return {};
return nlohmann::json::object();
}
virtual void Deserialize(const nlohmann::json&) {
@@ -0,0 +1,63 @@
#ifndef DESTRUM_GLMJSON_H
#define DESTRUM_GLMJSON_H
#include <stdexcept>
#include <string>
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
#include <nlohmann/json.hpp>
namespace destrum::json_detail {
inline void RequireNumberArray(const nlohmann::json& j, std::size_t size) {
if (!j.is_array() || j.size() != size) {
throw std::runtime_error(
"expected an array of " + std::to_string(size) + " numbers, got " + j.dump());
}
for (const auto& element : j) {
if (!element.is_number()) {
throw std::runtime_error(
"expected an array of " + std::to_string(size) + " numbers, got " + j.dump());
}
}
}
}
namespace nlohmann {
template <glm::length_t L, typename T, glm::qualifier Q>
struct adl_serializer<glm::vec<L, T, Q>> {
static void to_json(json& j, const glm::vec<L, T, Q>& value) {
j = json::array();
for (glm::length_t i = 0; i < L; ++i) {
j.push_back(value[i]);
}
}
static void from_json(const json& j, glm::vec<L, T, Q>& value) {
destrum::json_detail::RequireNumberArray(j, static_cast<std::size_t>(L));
for (glm::length_t i = 0; i < L; ++i) {
value[i] = j.at(static_cast<std::size_t>(i)).get<T>();
}
}
};
// Stored as [x, y, z, w], the same order as scene transforms
template <typename T, glm::qualifier Q>
struct adl_serializer<glm::qua<T, Q>> {
static void to_json(json& j, const glm::qua<T, Q>& value) {
j = json::array({value.x, value.y, value.z, value.w});
}
static void from_json(const json& j, glm::qua<T, Q>& value) {
destrum::json_detail::RequireNumberArray(j, 4);
value = glm::qua<T, Q>{
j.at(3).get<T>(),
j.at(0).get<T>(),
j.at(1).get<T>(),
j.at(2).get<T>()
};
}
};
}
#endif // DESTRUM_GLMJSON_H