Skip to content

Commit

Permalink
Added TryFrom for VertexAttributeValues (#1963)
Browse files Browse the repository at this point in the history
This implementations allows you
convert std::vec::Vec<T> to VertexAttributeValues::T and back.

# Examples

```rust
use std::convert::TryInto;
use bevy_render::mesh::VertexAttributeValues;

// creating vector of values
let before = vec![[0_u32; 4]; 10];
let values = VertexAttributeValues::from(before.clone());
let after: Vec<[u32; 4]> = values.try_into().unwrap();

assert_eq!(before, after);
```

Co-authored-by: aloucks <aloucks@cofront.net>
Co-authored-by: simens_green <34134129+simensgreen@users.noreply.github.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
  • Loading branch information
4 people committed Apr 20, 2021
1 parent 4a477e7 commit c74994b
Show file tree
Hide file tree
Showing 2 changed files with 608 additions and 78 deletions.
80 changes: 2 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 @@ -174,84 +176,6 @@ impl From<&VertexAttributeValues> for VertexFormat {
}
}

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

0 comments on commit c74994b

Please sign in to comment.