Skip to content

Commit

Permalink
Have EXT_texture_webp not overwrite the image sampler, and added write
Browse files Browse the repository at this point in the history
support for EXT_texture_webp.
  • Loading branch information
bobbens committed Jun 18, 2024
1 parent bc8fbd1 commit 9c131d8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ cgltf supports core glTF 2.0:
cgltf also supports some glTF extensions:
- EXT_mesh_gpu_instancing
- EXT_meshopt_compression
- EXT_texture_webp
- KHR_draco_mesh_compression (requires a library like [Google's Draco](https://github.com/google/draco) for decompression though)
- KHR_lights_punctual
- KHR_materials_anisotropy
Expand Down
5 changes: 4 additions & 1 deletion cgltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ typedef struct cgltf_texture
cgltf_sampler* sampler;
cgltf_bool has_basisu;
cgltf_image* basisu_image;
cgltf_bool has_webp;
cgltf_image* webp_image;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
Expand Down Expand Up @@ -4540,6 +4542,7 @@ static int cgltf_parse_json_texture(cgltf_options* options, jsmntok_t const* tok
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "EXT_texture_webp") == 0)
{
out_texture->has_webp = 1;
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int num_properties = tokens[i].size;
Expand All @@ -4552,7 +4555,7 @@ static int cgltf_parse_json_texture(cgltf_options* options, jsmntok_t const* tok
if (cgltf_json_strcmp(tokens + i, json_chunk, "source") == 0)
{
++i;
out_texture->image = CGLTF_PTRINDEX(cgltf_image, cgltf_json_to_int(tokens + i, json_chunk));
out_texture->webp_image = CGLTF_PTRINDEX(cgltf_image, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else
Expand Down
15 changes: 12 additions & 3 deletions cgltf_write.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size si
#define CGLTF_EXTENSION_FLAG_MATERIALS_IRIDESCENCE (1 << 15)
#define CGLTF_EXTENSION_FLAG_MATERIALS_ANISOTROPY (1 << 16)
#define CGLTF_EXTENSION_FLAG_MATERIALS_DISPERSION (1 << 17)
#define CGLTF_EXTENSION_FLAG_TEXTURE_WEBP (1 << 18)

typedef struct {
char* buffer;
Expand Down Expand Up @@ -506,7 +507,7 @@ static void cgltf_write_primitive(cgltf_write_context* context, const cgltf_prim
context->extension_flags |= CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION;
if (prim->attributes_count == 0 || prim->indices == 0)
{
context->required_extension_flags |= CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION;
context->required_extension_flags |= CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION;
}

cgltf_write_line(context, "\"KHR_draco_mesh_compression\": {");
Expand Down Expand Up @@ -732,7 +733,7 @@ static void cgltf_write_material(cgltf_write_context* context, const cgltf_mater
{
cgltf_write_floatarrayprop(context, "attenuationColor", params->attenuation_color, 3);
}
if (params->attenuation_distance < FLT_MAX)
if (params->attenuation_distance < FLT_MAX)
{
cgltf_write_floatprop(context, "attenuationDistance", params->attenuation_distance, FLT_MAX);
}
Expand Down Expand Up @@ -840,15 +841,23 @@ static void cgltf_write_texture(cgltf_write_context* context, const cgltf_textur
CGLTF_WRITE_IDXPROP("source", texture->image, context->data->images);
CGLTF_WRITE_IDXPROP("sampler", texture->sampler, context->data->samplers);

if (texture->has_basisu)
if (texture->has_basisu || texture->has_webp)
{
cgltf_write_line(context, "\"extensions\": {");
if (texture->has_basisu)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_TEXTURE_BASISU;
cgltf_write_line(context, "\"KHR_texture_basisu\": {");
CGLTF_WRITE_IDXPROP("source", texture->basisu_image, context->data->images);
cgltf_write_line(context, "}");
}
if (texture->has_webp)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_TEXTURE_WEBP;
cgltf_write_line(context, "\"EXT_texture_webp\": {");
CGLTF_WRITE_IDXPROP("source", texture->webp_image, context->data->images);
cgltf_write_line(context, "}");
}
cgltf_write_line(context, "}");
}
cgltf_write_extras(context, &texture->extras);
Expand Down

0 comments on commit 9c131d8

Please sign in to comment.