Parse blender extras in glTF
This commit is contained in:
parent
21e35a988a
commit
7ad2c92d31
2 changed files with 58 additions and 0 deletions
|
|
@ -18,6 +18,9 @@ namespace psemek::gfx
|
||||||
|
|
||||||
struct gltf_asset
|
struct gltf_asset
|
||||||
{
|
{
|
||||||
|
using extra = std::variant<float, std::vector<float>, std::string>;
|
||||||
|
using extras_map = std::unordered_map<std::string, extra>;
|
||||||
|
|
||||||
struct node
|
struct node
|
||||||
{
|
{
|
||||||
std::string name;
|
std::string name;
|
||||||
|
|
@ -31,6 +34,8 @@ namespace psemek::gfx
|
||||||
geom::quaternion<float> rotation;
|
geom::quaternion<float> rotation;
|
||||||
geom::vector<float, 3> scale;
|
geom::vector<float, 3> scale;
|
||||||
geom::affine_transform<float, 3, 3> transform;
|
geom::affine_transform<float, 3, 3> transform;
|
||||||
|
|
||||||
|
extras_map extras;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct mesh
|
struct mesh
|
||||||
|
|
@ -58,6 +63,8 @@ namespace psemek::gfx
|
||||||
std::optional<color_3f> emission;
|
std::optional<color_3f> emission;
|
||||||
std::optional<std::size_t> emission_texture;
|
std::optional<std::size_t> emission_texture;
|
||||||
std::optional<std::size_t> material_texture;
|
std::optional<std::size_t> material_texture;
|
||||||
|
|
||||||
|
extras_map extras;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct texture
|
struct texture
|
||||||
|
|
@ -146,6 +153,8 @@ namespace psemek::gfx
|
||||||
float intensity;
|
float intensity;
|
||||||
float range;
|
float range;
|
||||||
geom::interval<float> cone_angle;
|
geom::interval<float> cone_angle;
|
||||||
|
|
||||||
|
extras_map extras;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<node> nodes;
|
std::vector<node> nodes;
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
#include <psemek/geom/rotation.hpp>
|
#include <psemek/geom/rotation.hpp>
|
||||||
#include <psemek/geom/translation.hpp>
|
#include <psemek/geom/translation.hpp>
|
||||||
#include <psemek/util/to_string.hpp>
|
#include <psemek/util/to_string.hpp>
|
||||||
|
#include <psemek/log/log.hpp>
|
||||||
|
|
||||||
#include <boost/preprocessor/stringize.hpp>
|
#include <boost/preprocessor/stringize.hpp>
|
||||||
|
|
||||||
|
|
@ -73,6 +74,48 @@ namespace psemek::gfx
|
||||||
"KHR_materials_emissive_strength"
|
"KHR_materials_emissive_strength"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
gltf_asset::extras_map parse_extras(rapidjson::GenericValue<rapidjson::UTF8<>> const & value)
|
||||||
|
{
|
||||||
|
gltf_asset::extras_map result;
|
||||||
|
|
||||||
|
if (value.HasMember("extras"))
|
||||||
|
{
|
||||||
|
for (auto const & extra : value["extras"].GetObject())
|
||||||
|
{
|
||||||
|
std::string name = extra.name.GetString();
|
||||||
|
gltf_asset::extra value;
|
||||||
|
|
||||||
|
bool error = false;
|
||||||
|
|
||||||
|
if (extra.value.IsNumber())
|
||||||
|
value = extra.value.GetFloat();
|
||||||
|
else if (extra.value.IsString())
|
||||||
|
value = extra.value.GetString();
|
||||||
|
else if (extra.value.IsArray())
|
||||||
|
{
|
||||||
|
std::vector<float> v;
|
||||||
|
for (auto const & val : extra.value.GetArray())
|
||||||
|
{
|
||||||
|
if (!val.IsNumber())
|
||||||
|
error = true;
|
||||||
|
else
|
||||||
|
v.push_back(val.GetFloat());
|
||||||
|
}
|
||||||
|
value = std::move(v);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
error = true;
|
||||||
|
|
||||||
|
if (error)
|
||||||
|
log::warning() << "every 'extras' value must be either a number, an array of numbers, or a string (while parting glTF)";
|
||||||
|
else
|
||||||
|
result[name] = std::move(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gltf_asset parse_gltf(io::istream && stream)
|
gltf_asset parse_gltf(io::istream && stream)
|
||||||
|
|
@ -144,6 +187,8 @@ namespace psemek::gfx
|
||||||
target.light = extension.value["light"].GetUint64();
|
target.light = extension.value["light"].GetUint64();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
target.extras = parse_extras(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (document.HasMember("meshes")) for (auto const & mesh : document["meshes"].GetArray())
|
if (document.HasMember("meshes")) for (auto const & mesh : document["meshes"].GetArray())
|
||||||
|
|
@ -224,6 +269,8 @@ namespace psemek::gfx
|
||||||
*target.emission *= extensions["KHR_materials_emissive_strength"]["emissiveStrength"].GetFloat();
|
*target.emission *= extensions["KHR_materials_emissive_strength"]["emissiveStrength"].GetFloat();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
target.extras = parse_extras(material);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (document.HasMember("images")) for (auto const & image : document["images"].GetArray())
|
if (document.HasMember("images")) for (auto const & image : document["images"].GetArray())
|
||||||
|
|
@ -367,6 +414,8 @@ namespace psemek::gfx
|
||||||
if (spot.HasMember("outerConeAngle"))
|
if (spot.HasMember("outerConeAngle"))
|
||||||
target.cone_angle.max = spot["outerConeAngle"].GetFloat();
|
target.cone_angle.max = spot["outerConeAngle"].GetFloat();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
target.extras = parse_extras(light);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue