gltf-rs / gltf

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

Possible to return a slice intead of an iterator ? #397

Open ThomasCartier opened 12 months ago

ThomasCartier commented 12 months ago

Hi,

Not very experienced in Rust,

for skin in import.doc.skins() {
    let name = skin.name().map(|name| name.into());
    let joint_node_indices: Vec<usize> = skin.joints().map(|j| j.index()).collect();
    for node_index in joint_node_indices.iter().cloned() {
        nodes[node_index].is_joint = true;
    }
    let reader = skin.reader(|buffer| Some(&import.buffers[buffer.index()]));
    let inverse_bind_matrices = if let Some(inverse_bind_matrices) = reader.read_inverse_bind_matrices() {
        inverse_bind_matrices.collect()
    } else {
        let identity = *mat4(&cgmath::Matrix4::identity());
        vec![identity; joint_node_indices.len()]
    };

   ...
}

read_inverse_bind_matrices returns an iterator, which force to collect() afterwards.

Is it possible to return a slice over the buffer instead, avoiding to collect?

Maybe through a read_slice_inverse_bind_matrices ?

Thanks!

ThomasCartier commented 12 months ago

The context is random access of accessors, because iterators consume their view. A slice doesn't suffer from this and is more efficient in that case, instead of rebuilding a reader everytime. What do you think ?