-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Objective Allow the `Tetrahedron` primitive to be used for mesh generation. This is part of ongoing work to bring unify the capabilities of `bevy_math` primitives. ## Solution `Tetrahedron` implements `Meshable`. Essentially, each face is just meshed as a `Triangle3d`, but first there is an inversion step when the signed volume of the tetrahedron is negative to ensure that the faces all actually point outward. ## Testing I loaded up some examples and hackily exchanged existing meshes with the new one to see that it works as expected.
- Loading branch information
1 parent
60afec2
commit c7f7d90
Showing
4 changed files
with
64 additions
and
1 deletion.
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
55 changes: 55 additions & 0 deletions
55
crates/bevy_render/src/mesh/primitives/dim3/tetrahedron.rs
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use super::triangle3d; | ||
use crate::{ | ||
mesh::{Indices, Mesh, Meshable}, | ||
render_asset::RenderAssetUsages, | ||
}; | ||
use bevy_math::primitives::{Tetrahedron, Triangle3d}; | ||
use wgpu::PrimitiveTopology; | ||
|
||
impl Meshable for Tetrahedron { | ||
type Output = Mesh; | ||
|
||
fn mesh(&self) -> Self::Output { | ||
let mut faces: Vec<_> = self.faces().into(); | ||
|
||
// If the tetrahedron has negative orientation, reverse all the triangles so that | ||
// they still face outward. | ||
if self.signed_volume().is_sign_negative() { | ||
faces.iter_mut().for_each(Triangle3d::reverse); | ||
} | ||
|
||
let mut positions = vec![]; | ||
let mut normals = vec![]; | ||
let mut uvs = vec![]; | ||
|
||
// Each face is meshed as a `Triangle3d`, and we just shove the data into the | ||
// vertex attributes sequentially. | ||
for face in faces { | ||
positions.extend(face.vertices); | ||
|
||
let face_normal = triangle3d::normal_vec(&face); | ||
normals.extend(vec![face_normal; 3]); | ||
|
||
let face_uvs = triangle3d::uv_coords(&face); | ||
uvs.extend(face_uvs); | ||
} | ||
|
||
// There are four faces and none of them share vertices. | ||
let indices = Indices::U32(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); | ||
|
||
Mesh::new( | ||
PrimitiveTopology::TriangleList, | ||
RenderAssetUsages::default(), | ||
) | ||
.with_inserted_indices(indices) | ||
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions) | ||
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals) | ||
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs) | ||
} | ||
} | ||
|
||
impl From<Tetrahedron> for Mesh { | ||
fn from(tetrahedron: Tetrahedron) -> Self { | ||
tetrahedron.mesh() | ||
} | ||
} |
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