Skip to content

Commit

Permalink
Example for custom mesh attributes (#757)
Browse files Browse the repository at this point in the history
example for custom attributes + changelog
  • Loading branch information
Julian Heinken authored Nov 2, 2020
1 parent 44b3e24 commit f81ecdd
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 3 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ to view all changes since the `0.2.1` release.
### Added

- [Touch Input][696]
- [Do not depend on spirv on wasm32 target][689]
- [Another fast compile flag for macOS][552]
- [Introduce Mouse capture API][679]
- [`bevy_input::touch`: implement touch input][696]
- [D-pad support on MacOS][653]
Expand Down Expand Up @@ -70,9 +68,10 @@ to view all changes since the `0.2.1` release.
- New methods `Color::rgb_linear` and `Color::rgba_linear` will accept colors already in linear sRGB (the old behavior)
- Individual color-components must now be accessed through setters and getters.
- [`Mesh` overhaul with custom vertex attributes][599]
- Any vertex attribute can now be added over `mesh.attributes.insert()`.
- See `example/shader/mesh_custom_attribute.rs`
- Removed `VertexAttribute`, `Vertex`, `AsVertexBufferDescriptor`.
- For missing attributes (requested by shader, but not defined by mesh), Bevy will provide a zero-filled fallback buffer.
- Any vertex attribute can now be added over `mesh.attributes.insert()`. `mesh.attributes.insert(Cow::Borrowed(Mesh::ATTRIBUTE_POSITION), points.into())`
- Despawning an entity multiple times causes a debug-level log message to be emitted instead of a panic: [#649][649], [#651][651]
- [Migrated to Rodio 0.12][692]
- New method of playing audio can be found in the examples.
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ path = "examples/scene/scene.rs"
name = "properties"
path = "examples/scene/properties.rs"

[[example]]
name = "mesh_custom_attribute"
path = "examples/shader/mesh_custom_attribute.rs"

[[example]]
name = "shader_custom_material"
path = "examples/shader/shader_custom_material.rs"
Expand Down
156 changes: 156 additions & 0 deletions examples/shader/mesh_custom_attribute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
use bevy::{
prelude::*,
render::{
mesh::{shape, VertexAttributeValues},
pipeline::{DynamicBinding, PipelineDescriptor, PipelineSpecialization, RenderPipeline},
render_graph::{base, AssetRenderResourcesNode, RenderGraph},
renderer::RenderResources,
shader::{ShaderStage, ShaderStages},
},
type_registry::TypeUuid,
};

/// This example illustrates how to add a custom attribute to a mesh and use it in a custom shader.
fn main() {
App::build()
.add_default_plugins()
.add_asset::<MyMaterialWithVertexColorSupport>()
.add_startup_system(setup.system())
.run();
}

#[derive(RenderResources, Default, TypeUuid)]
#[uuid = "0320b9b8-b3a3-4baa-8bfa-c94008177b17"]
struct MyMaterialWithVertexColorSupport {}

const VERTEX_SHADER: &str = r#"
#version 450
layout(location = 0) in vec3 Vertex_Position;
layout(location = 1) in vec3 Vertex_Color;
layout(location = 0) out vec3 v_color;
layout(set = 0, binding = 0) uniform Camera {
mat4 ViewProj;
};
layout(set = 1, binding = 0) uniform Transform {
mat4 Model;
};
void main() {
gl_Position = ViewProj * Model * vec4(Vertex_Position, 1.0);
v_color = Vertex_Color;
}
"#;

const FRAGMENT_SHADER: &str = r#"
#version 450
layout(location = 0) out vec4 o_Target;
layout(location = 0) in vec3 v_color;
void main() {
o_Target = vec4(v_color, 1.0);
}
"#;

fn setup(
mut commands: Commands,
mut pipelines: ResMut<Assets<PipelineDescriptor>>,
mut shaders: ResMut<Assets<Shader>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<MyMaterialWithVertexColorSupport>>,
mut render_graph: ResMut<RenderGraph>,
) {
// Create a new shader pipeline
let pipeline_handle = pipelines.add(PipelineDescriptor::default_config(ShaderStages {
vertex: shaders.add(Shader::from_glsl(ShaderStage::Vertex, VERTEX_SHADER)),
fragment: Some(shaders.add(Shader::from_glsl(ShaderStage::Fragment, FRAGMENT_SHADER))),
}));

// Add an AssetRenderResourcesNode to our Render Graph. This will bind MyMaterialWithVertexColorSupport resources to our shader
render_graph.add_system_node(
"my_material_with_vertex_color_support",
AssetRenderResourcesNode::<MyMaterialWithVertexColorSupport>::new(true),
);

// Add a Render Graph edge connecting our new "my_material" node to the main pass node. This ensures "my_material" runs before the main pass
render_graph
.add_node_edge(
"my_material_with_vertex_color_support",
base::node::MAIN_PASS,
)
.unwrap();

// Create a new material
let material = materials.add(MyMaterialWithVertexColorSupport {});

// create a generic cube
let mut cube_with_vertex_colors = Mesh::from(shape::Cube { size: 1.0 });

// insert our custom color attribute with some nice colors!
cube_with_vertex_colors.set_attribute(
// name of the attribute
"Vertex_Color",
// the vertex attributes, represented by `VertexAttributeValues`
// NOTE: the attribute count has to be consistent across all attributes, otherwise bevy will panic.
VertexAttributeValues::from(vec![
// top
[0.79, 0.73, 0.07],
[0.74, 0.14, 0.29],
[0.08, 0.55, 0.74],
[0.20, 0.27, 0.29],
// bottom
[0.79, 0.73, 0.07],
[0.74, 0.14, 0.29],
[0.08, 0.55, 0.74],
[0.20, 0.27, 0.29],
// right
[0.79, 0.73, 0.07],
[0.74, 0.14, 0.29],
[0.08, 0.55, 0.74],
[0.20, 0.27, 0.29],
// left
[0.79, 0.73, 0.07],
[0.74, 0.14, 0.29],
[0.08, 0.55, 0.74],
[0.20, 0.27, 0.29],
// front
[0.79, 0.73, 0.07],
[0.74, 0.14, 0.29],
[0.08, 0.55, 0.74],
[0.20, 0.27, 0.29],
// back
[0.79, 0.73, 0.07],
[0.74, 0.14, 0.29],
[0.08, 0.55, 0.74],
[0.20, 0.27, 0.29],
]),
);
// Setup our world
commands
// cube
.spawn(MeshComponents {
mesh: meshes.add(cube_with_vertex_colors), // use our cube with vertex colors
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::specialized(
pipeline_handle,
// NOTE: in the future you wont need to manually declare dynamic bindings
PipelineSpecialization {
dynamic_bindings: vec![
// Transform
DynamicBinding {
bind_group: 1,
binding: 0,
},
],
..Default::default()
},
)]),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
..Default::default()
})
.with(material)
// camera
.spawn(Camera3dComponents {
transform: Transform::from_translation(Vec3::new(3.0, 5.0, -8.0))
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
}

0 comments on commit f81ecdd

Please sign in to comment.