From 7a0930a208cbcaa350ec60c4b48824ec95f0aed5 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 31 Mar 2024 13:07:06 +0200 Subject: [PATCH] chore: add json types for gltf --- src/ObjCommon/XModel/Gltf/JsonGltf.h | 256 +++++++++++++++++++++++++++ 1 file changed, 256 insertions(+) create mode 100644 src/ObjCommon/XModel/Gltf/JsonGltf.h diff --git a/src/ObjCommon/XModel/Gltf/JsonGltf.h b/src/ObjCommon/XModel/Gltf/JsonGltf.h new file mode 100644 index 00000000..5922df88 --- /dev/null +++ b/src/ObjCommon/XModel/Gltf/JsonGltf.h @@ -0,0 +1,256 @@ +#pragma once + +#include "Json/JsonExtension.h" +#include +#include +#include +#include + +namespace gltf +{ + class JsonAsset + { + public: + std::string version; + std::optional generator; + }; + + NLOHMANN_DEFINE_TYPE_EXTENSION(JsonAsset, version, generator); + + class JsonNode + { + public: + std::string name; + std::vector translation; + std::vector rotation; + std::vector scale; + std::vector children; + }; + + NLOHMANN_DEFINE_TYPE_EXTENSION(JsonNode, name, translation, rotation, scale, children); + + class JsonBuffer + { + public: + unsigned byteLength; + std::optional uri; + }; + + NLOHMANN_DEFINE_TYPE_EXTENSION(JsonBuffer, byteLength, uri); + + enum class JsonAccessorComponentType + { + SIGNED_BYTE = 5120, + UNSIGNED_BYTE = 5121, + SIGNED_SHORT = 5122, + UNSIGNED_SHORT = 5123, + UNSIGNED_INT = 5125, + FLOAT = 5126 + }; + + NLOHMANN_JSON_SERIALIZE_ENUM(JsonAccessorComponentType, + { + {JsonAccessorComponentType::SIGNED_BYTE, static_cast(JsonAccessorComponentType::SIGNED_BYTE) }, + {JsonAccessorComponentType::UNSIGNED_BYTE, static_cast(JsonAccessorComponentType::UNSIGNED_BYTE) }, + {JsonAccessorComponentType::SIGNED_SHORT, static_cast(JsonAccessorComponentType::SIGNED_SHORT) }, + {JsonAccessorComponentType::UNSIGNED_SHORT, static_cast(JsonAccessorComponentType::UNSIGNED_SHORT)}, + {JsonAccessorComponentType::UNSIGNED_INT, static_cast(JsonAccessorComponentType::UNSIGNED_INT) }, + {JsonAccessorComponentType::FLOAT, static_cast(JsonAccessorComponentType::FLOAT) }, + }); + + enum class JsonAccessorType + { + SCALAR, + VEC2, + VEC3, + VEC4, + MAT2, + MAT3, + MAT4 + }; + + NLOHMANN_JSON_SERIALIZE_ENUM(JsonAccessorType, + { + {JsonAccessorType::SCALAR, "SCALAR"}, + {JsonAccessorType::VEC2, "VEC2" }, + {JsonAccessorType::VEC3, "VEC3" }, + {JsonAccessorType::VEC4, "VEC4" }, + {JsonAccessorType::MAT2, "MAT2" }, + {JsonAccessorType::MAT3, "MAT3" }, + {JsonAccessorType::MAT4, "MAT4" }, + }); + + class JsonAccessor + { + public: + std::optional bufferView; + std::optional byteOffset; + JsonAccessorComponentType componentType; + // std::optional normalized + unsigned count; + JsonAccessorType type; + std::optional> max; + std::optional> min; + // std::optional sparse; + // std::optional name; + // extensions + // extras + }; + + NLOHMANN_DEFINE_TYPE_EXTENSION(JsonAccessor, bufferView, byteOffset, componentType, count, type, min, max); + + class JsonBufferView + { + public: + unsigned buffer; + unsigned byteLength; + unsigned byteOffset; + unsigned target; + }; + + NLOHMANN_DEFINE_TYPE_EXTENSION(JsonBufferView, buffer, byteLength, byteOffset, target); + + enum class JsonAnimationChannelTargetPath + { + TRANSLATION, + ROTATION, + SCALE, + WEIGHTS + }; + + NLOHMANN_JSON_SERIALIZE_ENUM(JsonAnimationChannelTargetPath, + { + {JsonAnimationChannelTargetPath::TRANSLATION, "translation"}, + {JsonAnimationChannelTargetPath::ROTATION, "rotation" }, + {JsonAnimationChannelTargetPath::SCALE, "scale" }, + {JsonAnimationChannelTargetPath::WEIGHTS, "weights" }, + }); + + class JsonAnimationChannelTarget + { + public: + unsigned node; + JsonAnimationChannelTargetPath path; + }; + + NLOHMANN_DEFINE_TYPE_EXTENSION(JsonAnimationChannelTarget, node, path); + + class JsonAnimationChannel + { + public: + unsigned sampler; + JsonAnimationChannelTarget target; + }; + + NLOHMANN_DEFINE_TYPE_EXTENSION(JsonAnimationChannel, sampler, target); + + enum class JsonAnimationSamplerInterpolation + { + LINEAR, + STEP, + CUBIC_SPLINE + }; + + NLOHMANN_JSON_SERIALIZE_ENUM(JsonAnimationSamplerInterpolation, + { + {JsonAnimationSamplerInterpolation::LINEAR, "LINEAR" }, + {JsonAnimationSamplerInterpolation::STEP, "STEP" }, + {JsonAnimationSamplerInterpolation::CUBIC_SPLINE, "CUBICSPLINE"}, + }); + + class JsonAnimationSampler + { + public: + unsigned input; + std::optional interpolation; + unsigned output; + }; + + NLOHMANN_DEFINE_TYPE_EXTENSION(JsonAnimationSampler, input); + + class JsonAnimation + { + public: + std::vector channels; + std::vector samplers; + std::optional name; + }; + + NLOHMANN_DEFINE_TYPE_EXTENSION(JsonAnimation, channels, samplers, name); + + class JsonMaterial + { + public: + std::optional name; + }; + + NLOHMANN_DEFINE_TYPE_EXTENSION(JsonMaterial, name); + + enum class JsonMeshPrimitivesMode + { + POINTS = 0, + LINES = 1, + LINE_LOOP = 2, + LINE_STRIP = 3, + TRIANGLES = 4, + TRIANGLES_STRIP = 5, + TRIANGLE_FAN = 6 + }; + + NLOHMANN_JSON_SERIALIZE_ENUM(JsonMeshPrimitivesMode, + { + {JsonMeshPrimitivesMode::POINTS, static_cast(JsonMeshPrimitivesMode::POINTS) }, + {JsonMeshPrimitivesMode::LINES, static_cast(JsonMeshPrimitivesMode::LINES) }, + {JsonMeshPrimitivesMode::LINE_LOOP, static_cast(JsonMeshPrimitivesMode::LINE_LOOP) }, + {JsonMeshPrimitivesMode::LINE_STRIP, static_cast(JsonMeshPrimitivesMode::LINE_STRIP) }, + {JsonMeshPrimitivesMode::TRIANGLES, static_cast(JsonMeshPrimitivesMode::TRIANGLES) }, + {JsonMeshPrimitivesMode::TRIANGLES_STRIP, static_cast(JsonMeshPrimitivesMode::TRIANGLES_STRIP)}, + {JsonMeshPrimitivesMode::TRIANGLE_FAN, static_cast(JsonMeshPrimitivesMode::TRIANGLE_FAN) }, + }); + + class JsonMeshPrimitives + { + public: + std::unordered_map attributes; + std::optional indices; + std::optional material; + std::optional mode; + }; + + NLOHMANN_DEFINE_TYPE_EXTENSION(JsonMeshPrimitives, attributes, indices, material, mode); + + class JsonMesh + { + public: + std::vector primitives; + std::optional> weights; + }; + + NLOHMANN_DEFINE_TYPE_EXTENSION(JsonMesh, primitives, weights); + + class JsonSkin + { + public: + std::optional inverseBindMatrices; + std::optional skeleton; + std::vector joints; + }; + + NLOHMANN_DEFINE_TYPE_EXTENSION(JsonSkin, inverseBindMatrices, skeleton, joints); + + class JsonRoot + { + public: + std::vector accessors; + std::vector animations; + JsonAsset asset; + std::vector buffers; + std::vector bufferViews; + std::vector materials; + std::vector meshes; + std::vector nodes; + std::vector skins; + }; + + NLOHMANN_DEFINE_TYPE_EXTENSION(JsonRoot, asset); +} // namespace gltf