
# Objective - Implement `Meshable` for `Extrusion<T>` ## Solution - `Meshable` requires `Meshable::Output: MeshBuilder` now. This means that all `some_primitive.mesh()` calls now return a `MeshBuilder`. These were added for primitives that did not have one prior to this. - A new trait `Extrudable: MeshBuilder` has been added. This trait allows you to specify the indices of the perimeter of the mesh created by this `MeshBuilder` and whether they are to be shaded smooth or flat. - `Extrusion<P: Primitive2d + Meshable>` is now `Meshable` aswell. The associated `MeshBuilder` is `ExtrusionMeshBuilder` which is generic over `P` and uses the `MeshBuilder` of its baseshape internally. - `ExtrusionMeshBuilder` exposes the configuration functions of its base-shapes builder. - Updated the `3d_shapes` example to include `Extrusion`s ## Migration Guide - Depending on the context, you may need to explicitly call `.mesh().build()` on primitives where you have previously called `.mesh()` - The `Output` type of custom `Meshable` implementations must now derive `MeshBuilder`. ## Additional information - The extrusions UVs are done so that - the front face (`+Z`) is in the area between `(0, 0)` and `(0.5, 0.5)`, - the back face (`-Z`) is in the area between `(0.5, 0)` and `(1, 0.5)` - the mantle is in the area between `(0, 0.5)` and `(1, 1)`. Each `PerimeterSegment` you specified in the `Extrudable` implementation will be allocated an equal portion of this area. - The UVs of the base shape are scaled to be in the front/back area so whatever method of filling the full UV-space the base shape used is how these areas will be filled. Here is an example of what that looks like on a capsule: https://github.com/bevyengine/bevy/assets/62256001/425ad288-fbbc-4634-9d3f-5e846cdce85f This is the texture used:  The `3d_shapes` example now looks like this:  --------- Co-authored-by: Lynn Büttgenbach <62256001+solis-lumine-vorago@users.noreply.github.com> Co-authored-by: Matty <weatherleymatthew@gmail.com> Co-authored-by: Matty <2975848+mweatherley@users.noreply.github.com>
67 lines
2.0 KiB
Rust
67 lines
2.0 KiB
Rust
use super::triangle3d;
|
|
use crate::{
|
|
mesh::{Indices, Mesh, MeshBuilder, Meshable},
|
|
render_asset::RenderAssetUsages,
|
|
};
|
|
use bevy_math::primitives::{Tetrahedron, Triangle3d};
|
|
use wgpu::PrimitiveTopology;
|
|
|
|
/// A builder used for creating a [`Mesh`] with a [`Tetrahedron`] shape.
|
|
pub struct TetrahedronMeshBuilder {
|
|
tetrahedron: Tetrahedron,
|
|
}
|
|
|
|
impl MeshBuilder for TetrahedronMeshBuilder {
|
|
fn build(&self) -> Mesh {
|
|
let mut faces: Vec<_> = self.tetrahedron.faces().into();
|
|
|
|
// If the tetrahedron has negative orientation, reverse all the triangles so that
|
|
// they still face outward.
|
|
if self.tetrahedron.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 Meshable for Tetrahedron {
|
|
type Output = TetrahedronMeshBuilder;
|
|
|
|
fn mesh(&self) -> Self::Output {
|
|
TetrahedronMeshBuilder { tetrahedron: *self }
|
|
}
|
|
}
|
|
|
|
impl From<Tetrahedron> for Mesh {
|
|
fn from(tetrahedron: Tetrahedron) -> Self {
|
|
tetrahedron.mesh().build()
|
|
}
|
|
}
|