Skip to content

Commit

Permalink
Tetrahedron mesh (#13463)
Browse files Browse the repository at this point in the history
# 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
mweatherley authored May 22, 2024
1 parent 60afec2 commit c7f7d90
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/bevy_render/src/mesh/primitives/dim3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod cuboid;
mod cylinder;
mod plane;
mod sphere;
mod tetrahedron;
mod torus;
pub(crate) mod triangle3d;

Expand Down
55 changes: 55 additions & 0 deletions crates/bevy_render/src/mesh/primitives/dim3/tetrahedron.rs
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()
}
}
8 changes: 7 additions & 1 deletion crates/bevy_render/src/mesh/primitives/dim3/triangle3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Meshable for Triangle3d {
let uvs: Vec<_> = uv_coords(self).into();

// Every vertex has the normal of the face of the triangle (or zero if the triangle is degenerate).
let normal: Vec3 = self.normal().map_or(Vec3::ZERO, |n| n.into());
let normal: Vec3 = normal_vec(self);
let normals = vec![normal; 3];

let indices = Indices::U32(vec![0, 1, 2]);
Expand All @@ -30,6 +30,12 @@ impl Meshable for Triangle3d {
}
}

/// The normal of a [`Triangle3d`] with zeroing so that a [`Vec3`] is always obtained for meshing.
#[inline]
pub(crate) fn normal_vec(triangle: &Triangle3d) -> Vec3 {
triangle.normal().map_or(Vec3::ZERO, |n| n.into())
}

/// Unskewed uv-coordinates for a [`Triangle3d`].
#[inline]
pub(crate) fn uv_coords(triangle: &Triangle3d) -> [[f32; 2]; 3] {
Expand Down
1 change: 1 addition & 0 deletions examples/3d/3d_shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn setup(

let shapes = [
meshes.add(Cuboid::default()),
meshes.add(Tetrahedron::default()),
meshes.add(Capsule3d::default()),
meshes.add(Torus::default()),
meshes.add(Cylinder::default()),
Expand Down

0 comments on commit c7f7d90

Please sign in to comment.