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

Wrapping Mesh fields in Option<T> #71

Open
EtaLoop opened this issue Oct 28, 2024 · 2 comments
Open

Wrapping Mesh fields in Option<T> #71

EtaLoop opened this issue Oct 28, 2024 · 2 comments

Comments

@EtaLoop
Copy link
Contributor

EtaLoop commented Oct 28, 2024

Wrapping Mesh fields (vertices, normals, texcoords, faces, colors) in Option would be easier to check.
For example, checking if mesh.normals is Some is more convenient that checking if normals.is_empty() && normals[0].is_empty().

@taiki-e
Copy link
Contributor

taiki-e commented Oct 31, 2024

I don't have a strong opinion on this, but if we do this, we need to figure out which ones are Optional in the spec and which ones should actually be treated as Optional.

For example, why do you want vertices and faces to be Optional as well?
(Btw, our examples do not treat them as options.)

let coords = mesh.vertices.into_iter().map(Into::into).collect();
let faces = mesh
.faces
.into_iter()
.map(|f| na::Point3::new(f[0], f[1], f[2]))
.collect();
let normals = if mesh.normals.is_empty() {
None
} else {
Some(mesh.normals.into_iter().map(Into::into).collect())
};
let uvs = if mesh.texcoords[0].is_empty() {
None
} else {
Some(mesh.texcoords[0].iter().copied().map(Into::into).collect())
};
let kiss3d_mesh = Rc::new(RefCell::new(kiss3d::resource::Mesh::new(
coords, faces, normals, uvs, false,
)));

bevy_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
if mesh.texcoords[0].is_empty() {
let uvs = vec![[0.0, 0.0]; num_vertices];
bevy_mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
} else {
bevy_mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, mem::take(&mut mesh.texcoords[0]));
}
if !mesh.colors[0].is_empty() {
let colors: Vec<[f32; 4]> = mesh.colors[0]
.iter()
.map(|c| {
// TODO
Color::Rgba {
red: c[0],
green: c[1],
blue: c[2],
alpha: c[3],
}
.as_linear_rgba_f32()
})
.collect();
bevy_mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
}
bevy_mesh.set_indices(Some(Indices::U32(indices)));
if mesh.normals.is_empty() {
bevy_mesh.duplicate_vertices();
bevy_mesh.compute_flat_normals();
} else {
bevy_mesh.insert_attribute(
Mesh::ATTRIBUTE_NORMAL,
VertexAttributeValues::Float32x3(mesh.normals),
);
}

@EtaLoop
Copy link
Contributor Author

EtaLoop commented Oct 31, 2024

You are right, vertices shouldn't be optional, it was my mistake.
I don't really know what are faces btw.. It is something you're making by yourself or is it given in the collada file?

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

2 participants