@@ -500,6 +500,14 @@ typedef struct cgltf_iridescence
500500 cgltf_texture_view iridescence_thickness_texture ;
501501} cgltf_iridescence ;
502502
503+ typedef struct cgltf_diffuse_transmission
504+ {
505+ cgltf_texture_view diffuse_transmission_texture ;
506+ cgltf_float diffuse_transmission_factor ;
507+ cgltf_float diffuse_transmission_color_factor [3 ];
508+ cgltf_texture_view diffuse_transmission_color_texture ;
509+ } cgltf_diffuse_transmission ;
510+
503511typedef struct cgltf_anisotropy
504512{
505513 cgltf_float anisotropy_strength ;
@@ -525,6 +533,7 @@ typedef struct cgltf_material
525533 cgltf_bool has_sheen ;
526534 cgltf_bool has_emissive_strength ;
527535 cgltf_bool has_iridescence ;
536+ cgltf_bool has_diffuse_transmission ;
528537 cgltf_bool has_anisotropy ;
529538 cgltf_bool has_dispersion ;
530539 cgltf_pbr_metallic_roughness pbr_metallic_roughness ;
@@ -537,6 +546,7 @@ typedef struct cgltf_material
537546 cgltf_volume volume ;
538547 cgltf_emissive_strength emissive_strength ;
539548 cgltf_iridescence iridescence ;
549+ cgltf_diffuse_transmission diffuse_transmission ;
540550 cgltf_anisotropy anisotropy ;
541551 cgltf_dispersion dispersion ;
542552 cgltf_texture_view normal_texture ;
@@ -844,6 +854,8 @@ void cgltf_node_transform_world(const cgltf_node* node, cgltf_float* out_matrix)
844854
845855const uint8_t * cgltf_buffer_view_data (const cgltf_buffer_view * view );
846856
857+ const cgltf_accessor * cgltf_find_accessor (const cgltf_primitive * prim , cgltf_attribute_type type , cgltf_int index );
858+
847859cgltf_bool cgltf_accessor_read_float (const cgltf_accessor * accessor , cgltf_size index , cgltf_float * out , cgltf_size element_size );
848860cgltf_bool cgltf_accessor_read_uint (const cgltf_accessor * accessor , cgltf_size index , cgltf_uint * out , cgltf_size element_size );
849861cgltf_size cgltf_accessor_read_index (const cgltf_accessor * accessor , cgltf_size index );
@@ -2312,6 +2324,18 @@ const uint8_t* cgltf_buffer_view_data(const cgltf_buffer_view* view)
23122324 return result ;
23132325}
23142326
2327+ const cgltf_accessor * cgltf_find_accessor (const cgltf_primitive * prim , cgltf_attribute_type type , cgltf_int index )
2328+ {
2329+ for (cgltf_size i = 0 ; i < prim -> attributes_count ; ++ i )
2330+ {
2331+ const cgltf_attribute * attr = & prim -> attributes [i ];
2332+ if (attr -> type == type && attr -> index == index )
2333+ return attr -> data ;
2334+ }
2335+
2336+ return NULL ;
2337+ }
2338+
23152339cgltf_bool cgltf_accessor_read_float (const cgltf_accessor * accessor , cgltf_size index , cgltf_float * out , cgltf_size element_size )
23162340{
23172341 if (accessor -> is_sparse )
@@ -4278,6 +4302,52 @@ static int cgltf_parse_json_iridescence(cgltf_options* options, jsmntok_t const*
42784302 return i ;
42794303}
42804304
4305+ static int cgltf_parse_json_diffuse_transmission (cgltf_options * options , jsmntok_t const * tokens , int i , const uint8_t * json_chunk , cgltf_diffuse_transmission * out_diff_transmission )
4306+ {
4307+ CGLTF_CHECK_TOKTYPE (tokens [i ], JSMN_OBJECT );
4308+ int size = tokens [i ].size ;
4309+ ++ i ;
4310+
4311+ // Defaults
4312+ cgltf_fill_float_array (out_diff_transmission -> diffuse_transmission_color_factor , 3 , 1.0f );
4313+ out_diff_transmission -> diffuse_transmission_factor = 0.f ;
4314+
4315+ for (int j = 0 ; j < size ; ++ j )
4316+ {
4317+ CGLTF_CHECK_KEY (tokens [i ]);
4318+
4319+ if (cgltf_json_strcmp (tokens + i , json_chunk , "diffuseTransmissionFactor" ) == 0 )
4320+ {
4321+ ++ i ;
4322+ out_diff_transmission -> diffuse_transmission_factor = cgltf_json_to_float (tokens + i , json_chunk );
4323+ ++ i ;
4324+ }
4325+ else if (cgltf_json_strcmp (tokens + i , json_chunk , "diffuseTransmissionTexture" ) == 0 )
4326+ {
4327+ i = cgltf_parse_json_texture_view (options , tokens , i + 1 , json_chunk , & out_diff_transmission -> diffuse_transmission_texture );
4328+ }
4329+ else if (cgltf_json_strcmp (tokens + i , json_chunk , "diffuseTransmissionColorFactor" ) == 0 )
4330+ {
4331+ i = cgltf_parse_json_float_array (tokens , i + 1 , json_chunk , out_diff_transmission -> diffuse_transmission_color_factor , 3 );
4332+ }
4333+ else if (cgltf_json_strcmp (tokens + i , json_chunk , "diffuseTransmissionColorTexture" ) == 0 )
4334+ {
4335+ i = cgltf_parse_json_texture_view (options , tokens , i + 1 , json_chunk , & out_diff_transmission -> diffuse_transmission_color_texture );
4336+ }
4337+ else
4338+ {
4339+ i = cgltf_skip_json (tokens , i + 1 );
4340+ }
4341+
4342+ if (i < 0 )
4343+ {
4344+ return i ;
4345+ }
4346+ }
4347+
4348+ return i ;
4349+ }
4350+
42814351static int cgltf_parse_json_anisotropy (cgltf_options * options , jsmntok_t const * tokens , int i , const uint8_t * json_chunk , cgltf_anisotropy * out_anisotropy )
42824352{
42834353 CGLTF_CHECK_TOKTYPE (tokens [i ], JSMN_OBJECT );
@@ -4766,6 +4836,11 @@ static int cgltf_parse_json_material(cgltf_options* options, jsmntok_t const* to
47664836 out_material -> has_iridescence = 1 ;
47674837 i = cgltf_parse_json_iridescence (options , tokens , i + 1 , json_chunk , & out_material -> iridescence );
47684838 }
4839+ else if (cgltf_json_strcmp (tokens + i , json_chunk , "KHR_materials_diffuse_transmission" ) == 0 )
4840+ {
4841+ out_material -> has_diffuse_transmission = 1 ;
4842+ i = cgltf_parse_json_diffuse_transmission (options , tokens , i + 1 , json_chunk , & out_material -> diffuse_transmission );
4843+ }
47694844 else if (cgltf_json_strcmp (tokens + i , json_chunk , "KHR_materials_anisotropy" ) == 0 )
47704845 {
47714846 out_material -> has_anisotropy = 1 ;
@@ -6629,6 +6704,9 @@ static int cgltf_fixup_pointers(cgltf_data* data)
66296704 CGLTF_PTRFIXUP (data -> materials [i ].iridescence .iridescence_texture .texture , data -> textures , data -> textures_count );
66306705 CGLTF_PTRFIXUP (data -> materials [i ].iridescence .iridescence_thickness_texture .texture , data -> textures , data -> textures_count );
66316706
6707+ CGLTF_PTRFIXUP (data -> materials [i ].diffuse_transmission .diffuse_transmission_texture .texture , data -> textures , data -> textures_count );
6708+ CGLTF_PTRFIXUP (data -> materials [i ].diffuse_transmission .diffuse_transmission_color_texture .texture , data -> textures , data -> textures_count );
6709+
66326710 CGLTF_PTRFIXUP (data -> materials [i ].anisotropy .anisotropy_texture .texture , data -> textures , data -> textures_count );
66336711 }
66346712
0 commit comments