From 362b8bd7618b4b862a39524a093b9268b42abae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A4r=20Winzell?= Date: Sun, 25 Feb 2018 18:13:19 -0800 Subject: [PATCH] Clamp FBX values to glTF specifications (#80) There seem to be few constraints on what values FBX properties can take. By contrast, glTF constrains e.g. common material factors to lie in [0, 1]. We take a simple approach and just clamp. --- src/glTF/MaterialData.cpp | 18 ++++++++++++++---- src/mathfu.h | 5 +++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/glTF/MaterialData.cpp b/src/glTF/MaterialData.cpp index c8a8d5f..0360c33 100644 --- a/src/glTF/MaterialData.cpp +++ b/src/glTF/MaterialData.cpp @@ -36,14 +36,24 @@ void to_json(json &j, const KHRCmnUnlitMaterial &d) j = json({}); } +inline float clamp(float d, float bottom = 0, float top = 1) { + return std::max(bottom, std::min(top, d)); +} +inline Vec3f clamp(const Vec3f &vec, const Vec3f &bottom = VEC3F_ZERO, const Vec3f &top = VEC3F_ONE) { + return Vec3f::Max(bottom, Vec3f::Min(top, vec)); +} +inline Vec4f clamp(const Vec4f &vec, const Vec4f &bottom = VEC4F_ZERO, const Vec4f &top = VEC4F_ONE) { + return Vec4f::Max(bottom, Vec4f::Min(top, vec)); +} + PBRMetallicRoughness::PBRMetallicRoughness( const TextureData *baseColorTexture, const TextureData *metRoughTexture, const Vec4f &baseColorFactor, float metallic, float roughness) : baseColorTexture(Tex::ref(baseColorTexture)), metRoughTexture(Tex::ref(metRoughTexture)), - baseColorFactor(baseColorFactor), - metallic(metallic), - roughness(roughness) + baseColorFactor(clamp(baseColorFactor)), + metallic(clamp(metallic)), + roughness(clamp(roughness)) { } @@ -78,7 +88,7 @@ MaterialData::MaterialData( isTransparent(isTransparent), normalTexture(Tex::ref(normalTexture)), emissiveTexture(Tex::ref(emissiveTexture)), - emissiveFactor(emissiveFactor), + emissiveFactor(clamp(emissiveFactor)), khrCmnConstantMaterial(khrCmnConstantMaterial), pbrMetallicRoughness(pbrMetallicRoughness) {} diff --git a/src/mathfu.h b/src/mathfu.h index 922267f..11cca93 100644 --- a/src/mathfu.h +++ b/src/mathfu.h @@ -58,6 +58,11 @@ typedef mathfu::Matrix Mat4f; typedef mathfu::Quaternion Quatf; typedef Bounds Boundsf; +const Vec3f VEC3F_ONE = Vec3f {1.0f}; +const Vec3f VEC3F_ZERO = Vec3f {0.0f}; +const Vec4f VEC4F_ONE = Vec4f {1.0f}; +const Vec4f VEC4F_ZERO = Vec4f {0.0f}; + template static inline std::vector toStdVec(const mathfu::Vector &vec) { std::vector result(d);