Skip to content

Commit

Permalink
Updated cgltf_write to support KHR_materials_emissive_strength.
Browse files Browse the repository at this point in the history
  • Loading branch information
abwood committed Dec 2, 2021
1 parent f8ea863 commit 79d6409
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ cgltf also supports some glTF extensions:
- KHR_materials_variants
- KHR_materials_volume
- KHR_texture_transform
- KHR_texture_basisu (requires a library like [Binomial Basisu](https://github.com/BinomialLLC/basis_universal) for transcoding to native compressed texture)
- KHR_materials_emissive_strength

cgltf does **not** yet support unlisted extensions. However, unlisted extensions can be accessed via "extensions" member on objects.

Expand Down
18 changes: 17 additions & 1 deletion cgltf_write.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size si
#define CGLTF_EXTENSION_FLAG_MATERIALS_VARIANTS (1 << 10)
#define CGLTF_EXTENSION_FLAG_MATERIALS_VOLUME (1 << 11)
#define CGLTF_EXTENSION_FLAG_TEXTURE_BASISU (1 << 12)
#define CGLTF_EXTENSION_FLAG_MATERIALS_EMISSIVE_STRENGTH (1 << 13)

typedef struct {
char* buffer;
Expand Down Expand Up @@ -587,6 +588,11 @@ static void cgltf_write_material(cgltf_write_context* context, const cgltf_mater
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_SHEEN;
}

if (material->has_emissive_strength)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_EMISSIVE_STRENGTH;
}

if (material->has_pbr_metallic_roughness)
{
const cgltf_pbr_metallic_roughness* params = &material->pbr_metallic_roughness;
Expand All @@ -603,7 +609,7 @@ static void cgltf_write_material(cgltf_write_context* context, const cgltf_mater
cgltf_write_line(context, "}");
}

if (material->unlit || material->has_pbr_specular_glossiness || material->has_clearcoat || material->has_ior || material->has_specular || material->has_transmission || material->has_sheen || material->has_volume)
if (material->unlit || material->has_pbr_specular_glossiness || material->has_clearcoat || material->has_ior || material->has_specular || material->has_transmission || material->has_sheen || material->has_volume || material->has_emissive_strength)
{
cgltf_write_line(context, "\"extensions\": {");
if (material->has_clearcoat)
Expand Down Expand Up @@ -695,6 +701,13 @@ static void cgltf_write_material(cgltf_write_context* context, const cgltf_mater
{
cgltf_write_line(context, "\"KHR_materials_unlit\": {}");
}
if (material->has_emissive_strength)
{
cgltf_write_line(context, "\"KHR_materials_emissive_strength\": {");
const cgltf_emissive_strength* params = &material->emissive_strength;
cgltf_write_floatprop(context, "emissiveStrength", params->emissive_strength, 1.f);
cgltf_write_line(context, "}");
}
cgltf_write_line(context, "}");
}

Expand Down Expand Up @@ -1097,6 +1110,9 @@ static void cgltf_write_extensions(cgltf_write_context* context, uint32_t extens
if (extension_flags & CGLTF_EXTENSION_FLAG_TEXTURE_BASISU) {
cgltf_write_stritem(context, "KHR_texture_basisu");
}
if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_EMISSIVE_STRENGTH) {
cgltf_write_stritem(context, "KHR_materials_emissive_strength");
}
}

cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size size, const cgltf_data* data)
Expand Down

0 comments on commit 79d6409

Please sign in to comment.