-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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] - Add support for vertex colors #4528
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use bevy::{prelude::*, render::mesh::VertexAttributeValues}; | ||
|
||
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>>, | ||
) { | ||
// plane | ||
commands.spawn_bundle(PbrBundle { | ||
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })), | ||
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), | ||
..default() | ||
}); | ||
// cube | ||
// Assign vertex colors based on vertex positions | ||
let mut colorful_cube = Mesh::from(shape::Cube { size: 1.0 }); | ||
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_bundle(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::rgb(1., 1., 1.).into()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It defaults to white, as I recall, to allow default to be identity for a material with a texture. That said, I think it's good to comment as it could easily be a source of confusion otherwise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 37 - 40 are really there just to explain why the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, it is. And it's an additional flexibility to be able to tint things, if nothing else. :) |
||
transform: Transform::from_xyz(0.0, 0.5, 0.0), | ||
..default() | ||
}); | ||
// light | ||
commands.spawn_bundle(PointLightBundle { | ||
point_light: PointLight { | ||
intensity: 1500.0, | ||
shadows_enabled: true, | ||
..default() | ||
}, | ||
transform: Transform::from_xyz(4.0, 8.0, 4.0), | ||
..default() | ||
}); | ||
// camera | ||
commands.spawn_bundle(PerspectiveCameraBundle { | ||
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), | ||
..default() | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shifted the locations to match the
id
s given in theMeshVertexAttribute
s inbevy_render/src/mesh/mesh/mod.rs
.