Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be tolerant when encountering emissiveFactor with array length 4 #445

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions tiny_gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ typedef enum {
OBJECT_TYPE
} Type;

typedef enum {
PERMISSIVE,
STRICT
} ParseStrictness;

static inline int32_t GetComponentSizeInBytes(uint32_t componentType) {
if (componentType == TINYGLTF_COMPONENT_TYPE_BYTE) {
return 1;
Expand Down Expand Up @@ -1463,6 +1468,11 @@ class TinyGLTF {
bool embedImages, bool embedBuffers,
bool prettyPrint, bool writeBinary);

///
/// Sets the parsing strictness.
///
void SetParseStrictness(ParseStrictness strictness);

///
/// Set callback to use for loading image data
///
Expand Down Expand Up @@ -1552,6 +1562,8 @@ class TinyGLTF {
size_t bin_size_ = 0;
bool is_binary_ = false;

ParseStrictness strictness_ = ParseStrictness::STRICT;

bool serialize_default_values_ = false; ///< Serialize default values?

bool store_original_json_for_extras_and_extensions_ = false;
Expand Down Expand Up @@ -2553,6 +2565,10 @@ static bool LoadExternalFile(std::vector<unsigned char> *out, std::string *err,
return true;
}

void TinyGLTF::SetParseStrictness(ParseStrictness strictness) {
strictness_ = strictness;
}

void TinyGLTF::SetImageLoader(LoadImageDataFunction func, void *user_data) {
LoadImageData = func;
load_image_user_data_ = user_data;
Expand Down Expand Up @@ -5192,15 +5208,24 @@ static bool ParsePbrMetallicRoughness(
return true;
}

static bool ParseMaterial(Material *material, std::string *err,
static bool ParseMaterial(Material *material, std::string *err, std::string *warn,
const detail::json &o,
bool store_original_json_for_extras_and_extensions) {
bool store_original_json_for_extras_and_extensions,
ParseStrictness strictness) {
ParseStringProperty(&material->name, err, o, "name", /* required */ false);

if (ParseNumberArrayProperty(&material->emissiveFactor, err, o,
"emissiveFactor",
/* required */ false)) {
if (material->emissiveFactor.size() != 3) {
if (strictness==ParseStrictness::PERMISSIVE && material->emissiveFactor.size() == 4) {
if (warn) {
(*warn) +=
"Array length of `emissiveFactor` parameter in "
"material must be 3, but got 4\n";
}
material->emissiveFactor.resize(3);
}
else if (material->emissiveFactor.size() != 3) {
if (err) {
(*err) +=
"Array length of `emissiveFactor` parameter in "
Expand Down Expand Up @@ -6198,8 +6223,9 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
Material material;
ParseStringProperty(&material.name, err, o, "name", false);

if (!ParseMaterial(&material, err, o,
store_original_json_for_extras_and_extensions_)) {
if (!ParseMaterial(&material, err, warn, o,
store_original_json_for_extras_and_extensions_,
strictness_)) {
return false;
}

Expand Down
Loading