gltf icon indicating copy to clipboard operation
gltf copied to clipboard

Failed to iter over sparse accessor

Open wsw0108 opened this issue 2 years ago • 5 comments

sample model: https://github.com/cx20/gltf-test/tree/master/tutorialModels/SimpleSparseAccessor

use gltf;

fn main() {
    let mut args = std::env::args();
    let (document, buffers, _) = gltf::import(args.nth(1).unwrap()).unwrap();
    for mesh in document.meshes() {
        for primitive in mesh.primitives() {
            let reader = primitive.reader(|buffer: gltf::Buffer| Some(&buffers[buffer.index()]));
            let positions: Vec<[f32; 3]> = reader.read_positions().unwrap().map(|p| p).collect();
            println!("{:?}", positions);
        }
    }
}

wsw0108 avatar Nov 12 '21 03:11 wsw0108

Could you elaborate on what you expected to happen and what actually happened?

Were any positions printed? Were some, but not all, positions printed? Did it panic?

aloucks avatar Nov 12 '21 15:11 aloucks

     Running `target\debug\gltf-issue-313.exe ..\m3t\models\tutorialModels\SimpleSparseAccessor\glTF\SimpleSparseAccessor.gltf`
thread 'main' panicked at 'attempt to subtract with overflow', C:\Users\wsw\scoop\persist\rustup\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\gltf-0.16.0\src\accessor\util.rs:149:20

wsw0108 avatar Nov 12 '21 15:11 wsw0108

https://github.com/gltf-rs/gltf/blob/47acbef482b525a31312dff332015affacaa5648/src/accessor/util.rs#L148-L152

Does it work as expected if you change line 149 to:

let hint = self.indices.len() - self.counter as usize; 

aloucks avatar Nov 12 '21 15:11 aloucks

No, it does not work.

The hint shoule be accessor.count() - self.counter according the gltf spec and trait size_hint.

wsw0108 avatar Nov 13 '21 00:11 wsw0108

We encountered this issue on one of our assets. I dug into it and wrote a fix for it:

  • #411

I added some unit tests based on the repro-code @wsw0108 posted above. Thanks for that 👍

At the same time I also discovered that the sparse accessor iterator could not read accessors where the bufferView field was unset. I took the opportunity to fix this issue as well.

derwiath avatar Jan 08 '24 20:01 derwiath