Skip to content

Commit

Permalink
Merge pull request #183 from zeux/gltf-morematerials
Browse files Browse the repository at this point in the history
gltfpack: Add support for KHR_materials_{ior,specular}
  • Loading branch information
zeux authored Oct 4, 2020
2 parents 3dad311 + 7f5acb3 commit a507623
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 6 deletions.
109 changes: 108 additions & 1 deletion extern/cgltf.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* cgltf - a single-file glTF 2.0 parser written in C99.
*
* Version: 1.6
* Version: 1.7
*
* Website: https://github.com/jkuhlmann/cgltf
*
Expand Down Expand Up @@ -403,16 +403,32 @@ typedef struct cgltf_transmission
cgltf_float transmission_factor;
} cgltf_transmission;

typedef struct cgltf_ior
{
cgltf_float ior;
} cgltf_ior;

typedef struct cgltf_specular
{
cgltf_texture_view specular_texture;
cgltf_float specular_color_factor[3];
cgltf_float specular_factor;
} cgltf_specular;

typedef struct cgltf_material
{
char* name;
cgltf_bool has_pbr_metallic_roughness;
cgltf_bool has_pbr_specular_glossiness;
cgltf_bool has_clearcoat;
cgltf_bool has_transmission;
cgltf_bool has_ior;
cgltf_bool has_specular;
cgltf_pbr_metallic_roughness pbr_metallic_roughness;
cgltf_pbr_specular_glossiness pbr_specular_glossiness;
cgltf_clearcoat clearcoat;
cgltf_ior ior;
cgltf_specular specular;
cgltf_transmission transmission;
cgltf_texture_view normal_texture;
cgltf_texture_view occlusion_texture;
Expand Down Expand Up @@ -1654,6 +1670,10 @@ void cgltf_free(cgltf_data* data)
cgltf_free_extensions(data, data->materials[i].clearcoat.clearcoat_roughness_texture.extensions, data->materials[i].clearcoat.clearcoat_roughness_texture.extensions_count);
cgltf_free_extensions(data, data->materials[i].clearcoat.clearcoat_normal_texture.extensions, data->materials[i].clearcoat.clearcoat_normal_texture.extensions_count);
}
if(data->materials[i].has_specular)
{
cgltf_free_extensions(data, data->materials[i].specular.specular_texture.extensions, data->materials[i].specular.specular_texture.extensions_count);
}
if(data->materials[i].has_transmission)
{
cgltf_free_extensions(data, data->materials[i].transmission.transmission_texture.extensions, data->materials[i].transmission.transmission_texture.extensions_count);
Expand Down Expand Up @@ -3275,6 +3295,81 @@ static int cgltf_parse_json_clearcoat(cgltf_options* options, jsmntok_t const* t
return i;
}

static int cgltf_parse_json_ior(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_ior* out_ior)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;

// Default values
out_ior->ior = 1.5f;

for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);

if (cgltf_json_strcmp(tokens+i, json_chunk, "ior") == 0)
{
++i;
out_ior->ior = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else
{
i = cgltf_skip_json(tokens, i+1);
}

if (i < 0)
{
return i;
}
}

return i;
}

static int cgltf_parse_json_specular(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_specular* out_specular)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;

// Default values
out_specular->specular_factor = 1.0f;
cgltf_fill_float_array(out_specular->specular_color_factor, 3, 1.0f);

for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);

if (cgltf_json_strcmp(tokens+i, json_chunk, "specularFactor") == 0)
{
++i;
out_specular->specular_factor = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularColorFactor") == 0)
{
i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_specular->specular_color_factor, 3);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularTexture") == 0)
{
i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_specular->specular_texture);
}
else
{
i = cgltf_skip_json(tokens, i+1);
}

if (i < 0)
{
return i;
}
}

return i;
}

static int cgltf_parse_json_transmission(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_transmission* out_transmission)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
Expand Down Expand Up @@ -3596,6 +3691,16 @@ static int cgltf_parse_json_material(cgltf_options* options, jsmntok_t const* to
out_material->has_clearcoat = 1;
i = cgltf_parse_json_clearcoat(options, tokens, i + 1, json_chunk, &out_material->clearcoat);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_ior") == 0)
{
out_material->has_ior = 1;
i = cgltf_parse_json_ior(tokens, i + 1, json_chunk, &out_material->ior);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_specular") == 0)
{
out_material->has_specular = 1;
i = cgltf_parse_json_specular(options, tokens, i + 1, json_chunk, &out_material->specular);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_transmission") == 0)
{
out_material->has_transmission = 1;
Expand Down Expand Up @@ -5200,6 +5305,8 @@ static int cgltf_fixup_pointers(cgltf_data* data)
CGLTF_PTRFIXUP(data->materials[i].clearcoat.clearcoat_roughness_texture.texture, data->textures, data->textures_count);
CGLTF_PTRFIXUP(data->materials[i].clearcoat.clearcoat_normal_texture.texture, data->textures, data->textures_count);

CGLTF_PTRFIXUP(data->materials[i].specular.specular_texture.texture, data->textures, data->textures_count);

CGLTF_PTRFIXUP(data->materials[i].transmission.transmission_texture.texture, data->textures, data->textures_count);
}

Expand Down
6 changes: 6 additions & 0 deletions gltf/gltfpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ static void process(cgltf_data* data, const char* input_path, const char* output
bool ext_pbr_specular_glossiness = false;
bool ext_clearcoat = false;
bool ext_transmission = false;
bool ext_ior = false;
bool ext_specular = false;
bool ext_unlit = false;
bool ext_instancing = false;

Expand Down Expand Up @@ -420,6 +422,8 @@ static void process(cgltf_data* data, const char* input_path, const char* output
ext_pbr_specular_glossiness = ext_pbr_specular_glossiness || material.has_pbr_specular_glossiness;
ext_clearcoat = ext_clearcoat || material.has_clearcoat;
ext_transmission = ext_transmission || material.has_transmission;
ext_ior = ext_ior || material.has_ior;
ext_specular = ext_specular || material.has_specular;
ext_unlit = ext_unlit || material.unlit;
}

Expand Down Expand Up @@ -632,6 +636,8 @@ static void process(cgltf_data* data, const char* input_path, const char* output
{"KHR_materials_pbrSpecularGlossiness", ext_pbr_specular_glossiness, false},
{"KHR_materials_clearcoat", ext_clearcoat, false},
{"KHR_materials_transmission", ext_transmission, false},
{"KHR_materials_ior", ext_ior, false},
{"KHR_materials_specular", ext_specular, false},
{"KHR_materials_unlit", ext_unlit, false},
{"KHR_lights_punctual", data->lights_count > 0, false},
{"KHR_texture_basisu", !json_textures.empty() && settings.texture_ktx2, true},
Expand Down
16 changes: 16 additions & 0 deletions gltf/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ void analyzeImages(cgltf_data* data, std::vector<ImageInfo>& images)
images[pbr.diffuse_texture.texture->image - data->images].srgb = true;
}

if (material.has_clearcoat)
{
const cgltf_clearcoat& clearcoat = material.clearcoat;

if (clearcoat.clearcoat_normal_texture.texture && clearcoat.clearcoat_normal_texture.texture->image)
images[clearcoat.clearcoat_normal_texture.texture->image - data->images].normal_map = true;
}

if (material.has_specular)
{
const cgltf_specular& specular = material.specular;

if (specular.specular_texture.texture && specular.specular_texture.texture->image)
images[specular.specular_texture.texture->image - data->images].srgb = true;
}

if (material.emissive_texture.texture && material.emissive_texture.texture->image)
images[material.emissive_texture.texture->image - data->images].srgb = true;

Expand Down
40 changes: 40 additions & 0 deletions gltf/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ static bool areMaterialComponentsEqual(const cgltf_transmission& lhs, const cglt
return true;
}

static bool areMaterialComponentsEqual(const cgltf_ior& lhs, const cgltf_ior& rhs)
{
if (lhs.ior != rhs.ior)
return false;

return true;
}

static bool areMaterialComponentsEqual(const cgltf_specular& lhs, const cgltf_specular& rhs)
{
if (!areTextureViewsEqual(lhs.specular_texture, rhs.specular_texture))
return false;

if (memcmp(lhs.specular_color_factor, rhs.specular_color_factor, sizeof(cgltf_float) * 3) != 0)
return false;

if (lhs.specular_factor != rhs.specular_factor)
return false;

return true;
}

static bool areMaterialsEqual(cgltf_data* data, const cgltf_material& lhs, const cgltf_material& rhs, const Settings& settings)
{
if (lhs.has_pbr_metallic_roughness != rhs.has_pbr_metallic_roughness)
Expand All @@ -146,6 +168,18 @@ static bool areMaterialsEqual(cgltf_data* data, const cgltf_material& lhs, const
if (lhs.has_transmission && !areMaterialComponentsEqual(lhs.transmission, rhs.transmission))
return false;

if (lhs.has_ior != rhs.has_ior)
return false;

if (lhs.has_ior && !areMaterialComponentsEqual(lhs.ior, rhs.ior))
return false;

if (lhs.has_specular != rhs.has_specular)
return false;

if (lhs.has_specular && !areMaterialComponentsEqual(lhs.specular, rhs.specular))
return false;

if (!areTextureViewsEqual(lhs.normal_texture, rhs.normal_texture))
return false;

Expand Down Expand Up @@ -269,6 +303,12 @@ bool usesTextureSet(const cgltf_material& material, int set)
return true;
}

if (material.has_specular)
{
if (usesTextureSet(material.specular.specular_texture, set))
return true;
}

if (usesTextureSet(material.normal_texture, set))
return true;

Expand Down
69 changes: 64 additions & 5 deletions gltf/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ static const char* compressionFilter(StreamFormat::Filter filter)
}
}

static void writeTextureInfo(std::string& json, const cgltf_data* data, const cgltf_texture_view& view, const QuantizationTexture* qt)
static void writeTextureInfo(std::string& json, const cgltf_data* data, const cgltf_texture_view& view, const QuantizationTexture* qt, const char* scale = NULL)
{
assert(view.texture);

Expand All @@ -188,6 +188,13 @@ static void writeTextureInfo(std::string& json, const cgltf_data* data, const cg
append(json, size_t(view.texture - data->textures));
append(json, ",\"texCoord\":");
append(json, size_t(view.texcoord));
if (scale && view.scale != 1)
{
append(json, ",\"");
append(json, scale);
append(json, "\":");
append(json, view.scale);
}
if (view.has_transform || qt)
{
append(json, ",\"extensions\":{\"KHR_texture_transform\":{");
Expand Down Expand Up @@ -326,7 +333,7 @@ static void writeMaterialComponent(std::string& json, const cgltf_data* data, co
{
comma(json);
append(json, "\"clearcoatNormalTexture\":");
writeTextureInfo(json, data, cc.clearcoat_normal_texture, qt);
writeTextureInfo(json, data, cc.clearcoat_normal_texture, qt, "scale");
}
if (cc.clearcoat_factor != 0)
{
Expand Down Expand Up @@ -362,6 +369,48 @@ static void writeMaterialComponent(std::string& json, const cgltf_data* data, co
append(json, "}");
}

static void writeMaterialComponent(std::string& json, const cgltf_data* data, const cgltf_ior& tm, const QuantizationTexture* qt)
{
(void)data;
(void)qt;

comma(json);
append(json, "\"KHR_materials_ior\":{");
append(json, "\"ior\":");
append(json, tm.ior);
append(json, "}");
}

static void writeMaterialComponent(std::string& json, const cgltf_data* data, const cgltf_specular& tm, const QuantizationTexture* qt)
{
comma(json);
append(json, "\"KHR_materials_specular\":{");
if (tm.specular_texture.texture)
{
comma(json);
append(json, "\"specularTexture\":");
writeTextureInfo(json, data, tm.specular_texture, qt);
}
if (memcmp(tm.specular_color_factor, white, 16) != 0)
{
comma(json);
append(json, "\"specularColorFactor\":[");
append(json, tm.specular_color_factor[0]);
append(json, ",");
append(json, tm.specular_color_factor[1]);
append(json, ",");
append(json, tm.specular_color_factor[2]);
append(json, "]");
}
if (tm.specular_factor != 1)
{
comma(json);
append(json, "\"specularFactor\":");
append(json, tm.specular_factor);
}
append(json, "}");
}

void writeMaterial(std::string& json, const cgltf_data* data, const cgltf_material& material, const QuantizationTexture* qt)
{
if (material.name && *material.name)
Expand All @@ -381,14 +430,14 @@ void writeMaterial(std::string& json, const cgltf_data* data, const cgltf_materi
{
comma(json);
append(json, "\"normalTexture\":");
writeTextureInfo(json, data, material.normal_texture, qt);
writeTextureInfo(json, data, material.normal_texture, qt, "scale");
}

if (material.occlusion_texture.texture)
{
comma(json);
append(json, "\"occlusionTexture\":");
writeTextureInfo(json, data, material.occlusion_texture, qt);
writeTextureInfo(json, data, material.occlusion_texture, qt, "strength");
}

if (material.emissive_texture.texture)
Expand Down Expand Up @@ -431,7 +480,7 @@ void writeMaterial(std::string& json, const cgltf_data* data, const cgltf_materi
append(json, "\"doubleSided\":true");
}

if (material.has_pbr_specular_glossiness || material.has_clearcoat || material.has_transmission || material.unlit)
if (material.has_pbr_specular_glossiness || material.has_clearcoat || material.has_transmission || material.has_ior || material.has_specular || material.unlit)
{
comma(json);
append(json, "\"extensions\":{");
Expand All @@ -451,6 +500,16 @@ void writeMaterial(std::string& json, const cgltf_data* data, const cgltf_materi
writeMaterialComponent(json, data, material.transmission, qt);
}

if (material.has_ior)
{
writeMaterialComponent(json, data, material.ior, qt);
}

if (material.has_specular)
{
writeMaterialComponent(json, data, material.specular, qt);
}

if (material.unlit)
{
comma(json);
Expand Down

0 comments on commit a507623

Please sign in to comment.