-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[Merged by Bors] - calculate flat normals for mesh if missing #1808
Closed
jakobhellermann
wants to merge
8
commits into
bevyengine:main
from
jakobhellermann:gltf-generate-normals
Closed
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c7898b0
calculate flat normals for mesh if missing
jakobhellermann 5ca2020
more detailed warning when computing normals
jakobhellermann 9534a2d
normalize normals (duh)
jakobhellermann 15aeafb
Merge branch 'main' into gltf-generate-normals
cart d8f57cf
add remaining VertexAttributesValues
jakobhellermann dfc9907
debug instead of warn, log always
jakobhellermann fc968d4
Merge branch 'main' into gltf-generate-normals
jakobhellermann ed0da67
fix duplicate if
jakobhellermann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,13 @@ impl VertexAttributeValues { | |
self.len() == 0 | ||
} | ||
|
||
fn as_float3(&self) -> Option<&[[f32; 3]]> { | ||
match self { | ||
VertexAttributeValues::Float3(values) => Some(values), | ||
_ => None, | ||
} | ||
} | ||
|
||
// TODO: add vertex format as parameter here and perform type conversions | ||
/// Flattens the VertexAttributeArray into a sequence of bytes. This is | ||
/// useful for serialization and sending to the GPU. | ||
|
@@ -254,6 +261,29 @@ pub enum Indices { | |
U32(Vec<u32>), | ||
} | ||
|
||
impl Indices { | ||
fn iter(&self) -> impl Iterator<Item = usize> + '_ { | ||
match self { | ||
Indices::U16(vec) => IndicesIter::U16(vec.iter()), | ||
Indices::U32(vec) => IndicesIter::U32(vec.iter()), | ||
} | ||
} | ||
} | ||
enum IndicesIter<'a> { | ||
U16(std::slice::Iter<'a, u16>), | ||
U32(std::slice::Iter<'a, u32>), | ||
} | ||
impl Iterator for IndicesIter<'_> { | ||
type Item = usize; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
match self { | ||
IndicesIter::U16(iter) => iter.next().map(|val| *val as usize), | ||
IndicesIter::U32(iter) => iter.next().map(|val| *val as usize), | ||
} | ||
} | ||
} | ||
|
||
impl From<&Indices> for IndexFormat { | ||
fn from(indices: &Indices) -> Self { | ||
match indices { | ||
|
@@ -431,6 +461,88 @@ impl Mesh { | |
|
||
attributes_interleaved_buffer | ||
} | ||
|
||
/// Duplicates the vertex attributes so that no vertices are shared. | ||
/// | ||
/// This can dramatically increase the vertex count, so make sure this is what you want. | ||
/// Does nothing if no [Indices] are set. | ||
pub fn duplicate_vertices(&mut self) { | ||
fn duplicate<T: Copy>(values: &[T], indices: impl Iterator<Item = usize>) -> Vec<T> { | ||
indices.map(|i| values[i]).collect() | ||
} | ||
|
||
assert!( | ||
matches!(self.primitive_topology, PrimitiveTopology::TriangleList), | ||
"can only duplicate vertices for `TriangleList`s" | ||
); | ||
|
||
let indices = match self.indices.take() { | ||
Some(indices) => indices, | ||
None => return, | ||
}; | ||
for (_, attributes) in self.attributes.iter_mut() { | ||
let indices = indices.iter(); | ||
match attributes { | ||
VertexAttributeValues::Float(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Int(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Uint(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Float2(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Int2(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Uint2(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Float3(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Int3(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Uint3(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Int4(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Uint4(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Float4(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Short2(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Short2Norm(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Ushort2(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Ushort2Norm(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Short4(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Short4Norm(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Ushort4(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Ushort4Norm(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Char2(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Char2Norm(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Uchar2(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Uchar2Norm(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Char4(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Char4Norm(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Uchar4(vec) => *vec = duplicate(&vec, indices), | ||
VertexAttributeValues::Uchar4Norm(vec) => *vec = duplicate(&vec, indices), | ||
} | ||
} | ||
} | ||
|
||
/// Calculates the [`Mesh::ATTRIBUTE_NORMAL`] of a mesh. | ||
/// | ||
/// Panics if [`Indices`] are set. | ||
/// Consider calling [Mesh::duplicate_vertices] or export your mesh with normal attributes. | ||
pub fn compute_flat_normals(&mut self) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normals should also be normalized/unit length. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, normals should be actually normal 😄 |
||
if self.indices().is_some() { | ||
panic!("`compute_flat_normals` can't work on indexed geometry. Consider calling `Mesh::duplicate_vertices`."); | ||
} | ||
|
||
let positions = self | ||
.attribute(Mesh::ATTRIBUTE_POSITION) | ||
.unwrap() | ||
.as_float3() | ||
.expect("`Mesh::ATTRIBUTE_POSITION` vertex attributes should be of type `float3`"); | ||
|
||
let normals: Vec<_> = positions | ||
.chunks_exact(3) | ||
.map(|p| face_normal(p[0], p[1], p[2])) | ||
.flat_map(|normal| std::array::IntoIter::new([normal, normal, normal])) | ||
.collect(); | ||
|
||
self.set_attribute(Mesh::ATTRIBUTE_NORMAL, normals); | ||
} | ||
} | ||
|
||
fn face_normal(a: [f32; 3], b: [f32; 3], c: [f32; 3]) -> [f32; 3] { | ||
let (a, b, c) = (Vec3::from(a), Vec3::from(b), Vec3::from(c)); | ||
(b - a).cross(c - a).normalize().into() | ||
} | ||
|
||
fn remove_resource_save( | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like we have dupe checks here