|
| 1 | +use bevy::render::{ |
| 2 | + mesh::{ |
| 3 | + VertexAttribute, |
| 4 | + Mesh |
| 5 | + }, |
| 6 | + pipeline::PrimitiveTopology, |
| 7 | +}; |
| 8 | + |
| 9 | +use lyon::{ |
| 10 | + math::{ |
| 11 | + self, |
| 12 | + Point |
| 13 | + }, |
| 14 | + tessellation as tess, |
| 15 | +}; |
| 16 | + |
| 17 | +use super::shapes::LyonShapeBuilder; |
| 18 | + |
| 19 | +/// Type alias for the type of a mesh index in bevy. |
| 20 | +pub type BevyIndex = u32; |
| 21 | +/// Type alias for a `VertexBuffers` of `BevyVertex`'s and `BevyIndex`'s. |
| 22 | +pub type BevyVertexBuffers = tess::VertexBuffers<BevyVertex, BevyIndex>; |
| 23 | +/// Type alias for a buffer builder that contains the information to properly convert lyon points to `BevyVertex`'s and `BevyIndex`'s. |
| 24 | +pub type BevyBuffersBuilder<'a> = tess::BuffersBuilder<'a, BevyVertex, BevyIndex, BevyVertexConstructor>; |
| 25 | + |
| 26 | +/// Builder that provides customizable functionality to create `lyon` tessellated meshes and build them so `bevy` can consume them. |
| 27 | +#[derive(Debug, Clone)] |
| 28 | +pub struct LyonMeshBuilder |
| 29 | +{ |
| 30 | + geometry: BevyVertexBuffers |
| 31 | +} |
| 32 | + |
| 33 | +impl LyonMeshBuilder |
| 34 | +{ |
| 35 | + /// Create a new mesh builder. |
| 36 | + pub fn new() -> Self |
| 37 | + { |
| 38 | + LyonMeshBuilder { |
| 39 | + geometry: BevyVertexBuffers::new() |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Finish the building and produce the final mesh. |
| 44 | + /// |
| 45 | + /// Uses TriangleStrip as the default primitive topology. |
| 46 | + pub fn build(self) -> Mesh |
| 47 | + { |
| 48 | + self.build_with_topology(PrimitiveTopology::TriangleStrip) |
| 49 | + } |
| 50 | + |
| 51 | + /// Finishes a mesh using a custom specified `PrimitiveTopology`. |
| 52 | + /// |
| 53 | + /// Prefer using `build()` as its default works in the vast majority of cases. |
| 54 | + pub fn build_with_topology(self, topology: PrimitiveTopology) -> Mesh |
| 55 | + { |
| 56 | + Mesh { |
| 57 | + primitive_topology: topology, |
| 58 | + attributes: self.verts_to_attributes(), |
| 59 | + indices: Some(self.geometry.indices), |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// Adds a shape specified by its `LyonShapeBuilder` implementation to the mesh being constructed. |
| 64 | + pub fn with(mut self, shape: impl LyonShapeBuilder) -> Self |
| 65 | + { |
| 66 | + shape.build(&mut self.buffers_builder()); |
| 67 | + self |
| 68 | + } |
| 69 | + |
| 70 | + /// A convenience function that makes a new `LyonMeshBuilder` and builds it with only the single shape provided. |
| 71 | + pub fn only(shape: impl LyonShapeBuilder) -> Mesh |
| 72 | + { |
| 73 | + LyonMeshBuilder::new() |
| 74 | + .with(shape) |
| 75 | + .build() |
| 76 | + } |
| 77 | + |
| 78 | + /// Internal utility function to simplify creation of an output buffer builder. |
| 79 | + fn buffers_builder(&mut self) -> tess::BuffersBuilder<BevyVertex, BevyIndex, BevyVertexConstructor> |
| 80 | + { |
| 81 | + tess::BuffersBuilder::new(&mut self.geometry, BevyVertexConstructor) |
| 82 | + } |
| 83 | + |
| 84 | + /// Internal utility function that transforms an iterator of `BevyVertex`'s into the proper array of vertex attributes. |
| 85 | + fn verts_to_attributes(&self) -> Vec<VertexAttribute> |
| 86 | + { |
| 87 | + let mut positions = vec![]; |
| 88 | + let mut normals = vec![]; |
| 89 | + let mut uvs = vec![]; |
| 90 | + |
| 91 | + for vertex in &self.geometry.vertices |
| 92 | + { |
| 93 | + positions.push(vertex.pos); |
| 94 | + normals.push(vertex.norm); |
| 95 | + uvs.push(vertex.uv); |
| 96 | + } |
| 97 | + |
| 98 | + vec![ |
| 99 | + VertexAttribute::position(positions), |
| 100 | + VertexAttribute::normal(normals), |
| 101 | + VertexAttribute::uv(uvs), |
| 102 | + ] |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/// Utility type for containing the trait implementations that transforms a lyon point into a `BevyVertex`. |
| 107 | +pub struct BevyVertexConstructor; |
| 108 | + |
| 109 | +// TODO: Figure out if uv mapping should be specific for this |
| 110 | +impl tess::BasicVertexConstructor<BevyVertex> for BevyVertexConstructor |
| 111 | +{ |
| 112 | + fn new_vertex(&mut self, point: Point) -> BevyVertex |
| 113 | + { |
| 114 | + point.into() |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// TODO: Figure out if uv mapping should be specific for this |
| 119 | +impl tess::FillVertexConstructor<BevyVertex> for BevyVertexConstructor |
| 120 | +{ |
| 121 | + fn new_vertex(&mut self, point: Point, _: tess::FillAttributes) -> BevyVertex |
| 122 | + { |
| 123 | + point.into() |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// TODO: Figure out if uv mapping should be specific for this |
| 128 | +impl tess::StrokeVertexConstructor<BevyVertex> for BevyVertexConstructor |
| 129 | +{ |
| 130 | + fn new_vertex(&mut self, point: Point, _: tess::StrokeAttributes) -> BevyVertex |
| 131 | + { |
| 132 | + point.into() |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/// Contains all the vertex information needed by bevy to correctly create a mesh. |
| 137 | +#[derive(Debug, Clone)] |
| 138 | +pub struct BevyVertex |
| 139 | +{ |
| 140 | + pub pos: [f32; 3], |
| 141 | + pub norm: [f32; 3], |
| 142 | + pub uv: [f32; 2], |
| 143 | +} |
| 144 | + |
| 145 | +/// Performs a trivial conversion from a lyon point into a `BevyVertex` |
| 146 | +impl From<math::Point> for BevyVertex |
| 147 | +{ |
| 148 | + fn from(point: math::Point) -> Self |
| 149 | + { |
| 150 | + // In 2d, Z can just be 0 |
| 151 | + BevyVertex { |
| 152 | + pos: [point.x, point.y, 0.0], |
| 153 | + norm: [0.0, 0.0, 1.0], |
| 154 | + uv: [point.x, point.y], |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
0 commit comments