Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Added TryFrom for VertexAttributeValues #1963

Closed
wants to merge 10 commits into from
140 changes: 62 additions & 78 deletions crates/bevy_render/src/mesh/mesh.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod conversions;

use crate::{
pipeline::{IndexFormat, PrimitiveTopology, RenderPipelines, VertexFormat},
renderer::{BufferInfo, BufferUsage, RenderResourceContext, RenderResourceId},
Expand Down Expand Up @@ -36,6 +38,21 @@ pub enum VertexAttributeValues {
Float4(Vec<[f32; 4]>),
Int4(Vec<[i32; 4]>),
Uint4(Vec<[u32; 4]>),
Short2(Vec<[i16; 2]>),
Short2Norm(Vec<[i16; 2]>),
Ushort2(Vec<[u16; 2]>),
Ushort2Norm(Vec<[u16; 2]>),
Short4(Vec<[i16; 4]>),
Short4Norm(Vec<[i16; 4]>),
Ushort4(Vec<[u16; 4]>),
Ushort4Norm(Vec<[u16; 4]>),
Char2(Vec<[i8; 2]>),
Char2Norm(Vec<[i8; 2]>),
Uchar2(Vec<[u8; 2]>),
Uchar2Norm(Vec<[u8; 2]>),
Char4(Vec<[i8; 4]>),
Char4Norm(Vec<[i8; 4]>),
Uchar4(Vec<[u8; 4]>),
Uchar4Norm(Vec<[u8; 4]>),
}

Expand All @@ -56,6 +73,21 @@ impl VertexAttributeValues {
VertexAttributeValues::Float4(ref values) => values.len(),
VertexAttributeValues::Int4(ref values) => values.len(),
VertexAttributeValues::Uint4(ref values) => values.len(),
VertexAttributeValues::Short2(ref values) => values.len(),
VertexAttributeValues::Short2Norm(ref values) => values.len(),
VertexAttributeValues::Ushort2(ref values) => values.len(),
VertexAttributeValues::Ushort2Norm(ref values) => values.len(),
VertexAttributeValues::Short4(ref values) => values.len(),
VertexAttributeValues::Short4Norm(ref values) => values.len(),
VertexAttributeValues::Ushort4(ref values) => values.len(),
VertexAttributeValues::Ushort4Norm(ref values) => values.len(),
VertexAttributeValues::Char2(ref values) => values.len(),
VertexAttributeValues::Char2Norm(ref values) => values.len(),
VertexAttributeValues::Uchar2(ref values) => values.len(),
VertexAttributeValues::Uchar2Norm(ref values) => values.len(),
VertexAttributeValues::Char4(ref values) => values.len(),
VertexAttributeValues::Char4Norm(ref values) => values.len(),
VertexAttributeValues::Uchar4(ref values) => values.len(),
VertexAttributeValues::Uchar4Norm(ref values) => values.len(),
}
}
Expand All @@ -82,6 +114,21 @@ impl VertexAttributeValues {
VertexAttributeValues::Float4(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Int4(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Uint4(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Short2(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Short2Norm(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Ushort2(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Ushort2Norm(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Short4(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Short4Norm(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Ushort4(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Ushort4Norm(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Char2(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Char2Norm(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Uchar2(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Uchar2Norm(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Char4(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Char4Norm(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Uchar4(values) => values.as_slice().as_bytes(),
VertexAttributeValues::Uchar4Norm(values) => values.as_slice().as_bytes(),
}
}
Expand All @@ -102,89 +149,26 @@ impl From<&VertexAttributeValues> for VertexFormat {
VertexAttributeValues::Float4(_) => VertexFormat::Float4,
VertexAttributeValues::Int4(_) => VertexFormat::Int4,
VertexAttributeValues::Uint4(_) => VertexFormat::Uint4,
VertexAttributeValues::Short2(_) => VertexFormat::Short2,
VertexAttributeValues::Short2Norm(_) => VertexFormat::Short2Norm,
VertexAttributeValues::Ushort2(_) => VertexFormat::Ushort2,
VertexAttributeValues::Ushort2Norm(_) => VertexFormat::Ushort2Norm,
VertexAttributeValues::Short4(_) => VertexFormat::Short4,
VertexAttributeValues::Short4Norm(_) => VertexFormat::Short4Norm,
VertexAttributeValues::Ushort4(_) => VertexFormat::Ushort4,
VertexAttributeValues::Ushort4Norm(_) => VertexFormat::Ushort4Norm,
VertexAttributeValues::Char2(_) => VertexFormat::Char2,
VertexAttributeValues::Char2Norm(_) => VertexFormat::Char2Norm,
VertexAttributeValues::Uchar2(_) => VertexFormat::Uchar2,
VertexAttributeValues::Uchar2Norm(_) => VertexFormat::Uchar2Norm,
VertexAttributeValues::Char4(_) => VertexFormat::Char4,
VertexAttributeValues::Char4Norm(_) => VertexFormat::Char4Norm,
VertexAttributeValues::Uchar4(_) => VertexFormat::Uchar4,
VertexAttributeValues::Uchar4Norm(_) => VertexFormat::Uchar4Norm,
}
}
}

impl From<Vec<f32>> for VertexAttributeValues {
fn from(vec: Vec<f32>) -> Self {
VertexAttributeValues::Float(vec)
}
}

impl From<Vec<i32>> for VertexAttributeValues {
fn from(vec: Vec<i32>) -> Self {
VertexAttributeValues::Int(vec)
}
}

impl From<Vec<u32>> for VertexAttributeValues {
fn from(vec: Vec<u32>) -> Self {
VertexAttributeValues::Uint(vec)
}
}

impl From<Vec<[f32; 2]>> for VertexAttributeValues {
fn from(vec: Vec<[f32; 2]>) -> Self {
VertexAttributeValues::Float2(vec)
}
}

impl From<Vec<[i32; 2]>> for VertexAttributeValues {
fn from(vec: Vec<[i32; 2]>) -> Self {
VertexAttributeValues::Int2(vec)
}
}

impl From<Vec<[u32; 2]>> for VertexAttributeValues {
fn from(vec: Vec<[u32; 2]>) -> Self {
VertexAttributeValues::Uint2(vec)
}
}

impl From<Vec<[f32; 3]>> for VertexAttributeValues {
fn from(vec: Vec<[f32; 3]>) -> Self {
VertexAttributeValues::Float3(vec)
}
}

impl From<Vec<[i32; 3]>> for VertexAttributeValues {
fn from(vec: Vec<[i32; 3]>) -> Self {
VertexAttributeValues::Int3(vec)
}
}

impl From<Vec<[u32; 3]>> for VertexAttributeValues {
fn from(vec: Vec<[u32; 3]>) -> Self {
VertexAttributeValues::Uint3(vec)
}
}

impl From<Vec<[f32; 4]>> for VertexAttributeValues {
fn from(vec: Vec<[f32; 4]>) -> Self {
VertexAttributeValues::Float4(vec)
}
}

impl From<Vec<[i32; 4]>> for VertexAttributeValues {
fn from(vec: Vec<[i32; 4]>) -> Self {
VertexAttributeValues::Int4(vec)
}
}

impl From<Vec<[u32; 4]>> for VertexAttributeValues {
fn from(vec: Vec<[u32; 4]>) -> Self {
VertexAttributeValues::Uint4(vec)
}
}

impl From<Vec<[u8; 4]>> for VertexAttributeValues {
fn from(vec: Vec<[u8; 4]>) -> Self {
VertexAttributeValues::Uchar4Norm(vec)
}
}

/// An array of indices into the VertexAttributeValues for a mesh.
///
/// It describes the order in which the vertex attributes should be joined into faces.
Expand Down
Loading