mirror of
https://git.mirrors.martin98.com/https://github.com/syoyo/tinygltf.git
synced 2025-07-05 04:55:12 +08:00
Serialize empty JSON object when material has all default parameters. Fixes #294 .
This commit is contained in:
parent
42a8cd50ff
commit
68adc4ba5e
@ -412,6 +412,28 @@ TEST_CASE("image-uri-spaces", "[issue-236]") {
|
||||
REQUIRE(true == ret);
|
||||
}
|
||||
|
||||
TEST_CASE("serialize-empty-material", "[issue-294]") {
|
||||
|
||||
tinygltf::Model m;
|
||||
|
||||
tinygltf::Material mat;
|
||||
mat.pbrMetallicRoughness.baseColorFactor = {1.0f, 1.0f, 1.0f, 1.0f}; // default baseColorFactor
|
||||
m.materials.push_back(mat);
|
||||
|
||||
std::stringstream os;
|
||||
|
||||
tinygltf::TinyGLTF ctx;
|
||||
ctx.WriteGltfSceneToStream(&m, os, false, false);
|
||||
|
||||
// use nlohmann json(included inside of tiny_gltf.h)
|
||||
json j = json::parse(os.str());
|
||||
|
||||
REQUIRE(1 == j["materials"].size());
|
||||
REQUIRE(j["asset"].is_null());
|
||||
REQUIRE(j["materials"][0].is_object());
|
||||
|
||||
}
|
||||
|
||||
#ifndef TINYGLTF_NO_FS
|
||||
TEST_CASE("expandpath-utf-8", "[pr-226]") {
|
||||
|
||||
|
10
tiny_gltf.h
10
tiny_gltf.h
@ -26,6 +26,7 @@
|
||||
// THE SOFTWARE.
|
||||
|
||||
// Version:
|
||||
// - v2.4.3 Fix null object output when when material has all default parameters.
|
||||
// - v2.4.2 Decode percent-encoded URI.
|
||||
// - v2.4.1 Fix some glTF object class does not have `extensions` and/or
|
||||
// `extras` property.
|
||||
@ -7225,6 +7226,15 @@ static void SerializeGltfModel(Model *model, json &o) {
|
||||
for (unsigned int i = 0; i < model->materials.size(); ++i) {
|
||||
json material;
|
||||
SerializeGltfMaterial(model->materials[i], material);
|
||||
|
||||
if (JsonIsNull(material)) {
|
||||
// Issue 294.
|
||||
// `material` does not have any required parameters
|
||||
// so the result may be null(unmodified) when all material parameters have default value.
|
||||
//
|
||||
// null is not allowed thus we create an empty JSON object.
|
||||
JsonSetObject(material);
|
||||
}
|
||||
JsonPushBack(materials, std::move(material));
|
||||
}
|
||||
JsonAddMember(o, "materials", std::move(materials));
|
||||
|
Loading…
x
Reference in New Issue
Block a user