@@ -21,6 +21,7 @@ use bevy_utils::{HashMap, HashSet};
21
21
pub const INDEX_BUFFER_ASSET_INDEX : u64 = 0 ;
22
22
pub const VERTEX_ATTRIBUTE_BUFFER_ID : u64 = 10 ;
23
23
24
+ /// An array where each entry describes a property of a single vertex.
24
25
#[ derive( Clone , Debug ) ]
25
26
pub enum VertexAttributeValues {
26
27
Float ( Vec < f32 > ) ,
@@ -39,6 +40,8 @@ pub enum VertexAttributeValues {
39
40
}
40
41
41
42
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.
42
45
pub fn len ( & self ) -> usize {
43
46
match * self {
44
47
VertexAttributeValues :: Float ( ref values) => values. len ( ) ,
@@ -57,11 +60,14 @@ impl VertexAttributeValues {
57
60
}
58
61
}
59
62
63
+ /// Returns `true` if there are no vertices in this VertexAttributeValue
60
64
pub fn is_empty ( & self ) -> bool {
61
65
self . len ( ) == 0
62
66
}
63
67
64
68
// 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.
65
71
pub fn get_bytes ( & self ) -> & [ u8 ] {
66
72
match self {
67
73
VertexAttributeValues :: Float ( values) => values. as_slice ( ) . as_bytes ( ) ,
@@ -179,6 +185,9 @@ impl From<Vec<[u8; 4]>> for VertexAttributeValues {
179
185
}
180
186
}
181
187
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.
182
191
#[ derive( Debug ) ]
183
192
pub enum Indices {
184
193
U16 ( Vec < u16 > ) ,
@@ -204,12 +213,39 @@ pub struct Mesh {
204
213
indices : Option < Indices > ,
205
214
}
206
215
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
+ /// ```
207
233
impl Mesh {
234
+ /// Per vertex coloring. Use in conjunction with [`Mesh::set_attribute`]
208
235
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`]
209
238
pub const ATTRIBUTE_NORMAL : & ' static str = "Vertex_Normal" ;
239
+
240
+ /// Where the vertex is located in space. Use in conjunction with [`Mesh::set_attribute`]
210
241
pub const ATTRIBUTE_POSITION : & ' static str = "Vertex_Position" ;
242
+
243
+ /// Texture coordinates for the vertex. Use in conjunction with [`Mesh::set_attribute`]
211
244
pub const ATTRIBUTE_UV_0 : & ' static str = "Vertex_Uv" ;
212
245
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`.
213
249
pub fn new ( primitive_topology : PrimitiveTopology ) -> Self {
214
250
Mesh {
215
251
primitive_topology,
@@ -222,6 +258,8 @@ impl Mesh {
222
258
self . primitive_topology
223
259
}
224
260
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`]
225
263
pub fn set_attribute (
226
264
& mut self ,
227
265
name : impl Into < Cow < ' static , str > > ,
@@ -231,6 +269,7 @@ impl Mesh {
231
269
self . attributes . insert ( name. into ( ) , values) ;
232
270
}
233
271
272
+ /// Retrieve the data currently set behind a vertex attribute.
234
273
pub fn attribute ( & self , name : impl Into < Cow < ' static , str > > ) -> Option < & VertexAttributeValues > {
235
274
self . attributes . get ( & name. into ( ) )
236
275
}
@@ -242,6 +281,8 @@ impl Mesh {
242
281
self . attributes . get_mut ( & name. into ( ) )
243
282
}
244
283
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
245
286
pub fn set_indices ( & mut self , indices : Option < Indices > ) {
246
287
self . indices = indices;
247
288
}
0 commit comments