Compare commits

...

2 commits

Author SHA1 Message Date
7e9596d77f Add math::quadratic_spline_acceleration helper
All checks were successful
Run tests / Run tests (push) Successful in 7m16s
2026-08-16 17:03:17 +03:00
156a58a59a Fix gltf parser in case a material doesn't have 'pbrMetallicRoughness' field 2026-08-16 17:03:01 +03:00
2 changed files with 42 additions and 25 deletions

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"))

View file

@ -235,4 +235,17 @@ namespace psemek::math
return T{1} / T{2} - std::sin(std::asin(T{1} - T{2} * x) / T{3});
}
// Stateless piecewise-quadratic spline acceleration
// based on current velocity, current distance to target,
// and max acceleration
template <typename T>
T quadratic_spline_acceleration(T velocity, T distance, T max_acceleration)
{
T max_velocity = std::sqrt(2.f * std::abs(distance) * max_acceleration);
return max_acceleration
* (distance < 0.f ? -1.f : 1.f)
* ((std::abs(velocity) < max_velocity || (distance > 0.f) != (velocity > 0.f))? 1.f : -1.f)
;
}
}