Path: blob/main/crates/bevy_mesh/src/primitives/dim3/tetrahedron.rs
6598 views
use super::triangle3d;1use crate::{Indices, Mesh, MeshBuilder, Meshable, PrimitiveTopology};2use bevy_asset::RenderAssetUsages;3use bevy_math::primitives::{Tetrahedron, Triangle3d};4use bevy_reflect::prelude::*;56/// A builder used for creating a [`Mesh`] with a [`Tetrahedron`] shape.7#[derive(Clone, Copy, Debug, Default, Reflect)]8#[reflect(Default, Debug, Clone)]9pub struct TetrahedronMeshBuilder {10tetrahedron: Tetrahedron,11}1213impl MeshBuilder for TetrahedronMeshBuilder {14fn build(&self) -> Mesh {15let mut faces: Vec<_> = self.tetrahedron.faces().into();1617// If the tetrahedron has negative orientation, reverse all the triangles so that18// they still face outward.19if self.tetrahedron.signed_volume().is_sign_negative() {20faces.iter_mut().for_each(Triangle3d::reverse);21}2223let mut positions = vec![];24let mut normals = vec![];25let mut uvs = vec![];2627// Each face is meshed as a `Triangle3d`, and we just shove the data into the28// vertex attributes sequentially.29for face in faces {30positions.extend(face.vertices);3132let face_normal = triangle3d::normal_vec(&face);33normals.extend(vec![face_normal; 3]);3435let face_uvs = triangle3d::uv_coords(&face);36uvs.extend(face_uvs);37}3839// There are four faces and none of them share vertices.40let indices = Indices::U32(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);4142Mesh::new(43PrimitiveTopology::TriangleList,44RenderAssetUsages::default(),45)46.with_inserted_indices(indices)47.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)48.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)49.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)50}51}5253impl Meshable for Tetrahedron {54type Output = TetrahedronMeshBuilder;5556fn mesh(&self) -> Self::Output {57TetrahedronMeshBuilder { tetrahedron: *self }58}59}6061impl From<Tetrahedron> for Mesh {62fn from(tetrahedron: Tetrahedron) -> Self {63tetrahedron.mesh().build()64}65}666768