Skip to content

Commit

Permalink
Merge pull request #170 from abwood/emissive-strength
Browse files Browse the repository at this point in the history
Add support for KHR_materials_emissive_strength.
  • Loading branch information
jkuhlmann authored Dec 6, 2021
2 parents dd70e93 + 79d6409 commit caf4337
Show file tree
Hide file tree
Showing 3 changed files with 64 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
45 changes: 45 additions & 0 deletions cgltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,11 @@ typedef struct cgltf_sheen
cgltf_float sheen_roughness_factor;
} cgltf_sheen;

typedef struct cgltf_emissive_strength
{
cgltf_float emissive_strength;
} cgltf_emissive_strength;

typedef struct cgltf_material
{
char* name;
Expand All @@ -485,6 +490,7 @@ typedef struct cgltf_material
cgltf_bool has_ior;
cgltf_bool has_specular;
cgltf_bool has_sheen;
cgltf_bool has_emissive_strength;
cgltf_pbr_metallic_roughness pbr_metallic_roughness;
cgltf_pbr_specular_glossiness pbr_specular_glossiness;
cgltf_clearcoat clearcoat;
Expand All @@ -493,6 +499,7 @@ typedef struct cgltf_material
cgltf_sheen sheen;
cgltf_transmission transmission;
cgltf_volume volume;
cgltf_emissive_strength emissive_strength;
cgltf_texture_view normal_texture;
cgltf_texture_view occlusion_texture;
cgltf_texture_view emissive_texture;
Expand Down Expand Up @@ -3844,6 +3851,39 @@ static int cgltf_parse_json_sheen(cgltf_options* options, jsmntok_t const* token
return i;
}

static int cgltf_parse_json_emissive_strength(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_emissive_strength* out_emissive_strength)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;

// Default
out_emissive_strength->emissive_strength = 1.f;

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

if (cgltf_json_strcmp(tokens + i, json_chunk, "emissiveStrength") == 0)
{
++i;
out_emissive_strength->emissive_strength = 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_image(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_image* out_image)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
Expand Down Expand Up @@ -4218,6 +4258,11 @@ static int cgltf_parse_json_material(cgltf_options* options, jsmntok_t const* to
out_material->has_sheen = 1;
i = cgltf_parse_json_sheen(options, tokens, i + 1, json_chunk, &out_material->sheen);
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "KHR_materials_emissive_strength") == 0)
{
out_material->has_emissive_strength = 1;
i = cgltf_parse_json_emissive_strength(tokens, i + 1, json_chunk, &out_material->emissive_strength);
}
else
{
i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_material->extensions[out_material->extensions_count++]));
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 caf4337

Please sign in to comment.