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

Add the ability to pass Vec<VecX> directly to mesh.set_attribute + simple example #2719

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ path = "examples/3d/wireframe.rs"
name = "z_sort_debug"
path = "examples/3d/z_sort_debug.rs"

[[example]]
name = "custom_mesh"
path = "examples/3d/custom_mesh.rs"

# Application
[[example]]
name = "custom_loop"
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ anyhow = "1.0.4"
hex = "0.4.2"
hexasphere = "4.0.0"
parking_lot = "0.11.0"
bytemuck = { version = "1.7.2", features = ["extern_crate_alloc"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
spirv-reflect = "0.2.3"
Expand Down
20 changes: 19 additions & 1 deletion crates/bevy_render/src/mesh/mesh/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
//! ```

use crate::mesh::VertexAttributeValues;
use bevy_math::{Vec2, Vec3, Vec4};
use bevy_utils::EnumVariantMeta;
use std::convert::TryFrom;
use thiserror::Error;

#[derive(Debug, Clone, Error)]
#[error("cannot convert VertexAttributeValues::{variant} to {into}")]
pub struct FromVertexAttributeError {
Expand Down Expand Up @@ -126,6 +126,24 @@ impl From<Vec<[u8; 4]>> for VertexAttributeValues {
}
}

impl From<Vec<Vec4>> for VertexAttributeValues {
fn from(vec: Vec<Vec4>) -> Self {
VertexAttributeValues::Float32x4(bytemuck::allocation::cast_vec(vec))
}
}

impl From<Vec<Vec3>> for VertexAttributeValues {
fn from(vec: Vec<Vec3>) -> Self {
VertexAttributeValues::Float32x3(bytemuck::allocation::cast_vec(vec))
}
}

impl From<Vec<Vec2>> for VertexAttributeValues {
fn from(vec: Vec<Vec2>) -> Self {
VertexAttributeValues::Float32x2(bytemuck::allocation::cast_vec(vec))
}
}

impl TryFrom<VertexAttributeValues> for Vec<[u8; 4]> {
type Error = FromVertexAttributeError;

Expand Down
69 changes: 69 additions & 0 deletions examples/3d/custom_mesh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use bevy::prelude::*;
use bevy::render::mesh::Indices;
use bevy::render::pipeline::PrimitiveTopology;

fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.run();
}

/// set up a simple 3D scene
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let origin = Vec3::new(0.0, 0.0, 0.0);
let right = Vec3::new(1.0, 0.0, 0.0);
let up = Vec3::new(0.0, 1.0, 0.0);

#[rustfmt::skip]
let positions = vec![
origin + up,
origin + up + right,
origin,
origin + right
];

let origin = Vec2::new(0.0, 0.0);
let right = Vec2::new(1.0, 0.0);
let up = Vec2::new(0.0, 1.0);

#[rustfmt::skip]
let uvs = vec![
origin + up,
origin + up + right,
origin,
origin + right
];

let normals = vec![[0.0, 0.0, 1.0]; 4];
let indices = Indices::U32(vec![0, 1, 2, 3, 2, 1]);

let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
mesh.set_indices(Some(indices));
mesh.set_attribute(Mesh::ATTRIBUTE_POSITION, positions);
mesh.set_attribute(Mesh::ATTRIBUTE_NORMAL, normals);
mesh.set_attribute(Mesh::ATTRIBUTE_UV_0, uvs);

// plane
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(mesh),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..Default::default()
});

// light
commands.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
});
// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.0, 5.0, -8.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});
}
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Example | File | Description
`update_gltf_scene` | [`3d/update_gltf_scene.rs`](./3d/update_gltf_scene.rs) | Update a scene from a gltf file, either by spawning the scene as a child of another entity, or by accessing the entities of the scene
`wireframe` | [`3d/wireframe.rs`](./3d/wireframe.rs) | Showcases wireframe rendering
`z_sort_debug` | [`3d/z_sort_debug.rs`](./3d/z_sort_debug.rs) | Visualizes camera Z-ordering
`custom_mesh` | [`3d/custom_mesh.rs`](./3d/custom_mesh.rs) | Simple example showing how to manually create a custom mesh.

## Application

Expand Down