Skip to content

Commit 4baefba

Browse files
committed
Implemented KHR_gaussian_splatting extension
1 parent 85cd623 commit 4baefba

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

cgltf.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,16 @@ typedef struct cgltf_draco_mesh_compression {
598598
cgltf_size attributes_count;
599599
} cgltf_draco_mesh_compression;
600600

601+
typedef struct cgltf_gaussian_splatting {
602+
char* kernel;
603+
char* color_space;
604+
char* sorting_method;
605+
char* projection;
606+
cgltf_extras extras;
607+
cgltf_size extensions_count;
608+
cgltf_extension* extensions;
609+
} cgltf_gaussian_splatting;
610+
601611
typedef struct cgltf_mesh_gpu_instancing {
602612
cgltf_attribute* attributes;
603613
cgltf_size attributes_count;
@@ -616,6 +626,8 @@ typedef struct cgltf_primitive {
616626
cgltf_draco_mesh_compression draco_mesh_compression;
617627
cgltf_material_mapping* mappings;
618628
cgltf_size mappings_count;
629+
cgltf_bool has_gaussian_splatting;
630+
cgltf_gaussian_splatting gaussian_splatting;
619631
cgltf_size extensions_count;
620632
cgltf_extension* extensions;
621633
} cgltf_primitive;
@@ -3139,6 +3151,55 @@ static int cgltf_parse_json_draco_mesh_compression(cgltf_options* options, jsmnt
31393151
return i;
31403152
}
31413153

3154+
static int cgltf_parse_json_gaussian_splatting(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_gaussian_splatting* out_gaussian_splatting)
3155+
{
3156+
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3157+
3158+
int size = tokens[i].size;
3159+
++i;
3160+
3161+
for (int j = 0; j < size; ++j)
3162+
{
3163+
CGLTF_CHECK_KEY(tokens[i]);
3164+
3165+
if (cgltf_json_strcmp(tokens + i, json_chunk, "kernel") == 0)
3166+
{
3167+
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_gaussian_splatting->kernel);
3168+
}
3169+
else if (cgltf_json_strcmp(tokens + i, json_chunk, "colorSpace") == 0)
3170+
{
3171+
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_gaussian_splatting->color_space);
3172+
}
3173+
else if (cgltf_json_strcmp(tokens + i, json_chunk, "sortingMethod") == 0)
3174+
{
3175+
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_gaussian_splatting->sorting_method);
3176+
}
3177+
else if (cgltf_json_strcmp(tokens + i, json_chunk, "projection") == 0)
3178+
{
3179+
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_gaussian_splatting->projection);
3180+
}
3181+
else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3182+
{
3183+
i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_gaussian_splatting->extras);
3184+
}
3185+
else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3186+
{
3187+
i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_gaussian_splatting->extensions_count, &out_gaussian_splatting->extensions);
3188+
}
3189+
else
3190+
{
3191+
i = cgltf_skip_json(tokens, i+1);
3192+
}
3193+
3194+
if (i < 0)
3195+
{
3196+
return i;
3197+
}
3198+
}
3199+
3200+
return i;
3201+
}
3202+
31423203
static int cgltf_parse_json_mesh_gpu_instancing(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_mesh_gpu_instancing* out_mesh_gpu_instancing)
31433204
{
31443205
CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
@@ -3410,6 +3471,11 @@ static int cgltf_parse_json_primitive(cgltf_options* options, jsmntok_t const* t
34103471
out_prim->has_draco_mesh_compression = 1;
34113472
i = cgltf_parse_json_draco_mesh_compression(options, tokens, i + 1, json_chunk, &out_prim->draco_mesh_compression);
34123473
}
3474+
else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_gaussian_splatting") == 0)
3475+
{
3476+
out_prim->has_gaussian_splatting = 1;
3477+
i = cgltf_parse_json_gaussian_splatting(options, tokens, i + 1, json_chunk, &out_prim->gaussian_splatting);
3478+
}
34133479
else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_variants") == 0)
34143480
{
34153481
i = cgltf_parse_json_material_mappings(options, tokens, i + 1, json_chunk, out_prim);

cgltf_write.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size si
8989
#define CGLTF_EXTENSION_FLAG_MATERIALS_DISPERSION (1 << 17)
9090
#define CGLTF_EXTENSION_FLAG_TEXTURE_WEBP (1 << 18)
9191
#define CGLTF_EXTENSION_FLAG_MATERIALS_DIFFUSE_TRANSMISSION (1 << 19)
92+
#define CGLTF_EXTENSION_FLAG_GAUSSIAN_SPLATTING (1 << 20)
9293

9394
typedef struct {
9495
char* buffer;
@@ -523,6 +524,18 @@ static void cgltf_write_primitive(cgltf_write_context* context, const cgltf_prim
523524
cgltf_write_line(context, "}");
524525
}
525526

527+
if (prim->has_gaussian_splatting)
528+
{
529+
context->extension_flags |= CGLTF_EXTENSION_FLAG_GAUSSIAN_SPLATTING;
530+
cgltf_write_line(context, "\"KHR_gaussian_splatting\": {");
531+
cgltf_write_strprop(context, "kernel", prim->gaussian_splatting.kernel);
532+
cgltf_write_strprop(context, "colorSpace", prim->gaussian_splatting.color_space);
533+
cgltf_write_strprop(context, "sortingMethod", prim->gaussian_splatting.sorting_method);
534+
cgltf_write_strprop(context, "projection", prim->gaussian_splatting.projection);
535+
cgltf_write_extras(context, &prim->gaussian_splatting.extras);
536+
cgltf_write_line(context, "}");
537+
}
538+
526539
if (prim->mappings_count > 0)
527540
{
528541
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_VARIANTS;
@@ -1329,6 +1342,9 @@ static void cgltf_write_extensions(cgltf_write_context* context, uint32_t extens
13291342
if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_DISPERSION) {
13301343
cgltf_write_stritem(context, "KHR_materials_dispersion");
13311344
}
1345+
if (extension_flags & CGLTF_EXTENSION_FLAG_GAUSSIAN_SPLATTING) {
1346+
cgltf_write_stritem(context, "KHR_gaussian_splatting");
1347+
}
13321348
}
13331349

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

0 commit comments

Comments
 (0)