gltf-rs / gltf

A crate for loading glTF 2.0
Apache License 2.0
535 stars 124 forks source link

support for `ReadIndices::try_into_u16` #436

Open ickk opened 1 month ago

ickk commented 1 month ago

I wanted a casting iterator for ReadIndices that would try to cast indices into u16s rather than u32s. I've implemented this feature, but not totally sure if it's something you would want to merge: here it is https://gist.github.com/ickk/da6a34cb6d3b4199d865d854fc8be0c4

With the linked gist, it becomes possible to call ReadIndices::try_into_u16, which returns an Option<CastingIter<'_, u16>>. It checks the variant of ReadIndices only once, making this efficient.

Caveats: It is obviously possible to have u32 indices that are all <= u16::MAX but in this case try_into_u16 cannot know this ahead of time and returns None. This makes try_into_u16 less useful in practice - you would have to have a separate branch to additionally handle TryFrom<u32> if assets may contain u32 indices.

If you'd like for me to PR this anyway (perhaps with a better name?) I will be glad to do so