pub struct Mesh {
pub asset_usage: RenderAssetUsages,
/* private fields */
}
Expand description
A 3D object made out of vertices representing triangles, lines, or points, with “attribute” values for each vertex.
Meshes can be automatically generated by a bevy AssetLoader
(generally by loading a Gltf
file),
or by converting a primitive using into
.
It is also possible to create one manually.
They can be edited after creation.
Meshes can be rendered with a Material
, like StandardMaterial
in PbrBundle
or ColorMaterial
in ColorMesh2dBundle
.
A Mesh
in Bevy is equivalent to a “primitive” in the glTF format, for a
glTF Mesh representation, see GltfMesh
.
§Manual creation
The following function will construct a flat mesh, to be rendered with a
StandardMaterial
or ColorMaterial
:
fn create_simple_parallelogram() -> Mesh {
// Create a new mesh using a triangle list topology, where each set of 3 vertices composes a triangle.
Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::default())
// Add 4 vertices, each with its own position attribute (coordinate in
// 3D space), for each of the corners of the parallelogram.
.with_inserted_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![[0.0, 0.0, 0.0], [1.0, 2.0, 0.0], [2.0, 2.0, 0.0], [1.0, 0.0, 0.0]]
)
// Assign a UV coordinate to each vertex.
.with_inserted_attribute(
Mesh::ATTRIBUTE_UV_0,
vec![[0.0, 1.0], [0.5, 0.0], [1.0, 0.0], [0.5, 1.0]]
)
// Assign normals (everything points outwards)
.with_inserted_attribute(
Mesh::ATTRIBUTE_NORMAL,
vec![[0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0]]
)
// After defining all the vertices and their attributes, build each triangle using the
// indices of the vertices that make it up in a counter-clockwise order.
.with_inserted_indices(Indices::U32(vec![
// First triangle
0, 3, 1,
// Second triangle
1, 3, 2
]))
}
You can see how it looks like here,
used in a PbrBundle
with a square bevy logo texture, with added axis, points,
lines and text for clarity.
§Other examples
For further visualization, explanation, and examples, see the built-in Bevy examples, and the implementation of the built-in shapes. In particular, generate_custom_mesh teaches you to access modify a Mesh’s attributes after creating it.
§Common points of confusion
- UV maps in Bevy start at the top-left, see
ATTRIBUTE_UV_0
, other APIs can have other conventions,OpenGL
starts at bottom-left. - It is possible and sometimes useful for multiple vertices to have the same position attribute value, it’s a common technique in 3D modelling for complex UV mapping or other calculations.
§Use with StandardMaterial
To render correctly with StandardMaterial
, a mesh needs to have properly defined:
UVs
: Bevy needs to know how to map a texture onto the mesh (also true forColorMaterial
).Normals
: Bevy needs to know how light interacts with your mesh. [0.0, 0.0, 1.0] is very common for simple flat meshes on the XY plane, because simple meshes are smooth and they don’t require complex light calculations.- Vertex winding order: by default,
StandardMaterial.cull_mode
isSome(Face::Back)
, which means that Bevy would only render the “front” of each triangle, which is the side of the triangle from where the vertices appear in a counter-clockwise order.
Fields§
§asset_usage: RenderAssetUsages
Implementations§
§impl Mesh
impl Mesh
pub const ATTRIBUTE_POSITION: MeshVertexAttribute = _
pub const ATTRIBUTE_POSITION: MeshVertexAttribute = _
Where the vertex is located in space. Use in conjunction with Mesh::insert_attribute
or Mesh::with_inserted_attribute
.
The format of this attribute is VertexFormat::Float32x3
.
pub const ATTRIBUTE_NORMAL: MeshVertexAttribute = _
pub const ATTRIBUTE_NORMAL: MeshVertexAttribute = _
The direction the vertex normal is facing in.
Use in conjunction with Mesh::insert_attribute
or Mesh::with_inserted_attribute
.
The format of this attribute is VertexFormat::Float32x3
.
pub const ATTRIBUTE_UV_0: MeshVertexAttribute = _
pub const ATTRIBUTE_UV_0: MeshVertexAttribute = _
Texture coordinates for the vertex. Use in conjunction with Mesh::insert_attribute
or Mesh::with_inserted_attribute
.
Generally [0.,0.]
is mapped to the top left of the texture, and [1.,1.]
to the bottom-right.
By default values outside will be clamped per pixel not for the vertex, “stretching” the borders of the texture. This behavior can be useful in some cases, usually when the borders have only one color, for example a logo, and you want to “extend” those borders.
For different mapping outside of 0..=1
range,
see ImageAddressMode
.
The format of this attribute is VertexFormat::Float32x2
.
pub const ATTRIBUTE_UV_1: MeshVertexAttribute = _
pub const ATTRIBUTE_UV_1: MeshVertexAttribute = _
Alternate texture coordinates for the vertex. Use in conjunction with
Mesh::insert_attribute
or Mesh::with_inserted_attribute
.
Typically, these are used for lightmaps, textures that provide precomputed illumination.
The format of this attribute is VertexFormat::Float32x2
.
pub const ATTRIBUTE_TANGENT: MeshVertexAttribute = _
pub const ATTRIBUTE_TANGENT: MeshVertexAttribute = _
The direction of the vertex tangent. Used for normal mapping.
Usually generated with generate_tangents
or
with_generated_tangents
.
The format of this attribute is VertexFormat::Float32x4
.
pub const ATTRIBUTE_COLOR: MeshVertexAttribute = _
pub const ATTRIBUTE_COLOR: MeshVertexAttribute = _
Per vertex coloring. Use in conjunction with Mesh::insert_attribute
or Mesh::with_inserted_attribute
.
The format of this attribute is VertexFormat::Float32x4
.
pub const ATTRIBUTE_JOINT_WEIGHT: MeshVertexAttribute = _
pub const ATTRIBUTE_JOINT_WEIGHT: MeshVertexAttribute = _
Per vertex joint transform matrix weight. Use in conjunction with Mesh::insert_attribute
or Mesh::with_inserted_attribute
.
The format of this attribute is VertexFormat::Float32x4
.
pub const ATTRIBUTE_JOINT_INDEX: MeshVertexAttribute = _
pub const ATTRIBUTE_JOINT_INDEX: MeshVertexAttribute = _
Per vertex joint transform matrix index. Use in conjunction with Mesh::insert_attribute
or Mesh::with_inserted_attribute
.
The format of this attribute is VertexFormat::Uint16x4
.
pub fn new(
primitive_topology: PrimitiveTopology,
asset_usage: RenderAssetUsages
) -> Mesh
pub fn new( primitive_topology: PrimitiveTopology, asset_usage: RenderAssetUsages ) -> Mesh
Construct a new mesh. You need to provide a PrimitiveTopology
so that the
renderer knows how to treat the vertex data. Most of the time this will be
PrimitiveTopology::TriangleList
.
Examples found in repository?
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
fn from(line: LineList) -> Self {
let vertices: Vec<_> = line.lines.into_iter().flat_map(|(a, b)| [a, b]).collect();
Mesh::new(
// This tells wgpu that the positions are list of lines
// where every pair is a start and end point
PrimitiveTopology::LineList,
RenderAssetUsages::RENDER_WORLD,
)
// Add the vertices positions as an attribute
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, vertices)
}
}
/// A list of points that will have a line drawn between each consecutive points
#[derive(Debug, Clone)]
struct LineStrip {
points: Vec<Vec3>,
}
impl From<LineStrip> for Mesh {
fn from(line: LineStrip) -> Self {
Mesh::new(
// This tells wgpu that the positions are a list of points
// where a line will be drawn between each consecutive point
PrimitiveTopology::LineStrip,
RenderAssetUsages::RENDER_WORLD,
)
// Add the point positions as an attribute
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, line.points)
}
More examples
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
fn star(
mut commands: Commands,
// We will add a new Mesh for the star being created
mut meshes: ResMut<Assets<Mesh>>,
) {
// Let's define the mesh for the object we want to draw: a nice star.
// We will specify here what kind of topology is used to define the mesh,
// that is, how triangles are built from the vertices. We will use a
// triangle list, meaning that each vertex of the triangle has to be
// specified. We set `RenderAssetUsages::RENDER_WORLD`, meaning this mesh
// will not be accessible in future frames from the `meshes` resource, in
// order to save on memory once it has been uploaded to the GPU.
let mut star = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::RENDER_WORLD,
);
// Vertices need to have a position attribute. We will use the following
// vertices (I hope you can spot the star in the schema).
//
// 1
//
// 10 2
// 9 0 3
// 8 4
// 6
// 7 5
//
// These vertices are specified in 3D space.
let mut v_pos = vec![[0.0, 0.0, 0.0]];
for i in 0..10 {
// The angle between each vertex is 1/10 of a full rotation.
let a = i as f32 * PI / 5.0;
// The radius of inner vertices (even indices) is 100. For outer vertices (odd indices) it's 200.
let r = (1 - i % 2) as f32 * 100.0 + 100.0;
// Add the vertex position.
v_pos.push([r * a.sin(), r * a.cos(), 0.0]);
}
// Set the position attribute
star.insert_attribute(Mesh::ATTRIBUTE_POSITION, v_pos);
// And a RGB color attribute as well
let mut v_color: Vec<u32> = vec![LinearRgba::BLACK.as_u32()];
v_color.extend_from_slice(&[LinearRgba::from(YELLOW).as_u32(); 10]);
star.insert_attribute(
MeshVertexAttribute::new("Vertex_Color", 1, VertexFormat::Uint32),
v_color,
);
// Now, we specify the indices of the vertex that are going to compose the
// triangles in our star. Vertices in triangles have to be specified in CCW
// winding (that will be the front face, colored). Since we are using
// triangle list, we will specify each triangle as 3 vertices
// First triangle: 0, 2, 1
// Second triangle: 0, 3, 2
// Third triangle: 0, 4, 3
// etc
// Last triangle: 0, 1, 10
let mut indices = vec![0, 1, 10];
for i in 2..=10 {
indices.extend_from_slice(&[0, i, i - 1]);
}
star.insert_indices(Indices::U32(indices));
// We can now spawn the entities for the star and the camera
commands.spawn((
// We use a marker component to identify the custom colored meshes
ColoredMesh2d,
// The `Handle<Mesh>` needs to be wrapped in a `Mesh2dHandle` to use 2d rendering instead of 3d
Mesh2dHandle(meshes.add(star)),
// This bundle's components are needed for something to be rendered
SpatialBundle::INHERITED_IDENTITY,
));
// Spawn the camera
commands.spawn(Camera2dBundle::default());
}
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut skinned_mesh_inverse_bindposes_assets: ResMut<Assets<SkinnedMeshInverseBindposes>>,
) {
// Create a camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
// Create inverse bindpose matrices for a skeleton consists of 2 joints
let inverse_bindposes = skinned_mesh_inverse_bindposes_assets.add(vec![
Mat4::from_translation(Vec3::new(-0.5, -1.0, 0.0)),
Mat4::from_translation(Vec3::new(-0.5, -1.0, 0.0)),
]);
// Create a mesh
let mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::RENDER_WORLD,
)
// Set mesh vertex positions
.with_inserted_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 0.5, 0.0],
[1.0, 0.5, 0.0],
[0.0, 1.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.5, 0.0],
[1.0, 1.5, 0.0],
[0.0, 2.0, 0.0],
[1.0, 2.0, 0.0],
],
)
// Set mesh vertex normals
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, vec![[0.0, 0.0, 1.0]; 10])
// Set mesh vertex joint indices for mesh skinning.
// Each vertex gets 4 indices used to address the `JointTransforms` array in the vertex shader
// as well as `SkinnedMeshJoint` array in the `SkinnedMesh` component.
// This means that a maximum of 4 joints can affect a single vertex.
.with_inserted_attribute(
Mesh::ATTRIBUTE_JOINT_INDEX,
// Need to be explicit here as [u16; 4] could be either Uint16x4 or Unorm16x4.
VertexAttributeValues::Uint16x4(vec![
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
]),
)
// Set mesh vertex joint weights for mesh skinning.
// Each vertex gets 4 joint weights corresponding to the 4 joint indices assigned to it.
// The sum of these weights should equal to 1.
.with_inserted_attribute(
Mesh::ATTRIBUTE_JOINT_WEIGHT,
vec![
[1.00, 0.00, 0.0, 0.0],
[1.00, 0.00, 0.0, 0.0],
[0.75, 0.25, 0.0, 0.0],
[0.75, 0.25, 0.0, 0.0],
[0.50, 0.50, 0.0, 0.0],
[0.50, 0.50, 0.0, 0.0],
[0.25, 0.75, 0.0, 0.0],
[0.25, 0.75, 0.0, 0.0],
[0.00, 1.00, 0.0, 0.0],
[0.00, 1.00, 0.0, 0.0],
],
)
// Tell bevy to construct triangles from a list of vertex indices,
// where each 3 vertex indices form an triangle.
.with_inserted_indices(Indices::U16(vec![
0, 1, 3, 0, 3, 2, 2, 3, 5, 2, 5, 4, 4, 5, 7, 4, 7, 6, 6, 7, 9, 6, 9, 8,
]));
let mesh = meshes.add(mesh);
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut rng = ChaCha8Rng::seed_from_u64(42);
for i in -5..5 {
// Create joint entities
let joint_0 = commands
.spawn(TransformBundle::from(Transform::from_xyz(
i as f32 * 1.5,
0.0,
i as f32 * 0.1,
)))
.id();
let joint_1 = commands
.spawn((AnimatedJoint, TransformBundle::IDENTITY))
.id();
// Set joint_1 as a child of joint_0.
commands.entity(joint_0).push_children(&[joint_1]);
// Each joint in this vector corresponds to each inverse bindpose matrix in `SkinnedMeshInverseBindposes`.
let joint_entities = vec![joint_0, joint_1];
// Create skinned mesh renderer. Note that its transform doesn't affect the position of the mesh.
commands.spawn((
PbrBundle {
mesh: mesh.clone(),
material: materials.add(Color::srgb(
rng.gen_range(0.0..1.0),
rng.gen_range(0.0..1.0),
rng.gen_range(0.0..1.0),
)),
..default()
},
SkinnedMesh {
inverse_bindposes: inverse_bindposes.clone(),
joints: joint_entities,
},
));
}
}
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
fn create_cube_mesh() -> Mesh {
// Keep the mesh data accessible in future frames to be able to mutate it in toggle_texture.
Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD)
.with_inserted_attribute(
Mesh::ATTRIBUTE_POSITION,
// Each array is an [x, y, z] coordinate in local space.
// The camera coordinate space is right-handed x-right, y-up, z-back. This means "forward" is -Z.
// Meshes always rotate around their local [0, 0, 0] when a rotation is applied to their Transform.
// By centering our mesh around the origin, rotating the mesh preserves its center of mass.
vec![
// top (facing towards +y)
[-0.5, 0.5, -0.5], // vertex with index 0
[0.5, 0.5, -0.5], // vertex with index 1
[0.5, 0.5, 0.5], // etc. until 23
[-0.5, 0.5, 0.5],
// bottom (-y)
[-0.5, -0.5, -0.5],
[0.5, -0.5, -0.5],
[0.5, -0.5, 0.5],
[-0.5, -0.5, 0.5],
// right (+x)
[0.5, -0.5, -0.5],
[0.5, -0.5, 0.5],
[0.5, 0.5, 0.5], // This vertex is at the same position as vertex with index 2, but they'll have different UV and normal
[0.5, 0.5, -0.5],
// left (-x)
[-0.5, -0.5, -0.5],
[-0.5, -0.5, 0.5],
[-0.5, 0.5, 0.5],
[-0.5, 0.5, -0.5],
// back (+z)
[-0.5, -0.5, 0.5],
[-0.5, 0.5, 0.5],
[0.5, 0.5, 0.5],
[0.5, -0.5, 0.5],
// forward (-z)
[-0.5, -0.5, -0.5],
[-0.5, 0.5, -0.5],
[0.5, 0.5, -0.5],
[0.5, -0.5, -0.5],
],
)
// Set-up UV coordinates to point to the upper (V < 0.5), "dirt+grass" part of the texture.
// Take a look at the custom image (assets/textures/array_texture.png)
// so the UV coords will make more sense
// Note: (0.0, 0.0) = Top-Left in UV mapping, (1.0, 1.0) = Bottom-Right in UV mapping
.with_inserted_attribute(
Mesh::ATTRIBUTE_UV_0,
vec![
// Assigning the UV coords for the top side.
[0.0, 0.2], [0.0, 0.0], [1.0, 0.0], [1.0, 0.2],
// Assigning the UV coords for the bottom side.
[0.0, 0.45], [0.0, 0.25], [1.0, 0.25], [1.0, 0.45],
// Assigning the UV coords for the right side.
[1.0, 0.45], [0.0, 0.45], [0.0, 0.2], [1.0, 0.2],
// Assigning the UV coords for the left side.
[1.0, 0.45], [0.0, 0.45], [0.0, 0.2], [1.0, 0.2],
// Assigning the UV coords for the back side.
[0.0, 0.45], [0.0, 0.2], [1.0, 0.2], [1.0, 0.45],
// Assigning the UV coords for the forward side.
[0.0, 0.45], [0.0, 0.2], [1.0, 0.2], [1.0, 0.45],
],
)
// For meshes with flat shading, normals are orthogonal (pointing out) from the direction of
// the surface.
// Normals are required for correct lighting calculations.
// Each array represents a normalized vector, which length should be equal to 1.0.
.with_inserted_attribute(
Mesh::ATTRIBUTE_NORMAL,
vec![
// Normals for the top side (towards +y)
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
// Normals for the bottom side (towards -y)
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
// Normals for the right side (towards +x)
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
// Normals for the left side (towards -x)
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
// Normals for the back side (towards +z)
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
// Normals for the forward side (towards -z)
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
],
)
// Create the triangles out of the 24 vertices we created.
// To construct a square, we need 2 triangles, therefore 12 triangles in total.
// To construct a triangle, we need the indices of its 3 defined vertices, adding them one
// by one, in a counter-clockwise order (relative to the position of the viewer, the order
// should appear counter-clockwise from the front of the triangle, in this case from outside the cube).
// Read more about how to correctly build a mesh manually in the Bevy documentation of a Mesh,
// further examples and the implementation of the built-in shapes.
.with_inserted_indices(Indices::U32(vec![
0,3,1 , 1,3,2, // triangles making up the top (+y) facing side.
4,5,7 , 5,6,7, // bottom (-y)
8,11,9 , 9,11,10, // right (+x)
12,13,15 , 13,14,15, // left (-x)
16,19,17 , 17,19,18, // back (+z)
20,21,23 , 21,22,23, // forward (-z)
]))
}
pub fn primitive_topology(&self) -> PrimitiveTopology
pub fn primitive_topology(&self) -> PrimitiveTopology
Returns the topology of the mesh.
Examples found in repository?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
meshes: Res<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// By default AssetServer will load assets from inside the "assets" folder.
// For example, the next line will load "ROOT/assets/models/cube/cube.gltf#Mesh0/Primitive0",
// where "ROOT" is the directory of the Application.
//
// This can be overridden by setting the "CARGO_MANIFEST_DIR" environment variable (see
// https://doc.rust-lang.org/cargo/reference/environment-variables.html)
// to another directory. When the Application is run through Cargo, "CARGO_MANIFEST_DIR" is
// automatically set to your crate (workspace) root directory.
let cube_handle = asset_server.load("models/cube/cube.gltf#Mesh0/Primitive0");
let sphere_handle = asset_server.load("models/sphere/sphere.gltf#Mesh0/Primitive0");
// All assets end up in their Assets<T> collection once they are done loading:
if let Some(sphere) = meshes.get(&sphere_handle) {
// You might notice that this doesn't run! This is because assets load in parallel without
// blocking. When an asset has loaded, it will appear in relevant Assets<T>
// collection.
info!("{:?}", sphere.primitive_topology());
} else {
info!("sphere hasn't loaded yet");
}
// You can load all assets in a folder like this. They will be loaded in parallel without
// blocking. The LoadedFolder asset holds handles to each asset in the folder. These are all
// dependencies of the LoadedFolder asset, meaning you can wait for the LoadedFolder asset to
// fire AssetEvent::LoadedWithDependencies if you want to wait for all assets in the folder
// to load.
// If you want to keep the assets in the folder alive, make sure you store the returned handle
// somewhere.
let _loaded_folder: Handle<LoadedFolder> = asset_server.load_folder("models/torus");
// If you want a handle to a specific asset in a loaded folder, the easiest way to get one is to call load.
// It will _not_ be loaded a second time.
// The LoadedFolder asset will ultimately also hold handles to the assets, but waiting for it to load
// and finding the right handle is more work!
let torus_handle = asset_server.load("models/torus/torus.gltf#Mesh0/Primitive0");
// You can also add assets directly to their Assets<T> storage:
let material_handle = materials.add(StandardMaterial {
base_color: Color::srgb(0.8, 0.7, 0.6),
..default()
});
// torus
commands.spawn(PbrBundle {
mesh: torus_handle,
material: material_handle.clone(),
transform: Transform::from_xyz(-3.0, 0.0, 0.0),
..default()
});
// cube
commands.spawn(PbrBundle {
mesh: cube_handle,
material: material_handle.clone(),
transform: Transform::from_xyz(0.0, 0.0, 0.0),
..default()
});
// sphere
commands.spawn(PbrBundle {
mesh: sphere_handle,
material: material_handle,
transform: Transform::from_xyz(3.0, 0.0, 0.0),
..default()
});
// light
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(4.0, 5.0, 4.0),
..default()
});
// camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0.0, 3.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}
pub fn insert_attribute(
&mut self,
attribute: MeshVertexAttribute,
values: impl Into<VertexAttributeValues>
)
pub fn insert_attribute( &mut self, attribute: MeshVertexAttribute, values: impl Into<VertexAttributeValues> )
Sets the data for a vertex attribute (position, normal, etc.). The name will
often be one of the associated constants such as Mesh::ATTRIBUTE_POSITION
.
§Panics
Panics when the format of the values does not match the attribute’s format.
Examples found in repository?
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
asset_server: Res<AssetServer>,
) {
// Load the Bevy logo as a texture
let texture_handle = asset_server.load("branding/banner.png");
// Build a default quad mesh
let mut mesh = Mesh::from(Rectangle::default());
// Build vertex colors for the quad. One entry per vertex (the corners of the quad)
let vertex_colors: Vec<[f32; 4]> = vec![
LinearRgba::RED.to_f32_array(),
LinearRgba::GREEN.to_f32_array(),
LinearRgba::BLUE.to_f32_array(),
LinearRgba::WHITE.to_f32_array(),
];
// Insert the vertex colors as an attribute
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, vertex_colors);
let mesh_handle: Mesh2dHandle = meshes.add(mesh).into();
// Spawn camera
commands.spawn(Camera2dBundle::default());
// Spawn the quad with vertex colors
commands.spawn(MaterialMesh2dBundle {
mesh: mesh_handle.clone(),
transform: Transform::from_translation(Vec3::new(-96., 0., 0.))
.with_scale(Vec3::splat(128.)),
material: materials.add(ColorMaterial::default()),
..default()
});
// Spawning the quad with vertex colors and a texture results in tinting
commands.spawn(MaterialMesh2dBundle {
mesh: mesh_handle,
transform: Transform::from_translation(Vec3::new(96., 0., 0.))
.with_scale(Vec3::splat(128.)),
material: materials.add(texture_handle),
..default()
});
}
More examples
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(5.0, 5.0)),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
..default()
});
// cube
// Assign vertex colors based on vertex positions
let mut colorful_cube = Mesh::from(Cuboid::default());
if let Some(VertexAttributeValues::Float32x3(positions)) =
colorful_cube.attribute(Mesh::ATTRIBUTE_POSITION)
{
let colors: Vec<[f32; 4]> = positions
.iter()
.map(|[r, g, b]| [(1. - *r) / 2., (1. - *g) / 2., (1. - *b) / 2., 1.])
.collect();
colorful_cube.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
}
commands.spawn(PbrBundle {
mesh: meshes.add(colorful_cube),
// This is the default color, but note that vertex colors are
// multiplied by the base color, so you'll likely want this to be
// white if using vertex colors.
material: materials.add(Color::srgb(1., 1., 1.)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
});
// Light
commands.spawn(PointLightBundle {
point_light: PointLight {
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 5.0, 4.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
// Camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
fn setup_scene(
asset_server: Res<AssetServer>,
mut images: ResMut<Assets<Image>>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 300.0,
});
commands.insert_resource(CameraMode::Chase);
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: 3_000.0,
shadows_enabled: true,
..default()
},
transform: Transform::default().looking_to(Vec3::new(-1.0, -0.7, -1.0), Vec3::X),
..default()
});
// Sky
commands.spawn(PbrBundle {
mesh: meshes.add(Sphere::default()),
material: materials.add(StandardMaterial {
unlit: true,
base_color: Color::linear_rgb(0.1, 0.6, 1.0),
..default()
}),
transform: Transform::default().with_scale(Vec3::splat(-4000.0)),
..default()
});
// Ground
let mut plane: Mesh = Plane3d::default().into();
let uv_size = 4000.0;
let uvs = vec![[uv_size, 0.0], [0.0, 0.0], [0.0, uv_size], [uv_size; 2]];
plane.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
commands.spawn(PbrBundle {
mesh: meshes.add(plane),
material: materials.add(StandardMaterial {
base_color: Color::WHITE,
perceptual_roughness: 1.0,
base_color_texture: Some(images.add(uv_debug_texture())),
..default()
}),
transform: Transform::from_xyz(0.0, -0.65, 0.0).with_scale(Vec3::splat(80.)),
..default()
});
spawn_cars(&asset_server, &mut meshes, &mut materials, &mut commands);
spawn_trees(&mut meshes, &mut materials, &mut commands);
spawn_barriers(&mut meshes, &mut materials, &mut commands);
}
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
fn star(
mut commands: Commands,
// We will add a new Mesh for the star being created
mut meshes: ResMut<Assets<Mesh>>,
) {
// Let's define the mesh for the object we want to draw: a nice star.
// We will specify here what kind of topology is used to define the mesh,
// that is, how triangles are built from the vertices. We will use a
// triangle list, meaning that each vertex of the triangle has to be
// specified. We set `RenderAssetUsages::RENDER_WORLD`, meaning this mesh
// will not be accessible in future frames from the `meshes` resource, in
// order to save on memory once it has been uploaded to the GPU.
let mut star = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::RENDER_WORLD,
);
// Vertices need to have a position attribute. We will use the following
// vertices (I hope you can spot the star in the schema).
//
// 1
//
// 10 2
// 9 0 3
// 8 4
// 6
// 7 5
//
// These vertices are specified in 3D space.
let mut v_pos = vec![[0.0, 0.0, 0.0]];
for i in 0..10 {
// The angle between each vertex is 1/10 of a full rotation.
let a = i as f32 * PI / 5.0;
// The radius of inner vertices (even indices) is 100. For outer vertices (odd indices) it's 200.
let r = (1 - i % 2) as f32 * 100.0 + 100.0;
// Add the vertex position.
v_pos.push([r * a.sin(), r * a.cos(), 0.0]);
}
// Set the position attribute
star.insert_attribute(Mesh::ATTRIBUTE_POSITION, v_pos);
// And a RGB color attribute as well
let mut v_color: Vec<u32> = vec![LinearRgba::BLACK.as_u32()];
v_color.extend_from_slice(&[LinearRgba::from(YELLOW).as_u32(); 10]);
star.insert_attribute(
MeshVertexAttribute::new("Vertex_Color", 1, VertexFormat::Uint32),
v_color,
);
// Now, we specify the indices of the vertex that are going to compose the
// triangles in our star. Vertices in triangles have to be specified in CCW
// winding (that will be the front face, colored). Since we are using
// triangle list, we will specify each triangle as 3 vertices
// First triangle: 0, 2, 1
// Second triangle: 0, 3, 2
// Third triangle: 0, 4, 3
// etc
// Last triangle: 0, 1, 10
let mut indices = vec![0, 1, 10];
for i in 2..=10 {
indices.extend_from_slice(&[0, i, i - 1]);
}
star.insert_indices(Indices::U32(indices));
// We can now spawn the entities for the star and the camera
commands.spawn((
// We use a marker component to identify the custom colored meshes
ColoredMesh2d,
// The `Handle<Mesh>` needs to be wrapped in a `Mesh2dHandle` to use 2d rendering instead of 3d
Mesh2dHandle(meshes.add(star)),
// This bundle's components are needed for something to be rendered
SpatialBundle::INHERITED_IDENTITY,
));
// Spawn the camera
commands.spawn(Camera2dBundle::default());
}
pub fn with_inserted_attribute(
self,
attribute: MeshVertexAttribute,
values: impl Into<VertexAttributeValues>
) -> Mesh
pub fn with_inserted_attribute( self, attribute: MeshVertexAttribute, values: impl Into<VertexAttributeValues> ) -> Mesh
Consumes the mesh and returns a mesh with data set for a vertex attribute (position, normal, etc.).
The name will often be one of the associated constants such as Mesh::ATTRIBUTE_POSITION
.
(Alternatively, you can use Mesh::insert_attribute
to mutate an existing mesh in-place)
§Panics
Panics when the format of the values does not match the attribute’s format.
Examples found in repository?
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
fn from(line: LineList) -> Self {
let vertices: Vec<_> = line.lines.into_iter().flat_map(|(a, b)| [a, b]).collect();
Mesh::new(
// This tells wgpu that the positions are list of lines
// where every pair is a start and end point
PrimitiveTopology::LineList,
RenderAssetUsages::RENDER_WORLD,
)
// Add the vertices positions as an attribute
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, vertices)
}
}
/// A list of points that will have a line drawn between each consecutive points
#[derive(Debug, Clone)]
struct LineStrip {
points: Vec<Vec3>,
}
impl From<LineStrip> for Mesh {
fn from(line: LineStrip) -> Self {
Mesh::new(
// This tells wgpu that the positions are a list of points
// where a line will be drawn between each consecutive point
PrimitiveTopology::LineStrip,
RenderAssetUsages::RENDER_WORLD,
)
// Add the point positions as an attribute
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, line.points)
}
More examples
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<CustomMaterial>>,
) {
let mesh = Mesh::from(Cuboid::default())
// Sets the custom attribute
.with_inserted_attribute(
ATTRIBUTE_BLEND_COLOR,
// The cube mesh has 24 vertices (6 faces, 4 vertices per face), so we insert one BlendColor for each
vec![[1.0, 0.0, 0.0, 1.0]; 24],
);
// cube
commands.spawn(MaterialMeshBundle {
mesh: meshes.add(mesh),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
material: materials.add(CustomMaterial {
color: LinearRgba::WHITE,
}),
..default()
});
// camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut skinned_mesh_inverse_bindposes_assets: ResMut<Assets<SkinnedMeshInverseBindposes>>,
) {
// Create a camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
// Create inverse bindpose matrices for a skeleton consists of 2 joints
let inverse_bindposes = skinned_mesh_inverse_bindposes_assets.add(vec![
Mat4::from_translation(Vec3::new(-0.5, -1.0, 0.0)),
Mat4::from_translation(Vec3::new(-0.5, -1.0, 0.0)),
]);
// Create a mesh
let mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::RENDER_WORLD,
)
// Set mesh vertex positions
.with_inserted_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 0.5, 0.0],
[1.0, 0.5, 0.0],
[0.0, 1.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.5, 0.0],
[1.0, 1.5, 0.0],
[0.0, 2.0, 0.0],
[1.0, 2.0, 0.0],
],
)
// Set mesh vertex normals
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, vec![[0.0, 0.0, 1.0]; 10])
// Set mesh vertex joint indices for mesh skinning.
// Each vertex gets 4 indices used to address the `JointTransforms` array in the vertex shader
// as well as `SkinnedMeshJoint` array in the `SkinnedMesh` component.
// This means that a maximum of 4 joints can affect a single vertex.
.with_inserted_attribute(
Mesh::ATTRIBUTE_JOINT_INDEX,
// Need to be explicit here as [u16; 4] could be either Uint16x4 or Unorm16x4.
VertexAttributeValues::Uint16x4(vec![
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
]),
)
// Set mesh vertex joint weights for mesh skinning.
// Each vertex gets 4 joint weights corresponding to the 4 joint indices assigned to it.
// The sum of these weights should equal to 1.
.with_inserted_attribute(
Mesh::ATTRIBUTE_JOINT_WEIGHT,
vec![
[1.00, 0.00, 0.0, 0.0],
[1.00, 0.00, 0.0, 0.0],
[0.75, 0.25, 0.0, 0.0],
[0.75, 0.25, 0.0, 0.0],
[0.50, 0.50, 0.0, 0.0],
[0.50, 0.50, 0.0, 0.0],
[0.25, 0.75, 0.0, 0.0],
[0.25, 0.75, 0.0, 0.0],
[0.00, 1.00, 0.0, 0.0],
[0.00, 1.00, 0.0, 0.0],
],
)
// Tell bevy to construct triangles from a list of vertex indices,
// where each 3 vertex indices form an triangle.
.with_inserted_indices(Indices::U16(vec![
0, 1, 3, 0, 3, 2, 2, 3, 5, 2, 5, 4, 4, 5, 7, 4, 7, 6, 6, 7, 9, 6, 9, 8,
]));
let mesh = meshes.add(mesh);
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut rng = ChaCha8Rng::seed_from_u64(42);
for i in -5..5 {
// Create joint entities
let joint_0 = commands
.spawn(TransformBundle::from(Transform::from_xyz(
i as f32 * 1.5,
0.0,
i as f32 * 0.1,
)))
.id();
let joint_1 = commands
.spawn((AnimatedJoint, TransformBundle::IDENTITY))
.id();
// Set joint_1 as a child of joint_0.
commands.entity(joint_0).push_children(&[joint_1]);
// Each joint in this vector corresponds to each inverse bindpose matrix in `SkinnedMeshInverseBindposes`.
let joint_entities = vec![joint_0, joint_1];
// Create skinned mesh renderer. Note that its transform doesn't affect the position of the mesh.
commands.spawn((
PbrBundle {
mesh: mesh.clone(),
material: materials.add(Color::srgb(
rng.gen_range(0.0..1.0),
rng.gen_range(0.0..1.0),
rng.gen_range(0.0..1.0),
)),
..default()
},
SkinnedMesh {
inverse_bindposes: inverse_bindposes.clone(),
joints: joint_entities,
},
));
}
}
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
fn create_cube_mesh() -> Mesh {
// Keep the mesh data accessible in future frames to be able to mutate it in toggle_texture.
Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD)
.with_inserted_attribute(
Mesh::ATTRIBUTE_POSITION,
// Each array is an [x, y, z] coordinate in local space.
// The camera coordinate space is right-handed x-right, y-up, z-back. This means "forward" is -Z.
// Meshes always rotate around their local [0, 0, 0] when a rotation is applied to their Transform.
// By centering our mesh around the origin, rotating the mesh preserves its center of mass.
vec![
// top (facing towards +y)
[-0.5, 0.5, -0.5], // vertex with index 0
[0.5, 0.5, -0.5], // vertex with index 1
[0.5, 0.5, 0.5], // etc. until 23
[-0.5, 0.5, 0.5],
// bottom (-y)
[-0.5, -0.5, -0.5],
[0.5, -0.5, -0.5],
[0.5, -0.5, 0.5],
[-0.5, -0.5, 0.5],
// right (+x)
[0.5, -0.5, -0.5],
[0.5, -0.5, 0.5],
[0.5, 0.5, 0.5], // This vertex is at the same position as vertex with index 2, but they'll have different UV and normal
[0.5, 0.5, -0.5],
// left (-x)
[-0.5, -0.5, -0.5],
[-0.5, -0.5, 0.5],
[-0.5, 0.5, 0.5],
[-0.5, 0.5, -0.5],
// back (+z)
[-0.5, -0.5, 0.5],
[-0.5, 0.5, 0.5],
[0.5, 0.5, 0.5],
[0.5, -0.5, 0.5],
// forward (-z)
[-0.5, -0.5, -0.5],
[-0.5, 0.5, -0.5],
[0.5, 0.5, -0.5],
[0.5, -0.5, -0.5],
],
)
// Set-up UV coordinates to point to the upper (V < 0.5), "dirt+grass" part of the texture.
// Take a look at the custom image (assets/textures/array_texture.png)
// so the UV coords will make more sense
// Note: (0.0, 0.0) = Top-Left in UV mapping, (1.0, 1.0) = Bottom-Right in UV mapping
.with_inserted_attribute(
Mesh::ATTRIBUTE_UV_0,
vec![
// Assigning the UV coords for the top side.
[0.0, 0.2], [0.0, 0.0], [1.0, 0.0], [1.0, 0.2],
// Assigning the UV coords for the bottom side.
[0.0, 0.45], [0.0, 0.25], [1.0, 0.25], [1.0, 0.45],
// Assigning the UV coords for the right side.
[1.0, 0.45], [0.0, 0.45], [0.0, 0.2], [1.0, 0.2],
// Assigning the UV coords for the left side.
[1.0, 0.45], [0.0, 0.45], [0.0, 0.2], [1.0, 0.2],
// Assigning the UV coords for the back side.
[0.0, 0.45], [0.0, 0.2], [1.0, 0.2], [1.0, 0.45],
// Assigning the UV coords for the forward side.
[0.0, 0.45], [0.0, 0.2], [1.0, 0.2], [1.0, 0.45],
],
)
// For meshes with flat shading, normals are orthogonal (pointing out) from the direction of
// the surface.
// Normals are required for correct lighting calculations.
// Each array represents a normalized vector, which length should be equal to 1.0.
.with_inserted_attribute(
Mesh::ATTRIBUTE_NORMAL,
vec![
// Normals for the top side (towards +y)
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
// Normals for the bottom side (towards -y)
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
// Normals for the right side (towards +x)
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
// Normals for the left side (towards -x)
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
// Normals for the back side (towards +z)
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
// Normals for the forward side (towards -z)
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
],
)
// Create the triangles out of the 24 vertices we created.
// To construct a square, we need 2 triangles, therefore 12 triangles in total.
// To construct a triangle, we need the indices of its 3 defined vertices, adding them one
// by one, in a counter-clockwise order (relative to the position of the viewer, the order
// should appear counter-clockwise from the front of the triangle, in this case from outside the cube).
// Read more about how to correctly build a mesh manually in the Bevy documentation of a Mesh,
// further examples and the implementation of the built-in shapes.
.with_inserted_indices(Indices::U32(vec![
0,3,1 , 1,3,2, // triangles making up the top (+y) facing side.
4,5,7 , 5,6,7, // bottom (-y)
8,11,9 , 9,11,10, // right (+x)
12,13,15 , 13,14,15, // left (-x)
16,19,17 , 17,19,18, // back (+z)
20,21,23 , 21,22,23, // forward (-z)
]))
}
pub fn remove_attribute(
&mut self,
attribute: impl Into<MeshVertexAttributeId>
) -> Option<VertexAttributeValues>
pub fn remove_attribute( &mut self, attribute: impl Into<MeshVertexAttributeId> ) -> Option<VertexAttributeValues>
Removes the data for a vertex attribute
pub fn with_removed_attribute(
self,
attribute: impl Into<MeshVertexAttributeId>
) -> Mesh
pub fn with_removed_attribute( self, attribute: impl Into<MeshVertexAttributeId> ) -> Mesh
Consumes the mesh and returns a mesh without the data for a vertex attribute
(Alternatively, you can use Mesh::remove_attribute
to mutate an existing mesh in-place)
pub fn contains_attribute(&self, id: impl Into<MeshVertexAttributeId>) -> bool
pub fn attribute(
&self,
id: impl Into<MeshVertexAttributeId>
) -> Option<&VertexAttributeValues>
pub fn attribute( &self, id: impl Into<MeshVertexAttributeId> ) -> Option<&VertexAttributeValues>
Retrieves the data currently set to the vertex attribute with the specified name
.
Examples found in repository?
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(5.0, 5.0)),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
..default()
});
// cube
// Assign vertex colors based on vertex positions
let mut colorful_cube = Mesh::from(Cuboid::default());
if let Some(VertexAttributeValues::Float32x3(positions)) =
colorful_cube.attribute(Mesh::ATTRIBUTE_POSITION)
{
let colors: Vec<[f32; 4]> = positions
.iter()
.map(|[r, g, b]| [(1. - *r) / 2., (1. - *g) / 2., (1. - *b) / 2., 1.])
.collect();
colorful_cube.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
}
commands.spawn(PbrBundle {
mesh: meshes.add(colorful_cube),
// This is the default color, but note that vertex colors are
// multiplied by the base color, so you'll likely want this to be
// white if using vertex colors.
material: materials.add(Color::srgb(1., 1., 1.)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
});
// Light
commands.spawn(PointLightBundle {
point_light: PointLight {
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 5.0, 4.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
// Camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}
pub fn attribute_mut(
&mut self,
id: impl Into<MeshVertexAttributeId>
) -> Option<&mut VertexAttributeValues>
pub fn attribute_mut( &mut self, id: impl Into<MeshVertexAttributeId> ) -> Option<&mut VertexAttributeValues>
Retrieves the data currently set to the vertex attribute with the specified name
mutably.
Examples found in repository?
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
fn toggle_texture(mesh_to_change: &mut Mesh) {
// Get a mutable reference to the values of the UV attribute, so we can iterate over it.
let uv_attribute = mesh_to_change.attribute_mut(Mesh::ATTRIBUTE_UV_0).unwrap();
// The format of the UV coordinates should be Float32x2.
let VertexAttributeValues::Float32x2(uv_attribute) = uv_attribute else {
panic!("Unexpected vertex format, expected Float32x2.");
};
// Iterate over the UV coordinates, and change them as we want.
for uv_coord in uv_attribute.iter_mut() {
// If the UV coordinate points to the upper, "dirt+grass" part of the texture...
if (uv_coord[1] + 0.5) < 1.0 {
// ... point to the equivalent lower, "sand+water" part instead,
uv_coord[1] += 0.5;
} else {
// else, point back to the upper, "dirt+grass" part.
uv_coord[1] -= 0.5;
}
}
}
pub fn attributes(
&self
) -> impl Iterator<Item = (MeshVertexAttributeId, &VertexAttributeValues)>
pub fn attributes( &self ) -> impl Iterator<Item = (MeshVertexAttributeId, &VertexAttributeValues)>
Returns an iterator that yields references to the data of each vertex attribute.
pub fn attributes_mut(
&mut self
) -> impl Iterator<Item = (MeshVertexAttributeId, &mut VertexAttributeValues)>
pub fn attributes_mut( &mut self ) -> impl Iterator<Item = (MeshVertexAttributeId, &mut VertexAttributeValues)>
Returns an iterator that yields mutable references to the data of each vertex attribute.
pub fn insert_indices(&mut self, indices: Indices)
pub fn insert_indices(&mut self, indices: Indices)
Sets the vertex indices of the mesh. They describe how triangles are constructed out of the
vertex attributes and are therefore only useful for the PrimitiveTopology
variants
that use triangles.
Examples found in repository?
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
fn star(
mut commands: Commands,
// We will add a new Mesh for the star being created
mut meshes: ResMut<Assets<Mesh>>,
) {
// Let's define the mesh for the object we want to draw: a nice star.
// We will specify here what kind of topology is used to define the mesh,
// that is, how triangles are built from the vertices. We will use a
// triangle list, meaning that each vertex of the triangle has to be
// specified. We set `RenderAssetUsages::RENDER_WORLD`, meaning this mesh
// will not be accessible in future frames from the `meshes` resource, in
// order to save on memory once it has been uploaded to the GPU.
let mut star = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::RENDER_WORLD,
);
// Vertices need to have a position attribute. We will use the following
// vertices (I hope you can spot the star in the schema).
//
// 1
//
// 10 2
// 9 0 3
// 8 4
// 6
// 7 5
//
// These vertices are specified in 3D space.
let mut v_pos = vec![[0.0, 0.0, 0.0]];
for i in 0..10 {
// The angle between each vertex is 1/10 of a full rotation.
let a = i as f32 * PI / 5.0;
// The radius of inner vertices (even indices) is 100. For outer vertices (odd indices) it's 200.
let r = (1 - i % 2) as f32 * 100.0 + 100.0;
// Add the vertex position.
v_pos.push([r * a.sin(), r * a.cos(), 0.0]);
}
// Set the position attribute
star.insert_attribute(Mesh::ATTRIBUTE_POSITION, v_pos);
// And a RGB color attribute as well
let mut v_color: Vec<u32> = vec![LinearRgba::BLACK.as_u32()];
v_color.extend_from_slice(&[LinearRgba::from(YELLOW).as_u32(); 10]);
star.insert_attribute(
MeshVertexAttribute::new("Vertex_Color", 1, VertexFormat::Uint32),
v_color,
);
// Now, we specify the indices of the vertex that are going to compose the
// triangles in our star. Vertices in triangles have to be specified in CCW
// winding (that will be the front face, colored). Since we are using
// triangle list, we will specify each triangle as 3 vertices
// First triangle: 0, 2, 1
// Second triangle: 0, 3, 2
// Third triangle: 0, 4, 3
// etc
// Last triangle: 0, 1, 10
let mut indices = vec![0, 1, 10];
for i in 2..=10 {
indices.extend_from_slice(&[0, i, i - 1]);
}
star.insert_indices(Indices::U32(indices));
// We can now spawn the entities for the star and the camera
commands.spawn((
// We use a marker component to identify the custom colored meshes
ColoredMesh2d,
// The `Handle<Mesh>` needs to be wrapped in a `Mesh2dHandle` to use 2d rendering instead of 3d
Mesh2dHandle(meshes.add(star)),
// This bundle's components are needed for something to be rendered
SpatialBundle::INHERITED_IDENTITY,
));
// Spawn the camera
commands.spawn(Camera2dBundle::default());
}
pub fn with_inserted_indices(self, indices: Indices) -> Mesh
pub fn with_inserted_indices(self, indices: Indices) -> Mesh
Consumes the mesh and returns a mesh with the given vertex indices. They describe how triangles
are constructed out of the vertex attributes and are therefore only useful for the
PrimitiveTopology
variants that use triangles.
(Alternatively, you can use Mesh::insert_indices
to mutate an existing mesh in-place)
Examples found in repository?
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut skinned_mesh_inverse_bindposes_assets: ResMut<Assets<SkinnedMeshInverseBindposes>>,
) {
// Create a camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
// Create inverse bindpose matrices for a skeleton consists of 2 joints
let inverse_bindposes = skinned_mesh_inverse_bindposes_assets.add(vec![
Mat4::from_translation(Vec3::new(-0.5, -1.0, 0.0)),
Mat4::from_translation(Vec3::new(-0.5, -1.0, 0.0)),
]);
// Create a mesh
let mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::RENDER_WORLD,
)
// Set mesh vertex positions
.with_inserted_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 0.5, 0.0],
[1.0, 0.5, 0.0],
[0.0, 1.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.5, 0.0],
[1.0, 1.5, 0.0],
[0.0, 2.0, 0.0],
[1.0, 2.0, 0.0],
],
)
// Set mesh vertex normals
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, vec![[0.0, 0.0, 1.0]; 10])
// Set mesh vertex joint indices for mesh skinning.
// Each vertex gets 4 indices used to address the `JointTransforms` array in the vertex shader
// as well as `SkinnedMeshJoint` array in the `SkinnedMesh` component.
// This means that a maximum of 4 joints can affect a single vertex.
.with_inserted_attribute(
Mesh::ATTRIBUTE_JOINT_INDEX,
// Need to be explicit here as [u16; 4] could be either Uint16x4 or Unorm16x4.
VertexAttributeValues::Uint16x4(vec![
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
]),
)
// Set mesh vertex joint weights for mesh skinning.
// Each vertex gets 4 joint weights corresponding to the 4 joint indices assigned to it.
// The sum of these weights should equal to 1.
.with_inserted_attribute(
Mesh::ATTRIBUTE_JOINT_WEIGHT,
vec![
[1.00, 0.00, 0.0, 0.0],
[1.00, 0.00, 0.0, 0.0],
[0.75, 0.25, 0.0, 0.0],
[0.75, 0.25, 0.0, 0.0],
[0.50, 0.50, 0.0, 0.0],
[0.50, 0.50, 0.0, 0.0],
[0.25, 0.75, 0.0, 0.0],
[0.25, 0.75, 0.0, 0.0],
[0.00, 1.00, 0.0, 0.0],
[0.00, 1.00, 0.0, 0.0],
],
)
// Tell bevy to construct triangles from a list of vertex indices,
// where each 3 vertex indices form an triangle.
.with_inserted_indices(Indices::U16(vec![
0, 1, 3, 0, 3, 2, 2, 3, 5, 2, 5, 4, 4, 5, 7, 4, 7, 6, 6, 7, 9, 6, 9, 8,
]));
let mesh = meshes.add(mesh);
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut rng = ChaCha8Rng::seed_from_u64(42);
for i in -5..5 {
// Create joint entities
let joint_0 = commands
.spawn(TransformBundle::from(Transform::from_xyz(
i as f32 * 1.5,
0.0,
i as f32 * 0.1,
)))
.id();
let joint_1 = commands
.spawn((AnimatedJoint, TransformBundle::IDENTITY))
.id();
// Set joint_1 as a child of joint_0.
commands.entity(joint_0).push_children(&[joint_1]);
// Each joint in this vector corresponds to each inverse bindpose matrix in `SkinnedMeshInverseBindposes`.
let joint_entities = vec![joint_0, joint_1];
// Create skinned mesh renderer. Note that its transform doesn't affect the position of the mesh.
commands.spawn((
PbrBundle {
mesh: mesh.clone(),
material: materials.add(Color::srgb(
rng.gen_range(0.0..1.0),
rng.gen_range(0.0..1.0),
rng.gen_range(0.0..1.0),
)),
..default()
},
SkinnedMesh {
inverse_bindposes: inverse_bindposes.clone(),
joints: joint_entities,
},
));
}
}
More examples
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
fn create_cube_mesh() -> Mesh {
// Keep the mesh data accessible in future frames to be able to mutate it in toggle_texture.
Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD)
.with_inserted_attribute(
Mesh::ATTRIBUTE_POSITION,
// Each array is an [x, y, z] coordinate in local space.
// The camera coordinate space is right-handed x-right, y-up, z-back. This means "forward" is -Z.
// Meshes always rotate around their local [0, 0, 0] when a rotation is applied to their Transform.
// By centering our mesh around the origin, rotating the mesh preserves its center of mass.
vec![
// top (facing towards +y)
[-0.5, 0.5, -0.5], // vertex with index 0
[0.5, 0.5, -0.5], // vertex with index 1
[0.5, 0.5, 0.5], // etc. until 23
[-0.5, 0.5, 0.5],
// bottom (-y)
[-0.5, -0.5, -0.5],
[0.5, -0.5, -0.5],
[0.5, -0.5, 0.5],
[-0.5, -0.5, 0.5],
// right (+x)
[0.5, -0.5, -0.5],
[0.5, -0.5, 0.5],
[0.5, 0.5, 0.5], // This vertex is at the same position as vertex with index 2, but they'll have different UV and normal
[0.5, 0.5, -0.5],
// left (-x)
[-0.5, -0.5, -0.5],
[-0.5, -0.5, 0.5],
[-0.5, 0.5, 0.5],
[-0.5, 0.5, -0.5],
// back (+z)
[-0.5, -0.5, 0.5],
[-0.5, 0.5, 0.5],
[0.5, 0.5, 0.5],
[0.5, -0.5, 0.5],
// forward (-z)
[-0.5, -0.5, -0.5],
[-0.5, 0.5, -0.5],
[0.5, 0.5, -0.5],
[0.5, -0.5, -0.5],
],
)
// Set-up UV coordinates to point to the upper (V < 0.5), "dirt+grass" part of the texture.
// Take a look at the custom image (assets/textures/array_texture.png)
// so the UV coords will make more sense
// Note: (0.0, 0.0) = Top-Left in UV mapping, (1.0, 1.0) = Bottom-Right in UV mapping
.with_inserted_attribute(
Mesh::ATTRIBUTE_UV_0,
vec![
// Assigning the UV coords for the top side.
[0.0, 0.2], [0.0, 0.0], [1.0, 0.0], [1.0, 0.2],
// Assigning the UV coords for the bottom side.
[0.0, 0.45], [0.0, 0.25], [1.0, 0.25], [1.0, 0.45],
// Assigning the UV coords for the right side.
[1.0, 0.45], [0.0, 0.45], [0.0, 0.2], [1.0, 0.2],
// Assigning the UV coords for the left side.
[1.0, 0.45], [0.0, 0.45], [0.0, 0.2], [1.0, 0.2],
// Assigning the UV coords for the back side.
[0.0, 0.45], [0.0, 0.2], [1.0, 0.2], [1.0, 0.45],
// Assigning the UV coords for the forward side.
[0.0, 0.45], [0.0, 0.2], [1.0, 0.2], [1.0, 0.45],
],
)
// For meshes with flat shading, normals are orthogonal (pointing out) from the direction of
// the surface.
// Normals are required for correct lighting calculations.
// Each array represents a normalized vector, which length should be equal to 1.0.
.with_inserted_attribute(
Mesh::ATTRIBUTE_NORMAL,
vec![
// Normals for the top side (towards +y)
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
// Normals for the bottom side (towards -y)
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
// Normals for the right side (towards +x)
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
// Normals for the left side (towards -x)
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
// Normals for the back side (towards +z)
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
// Normals for the forward side (towards -z)
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
],
)
// Create the triangles out of the 24 vertices we created.
// To construct a square, we need 2 triangles, therefore 12 triangles in total.
// To construct a triangle, we need the indices of its 3 defined vertices, adding them one
// by one, in a counter-clockwise order (relative to the position of the viewer, the order
// should appear counter-clockwise from the front of the triangle, in this case from outside the cube).
// Read more about how to correctly build a mesh manually in the Bevy documentation of a Mesh,
// further examples and the implementation of the built-in shapes.
.with_inserted_indices(Indices::U32(vec![
0,3,1 , 1,3,2, // triangles making up the top (+y) facing side.
4,5,7 , 5,6,7, // bottom (-y)
8,11,9 , 9,11,10, // right (+x)
12,13,15 , 13,14,15, // left (-x)
16,19,17 , 17,19,18, // back (+z)
20,21,23 , 21,22,23, // forward (-z)
]))
}
pub fn indices_mut(&mut self) -> Option<&mut Indices>
pub fn indices_mut(&mut self) -> Option<&mut Indices>
Retrieves the vertex indices
of the mesh mutably.
pub fn remove_indices(&mut self) -> Option<Indices>
pub fn remove_indices(&mut self) -> Option<Indices>
Removes the vertex indices
from the mesh and returns them.
pub fn with_removed_indices(self) -> Mesh
pub fn with_removed_indices(self) -> Mesh
Consumes the mesh and returns a mesh without the vertex indices
of the mesh.
(Alternatively, you can use Mesh::remove_indices
to mutate an existing mesh in-place)
pub fn get_vertex_size(&self) -> u64
pub fn get_vertex_size(&self) -> u64
Returns the size of a vertex in bytes.
pub fn get_index_buffer_bytes(&self) -> Option<&[u8]>
pub fn get_index_buffer_bytes(&self) -> Option<&[u8]>
Computes and returns the index data of the mesh as bytes. This is used to transform the index data into a GPU friendly format.
pub fn get_mesh_vertex_buffer_layout(
&self,
mesh_vertex_buffer_layouts: &mut MeshVertexBufferLayouts
) -> MeshVertexBufferLayoutRef
pub fn get_mesh_vertex_buffer_layout( &self, mesh_vertex_buffer_layouts: &mut MeshVertexBufferLayouts ) -> MeshVertexBufferLayoutRef
Get this Mesh
’s MeshVertexBufferLayout
, used in SpecializedMeshPipeline
.
pub fn count_vertices(&self) -> usize
pub fn count_vertices(&self) -> usize
Counts all vertices of the mesh.
If the attributes have different vertex counts, the smallest is returned.
pub fn get_vertex_buffer_data(&self) -> Vec<u8> ⓘ
pub fn get_vertex_buffer_data(&self) -> Vec<u8> ⓘ
Computes and returns the vertex data of the mesh as bytes.
Therefore the attributes are located in the order of their MeshVertexAttribute::id
.
This is used to transform the vertex data into a GPU friendly format.
If the vertex attributes have different lengths, they are all truncated to the length of the smallest.
pub fn duplicate_vertices(&mut self)
pub fn duplicate_vertices(&mut self)
Duplicates the vertex attributes so that no vertices are shared.
This can dramatically increase the vertex count, so make sure this is what you want. Does nothing if no Indices are set.
pub fn with_duplicated_vertices(self) -> Mesh
pub fn with_duplicated_vertices(self) -> Mesh
Consumes the mesh and returns a mesh with no shared vertices.
This can dramatically increase the vertex count, so make sure this is what you want. Does nothing if no Indices are set.
(Alternatively, you can use Mesh::duplicate_vertices
to mutate an existing mesh in-place)
pub fn compute_flat_normals(&mut self)
pub fn compute_flat_normals(&mut self)
Calculates the Mesh::ATTRIBUTE_NORMAL
of a mesh.
§Panics
Panics if Mesh::ATTRIBUTE_POSITION
is not of type float3
.
Panics if the mesh has any other topology than PrimitiveTopology::TriangleList
.
FIXME: The should handle more cases since this is called as a part of gltf mesh loading where we can’t really blame users for loading meshes that might not conform to the limitations here!
pub fn with_computed_flat_normals(self) -> Mesh
pub fn with_computed_flat_normals(self) -> Mesh
Consumes the mesh and returns a mesh with calculated Mesh::ATTRIBUTE_NORMAL
.
(Alternatively, you can use Mesh::with_computed_flat_normals
to mutate an existing mesh in-place)
§Panics
Panics if Indices
are set or Mesh::ATTRIBUTE_POSITION
is not of type float3
or
if the mesh has any other topology than PrimitiveTopology::TriangleList
.
Consider calling Mesh::with_duplicated_vertices
or export your mesh with normal attributes.
pub fn generate_tangents(&mut self) -> Result<(), GenerateTangentsError>
pub fn generate_tangents(&mut self) -> Result<(), GenerateTangentsError>
Generate tangents for the mesh using the mikktspace
algorithm.
Sets the Mesh::ATTRIBUTE_TANGENT
attribute if successful.
Requires a PrimitiveTopology::TriangleList
topology and the Mesh::ATTRIBUTE_POSITION
, Mesh::ATTRIBUTE_NORMAL
and Mesh::ATTRIBUTE_UV_0
attributes set.
Examples found in repository?
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
fn setup_parallax(
mut commands: Commands,
mut materials: ResMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
mut normal: ResMut<Normal>,
asset_server: Res<AssetServer>,
) {
// The normal map. Note that to generate it in the GIMP image editor, you should
// open the depth map, and do Filters → Generic → Normal Map
// You should enable the "flip X" checkbox.
let normal_handle = asset_server.load("textures/parallax_example/cube_normal.png");
normal.0 = Some(normal_handle);
let mut cube = Mesh::from(Cuboid::new(0.15, 0.15, 0.15));
// NOTE: for normal maps and depth maps to work, the mesh
// needs tangents generated.
cube.generate_tangents().unwrap();
let parallax_material = materials.add(StandardMaterial {
perceptual_roughness: 0.4,
base_color_texture: Some(asset_server.load("textures/parallax_example/cube_color.png")),
normal_map_texture: normal.0.clone(),
// The depth map is a greyscale texture where black is the highest level and
// white the lowest.
depth_map: Some(asset_server.load("textures/parallax_example/cube_depth.png")),
parallax_depth_scale: 0.09,
parallax_mapping_method: ParallaxMappingMethod::Relief { max_steps: 4 },
max_parallax_layer_count: 5.0f32.exp2(),
..default()
});
commands.spawn((
PbrBundle {
mesh: meshes.add(cube),
material: parallax_material,
transform: Transform::from_xyz(0.4, 0.2, -0.8),
..default()
},
Spin { speed: 0.3 },
));
}
pub fn with_generated_tangents(self) -> Result<Mesh, GenerateTangentsError>
pub fn with_generated_tangents(self) -> Result<Mesh, GenerateTangentsError>
Consumes the mesh and returns a mesh with tangents generated using the mikktspace
algorithm.
The resulting mesh will have the Mesh::ATTRIBUTE_TANGENT
attribute if successful.
(Alternatively, you can use Mesh::generate_tangents
to mutate an existing mesh in-place)
Requires a PrimitiveTopology::TriangleList
topology and the Mesh::ATTRIBUTE_POSITION
, Mesh::ATTRIBUTE_NORMAL
and Mesh::ATTRIBUTE_UV_0
attributes set.
Examples found in repository?
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
fn setup(
mut commands: Commands,
mut materials: ResMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
mut normal: ResMut<Normal>,
asset_server: Res<AssetServer>,
) {
// The normal map. Note that to generate it in the GIMP image editor, you should
// open the depth map, and do Filters → Generic → Normal Map
// You should enable the "flip X" checkbox.
let normal_handle = asset_server.load("textures/parallax_example/cube_normal.png");
normal.0 = Some(normal_handle);
// Camera
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(1.5, 1.5, 1.5).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
CameraController,
));
// light
commands
.spawn(PointLightBundle {
transform: Transform::from_xyz(2.0, 1.0, -1.1),
point_light: PointLight {
shadows_enabled: true,
..default()
},
..default()
})
.with_children(|commands| {
// represent the light source as a sphere
let mesh = meshes.add(Sphere::new(0.05).mesh().ico(3).unwrap());
commands.spawn(PbrBundle { mesh, ..default() });
});
// Plane
commands.spawn(PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(10.0, 10.0)),
material: materials.add(StandardMaterial {
// standard material derived from dark green, but
// with roughness and reflectance set.
perceptual_roughness: 0.45,
reflectance: 0.18,
..Color::srgb_u8(0, 80, 0).into()
}),
transform: Transform::from_xyz(0.0, -1.0, 0.0),
..default()
});
let parallax_depth_scale = TargetDepth::default().0;
let max_parallax_layer_count = TargetLayers::default().0.exp2();
let parallax_mapping_method = CurrentMethod::default();
let parallax_material = materials.add(StandardMaterial {
perceptual_roughness: 0.4,
base_color_texture: Some(asset_server.load("textures/parallax_example/cube_color.png")),
normal_map_texture: normal.0.clone(),
// The depth map is a greyscale texture where black is the highest level and
// white the lowest.
depth_map: Some(asset_server.load("textures/parallax_example/cube_depth.png")),
parallax_depth_scale,
parallax_mapping_method: parallax_mapping_method.0,
max_parallax_layer_count,
..default()
});
commands.spawn((
PbrBundle {
mesh: meshes.add(
// NOTE: for normal maps and depth maps to work, the mesh
// needs tangents generated.
Mesh::from(Cuboid::default())
.with_generated_tangents()
.unwrap(),
),
material: parallax_material.clone_weak(),
..default()
},
Spin { speed: 0.3 },
));
let background_cube = meshes.add(
Mesh::from(Cuboid::new(40.0, 40.0, 40.0))
.with_generated_tangents()
.unwrap(),
);
let background_cube_bundle = |translation| {
(
PbrBundle {
transform: Transform::from_translation(translation),
mesh: background_cube.clone(),
material: parallax_material.clone(),
..default()
},
Spin { speed: -0.1 },
)
};
commands.spawn(background_cube_bundle(Vec3::new(45., 0., 0.)));
commands.spawn(background_cube_bundle(Vec3::new(-45., 0., 0.)));
commands.spawn(background_cube_bundle(Vec3::new(0., 0., 45.)));
commands.spawn(background_cube_bundle(Vec3::new(0., 0., -45.)));
let style = TextStyle {
font_size: 20.0,
..default()
};
// example instructions
commands.spawn(
TextBundle::from_sections(vec![
TextSection::new(
format!("Parallax depth scale: {parallax_depth_scale:.5}\n"),
style.clone(),
),
TextSection::new(
format!("Layers: {max_parallax_layer_count:.0}\n"),
style.clone(),
),
TextSection::new(format!("{parallax_mapping_method}\n"), style.clone()),
TextSection::new("\n\n", style.clone()),
TextSection::new("Controls:\n", style.clone()),
TextSection::new("Left click - Change view angle\n", style.clone()),
TextSection::new(
"1/2 - Decrease/Increase parallax depth scale\n",
style.clone(),
),
TextSection::new("3/4 - Decrease/Increase layer count\n", style.clone()),
TextSection::new("Space - Switch parallaxing algorithm\n", style),
])
.with_style(Style {
position_type: PositionType::Absolute,
top: Val::Px(12.0),
left: Val::Px(12.0),
..default()
}),
);
}
pub fn merge(&mut self, other: Mesh)
pub fn merge(&mut self, other: Mesh)
Merges the Mesh
data of other
with self
. The attributes and indices of other
will be appended to self
.
Note that attributes of other
that don’t exist on self
will be ignored.
§Panics
Panics if the vertex attribute values of other
are incompatible with self
.
For example, VertexAttributeValues::Float32
is incompatible with VertexAttributeValues::Float32x3
.
pub fn transformed_by(self, transform: Transform) -> Mesh
pub fn transformed_by(self, transform: Transform) -> Mesh
Transforms the vertex positions, normals, and tangents of the mesh by the given Transform
.
pub fn transform_by(&mut self, transform: Transform)
pub fn transform_by(&mut self, transform: Transform)
Transforms the vertex positions, normals, and tangents of the mesh in place by the given Transform
.
pub fn translated_by(self, translation: Vec3) -> Mesh
pub fn translated_by(self, translation: Vec3) -> Mesh
Translates the vertex positions of the mesh by the given Vec3
.
pub fn translate_by(&mut self, translation: Vec3)
pub fn translate_by(&mut self, translation: Vec3)
Translates the vertex positions of the mesh in place by the given Vec3
.
pub fn rotated_by(self, rotation: Quat) -> Mesh
pub fn rotated_by(self, rotation: Quat) -> Mesh
Rotates the vertex positions, normals, and tangents of the mesh by the given Quat
.
pub fn rotate_by(&mut self, rotation: Quat)
pub fn rotate_by(&mut self, rotation: Quat)
Rotates the vertex positions, normals, and tangents of the mesh in place by the given Quat
.
pub fn scaled_by(self, scale: Vec3) -> Mesh
pub fn scaled_by(self, scale: Vec3) -> Mesh
Scales the vertex positions, normals, and tangents of the mesh by the given Vec3
.
pub fn scale_by(&mut self, scale: Vec3)
pub fn scale_by(&mut self, scale: Vec3)
Scales the vertex positions, normals, and tangents of the mesh in place by the given Vec3
.
pub fn compute_aabb(&self) -> Option<Aabb>
pub fn compute_aabb(&self) -> Option<Aabb>
Compute the Axis-Aligned Bounding Box of the mesh vertices in model space
Returns None
if self
doesn’t have Mesh::ATTRIBUTE_POSITION
of
type VertexAttributeValues::Float32x3
, or if self
doesn’t have any vertices.
pub fn has_morph_targets(&self) -> bool
pub fn has_morph_targets(&self) -> bool
Whether this mesh has morph targets.
pub fn set_morph_targets(&mut self, morph_targets: Handle<Image>)
pub fn set_morph_targets(&mut self, morph_targets: Handle<Image>)
Set morph targets image for this mesh. This requires a “morph target image”. See MorphTargetImage
for info.
pub fn with_morph_targets(self, morph_targets: Handle<Image>) -> Mesh
pub fn with_morph_targets(self, morph_targets: Handle<Image>) -> Mesh
Consumes the mesh and returns a mesh with the given morph targets.
This requires a “morph target image”. See MorphTargetImage
for info.
(Alternatively, you can use Mesh::set_morph_targets
to mutate an existing mesh in-place)
pub fn set_morph_target_names(&mut self, names: Vec<String>)
pub fn set_morph_target_names(&mut self, names: Vec<String>)
Sets the names of each morph target. This should correspond to the order of the morph targets in set_morph_targets
.
pub fn with_morph_target_names(self, names: Vec<String>) -> Mesh
pub fn with_morph_target_names(self, names: Vec<String>) -> Mesh
Consumes the mesh and returns a mesh with morph target names.
Names should correspond to the order of the morph targets in set_morph_targets
.
(Alternatively, you can use Mesh::set_morph_target_names
to mutate an existing mesh in-place)
pub fn morph_target_names(&self) -> Option<&[String]>
pub fn morph_target_names(&self) -> Option<&[String]>
Gets a list of all morph target names, if they exist.
Examples found in repository?
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
fn name_morphs(
mut has_printed: Local<bool>,
morph_data: Res<MorphData>,
meshes: Res<Assets<Mesh>>,
) {
if *has_printed {
return;
}
let Some(mesh) = meshes.get(&morph_data.mesh) else {
return;
};
let Some(names) = mesh.morph_target_names() else {
return;
};
for name in names {
println!(" {name}");
}
*has_printed = true;
}
More examples
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
fn detect_morphs(
mut commands: Commands,
morphs: Query<(Entity, &MorphWeights, Option<&Name>)>,
meshes: Res<Assets<Mesh>>,
scene_handle: Res<SceneHandle>,
mut setup: Local<bool>,
asset_server: Res<AssetServer>,
) {
let no_morphing = morphs.iter().len() == 0;
if no_morphing {
return;
}
if scene_handle.is_loaded && !*setup {
*setup = true;
} else {
return;
}
let mut detected = Vec::new();
for (entity, weights, name) in &morphs {
let target_names = weights
.first_mesh()
.and_then(|h| meshes.get(h))
.and_then(|m| m.morph_target_names());
let targets = Target::new(name, weights.weights(), target_names, entity);
detected.extend(targets);
}
detected.truncate(AVAILABLE_KEYS.len());
let style = TextStyle {
font: asset_server.load("assets/fonts/FiraMono-Medium.ttf"),
font_size: 13.0,
..default()
};
let mut sections = vec![
TextSection::new("Morph Target Controls\n", style.clone()),
TextSection::new("---------------\n", style.clone()),
];
let target_to_text =
|(i, target): (usize, &Target)| target.text_section(AVAILABLE_KEYS[i].name, style.clone());
sections.extend(detected.iter().enumerate().map(target_to_text));
commands.insert_resource(WeightsControl { weights: detected });
commands.spawn(TextBundle::from_sections(sections).with_style(Style {
position_type: PositionType::Absolute,
top: Val::Px(10.0),
left: Val::Px(10.0),
..default()
}));
}
pub fn normalize_joint_weights(&mut self)
pub fn normalize_joint_weights(&mut self)
Normalize joint weights so they sum to 1.
Trait Implementations§
§impl From<Capsule3dMeshBuilder> for Mesh
impl From<Capsule3dMeshBuilder> for Mesh
§fn from(capsule: Capsule3dMeshBuilder) -> Mesh
fn from(capsule: Capsule3dMeshBuilder) -> Mesh
§impl From<CircleMeshBuilder> for Mesh
impl From<CircleMeshBuilder> for Mesh
§fn from(circle: CircleMeshBuilder) -> Mesh
fn from(circle: CircleMeshBuilder) -> Mesh
§impl From<CylinderMeshBuilder> for Mesh
impl From<CylinderMeshBuilder> for Mesh
§fn from(cylinder: CylinderMeshBuilder) -> Mesh
fn from(cylinder: CylinderMeshBuilder) -> Mesh
§impl From<EllipseMeshBuilder> for Mesh
impl From<EllipseMeshBuilder> for Mesh
§fn from(ellipse: EllipseMeshBuilder) -> Mesh
fn from(ellipse: EllipseMeshBuilder) -> Mesh
§impl From<PlaneMeshBuilder> for Mesh
impl From<PlaneMeshBuilder> for Mesh
§fn from(plane: PlaneMeshBuilder) -> Mesh
fn from(plane: PlaneMeshBuilder) -> Mesh
§impl From<RegularPolygon> for Mesh
impl From<RegularPolygon> for Mesh
§fn from(polygon: RegularPolygon) -> Mesh
fn from(polygon: RegularPolygon) -> Mesh
§impl From<SphereMeshBuilder> for Mesh
impl From<SphereMeshBuilder> for Mesh
§fn from(sphere: SphereMeshBuilder) -> Mesh
fn from(sphere: SphereMeshBuilder) -> Mesh
§impl From<TorusMeshBuilder> for Mesh
impl From<TorusMeshBuilder> for Mesh
§fn from(torus: TorusMeshBuilder) -> Mesh
fn from(torus: TorusMeshBuilder) -> Mesh
§impl From<Triangle2d> for Mesh
impl From<Triangle2d> for Mesh
§fn from(triangle: Triangle2d) -> Mesh
fn from(triangle: Triangle2d) -> Mesh
§impl From<Triangle3d> for Mesh
impl From<Triangle3d> for Mesh
§fn from(triangle: Triangle3d) -> Mesh
fn from(triangle: Triangle3d) -> Mesh
§impl FromReflect for Meshwhere
Mesh: Any + Send + Sync,
Option<Indices>: FromReflect + TypePath + RegisterForReflection,
Option<Handle<Image>>: FromReflect + TypePath + RegisterForReflection,
Option<Vec<String>>: FromReflect + TypePath + RegisterForReflection,
RenderAssetUsages: FromReflect + TypePath + RegisterForReflection,
impl FromReflect for Meshwhere
Mesh: Any + Send + Sync,
Option<Indices>: FromReflect + TypePath + RegisterForReflection,
Option<Handle<Image>>: FromReflect + TypePath + RegisterForReflection,
Option<Vec<String>>: FromReflect + TypePath + RegisterForReflection,
RenderAssetUsages: FromReflect + TypePath + RegisterForReflection,
§fn from_reflect(reflect: &(dyn Reflect + 'static)) -> Option<Mesh>
fn from_reflect(reflect: &(dyn Reflect + 'static)) -> Option<Mesh>
Self
from a reflected value.§fn take_from_reflect(
reflect: Box<dyn Reflect>
) -> Result<Self, Box<dyn Reflect>>
fn take_from_reflect( reflect: Box<dyn Reflect> ) -> Result<Self, Box<dyn Reflect>>
Self
using,
constructing the value using from_reflect
if that fails. Read more§impl GetTypeRegistration for Meshwhere
Mesh: Any + Send + Sync,
Option<Indices>: FromReflect + TypePath + RegisterForReflection,
Option<Handle<Image>>: FromReflect + TypePath + RegisterForReflection,
Option<Vec<String>>: FromReflect + TypePath + RegisterForReflection,
RenderAssetUsages: FromReflect + TypePath + RegisterForReflection,
impl GetTypeRegistration for Meshwhere
Mesh: Any + Send + Sync,
Option<Indices>: FromReflect + TypePath + RegisterForReflection,
Option<Handle<Image>>: FromReflect + TypePath + RegisterForReflection,
Option<Vec<String>>: FromReflect + TypePath + RegisterForReflection,
RenderAssetUsages: FromReflect + TypePath + RegisterForReflection,
§fn get_type_registration() -> TypeRegistration
fn get_type_registration() -> TypeRegistration
TypeRegistration
for this type.§fn register_type_dependencies(registry: &mut TypeRegistry)
fn register_type_dependencies(registry: &mut TypeRegistry)
§impl Reflect for Meshwhere
Mesh: Any + Send + Sync,
Option<Indices>: FromReflect + TypePath + RegisterForReflection,
Option<Handle<Image>>: FromReflect + TypePath + RegisterForReflection,
Option<Vec<String>>: FromReflect + TypePath + RegisterForReflection,
RenderAssetUsages: FromReflect + TypePath + RegisterForReflection,
impl Reflect for Meshwhere
Mesh: Any + Send + Sync,
Option<Indices>: FromReflect + TypePath + RegisterForReflection,
Option<Handle<Image>>: FromReflect + TypePath + RegisterForReflection,
Option<Vec<String>>: FromReflect + TypePath + RegisterForReflection,
RenderAssetUsages: FromReflect + TypePath + RegisterForReflection,
§fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut dyn Any
.§fn as_reflect(&self) -> &(dyn Reflect + 'static)
fn as_reflect(&self) -> &(dyn Reflect + 'static)
§fn as_reflect_mut(&mut self) -> &mut (dyn Reflect + 'static)
fn as_reflect_mut(&mut self) -> &mut (dyn Reflect + 'static)
§fn clone_value(&self) -> Box<dyn Reflect>
fn clone_value(&self) -> Box<dyn Reflect>
Reflect
trait object. Read more§fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
§fn apply(&mut self, value: &(dyn Reflect + 'static))
fn apply(&mut self, value: &(dyn Reflect + 'static))
§fn reflect_kind(&self) -> ReflectKind
fn reflect_kind(&self) -> ReflectKind
§fn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
§fn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
§fn reflect_owned(self: Box<Mesh>) -> ReflectOwned
fn reflect_owned(self: Box<Mesh>) -> ReflectOwned
§fn reflect_partial_eq(&self, value: &(dyn Reflect + 'static)) -> Option<bool>
fn reflect_partial_eq(&self, value: &(dyn Reflect + 'static)) -> Option<bool>
§fn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
§fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
§fn serializable(&self) -> Option<Serializable<'_>>
fn serializable(&self) -> Option<Serializable<'_>>
§fn is_dynamic(&self) -> bool
fn is_dynamic(&self) -> bool
§impl Struct for Meshwhere
Mesh: Any + Send + Sync,
Option<Indices>: FromReflect + TypePath + RegisterForReflection,
Option<Handle<Image>>: FromReflect + TypePath + RegisterForReflection,
Option<Vec<String>>: FromReflect + TypePath + RegisterForReflection,
RenderAssetUsages: FromReflect + TypePath + RegisterForReflection,
impl Struct for Meshwhere
Mesh: Any + Send + Sync,
Option<Indices>: FromReflect + TypePath + RegisterForReflection,
Option<Handle<Image>>: FromReflect + TypePath + RegisterForReflection,
Option<Vec<String>>: FromReflect + TypePath + RegisterForReflection,
RenderAssetUsages: FromReflect + TypePath + RegisterForReflection,
§fn field(&self, name: &str) -> Option<&(dyn Reflect + 'static)>
fn field(&self, name: &str) -> Option<&(dyn Reflect + 'static)>
name
as a &dyn Reflect
.§fn field_mut(&mut self, name: &str) -> Option<&mut (dyn Reflect + 'static)>
fn field_mut(&mut self, name: &str) -> Option<&mut (dyn Reflect + 'static)>
name
as a
&mut dyn Reflect
.§fn field_at(&self, index: usize) -> Option<&(dyn Reflect + 'static)>
fn field_at(&self, index: usize) -> Option<&(dyn Reflect + 'static)>
index
as a
&dyn Reflect
.§fn field_at_mut(&mut self, index: usize) -> Option<&mut (dyn Reflect + 'static)>
fn field_at_mut(&mut self, index: usize) -> Option<&mut (dyn Reflect + 'static)>
index
as a &mut dyn Reflect
.§fn iter_fields(&self) -> FieldIter<'_> ⓘ
fn iter_fields(&self) -> FieldIter<'_> ⓘ
§fn clone_dynamic(&self) -> DynamicStruct
fn clone_dynamic(&self) -> DynamicStruct
DynamicStruct
.§impl TypePath for Mesh
impl TypePath for Mesh
§fn short_type_path() -> &'static str
fn short_type_path() -> &'static str
§fn type_ident() -> Option<&'static str>
fn type_ident() -> Option<&'static str>
§fn crate_name() -> Option<&'static str>
fn crate_name() -> Option<&'static str>
§impl Typed for Meshwhere
Mesh: Any + Send + Sync,
Option<Indices>: FromReflect + TypePath + RegisterForReflection,
Option<Handle<Image>>: FromReflect + TypePath + RegisterForReflection,
Option<Vec<String>>: FromReflect + TypePath + RegisterForReflection,
RenderAssetUsages: FromReflect + TypePath + RegisterForReflection,
impl Typed for Meshwhere
Mesh: Any + Send + Sync,
Option<Indices>: FromReflect + TypePath + RegisterForReflection,
Option<Handle<Image>>: FromReflect + TypePath + RegisterForReflection,
Option<Vec<String>>: FromReflect + TypePath + RegisterForReflection,
RenderAssetUsages: FromReflect + TypePath + RegisterForReflection,
§impl VisitAssetDependencies for Mesh
impl VisitAssetDependencies for Mesh
fn visit_dependencies(&self, visit: &mut impl FnMut(UntypedAssetId))
impl Asset for Mesh
Auto Trait Implementations§
impl Freeze for Mesh
impl !RefUnwindSafe for Mesh
impl Send for Mesh
impl Sync for Mesh
impl Unpin for Mesh
impl !UnwindSafe for Mesh
Blanket Implementations§
§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
§fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
T
ShaderType
for self
. When used in AsBindGroup
derives, it is safe to assume that all images in self
exist.§impl<A> AssetContainer for Awhere
A: Asset,
impl<A> AssetContainer for Awhere
A: Asset,
fn insert(self: Box<A>, id: UntypedAssetId, world: &mut World)
fn asset_type_name(&self) -> &'static str
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> DynamicTypePath for Twhere
T: TypePath,
impl<T> DynamicTypePath for Twhere
T: TypePath,
§fn reflect_type_path(&self) -> &str
fn reflect_type_path(&self) -> &str
TypePath::type_path
.§fn reflect_short_type_path(&self) -> &str
fn reflect_short_type_path(&self) -> &str
§fn reflect_type_ident(&self) -> Option<&str>
fn reflect_type_ident(&self) -> Option<&str>
TypePath::type_ident
.§fn reflect_crate_name(&self) -> Option<&str>
fn reflect_crate_name(&self) -> Option<&str>
TypePath::crate_name
.§fn reflect_module_path(&self) -> Option<&str>
fn reflect_module_path(&self) -> Option<&str>
§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
§impl<T> GetPath for T
impl<T> GetPath for T
§fn reflect_path<'p>(
&self,
path: impl ReflectPath<'p>
) -> Result<&(dyn Reflect + 'static), ReflectPathError<'p>>
fn reflect_path<'p>( &self, path: impl ReflectPath<'p> ) -> Result<&(dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read more§fn reflect_path_mut<'p>(
&mut self,
path: impl ReflectPath<'p>
) -> Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>
fn reflect_path_mut<'p>( &mut self, path: impl ReflectPath<'p> ) -> Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read more§fn path<'p, T>(
&self,
path: impl ReflectPath<'p>
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
fn path<'p, T>(
&self,
path: impl ReflectPath<'p>
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
path
. Read more§fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
path
. Read more§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more