From a42263bdbac80d639408a874a92619e22cc50508 Mon Sep 17 00:00:00 2001 From: Thomas Gamper Date: Mon, 5 Feb 2024 15:42:49 +0100 Subject: [PATCH] tiny_gltf.h - parse node and material lods --- tiny_gltf.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tiny_gltf.h b/tiny_gltf.h index 7c7227c..6a04d6d 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -764,6 +764,7 @@ struct Material { std::string alphaMode{"OPAQUE"}; // default "OPAQUE" double alphaCutoff{0.5}; // default 0.5 bool doubleSided{false}; // default false + std::vector lods; // level of detail materials (MSFT_lod) PbrMetallicRoughness pbrMetallicRoughness; @@ -1018,6 +1019,7 @@ class Node { int mesh{-1}; int light{-1}; // light source index (KHR_lights_punctual) int emitter{-1}; // audio emitter index (KHR_audio) + std::vector lods; // level of detail nodes (MSFT_lod) std::vector children; std::vector rotation; // length must be 0 or 4 std::vector scale; // length must be 0 or 3 @@ -5165,6 +5167,24 @@ static bool ParseNode(Node *node, std::string *err, const detail::json &o, } node->emitter = emitter; + node->lods.clear(); + if (node->extensions.count("MSFT_lod") != 0) { + auto const &msft_lod_ext = node->extensions["MSFT_lod"]; + if (msft_lod_ext.Has("ids")) { + auto idsArr = msft_lod_ext.Get("ids"); + for (size_t i = 0; i < idsArr.ArrayLen(); ++i) { + node->lods.emplace_back(idsArr.Get(i).GetNumberAsInt()); + } + } else { + if (err) { + *err += + "Node has extension MSFT_lod, but does not reference " + "other nodes via their ids.\n"; + } + return false; + } + } + return true; } @@ -5364,6 +5384,24 @@ static bool ParseMaterial(Material *material, std::string *err, std::string *war ParseExtrasAndExtensions(material, err, o, store_original_json_for_extras_and_extensions); + material->lods.clear(); + if (material->extensions.count("MSFT_lod") != 0) { + auto const &msft_lod_ext = material->extensions["MSFT_lod"]; + if (msft_lod_ext.Has("ids")) { + auto idsArr = msft_lod_ext.Get("ids"); + for (size_t i = 0; i < idsArr.ArrayLen(); ++i) { + material->lods.emplace_back(idsArr.Get(i).GetNumberAsInt()); + } + } else { + if (err) { + *err += + "Material has extension MSFT_lod, but does not reference " + "other materials via their ids.\n"; + } + return false; + } + } + return true; }