Fix gltf parser in case a material doesn't have 'pbrMetallicRoughness' field

This commit is contained in:
Nikita Lisitsa 2026-08-16 17:03:01 +03:00
parent ff7eb827fe
commit 156a58a59a

View file

@ -228,39 +228,43 @@ namespace psemek::gfx
target.name = material["name"].GetString();
target.two_sided = false;
if (material.HasMember("doubleSided"))
target.two_sided = material["doubleSided"].GetBool();
else
target.two_sided = false;
auto const & pbr = material["pbrMetallicRoughness"];
if (pbr.HasMember("baseColorFactor"))
// Material can omit pbrMetallicRoughness entry if
// all the values are defaulted
if (material.HasMember("pbrMetallicRoughness"))
{
auto const & color = pbr["baseColorFactor"].GetArray();
gfx::color_4f & target_color = target.albedo.emplace();
for (std::size_t i = 0; i < 4; ++i)
target_color[i] = color[i].GetFloat();
}
auto const & pbr = material["pbrMetallicRoughness"];
if (pbr.HasMember("baseColorTexture"))
{
target.texture = document["textures"].GetArray()[pbr["baseColorTexture"]["index"].GetUint64()]["source"].GetInt64();
}
if (pbr.HasMember("baseColorFactor"))
{
auto const & color = pbr["baseColorFactor"].GetArray();
gfx::color_4f & target_color = target.albedo.emplace();
for (std::size_t i = 0; i < 4; ++i)
target_color[i] = color[i].GetFloat();
}
if (pbr.HasMember("metallicFactor"))
{
target.metallic = pbr["metallicFactor"].GetFloat();
}
if (pbr.HasMember("baseColorTexture"))
{
target.texture = document["textures"].GetArray()[pbr["baseColorTexture"]["index"].GetUint64()]["source"].GetInt64();
}
if (pbr.HasMember("roughnessFactor"))
{
target.roughness = pbr["roughnessFactor"].GetFloat();
}
if (pbr.HasMember("metallicFactor"))
{
target.metallic = pbr["metallicFactor"].GetFloat();
}
if (pbr.HasMember("metallicRoughnessTexture"))
{
target.material_texture = document["textures"].GetArray()[pbr["metallicRoughnessTexture"]["index"].GetUint64()]["source"].GetInt64();
if (pbr.HasMember("roughnessFactor"))
{
target.roughness = pbr["roughnessFactor"].GetFloat();
}
if (pbr.HasMember("metallicRoughnessTexture"))
{
target.material_texture = document["textures"].GetArray()[pbr["metallicRoughnessTexture"]["index"].GetUint64()]["source"].GetInt64();
}
}
if (material.HasMember("emissiveFactor"))