Merge pull request #460 from ptc-tgamper/bug/issue459

Fix Default constructed Material has wrong emissiveFactor #459
This commit is contained in:
Syoyo Fujita 2023-11-23 02:21:28 +09:00 committed by GitHub
commit f32475c952
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 20 deletions

View File

@ -494,25 +494,23 @@ TEST_CASE("image-uri-spaces", "[issue-236]") {
} }
TEST_CASE("serialize-empty-material", "[issue-294]") { TEST_CASE("serialize-empty-material", "[issue-294]") {
tinygltf::Model m; tinygltf::Model m;
// Add default constructed material to model
tinygltf::Material mat; m.materials.push_back({});
mat.pbrMetallicRoughness.baseColorFactor = {1.0f, 1.0f, 1.0f, 1.0f}; // default baseColorFactor // Serialize model to output stream
m.materials.push_back(mat);
std::stringstream os; std::stringstream os;
tinygltf::TinyGLTF ctx; tinygltf::TinyGLTF ctx;
bool ret = ctx.WriteGltfSceneToStream(&m, os, false, false); bool ret = ctx.WriteGltfSceneToStream(&m, os, false, false);
REQUIRE(true == ret); REQUIRE(true == ret);
// Parse serialized model
// use nlohmann json
nlohmann::json j = nlohmann::json::parse(os.str()); nlohmann::json j = nlohmann::json::parse(os.str());
// Serialized materials shall hold an empty object that
// represents the default constructed material
REQUIRE(j.find("materials") != j.end());
REQUIRE(j["materials"].is_array());
REQUIRE(1 == j["materials"].size()); REQUIRE(1 == j["materials"].size());
REQUIRE(j["materials"][0].is_object()); CHECK(j["materials"][0].is_object());
CHECK(j["materials"][0].empty());
} }
TEST_CASE("empty-skeleton-id", "[issue-321]") { TEST_CASE("empty-skeleton-id", "[issue-321]") {
@ -757,3 +755,27 @@ TEST_CASE("load-issue-416-model", "[issue-416]") {
// external file load fails, but reading glTF itself is ok. // external file load fails, but reading glTF itself is ok.
REQUIRE(true == ret); REQUIRE(true == ret);
} }
TEST_CASE("default-material", "[issue-459]") {
const std::vector<double> default_emissive_factor{ 0.0, 0.0, 0.0 };
const std::vector<double> default_base_color_factor{ 1.0, 1.0, 1.0, 1.0 };
const std::string default_alpha_mode = "OPAQUE";
const double default_alpha_cutoff = 0.5;
const bool default_double_sided = false;
const double default_metallic_factor = 1.0;
const double default_roughness_factor = 1.0;
// Check that default constructed material
// holds actual default GLTF material properties
tinygltf::Material mat;
CHECK(mat.alphaMode == default_alpha_mode);
CHECK(mat.alphaCutoff == default_alpha_cutoff);
CHECK(mat.doubleSided == default_double_sided);
CHECK(mat.emissiveFactor == default_emissive_factor);
CHECK(mat.pbrMetallicRoughness.baseColorFactor == default_base_color_factor);
CHECK(mat.pbrMetallicRoughness.metallicFactor == default_metallic_factor);
CHECK(mat.pbrMetallicRoughness.roughnessFactor == default_roughness_factor);
// None of the textures should be set
CHECK(mat.normalTexture.index == -1);
CHECK(mat.occlusionTexture.index == -1);
CHECK(mat.emissiveTexture.index == -1);
}

View File

@ -735,7 +735,7 @@ struct OcclusionTextureInfo {
// pbrMetallicRoughness class defined in glTF 2.0 spec. // pbrMetallicRoughness class defined in glTF 2.0 spec.
struct PbrMetallicRoughness { struct PbrMetallicRoughness {
std::vector<double> baseColorFactor; // len = 4. default [1,1,1,1] std::vector<double> baseColorFactor{1.0, 1.0, 1.0, 1.0}; // len = 4. default [1,1,1,1]
TextureInfo baseColorTexture; TextureInfo baseColorTexture;
double metallicFactor{1.0}; // default 1 double metallicFactor{1.0}; // default 1
double roughnessFactor{1.0}; // default 1 double roughnessFactor{1.0}; // default 1
@ -748,9 +748,9 @@ struct PbrMetallicRoughness {
std::string extras_json_string; std::string extras_json_string;
std::string extensions_json_string; std::string extensions_json_string;
PbrMetallicRoughness() PbrMetallicRoughness() = default;
: baseColorFactor(std::vector<double>{1.0, 1.0, 1.0, 1.0}) {}
DEFAULT_METHODS(PbrMetallicRoughness) DEFAULT_METHODS(PbrMetallicRoughness)
bool operator==(const PbrMetallicRoughness &) const; bool operator==(const PbrMetallicRoughness &) const;
}; };
@ -760,10 +760,10 @@ struct PbrMetallicRoughness {
struct Material { struct Material {
std::string name; std::string name;
std::vector<double> emissiveFactor; // length 3. default [0, 0, 0] std::vector<double> emissiveFactor{0.0, 0.0, 0.0}; // length 3. default [0, 0, 0]
std::string alphaMode; // default "OPAQUE" std::string alphaMode{"OPAQUE"}; // default "OPAQUE"
double alphaCutoff{0.5}; // default 0.5 double alphaCutoff{0.5}; // default 0.5
bool doubleSided{false}; // default false; bool doubleSided{false}; // default false
PbrMetallicRoughness pbrMetallicRoughness; PbrMetallicRoughness pbrMetallicRoughness;
@ -783,7 +783,7 @@ struct Material {
std::string extras_json_string; std::string extras_json_string;
std::string extensions_json_string; std::string extensions_json_string;
Material() : alphaMode("OPAQUE") {} Material() = default;
DEFAULT_METHODS(Material) DEFAULT_METHODS(Material)
bool operator==(const Material &) const; bool operator==(const Material &) const;