learn-wgpu
learn-wgpu copied to clipboard
Panic when loading .obj without normals.
In load_model, it is presumed that the mesh has normal's. If a model is loaded which doesn't have them it panics with an out of bounds exception.
let vertices = (0..m.mesh.positions.len() / 3)
.map(|i| {
if m.mesh.normals.is_empty(){
model::ModelVertex {
position: [
m.mesh.positions[i * 3],
m.mesh.positions[i * 3 + 1],
m.mesh.positions[i * 3 + 2],
],
tex_coords: [m.mesh.texcoords[i * 2], 1.0 - m.mesh.texcoords[i * 2 + 1]],
normal: [0.0, 0.0, 0.0],
}
}else{
model::ModelVertex {
position: [
m.mesh.positions[i * 3],
m.mesh.positions[i * 3 + 1],
m.mesh.positions[i * 3 + 2],
],
tex_coords: [m.mesh.texcoords[i * 2], 1.0 - m.mesh.texcoords[i * 2 + 1]],
normal: [
m.mesh.normals[i * 3],
m.mesh.normals[i * 3 + 1],
m.mesh.normals[i * 3 + 2],
],
}
}
})
.collect::<Vec<_>>();
There is probably a more elegant way to do this but I am not the best with Rust.
That's a perfectly fine way of doing it. There might be a way to do it with just iterators, but this is good enough. Mind making a PR?
Yeah will do.
since the vertex normals are derived from the face normals, they can be calculated from just the positions and indices.