mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
Tetrahedron mesh (#13463)
# 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.
This commit is contained in:
parent
60afec2a00
commit
c7f7d906ca
4 changed files with 64 additions and 1 deletions
|
@ -4,6 +4,7 @@ mod cuboid;
|
|||
mod cylinder;
|
||||
mod plane;
|
||||
mod sphere;
|
||||
mod tetrahedron;
|
||||
mod torus;
|
||||
pub(crate) mod triangle3d;
|
||||
|
||||
|
|
55
crates/bevy_render/src/mesh/primitives/dim3/tetrahedron.rs
Normal file
55
crates/bevy_render/src/mesh/primitives/dim3/tetrahedron.rs
Normal file
|
@ -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()
|
||||
}
|
||||
}
|
|
@ -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]);
|
||||
|
@ -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] {
|
||||
|
|
|
@ -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()),
|
||||
|
|
Loading…
Reference in a new issue