Skip to content

Commit 0068483

Browse files
committed
Documented some of the Mesh properties (#1566)
I was fiddling with creating a mesh importer today, and decided to write some more docs. A lot of this is describing general renderer/GL stuff, so you'll probably find most of it self explanatory anyway, but perhaps it will be useful for someone.
1 parent 64b2961 commit 0068483

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

crates/bevy_render/src/mesh/mesh.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use bevy_utils::{HashMap, HashSet};
2121
pub const INDEX_BUFFER_ASSET_INDEX: u64 = 0;
2222
pub const VERTEX_ATTRIBUTE_BUFFER_ID: u64 = 10;
2323

24+
/// An array where each entry describes a property of a single vertex.
2425
#[derive(Clone, Debug)]
2526
pub enum VertexAttributeValues {
2627
Float(Vec<f32>),
@@ -39,6 +40,8 @@ pub enum VertexAttributeValues {
3940
}
4041

4142
impl VertexAttributeValues {
43+
/// Returns the number of vertices in this VertexAttribute. For a single
44+
/// mesh, all of the VertexAttributeValues must have the same length.
4245
pub fn len(&self) -> usize {
4346
match *self {
4447
VertexAttributeValues::Float(ref values) => values.len(),
@@ -57,11 +60,14 @@ impl VertexAttributeValues {
5760
}
5861
}
5962

63+
/// Returns `true` if there are no vertices in this VertexAttributeValue
6064
pub fn is_empty(&self) -> bool {
6165
self.len() == 0
6266
}
6367

6468
// TODO: add vertex format as parameter here and perform type conversions
69+
/// Flattens the VertexAttributeArray into a sequence of bytes. This is
70+
/// useful for serialization and sending to the GPU.
6571
pub fn get_bytes(&self) -> &[u8] {
6672
match self {
6773
VertexAttributeValues::Float(values) => values.as_slice().as_bytes(),
@@ -179,6 +185,9 @@ impl From<Vec<[u8; 4]>> for VertexAttributeValues {
179185
}
180186
}
181187

188+
/// An array of indices into the VertexAttributeValues for a mesh.
189+
///
190+
/// It describes the order in which the vertex attributes should be joined into faces.
182191
#[derive(Debug)]
183192
pub enum Indices {
184193
U16(Vec<u16>),
@@ -204,12 +213,39 @@ pub struct Mesh {
204213
indices: Option<Indices>,
205214
}
206215

216+
/// Contains geometry in the form of a mesh.
217+
///
218+
/// Often meshes are automatically generated by bevy's asset loaders or primitives, such as
219+
/// [`crate::shape::Cube`] or [`crate::shape::Box`], but you can also construct
220+
/// one yourself.
221+
///
222+
/// Example of constructing a mesh:
223+
/// ```
224+
/// # use bevy_render::mesh::{Mesh, Indices};
225+
/// # use bevy_render::pipeline::PrimitiveTopology;
226+
/// fn create_triangle() -> Mesh {
227+
/// let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
228+
/// mesh.set_attribute(Mesh::ATTRIBUTE_POSITION, vec![[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0, 0.0]]);
229+
/// mesh.set_indices(Some(Indices::U32(vec![0,1,2])));
230+
/// mesh
231+
/// }
232+
/// ```
207233
impl Mesh {
234+
/// Per vertex coloring. Use in conjunction with [`Mesh::set_attribute`]
208235
pub const ATTRIBUTE_COLOR: &'static str = "Vertex_Color";
236+
237+
/// The direction the vertex normal is facing in. Use in conjunction with [`Mesh::set_attribute`]
209238
pub const ATTRIBUTE_NORMAL: &'static str = "Vertex_Normal";
239+
240+
/// Where the vertex is located in space. Use in conjunction with [`Mesh::set_attribute`]
210241
pub const ATTRIBUTE_POSITION: &'static str = "Vertex_Position";
242+
243+
/// Texture coordinates for the vertex. Use in conjunction with [`Mesh::set_attribute`]
211244
pub const ATTRIBUTE_UV_0: &'static str = "Vertex_Uv";
212245

246+
/// Construct a new mesh. You need to provide a PrimitiveTopology so that the
247+
/// renderer knows how to treat the vertex data. Most of the time this will be
248+
/// `PrimitiveTopology::TriangleList`.
213249
pub fn new(primitive_topology: PrimitiveTopology) -> Self {
214250
Mesh {
215251
primitive_topology,
@@ -222,6 +258,8 @@ impl Mesh {
222258
self.primitive_topology
223259
}
224260

261+
/// Sets the data for a vertex attribute (position, normal etc.). The name will
262+
/// often be one of the associated constants such as [`Mesh::ATTRIBUTE_POSITION`]
225263
pub fn set_attribute(
226264
&mut self,
227265
name: impl Into<Cow<'static, str>>,
@@ -231,6 +269,7 @@ impl Mesh {
231269
self.attributes.insert(name.into(), values);
232270
}
233271

272+
/// Retrieve the data currently set behind a vertex attribute.
234273
pub fn attribute(&self, name: impl Into<Cow<'static, str>>) -> Option<&VertexAttributeValues> {
235274
self.attributes.get(&name.into())
236275
}
@@ -242,6 +281,8 @@ impl Mesh {
242281
self.attributes.get_mut(&name.into())
243282
}
244283

284+
/// Indices describe how triangles are constructed out of the vertex attributes.
285+
/// They are only useful for the [`crate::pipeline::PrimitiveTopology`] variants that use triangles
245286
pub fn set_indices(&mut self, indices: Option<Indices>) {
246287
self.indices = indices;
247288
}

crates/bevy_render/src/pipeline/state_descriptors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub enum CompareFunction {
8787
Always = 7,
8888
}
8989

90+
/// Describes how the VertexAttributes should be interpreted while rendering
9091
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize, Reflect)]
9192
pub enum PrimitiveTopology {
9293
PointList = 0,

0 commit comments

Comments
 (0)