gltf
gltf copied to clipboard
Failed to iter over sparse accessor
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);
}
}
}
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?
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
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;
No, it does not work.
The hint
shoule be accessor.count() - self.counter
according the gltf spec and trait size_hint.
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.