@@ -2638,7 +2638,10 @@ cgltf_size cgltf_accessor_unpack_indices(const cgltf_accessor* accessor, void* o
26382638 return accessor -> count ;
26392639 }
26402640
2641- index_count = accessor -> count < index_count ? accessor -> count : index_count ;
2641+ cgltf_size numbers_per_element = cgltf_num_components (accessor -> type );
2642+ cgltf_size available_numbers = accessor -> count * numbers_per_element ;
2643+
2644+ index_count = available_numbers < index_count ? available_numbers : index_count ;
26422645 cgltf_size index_component_size = cgltf_component_size (accessor -> component_type );
26432646
26442647 if (accessor -> is_sparse )
@@ -2660,15 +2663,23 @@ cgltf_size cgltf_accessor_unpack_indices(const cgltf_accessor* accessor, void* o
26602663 }
26612664 element += accessor -> offset ;
26622665
2663- if (index_component_size == out_component_size && accessor -> stride == out_component_size )
2666+ if (index_component_size == out_component_size && accessor -> stride == out_component_size * numbers_per_element )
26642667 {
26652668 memcpy (out , element , index_count * index_component_size );
26662669 return index_count ;
26672670 }
26682671
2672+ // Data couldn't be copied with memcpy due to stride being larger than the component size.
2673+ // OR
26692674 // The component size of the output array is larger than the component size of the index data, so index data will be padded.
26702675 switch (out_component_size )
26712676 {
2677+ case 1 :
2678+ for (cgltf_size index = 0 ; index < index_count ; index ++ , element += accessor -> stride )
2679+ {
2680+ ((uint8_t * )out )[index ] = (uint8_t )cgltf_component_read_index (element , accessor -> component_type );
2681+ }
2682+ break ;
26722683 case 2 :
26732684 for (cgltf_size index = 0 ; index < index_count ; index ++ , element += accessor -> stride )
26742685 {
@@ -2682,7 +2693,7 @@ cgltf_size cgltf_accessor_unpack_indices(const cgltf_accessor* accessor, void* o
26822693 }
26832694 break ;
26842695 default :
2685- break ;
2696+ return 0 ;
26862697 }
26872698
26882699 return index_count ;
0 commit comments