Skip to content

Commit

Permalink
tiny_gltf.h - parse node and material lods
Browse files Browse the repository at this point in the history
  • Loading branch information
ptc-tgamper committed Feb 5, 2024
1 parent 4fea26f commit a42263b
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tiny_gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> lods; // level of detail materials (MSFT_lod)

PbrMetallicRoughness pbrMetallicRoughness;

Expand Down Expand Up @@ -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<int> lods; // level of detail nodes (MSFT_lod)
std::vector<int> children;
std::vector<double> rotation; // length must be 0 or 4
std::vector<double> scale; // length must be 0 or 3
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit a42263b

Please sign in to comment.