Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to iter over sparse accessor #313

Open
wsw0108 opened this issue Nov 12, 2021 · 5 comments
Open

Failed to iter over sparse accessor #313

wsw0108 opened this issue Nov 12, 2021 · 5 comments

Comments

@wsw0108
Copy link

wsw0108 commented Nov 12, 2021

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);
        }
    }
}
@aloucks
Copy link
Contributor

aloucks commented Nov 12, 2021

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?

@wsw0108
Copy link
Author

wsw0108 commented Nov 12, 2021

     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

@aloucks
Copy link
Contributor

aloucks commented Nov 12, 2021

gltf/src/accessor/util.rs

Lines 148 to 152 in 47acbef

fn size_hint(&self) -> (usize, Option<usize>) {
let hint = self.values.len() - self.counter as usize;
(hint, Some(hint))
}
}

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

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

@wsw0108
Copy link
Author

wsw0108 commented Nov 13, 2021

No, it does not work.

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

@wsw0108 wsw0108 changed the title failed to iter over sparse accessor Failed to iter over sparse accessor Nov 19, 2021
@derwiath
Copy link
Contributor

derwiath commented Jan 8, 2024

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants