Skip to content

Commit

Permalink
gltfpack: Implement support for KHR_materials_transmission
Browse files Browse the repository at this point in the history
This change simply propagates KHR_materials_transmission parameters
through, supporting material merging as well.

It also fixes a problem with texture set filtering, where we ignored
texture sets that might have only been used in clearcoat layer.
  • Loading branch information
zeux committed Jul 20, 2020
1 parent ba1d638 commit 9e27a91
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
33 changes: 33 additions & 0 deletions gltf/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ static bool areMaterialComponentsEqual(const cgltf_clearcoat& lhs, const cgltf_c
return true;
}

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

if (lhs.transmission_factor != rhs.transmission_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 @@ -129,6 +140,12 @@ static bool areMaterialsEqual(cgltf_data* data, const cgltf_material& lhs, const
if (lhs.has_clearcoat && !areMaterialComponentsEqual(lhs.clearcoat, rhs.clearcoat))
return false;

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

if (lhs.has_transmission && !areMaterialComponentsEqual(lhs.transmission, rhs.transmission))
return false;

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

Expand Down Expand Up @@ -239,6 +256,22 @@ bool usesTextureSet(const cgltf_material& material, int set)
return true;
}

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

if (clearcoat.clearcoat_texture.texture && clearcoat.clearcoat_texture.texcoord == set)
return true;
}

if (material.has_transmission)
{
const cgltf_transmission& transmission = material.transmission;

if (transmission.transmission_texture.texture && transmission.transmission_texture.texcoord == set)
return true;
}

if (material.normal_texture.texture && material.normal_texture.texcoord == set)
return true;

Expand Down
26 changes: 25 additions & 1 deletion gltf/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,25 @@ 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_transmission& tm, const QuantizationTexture* qt)
{
comma(json);
append(json, "\"KHR_materials_transmission\":{");
if (tm.transmission_texture.texture)
{
comma(json);
append(json, "\"transmissionTexture\":");
writeTextureInfo(json, data, tm.transmission_texture, qt);
}
if (tm.transmission_factor != 0)
{
comma(json);
append(json, "\"transmissionFactor\":");
append(json, tm.transmission_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 Down Expand Up @@ -412,7 +431,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.unlit)
if (material.has_pbr_specular_glossiness || material.has_clearcoat || material.has_transmission || material.unlit)
{
comma(json);
append(json, "\"extensions\":{");
Expand All @@ -427,6 +446,11 @@ void writeMaterial(std::string& json, const cgltf_data* data, const cgltf_materi
writeMaterialComponent(json, data, material.clearcoat, qt);
}

if (material.has_transmission)
{
writeMaterialComponent(json, data, material.transmission, qt);
}

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

0 comments on commit 9e27a91

Please sign in to comment.