@@ -26,28 +26,24 @@ use wgpu::{
26
26
pub const INDEX_BUFFER_ASSET_INDEX : u64 = 0 ;
27
27
pub const VERTEX_ATTRIBUTE_BUFFER_ID : u64 = 10 ;
28
28
29
- // TODO: allow values to be unloaded after been submitting to the GPU to conserve memory
30
- #[ derive( Debug , TypeUuid , TypePath , Clone ) ]
31
- #[ uuid = "8ecbac0f-f545-4473-ad43-e1f4243af51e" ]
32
- pub struct Mesh {
33
- primitive_topology : PrimitiveTopology ,
34
- /// `std::collections::BTreeMap` with all defined vertex attributes (Positions, Normals, ...)
35
- /// for this mesh. Attribute ids to attribute values.
36
- /// Uses a BTreeMap because, unlike HashMap, it has a defined iteration order,
37
- /// which allows easy stable VertexBuffers (i.e. same buffer order)
38
- attributes : BTreeMap < MeshVertexAttributeId , MeshAttributeData > ,
39
- indices : Option < Indices > ,
40
- morph_targets : Option < Handle < Image > > ,
41
- morph_target_names : Option < Vec < String > > ,
42
- }
43
-
44
- /// Contains geometry in the form of a mesh.
29
+ /// A 3D object made out of vertices representing triangles, lines, or points,
30
+ /// with "attribute" values for each vertex.
31
+ ///
32
+ /// Meshes can be automatically generated by a bevy `AssetLoader` (generally by loading a `Gltf` file),
33
+ /// or by converting a primitive [`shape`](crate::mesh::shape) using [`into`](std::convert::Into).
34
+ /// It is also possible to create one manually.
35
+ /// They can be edited after creation.
45
36
///
46
- /// Often meshes are automatically generated by bevy's asset loaders or primitives, such as
47
- /// [`shape::Cube`](crate::mesh::shape::Cube) or [`shape::Box`](crate::mesh::shape::Box), but you can also construct
48
- /// one yourself.
37
+ /// Meshes can be rendered with a `Material`, like `StandardMaterial` in `PbrBundle`
38
+ /// or `ColorMaterial` in `ColorMesh2dBundle`.
49
39
///
50
- /// Example of constructing a mesh (to be rendered with a `StandardMaterial`):
40
+ /// A [`Mesh`] in Bevy is equivalent to a "primitive" in the glTF format, for a
41
+ /// glTF Mesh representation, see `GltfMesh`.
42
+ ///
43
+ /// ## Manual creation
44
+ ///
45
+ /// The following function will construct a flat mesh, to be rendered with a
46
+ /// `StandardMaterial` or `ColorMaterial`:
51
47
/// ```
52
48
/// # use bevy_render::mesh::{Mesh, Indices};
53
49
/// # use bevy_render::render_resource::PrimitiveTopology;
@@ -78,50 +74,90 @@ pub struct Mesh {
78
74
/// 1, 3, 2
79
75
/// ])));
80
76
/// mesh
81
- /// // For further visualization, explanation, and examples see the built-in Bevy examples
82
- /// // and the implementation of the built-in shapes.
83
77
/// }
84
78
/// ```
85
- /// Common points of confusion:
86
- /// - UV maps in Bevy are "flipped", (0.0, 0.0) = Top-Left (not Bot-Left like `OpenGL`)
87
- /// - It is normal for multiple vertices to have the same position
88
- /// attribute - it's a common technique in 3D modelling for complex UV mapping or other calculations.
89
79
///
90
- /// To render correctly with `StandardMaterial` a mesh needs to have properly defined:
91
- /// - [`UVs`](Mesh::ATTRIBUTE_UV_0): Bevy needs to know how to map a texture onto the mesh.
92
- /// - [`Normals`](Mesh::ATTRIBUTE_NORMAL): Bevy needs to know how light interacts with your mesh. ([0.0, 0.0, 1.0] is very
93
- /// common for simple meshes because simple meshes are smooth, and they don't require complex light calculations.)
94
- /// - Vertex winding order -
95
- /// the default behavior is with `StandardMaterial.cull_mode` = Some([`Face::Front`](crate::render_resource::Face::Front)) which means
96
- /// that by default Bevy would *only* render the front of each triangle, and the front
97
- /// is the side of the triangle in which the vertices appear in a *counter-clockwise* order.
80
+ /// You can see how it looks like [here](https://github.com/bevyengine/bevy/blob/main/assets/dovs/Mesh.png),
81
+ /// used in a `PbrBundle` with a square bevy logo texture, with added axis, points,
82
+ /// lines and text for clarity.
83
+ ///
84
+ /// ## Other examples
85
+ ///
86
+ /// For further visualization, explanation, and examples, see the built-in Bevy examples,
87
+ /// and the [implementation of the built-in shapes](https://github.com/bevyengine/bevy/tree/main/crates/bevy_render/src/mesh/shape).
88
+ /// In particular, [generate_custom_mesh](https://github.com/bevyengine/bevy/blob/main/examples/3d/generate_custom_mesh.rs)
89
+ /// teaches you to access modify a Mesh's attributes after creating it.
90
+ ///
91
+ /// ## Common points of confusion
98
92
///
93
+ /// - UV maps in Bevy start at the top-left, see [`ATTRIBUTE_UV_0`](Mesh::ATTRIBUTE_UV_0),
94
+ /// other APIs can have other conventions, `OpenGL` starts at bottom-left.
95
+ /// - It is possible and sometimes useful for multiple vertices to have the same
96
+ /// [position attribute](Mesh::ATTRIBUTE_POSITION) value,
97
+ /// it's a common technique in 3D modelling for complex UV mapping or other calculations.
98
+ ///
99
+ /// ## Use with `StandardMaterial`
100
+ ///
101
+ /// To render correctly with `StandardMaterial`, a mesh needs to have properly defined:
102
+ /// - [`UVs`](Mesh::ATTRIBUTE_UV_0): Bevy needs to know how to map a texture onto the mesh
103
+ /// (also true for `ColorMaterial`).
104
+ /// - [`Normals`](Mesh::ATTRIBUTE_NORMAL): Bevy needs to know how light interacts with your mesh.
105
+ /// [0.0, 0.0, 1.0] is very common for simple flat meshes on the XY plane,
106
+ /// because simple meshes are smooth and they don't require complex light calculations.
107
+ /// - Vertex winding order: by default, `StandardMaterial.cull_mode` is [`Some(Face::Back)`](crate::render_resource::Face),
108
+ /// which means that Bevy would *only* render the "front" of each triangle, which
109
+ /// is the side of the triangle from where the vertices appear in a *counter-clockwise* order.
110
+ ///
111
+ // TODO: allow values to be unloaded after been submitting to the GPU to conserve memory
112
+ #[ derive( Debug , TypeUuid , TypePath , Clone ) ]
113
+ #[ uuid = "8ecbac0f-f545-4473-ad43-e1f4243af51e" ]
114
+ pub struct Mesh {
115
+ primitive_topology : PrimitiveTopology ,
116
+ /// `std::collections::BTreeMap` with all defined vertex attributes (Positions, Normals, ...)
117
+ /// for this mesh. Attribute ids to attribute values.
118
+ /// Uses a BTreeMap because, unlike HashMap, it has a defined iteration order,
119
+ /// which allows easy stable VertexBuffers (i.e. same buffer order)
120
+ attributes : BTreeMap < MeshVertexAttributeId , MeshAttributeData > ,
121
+ indices : Option < Indices > ,
122
+ morph_targets : Option < Handle < Image > > ,
123
+ morph_target_names : Option < Vec < String > > ,
124
+ }
125
+
99
126
impl Mesh {
100
- /// Where the vertex is located in space. Use in conjunction with [`Mesh::insert_attribute`]
127
+ /// Where the vertex is located in space. Use in conjunction with [`Mesh::insert_attribute`].
101
128
pub const ATTRIBUTE_POSITION : MeshVertexAttribute =
102
129
MeshVertexAttribute :: new ( "Vertex_Position" , 0 , VertexFormat :: Float32x3 ) ;
103
130
104
131
/// The direction the vertex normal is facing in.
105
- /// Use in conjunction with [`Mesh::insert_attribute`]
132
+ /// Use in conjunction with [`Mesh::insert_attribute`].
106
133
pub const ATTRIBUTE_NORMAL : MeshVertexAttribute =
107
134
MeshVertexAttribute :: new ( "Vertex_Normal" , 1 , VertexFormat :: Float32x3 ) ;
108
135
109
- /// Texture coordinates for the vertex. Use in conjunction with [`Mesh::insert_attribute`]
136
+ /// Texture coordinates for the vertex. Use in conjunction with [`Mesh::insert_attribute`].
137
+ ///
138
+ /// Values are generally between 0. and 1., with `StandardMaterial` and `ColorMaterial`
139
+ /// `[0.,0.]` is the top left of the texture, and [1.,1.] the bottom-right.
140
+ /// You usually want to only use values in that range, values outside will be
141
+ /// clamped per pixel not for the vertex, "stretching" the borders of the texture.
142
+ /// This behavior can be useful in some cases, usually when the borders have only
143
+ /// one color, for example a logo, and you want to "extend" those borders.
110
144
pub const ATTRIBUTE_UV_0 : MeshVertexAttribute =
111
145
MeshVertexAttribute :: new ( "Vertex_Uv" , 2 , VertexFormat :: Float32x2 ) ;
112
146
113
- /// The direction of the vertex tangent. Used for normal mapping
147
+ /// The direction of the vertex tangent. Used for normal mapping.
148
+ /// Usually generated with [`generate_tangents`](Mesh::generate_tangents).
114
149
pub const ATTRIBUTE_TANGENT : MeshVertexAttribute =
115
150
MeshVertexAttribute :: new ( "Vertex_Tangent" , 3 , VertexFormat :: Float32x4 ) ;
116
151
117
- /// Per vertex coloring. Use in conjunction with [`Mesh::insert_attribute`]
152
+ /// Per vertex coloring. Use in conjunction with [`Mesh::insert_attribute`].
118
153
pub const ATTRIBUTE_COLOR : MeshVertexAttribute =
119
154
MeshVertexAttribute :: new ( "Vertex_Color" , 4 , VertexFormat :: Float32x4 ) ;
120
155
121
- /// Per vertex joint transform matrix weight. Use in conjunction with [`Mesh::insert_attribute`]
156
+ /// Per vertex joint transform matrix weight. Use in conjunction with [`Mesh::insert_attribute`].
122
157
pub const ATTRIBUTE_JOINT_WEIGHT : MeshVertexAttribute =
123
158
MeshVertexAttribute :: new ( "Vertex_JointWeight" , 5 , VertexFormat :: Float32x4 ) ;
124
- /// Per vertex joint transform matrix index. Use in conjunction with [`Mesh::insert_attribute`]
159
+
160
+ /// Per vertex joint transform matrix index. Use in conjunction with [`Mesh::insert_attribute`].
125
161
pub const ATTRIBUTE_JOINT_INDEX : MeshVertexAttribute =
126
162
MeshVertexAttribute :: new ( "Vertex_JointIndex" , 6 , VertexFormat :: Uint16x4 ) ;
127
163
0 commit comments