Updated cgltf.

This commit is contained in:
Бранимир Караџић 2020-12-05 20:32:25 -08:00
parent 2d2dbf7d2c
commit 0bef8d7998
2 changed files with 494 additions and 69 deletions

539
3rdparty/cgltf/cgltf.h vendored
View File

@ -1,7 +1,7 @@
/**
* cgltf - a single-file glTF 2.0 parser written in C99.
*
* Version: 1.7
* Version: 1.8
*
* Website: https://github.com/jkuhlmann/cgltf
*
@ -254,6 +254,31 @@ typedef struct cgltf_buffer
cgltf_extension* extensions;
} cgltf_buffer;
typedef enum cgltf_meshopt_compression_mode {
cgltf_meshopt_compression_mode_invalid,
cgltf_meshopt_compression_mode_attributes,
cgltf_meshopt_compression_mode_triangles,
cgltf_meshopt_compression_mode_indices,
} cgltf_meshopt_compression_mode;
typedef enum cgltf_meshopt_compression_filter {
cgltf_meshopt_compression_filter_none,
cgltf_meshopt_compression_filter_octahedral,
cgltf_meshopt_compression_filter_quaternion,
cgltf_meshopt_compression_filter_exponential,
} cgltf_meshopt_compression_filter;
typedef struct cgltf_meshopt_compression
{
cgltf_buffer* buffer;
cgltf_size offset;
cgltf_size size;
cgltf_size stride;
cgltf_size count;
cgltf_meshopt_compression_mode mode;
cgltf_meshopt_compression_filter filter;
} cgltf_meshopt_compression;
typedef struct cgltf_buffer_view
{
cgltf_buffer* buffer;
@ -261,6 +286,9 @@ typedef struct cgltf_buffer_view
cgltf_size size;
cgltf_size stride; /* 0 == automatically determined by accessor */
cgltf_buffer_view_type type;
void* data; /* overrides buffer->data if present, filled by extensions */
cgltf_bool has_meshopt_compression;
cgltf_meshopt_compression meshopt_compression;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
@ -403,6 +431,26 @@ 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_sheen
{
cgltf_texture_view sheen_color_texture;
cgltf_float sheen_color_factor[3];
cgltf_texture_view sheen_roughness_texture;
cgltf_float sheen_roughness_factor;
} cgltf_sheen;
typedef struct cgltf_material
{
char* name;
@ -410,9 +458,15 @@ typedef struct cgltf_material
cgltf_bool has_pbr_specular_glossiness;
cgltf_bool has_clearcoat;
cgltf_bool has_transmission;
cgltf_bool has_ior;
cgltf_bool has_specular;
cgltf_bool has_sheen;
cgltf_pbr_metallic_roughness pbr_metallic_roughness;
cgltf_pbr_specular_glossiness pbr_specular_glossiness;
cgltf_clearcoat clearcoat;
cgltf_ior ior;
cgltf_specular specular;
cgltf_sheen sheen;
cgltf_transmission transmission;
cgltf_texture_view normal_texture;
cgltf_texture_view occlusion_texture;
@ -1349,6 +1403,61 @@ cgltf_result cgltf_validate(cgltf_data* data)
{
return cgltf_result_data_too_short;
}
if (data->buffer_views[i].has_meshopt_compression)
{
cgltf_meshopt_compression* mc = &data->buffer_views[i].meshopt_compression;
if (mc->buffer == NULL || mc->buffer->size < mc->offset + mc->size)
{
return cgltf_result_data_too_short;
}
if (data->buffer_views[i].stride && mc->stride != data->buffer_views[i].stride)
{
return cgltf_result_invalid_gltf;
}
if (data->buffer_views[i].size != mc->stride * mc->count)
{
return cgltf_result_invalid_gltf;
}
if (mc->mode == cgltf_meshopt_compression_mode_invalid)
{
return cgltf_result_invalid_gltf;
}
if (mc->mode == cgltf_meshopt_compression_mode_attributes && !(mc->stride % 4 == 0 && mc->stride <= 256))
{
return cgltf_result_invalid_gltf;
}
if (mc->mode == cgltf_meshopt_compression_mode_triangles && mc->count % 3 != 0)
{
return cgltf_result_invalid_gltf;
}
if ((mc->mode == cgltf_meshopt_compression_mode_triangles || mc->mode == cgltf_meshopt_compression_mode_indices) && mc->stride != 2 && mc->stride != 4)
{
return cgltf_result_invalid_gltf;
}
if ((mc->mode == cgltf_meshopt_compression_mode_triangles || mc->mode == cgltf_meshopt_compression_mode_indices) && mc->filter != cgltf_meshopt_compression_filter_none)
{
return cgltf_result_invalid_gltf;
}
if (mc->filter == cgltf_meshopt_compression_filter_octahedral && mc->stride != 4 && mc->stride != 8)
{
return cgltf_result_invalid_gltf;
}
if (mc->filter == cgltf_meshopt_compression_filter_quaternion && mc->stride != 8)
{
return cgltf_result_invalid_gltf;
}
}
}
for (cgltf_size i = 0; i < data->meshes_count; ++i)
@ -1564,6 +1673,8 @@ void cgltf_free(cgltf_data* data)
for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
{
data->memory.free(data->memory.user_data, data->buffer_views[i].data);
cgltf_free_extensions(data, data->buffer_views[i].extensions, data->buffer_views[i].extensions_count);
}
data->memory.free(data->memory.user_data, data->buffer_views);
@ -1654,10 +1765,19 @@ 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);
}
if(data->materials[i].has_sheen)
{
cgltf_free_extensions(data, data->materials[i].sheen.sheen_color_texture.extensions, data->materials[i].sheen.sheen_color_texture.extensions_count);
cgltf_free_extensions(data, data->materials[i].sheen.sheen_roughness_texture.extensions, data->materials[i].sheen.sheen_roughness_texture.extensions_count);
}
cgltf_free_extensions(data, data->materials[i].normal_texture.extensions, data->materials[i].normal_texture.extensions_count);
cgltf_free_extensions(data, data->materials[i].occlusion_texture.extensions, data->materials[i].occlusion_texture.extensions_count);
@ -1967,6 +2087,19 @@ static cgltf_bool cgltf_element_read_float(const uint8_t* element, cgltf_type ty
return 1;
}
const uint8_t* cgltf_buffer_view_data(const cgltf_buffer_view* view)
{
if (view->data)
return (const uint8_t*)view->data;
if (!view->buffer->data)
return NULL;
const uint8_t* result = (const uint8_t*)view->buffer->data;
result += view->offset;
return result;
}
cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size index, cgltf_float* out, cgltf_size element_size)
{
if (accessor->is_sparse)
@ -1978,13 +2111,12 @@ cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size
memset(out, 0, element_size * sizeof(cgltf_float));
return 1;
}
if (accessor->buffer_view->buffer->data == NULL)
const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
if (element == NULL)
{
return 0;
}
cgltf_size offset = accessor->offset + accessor->buffer_view->offset;
const uint8_t* element = (const uint8_t*) accessor->buffer_view->buffer->data;
element += offset + accessor->stride * index;
element += accessor->offset + accessor->stride * index;
return cgltf_element_read_float(element, accessor->type, accessor->component_type, accessor->normalized, out, element_size);
}
@ -2017,16 +2149,18 @@ cgltf_size cgltf_accessor_unpack_floats(const cgltf_accessor* accessor, cgltf_fl
{
const cgltf_accessor_sparse* sparse = &dense.sparse;
if (sparse->indices_buffer_view->buffer->data == NULL || sparse->values_buffer_view->buffer->data == NULL)
const uint8_t* index_data = cgltf_buffer_view_data(sparse->indices_buffer_view);
const uint8_t* reader_head = cgltf_buffer_view_data(sparse->values_buffer_view);
if (index_data == NULL || reader_head == NULL)
{
return 0;
}
const uint8_t* index_data = (const uint8_t*) sparse->indices_buffer_view->buffer->data;
index_data += sparse->indices_byte_offset + sparse->indices_buffer_view->offset;
index_data += sparse->indices_byte_offset;
reader_head += sparse->values_byte_offset;
cgltf_size index_stride = cgltf_component_size(sparse->indices_component_type);
const uint8_t* reader_head = (const uint8_t*) sparse->values_buffer_view->buffer->data;
reader_head += sparse->values_byte_offset + sparse->values_buffer_view->offset;
for (cgltf_size reader_index = 0; reader_index < sparse->count; reader_index++, index_data += index_stride)
{
size_t writer_index = cgltf_component_read_index(index_data, sparse->indices_component_type);
@ -2070,47 +2204,46 @@ static cgltf_uint cgltf_component_read_uint(const void* in, cgltf_component_type
static cgltf_bool cgltf_element_read_uint(const uint8_t* element, cgltf_type type, cgltf_component_type component_type, cgltf_uint* out, cgltf_size element_size)
{
cgltf_size num_components = cgltf_num_components(type);
cgltf_size num_components = cgltf_num_components(type);
if (element_size < num_components)
{
return 0;
}
if (element_size < num_components)
{
return 0;
}
// Reading integer matrices is not a valid use case
if (type == cgltf_type_mat2 || type == cgltf_type_mat3 || type == cgltf_type_mat4)
{
return 0;
}
if (type == cgltf_type_mat2 || type == cgltf_type_mat3 || type == cgltf_type_mat4)
{
return 0;
}
cgltf_size component_size = cgltf_component_size(component_type);
cgltf_size component_size = cgltf_component_size(component_type);
for (cgltf_size i = 0; i < num_components; ++i)
{
out[i] = cgltf_component_read_uint(element + component_size * i, component_type);
}
return 1;
for (cgltf_size i = 0; i < num_components; ++i)
{
out[i] = cgltf_component_read_uint(element + component_size * i, component_type);
}
return 1;
}
cgltf_bool cgltf_accessor_read_uint(const cgltf_accessor* accessor, cgltf_size index, cgltf_uint* out, cgltf_size element_size)
{
if (accessor->is_sparse)
{
return 0;
}
if (accessor->buffer_view == NULL)
{
memset(out, 0, element_size * sizeof( cgltf_uint ));
return 1;
}
if (accessor->buffer_view->buffer->data == NULL)
{
return 0;
}
cgltf_size offset = accessor->offset + accessor->buffer_view->offset;
const uint8_t* element = (const uint8_t*) accessor->buffer_view->buffer->data;
element += offset + accessor->stride * index;
return cgltf_element_read_uint(element, accessor->type, accessor->component_type, out, element_size);
if (accessor->is_sparse)
{
return 0;
}
if (accessor->buffer_view == NULL)
{
memset(out, 0, element_size * sizeof( cgltf_uint ));
return 1;
}
const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
if (element == NULL)
{
return 0;
}
element += accessor->offset + accessor->stride * index;
return cgltf_element_read_uint(element, accessor->type, accessor->component_type, out, element_size);
}
cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index)
@ -2123,14 +2256,12 @@ cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size
{
return 0;
}
if (accessor->buffer_view->buffer->data == NULL)
const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
if (element == NULL)
{
return 0; // This is an error case, but we can't communicate the error with existing interface.
}
cgltf_size offset = accessor->offset + accessor->buffer_view->offset;
const uint8_t* element = (const uint8_t*) accessor->buffer_view->buffer->data;
element += offset + accessor->stride * index;
element += accessor->offset + accessor->stride * index;
return cgltf_component_read_index(element, accessor->component_type);
}
@ -2277,22 +2408,22 @@ static int cgltf_parse_json_array(cgltf_options* options, jsmntok_t const* token
static int cgltf_parse_json_string_array(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, char*** out_array, cgltf_size* out_size)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(char*), (void**)out_array, out_size);
if (i < 0)
{
return i;
}
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(char*), (void**)out_array, out_size);
if (i < 0)
{
return i;
}
for (cgltf_size j = 0; j < *out_size; ++j)
{
i = cgltf_parse_json_string(options, tokens, i, json_chunk, j + (*out_array));
if (i < 0)
{
return i;
}
}
return i;
for (cgltf_size j = 0; j < *out_size; ++j)
{
i = cgltf_parse_json_string(options, tokens, i, json_chunk, j + (*out_array));
if (i < 0)
{
return i;
}
}
return i;
}
static void cgltf_parse_attribute_type(const char* name, cgltf_attribute_type* out_type, int* out_index)
@ -3275,6 +3406,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);
@ -3309,6 +3515,48 @@ static int cgltf_parse_json_transmission(cgltf_options* options, jsmntok_t const
return i;
}
static int cgltf_parse_json_sheen(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_sheen* out_sheen)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenColorFactor") == 0)
{
i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_sheen->sheen_color_factor, 3);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenColorTexture") == 0)
{
i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_sheen->sheen_color_texture);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenRoughnessFactor") == 0)
{
++i;
out_sheen->sheen_roughness_factor = cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenRoughnessTexture") == 0)
{
i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_sheen->sheen_roughness_texture);
}
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);
@ -3596,11 +3844,26 @@ 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;
i = cgltf_parse_json_transmission(options, tokens, i + 1, json_chunk, &out_material->transmission);
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_sheen") == 0)
{
out_material->has_sheen = 1;
i = cgltf_parse_json_sheen(options, tokens, i + 1, json_chunk, &out_material->sheen);
}
else
{
i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_material->extensions[out_material->extensions_count++]));
@ -3721,6 +3984,100 @@ static int cgltf_parse_json_samplers(cgltf_options* options, jsmntok_t const* to
return i;
}
static int cgltf_parse_json_meshopt_compression(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_meshopt_compression* out_meshopt_compression)
{
(void)options;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
int size = tokens[i].size;
++i;
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "buffer") == 0)
{
++i;
out_meshopt_compression->buffer = CGLTF_PTRINDEX(cgltf_buffer, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
{
++i;
out_meshopt_compression->offset = cgltf_json_to_int(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
{
++i;
out_meshopt_compression->size = cgltf_json_to_int(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteStride") == 0)
{
++i;
out_meshopt_compression->stride = cgltf_json_to_int(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
{
++i;
out_meshopt_compression->count = cgltf_json_to_int(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "mode") == 0)
{
++i;
if (cgltf_json_strcmp(tokens+i, json_chunk, "ATTRIBUTES") == 0)
{
out_meshopt_compression->mode = cgltf_meshopt_compression_mode_attributes;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "TRIANGLES") == 0)
{
out_meshopt_compression->mode = cgltf_meshopt_compression_mode_triangles;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "INDICES") == 0)
{
out_meshopt_compression->mode = cgltf_meshopt_compression_mode_indices;
}
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "filter") == 0)
{
++i;
if (cgltf_json_strcmp(tokens+i, json_chunk, "NONE") == 0)
{
out_meshopt_compression->filter = cgltf_meshopt_compression_filter_none;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "OCTAHEDRAL") == 0)
{
out_meshopt_compression->filter = cgltf_meshopt_compression_filter_octahedral;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "QUATERNION") == 0)
{
out_meshopt_compression->filter = cgltf_meshopt_compression_filter_quaternion;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "EXPONENTIAL") == 0)
{
out_meshopt_compression->filter = cgltf_meshopt_compression_filter_exponential;
}
++i;
}
else
{
i = cgltf_skip_json(tokens, i+1);
}
if (i < 0)
{
return i;
}
}
return i;
}
static int cgltf_parse_json_buffer_view(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_buffer_view* out_buffer_view)
{
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
@ -3784,7 +4141,43 @@ static int cgltf_parse_json_buffer_view(cgltf_options* options, jsmntok_t const*
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
{
i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_buffer_view->extensions_count, &out_buffer_view->extensions);
++i;
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
if(out_buffer_view->extensions)
{
return CGLTF_ERROR_JSON;
}
int extensions_size = tokens[i].size;
out_buffer_view->extensions_count = 0;
out_buffer_view->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
if (!out_buffer_view->extensions)
{
return CGLTF_ERROR_NOMEM;
}
++i;
for (int k = 0; k < extensions_size; ++k)
{
CGLTF_CHECK_KEY(tokens[i]);
if (cgltf_json_strcmp(tokens+i, json_chunk, "EXT_meshopt_compression") == 0)
{
out_buffer_view->has_meshopt_compression = 1;
i = cgltf_parse_json_meshopt_compression(options, tokens, i + 1, json_chunk, &out_buffer_view->meshopt_compression);
}
else
{
i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_buffer_view->extensions[out_buffer_view->extensions_count++]));
}
if (i < 0)
{
return i;
}
}
}
else
{
@ -5200,12 +5593,22 @@ 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);
CGLTF_PTRFIXUP(data->materials[i].sheen.sheen_color_texture.texture, data->textures, data->textures_count);
CGLTF_PTRFIXUP(data->materials[i].sheen.sheen_roughness_texture.texture, data->textures, data->textures_count);
}
for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
{
CGLTF_PTRFIXUP_REQ(data->buffer_views[i].buffer, data->buffers, data->buffers_count);
if (data->buffer_views[i].has_meshopt_compression)
{
CGLTF_PTRFIXUP_REQ(data->buffer_views[i].meshopt_compression.buffer, data->buffers, data->buffers_count);
}
}
for (cgltf_size i = 0; i < data->skins_count; ++i)
@ -5320,7 +5723,7 @@ static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
* Fills token type and boundaries.
*/
static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
int start, int end) {
int start, int end) {
token->type = type;
token->start = start;
token->end = end;
@ -5380,7 +5783,7 @@ found:
* Fills next token with JSON string.
*/
static int jsmn_parse_string(jsmn_parser *parser, const char *js,
size_t len, jsmntok_t *tokens, size_t num_tokens) {
size_t len, jsmntok_t *tokens, size_t num_tokens) {
jsmntok_t *token;
int start = parser->pos;
@ -5423,8 +5826,8 @@ static int jsmn_parse_string(jsmn_parser *parser, const char *js,
for(i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++) {
/* If it isn't a hex character we have an error */
if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
(js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
(js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
(js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
(js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
parser->pos = start;
return JSMN_ERROR_INVAL;
}
@ -5447,7 +5850,7 @@ static int jsmn_parse_string(jsmn_parser *parser, const char *js,
* Parse JSON string and fill tokens.
*/
static int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
jsmntok_t *tokens, size_t num_tokens) {
jsmntok_t *tokens, size_t num_tokens) {
int r;
int i;
jsmntok_t *token;

View File

@ -78,6 +78,7 @@ cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size si
#define CGLTF_EXTENSION_FLAG_MATERIALS_IOR (1 << 6)
#define CGLTF_EXTENSION_FLAG_MATERIALS_SPECULAR (1 << 7)
#define CGLTF_EXTENSION_FLAG_MATERIALS_TRANSMISSION (1 << 8)
#define CGLTF_EXTENSION_FLAG_MATERIALS_SHEEN (1 << 9)
typedef struct {
char* buffer;
@ -527,6 +528,11 @@ static void cgltf_write_material(cgltf_write_context* context, const cgltf_mater
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_SPECULAR;
}
if (material->has_sheen)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_SHEEN;
}
if (material->has_pbr_metallic_roughness)
{
const cgltf_pbr_metallic_roughness* params = &material->pbr_metallic_roughness;
@ -543,7 +549,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)
if (material->unlit || material->has_pbr_specular_glossiness || material->has_clearcoat || material->has_ior || material->has_specular || material->has_transmission || material->has_sheen)
{
cgltf_write_line(context, "\"extensions\": {");
if (material->has_clearcoat)
@ -584,6 +590,19 @@ static void cgltf_write_material(cgltf_write_context* context, const cgltf_mater
cgltf_write_floatprop(context, "transmissionFactor", params->transmission_factor, 0.0f);
cgltf_write_line(context, "}");
}
if (material->has_sheen)
{
const cgltf_sheen* params = &material->sheen;
cgltf_write_line(context, "\"KHR_materials_sheen\": {");
CGLTF_WRITE_TEXTURE_INFO("sheenColorTexture", params->sheen_color_texture);
CGLTF_WRITE_TEXTURE_INFO("sheenRoughnessTexture", params->sheen_roughness_texture);
if (cgltf_check_floatarray(params->sheen_color_factor, 3, 0.0f))
{
cgltf_write_floatarrayprop(context, "sheenColorFactor", params->sheen_color_factor, 3);
}
cgltf_write_floatprop(context, "sheenRoughnessFactor", params->sheen_roughness_factor, 0.0f);
cgltf_write_line(context, "}");
}
if (material->has_pbr_specular_glossiness)
{
const cgltf_pbr_specular_glossiness* params = &material->pbr_specular_glossiness;
@ -961,6 +980,9 @@ static void cgltf_write_extensions(cgltf_write_context* context, uint32_t extens
if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_TRANSMISSION) {
cgltf_write_stritem(context, "KHR_materials_transmission");
}
if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_SHEEN) {
cgltf_write_stritem(context, "KHR_materials_sheen");
}
}
cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size size, const cgltf_data* data)