@@ -408,6 +408,13 @@ typedef struct cgltf_ior
408
408
cgltf_float ior ;
409
409
} cgltf_ior ;
410
410
411
+ typedef struct cgltf_specular
412
+ {
413
+ cgltf_texture_view specular_texture ;
414
+ cgltf_float specular_color_factor [3 ];
415
+ cgltf_float specular_factor ;
416
+ } cgltf_specular ;
417
+
411
418
typedef struct cgltf_material
412
419
{
413
420
char * name ;
@@ -416,10 +423,12 @@ typedef struct cgltf_material
416
423
cgltf_bool has_clearcoat ;
417
424
cgltf_bool has_transmission ;
418
425
cgltf_bool has_ior ;
426
+ cgltf_bool has_specular ;
419
427
cgltf_pbr_metallic_roughness pbr_metallic_roughness ;
420
428
cgltf_pbr_specular_glossiness pbr_specular_glossiness ;
421
429
cgltf_clearcoat clearcoat ;
422
430
cgltf_ior ior ;
431
+ cgltf_specular specular ;
423
432
cgltf_transmission transmission ;
424
433
cgltf_texture_view normal_texture ;
425
434
cgltf_texture_view occlusion_texture ;
@@ -1661,6 +1670,10 @@ void cgltf_free(cgltf_data* data)
1661
1670
cgltf_free_extensions (data , data -> materials [i ].clearcoat .clearcoat_roughness_texture .extensions , data -> materials [i ].clearcoat .clearcoat_roughness_texture .extensions_count );
1662
1671
cgltf_free_extensions (data , data -> materials [i ].clearcoat .clearcoat_normal_texture .extensions , data -> materials [i ].clearcoat .clearcoat_normal_texture .extensions_count );
1663
1672
}
1673
+ if (data -> materials [i ].has_specular )
1674
+ {
1675
+ cgltf_free_extensions (data , data -> materials [i ].specular .specular_texture .extensions , data -> materials [i ].specular .specular_texture .extensions_count );
1676
+ }
1664
1677
if (data -> materials [i ].has_transmission )
1665
1678
{
1666
1679
cgltf_free_extensions (data , data -> materials [i ].transmission .transmission_texture .extensions , data -> materials [i ].transmission .transmission_texture .extensions_count );
@@ -3288,6 +3301,9 @@ static int cgltf_parse_json_ior(jsmntok_t const* tokens, int i, const uint8_t* j
3288
3301
int size = tokens [i ].size ;
3289
3302
++ i ;
3290
3303
3304
+ // Default values
3305
+ out_ior -> ior = 1.5f ;
3306
+
3291
3307
for (int j = 0 ; j < size ; ++ j )
3292
3308
{
3293
3309
CGLTF_CHECK_KEY (tokens [i ]);
@@ -3312,6 +3328,48 @@ static int cgltf_parse_json_ior(jsmntok_t const* tokens, int i, const uint8_t* j
3312
3328
return i ;
3313
3329
}
3314
3330
3331
+ static int cgltf_parse_json_specular (cgltf_options * options , jsmntok_t const * tokens , int i , const uint8_t * json_chunk , cgltf_specular * out_specular )
3332
+ {
3333
+ CGLTF_CHECK_TOKTYPE (tokens [i ], JSMN_OBJECT );
3334
+ int size = tokens [i ].size ;
3335
+ ++ i ;
3336
+
3337
+ // Default values
3338
+ out_specular -> specular_factor = 1.0f ;
3339
+ cgltf_fill_float_array (out_specular -> specular_color_factor , 3 , 1.0f );
3340
+
3341
+ for (int j = 0 ; j < size ; ++ j )
3342
+ {
3343
+ CGLTF_CHECK_KEY (tokens [i ]);
3344
+
3345
+ if (cgltf_json_strcmp (tokens + i , json_chunk , "specularFactor" ) == 0 )
3346
+ {
3347
+ ++ i ;
3348
+ out_specular -> specular_factor = cgltf_json_to_float (tokens + i , json_chunk );
3349
+ ++ i ;
3350
+ }
3351
+ else if (cgltf_json_strcmp (tokens + i , json_chunk , "specularColorFactor" ) == 0 )
3352
+ {
3353
+ i = cgltf_parse_json_float_array (tokens , i + 1 , json_chunk , out_specular -> specular_color_factor , 3 );
3354
+ }
3355
+ else if (cgltf_json_strcmp (tokens + i , json_chunk , "specularTexture" ) == 0 )
3356
+ {
3357
+ i = cgltf_parse_json_texture_view (options , tokens , i + 1 , json_chunk , & out_specular -> specular_texture );
3358
+ }
3359
+ else
3360
+ {
3361
+ i = cgltf_skip_json (tokens , i + 1 );
3362
+ }
3363
+
3364
+ if (i < 0 )
3365
+ {
3366
+ return i ;
3367
+ }
3368
+ }
3369
+
3370
+ return i ;
3371
+ }
3372
+
3315
3373
static int cgltf_parse_json_transmission (cgltf_options * options , jsmntok_t const * tokens , int i , const uint8_t * json_chunk , cgltf_transmission * out_transmission )
3316
3374
{
3317
3375
CGLTF_CHECK_TOKTYPE (tokens [i ], JSMN_OBJECT );
@@ -3638,6 +3696,11 @@ static int cgltf_parse_json_material(cgltf_options* options, jsmntok_t const* to
3638
3696
out_material -> has_ior = 1 ;
3639
3697
i = cgltf_parse_json_ior (tokens , i + 1 , json_chunk , & out_material -> ior );
3640
3698
}
3699
+ else if (cgltf_json_strcmp (tokens + i , json_chunk , "KHR_materials_specular" ) == 0 )
3700
+ {
3701
+ out_material -> has_specular = 1 ;
3702
+ i = cgltf_parse_json_specular (options , tokens , i + 1 , json_chunk , & out_material -> specular );
3703
+ }
3641
3704
else if (cgltf_json_strcmp (tokens + i , json_chunk , "KHR_materials_transmission" ) == 0 )
3642
3705
{
3643
3706
out_material -> has_transmission = 1 ;
@@ -5242,6 +5305,8 @@ static int cgltf_fixup_pointers(cgltf_data* data)
5242
5305
CGLTF_PTRFIXUP (data -> materials [i ].clearcoat .clearcoat_roughness_texture .texture , data -> textures , data -> textures_count );
5243
5306
CGLTF_PTRFIXUP (data -> materials [i ].clearcoat .clearcoat_normal_texture .texture , data -> textures , data -> textures_count );
5244
5307
5308
+ CGLTF_PTRFIXUP (data -> materials [i ].specular .specular_texture .texture , data -> textures , data -> textures_count );
5309
+
5245
5310
CGLTF_PTRFIXUP (data -> materials [i ].transmission .transmission_texture .texture , data -> textures , data -> textures_count );
5246
5311
}
5247
5312
0 commit comments