pub struct Mesh { /* private fields */ }
Implementations§
§impl Mesh
impl Mesh
Contains geometry in the form of a mesh.
Often meshes are automatically generated by bevy’s asset loaders or primitives, such as
shape::Cube
or shape::Box
, but you can also construct
one yourself.
Example of constructing a mesh:
fn create_triangle() -> Mesh {
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, vec![[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0, 0.0]]);
mesh.set_indices(Some(Indices::U32(vec![0,1,2])));
mesh
}
pub const ATTRIBUTE_POSITION: MeshVertexAttribute = MeshVertexAttribute::new("Vertex_Position", 0, VertexFormat::Float32x3)
pub const ATTRIBUTE_POSITION: MeshVertexAttribute = MeshVertexAttribute::new("Vertex_Position", 0, VertexFormat::Float32x3)
Where the vertex is located in space. Use in conjunction with Mesh::insert_attribute
pub const ATTRIBUTE_NORMAL: MeshVertexAttribute = MeshVertexAttribute::new("Vertex_Normal", 1, VertexFormat::Float32x3)
pub const ATTRIBUTE_NORMAL: MeshVertexAttribute = MeshVertexAttribute::new("Vertex_Normal", 1, VertexFormat::Float32x3)
The direction the vertex normal is facing in.
Use in conjunction with Mesh::insert_attribute
pub const ATTRIBUTE_UV_0: MeshVertexAttribute = MeshVertexAttribute::new("Vertex_Uv", 2, VertexFormat::Float32x2)
pub const ATTRIBUTE_UV_0: MeshVertexAttribute = MeshVertexAttribute::new("Vertex_Uv", 2, VertexFormat::Float32x2)
Texture coordinates for the vertex. Use in conjunction with Mesh::insert_attribute
pub const ATTRIBUTE_TANGENT: MeshVertexAttribute = MeshVertexAttribute::new("Vertex_Tangent", 3, VertexFormat::Float32x4)
pub const ATTRIBUTE_TANGENT: MeshVertexAttribute = MeshVertexAttribute::new("Vertex_Tangent", 3, VertexFormat::Float32x4)
The direction of the vertex tangent. Used for normal mapping
pub const ATTRIBUTE_COLOR: MeshVertexAttribute = MeshVertexAttribute::new("Vertex_Color", 4, VertexFormat::Float32x4)
pub const ATTRIBUTE_COLOR: MeshVertexAttribute = MeshVertexAttribute::new("Vertex_Color", 4, VertexFormat::Float32x4)
Per vertex coloring. Use in conjunction with Mesh::insert_attribute
pub const ATTRIBUTE_JOINT_WEIGHT: MeshVertexAttribute = MeshVertexAttribute::new("Vertex_JointWeight", 5, VertexFormat::Float32x4)
pub const ATTRIBUTE_JOINT_WEIGHT: MeshVertexAttribute = MeshVertexAttribute::new("Vertex_JointWeight", 5, VertexFormat::Float32x4)
Per vertex joint transform matrix weight. Use in conjunction with Mesh::insert_attribute
pub const ATTRIBUTE_JOINT_INDEX: MeshVertexAttribute = MeshVertexAttribute::new("Vertex_JointIndex", 6, VertexFormat::Uint16x4)
pub const ATTRIBUTE_JOINT_INDEX: MeshVertexAttribute = MeshVertexAttribute::new("Vertex_JointIndex", 6, VertexFormat::Uint16x4)
Per vertex joint transform matrix index. Use in conjunction with Mesh::insert_attribute
pub fn new(primitive_topology: PrimitiveTopology) -> Mesh
pub fn new(primitive_topology: PrimitiveTopology) -> 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
.
pub fn primitive_topology(&self) -> PrimitiveTopology
pub fn primitive_topology(&self) -> PrimitiveTopology
Returns the topology of the mesh.
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.
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 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
.
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.
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 set_indices(&mut self, indices: Option<Indices>)
pub fn set_indices(&mut self, indices: Option<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.
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 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
) -> Hashed<InnerMeshVertexBufferLayout, FixedState>
pub fn get_mesh_vertex_buffer_layout(
&self
) -> Hashed<InnerMeshVertexBufferLayout, FixedState>
For a given descriptor
returns a VertexBufferLayout
compatible with this mesh. If this
mesh is not compatible with the given descriptor
(ex: it is missing vertex attributes), None
will
be returned.
pub fn count_vertices(&self) -> usize
pub fn count_vertices(&self) -> usize
pub fn get_vertex_buffer_data(&self) -> Vec<u8, Global> ⓘ
pub fn get_vertex_buffer_data(&self) -> Vec<u8, Global> ⓘ
Computes and returns the vertex data of the mesh as bytes. Therefore the attributes are located in alphabetical order. This is used to transform the vertex data into a GPU friendly format.
Panics
Panics if the attributes have different vertex counts.
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 compute_flat_normals(&mut self)
pub fn compute_flat_normals(&mut self)
Calculates the Mesh::ATTRIBUTE_NORMAL
of a mesh.
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::duplicate_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.
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
Trait Implementations§
§impl From<RegularPolygon> for Mesh
impl From<RegularPolygon> for Mesh
§fn from(polygon: RegularPolygon) -> Mesh
fn from(polygon: RegularPolygon) -> Mesh
§impl RenderAsset for Mesh
impl RenderAsset for Mesh
§fn extract_asset(&self) -> <Mesh as RenderAsset>::ExtractedAsset
fn extract_asset(&self) -> <Mesh as RenderAsset>::ExtractedAsset
Clones the mesh.
§fn prepare_asset(
mesh: <Mesh as RenderAsset>::ExtractedAsset,
render_device: &mut <<<Mesh as RenderAsset>::Param as SystemParam>::Fetch as SystemParamFetch<'_, '_>>::Item
) -> Result<<Mesh as RenderAsset>::PreparedAsset, PrepareAssetError<<Mesh as RenderAsset>::ExtractedAsset>>
fn prepare_asset(
mesh: <Mesh as RenderAsset>::ExtractedAsset,
render_device: &mut <<<Mesh as RenderAsset>::Param as SystemParam>::Fetch as SystemParamFetch<'_, '_>>::Item
) -> Result<<Mesh as RenderAsset>::PreparedAsset, PrepareAssetError<<Mesh as RenderAsset>::ExtractedAsset>>
Converts the extracted mesh a into GpuMesh
.
§type ExtractedAsset = Mesh
type ExtractedAsset = Mesh
§type PreparedAsset = GpuMesh
type PreparedAsset = GpuMesh
§type Param = Res<'static, RenderDevice>
type Param = Res<'static, RenderDevice>
RenderAsset::prepare_asset
.
For convenience use the lifetimeless
SystemParam
. Read moreAuto Trait Implementations§
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 Twhere
U: ShaderType,
&'a T: for<'a> Into<U>,
impl<T, U> AsBindGroupShaderType<U> for Twhere
U: ShaderType,
&'a T: for<'a> Into<U>,
§fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
T
ShaderType
for self
. When used in AsBindGroup
derives, it is safe to assume that all images in self
exist. Read more§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
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
. Read more§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more§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. Read more§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. Read more