From f6ab7c2847998dab79c8bc02c110728f748fd1a7 Mon Sep 17 00:00:00 2001 From: Jay Oster Date: Sat, 13 Nov 2021 09:49:39 -0800 Subject: [PATCH] Add a new vertex attribute to the pipelined custom shader example --- assets/shaders/custom_material.wgsl | 36 ++++++++++++++++++-- examples/shader/shader_material.rs | 53 +++++++++++++++++++++++++++-- 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/assets/shaders/custom_material.wgsl b/assets/shaders/custom_material.wgsl index 9dfaa6f568f43c..75642b60df8a41 100644 --- a/assets/shaders/custom_material.wgsl +++ b/assets/shaders/custom_material.wgsl @@ -1,3 +1,31 @@ +#import bevy_pbr::mesh_view_bind_group +#import bevy_pbr::mesh_struct + +struct Vertex { + [[location(0)]] position: vec3; + [[location(1)]] normal: vec3; + [[location(2)]] uv: vec2; + [[location(3)]] color: vec4; +}; + +struct VertexOutput { + [[builtin(position)]] clip_position: vec4; + [[location(0)]] color: vec4; +}; + +[[group(2), binding(0)]] +var mesh: Mesh; + +[[stage(vertex)]] +fn vertex(vertex: Vertex) -> VertexOutput { + let world_position = mesh.model * vec4(vertex.position, 1.0); + + var out: VertexOutput; + out.clip_position = view.view_proj * world_position; + out.color = vertex.color; + return out; +} + [[block]] struct CustomMaterial { color: vec4; @@ -5,7 +33,11 @@ struct CustomMaterial { [[group(1), binding(0)]] var material: CustomMaterial; +struct FragmentInput { + [[location(0)]] color: vec4; +}; + [[stage(fragment)]] -fn fragment() -> [[location(0)]] vec4 { - return material.color; +fn fragment(fragment: FragmentInput) -> [[location(0)]] vec4 { + return (material.color + fragment.color) / 2.0; } diff --git a/examples/shader/shader_material.rs b/examples/shader/shader_material.rs index 1eabde64d1f277..b219e8b78e643e 100644 --- a/examples/shader/shader_material.rs +++ b/examples/shader/shader_material.rs @@ -1,6 +1,8 @@ use bevy::{ + core::Time, core_pipeline::Transparent3d, ecs::system::{lifetimeless::*, SystemParamItem}, + math::Quat, pbr::{ DrawMesh, MeshPipeline, MeshPipelineFlags, MeshPipelineKey, MeshUniform, SetMeshBindGroup, SetMeshViewBindGroup, @@ -28,6 +30,7 @@ fn main() { .add_plugins(DefaultPlugins) .add_plugin(CustomMaterialPlugin) .add_startup_system(setup) + .add_system(rotate_mesh) .run(); } @@ -37,9 +40,13 @@ fn setup( mut meshes: ResMut>, mut materials: ResMut>, ) { - // cube + // cube with custom vertex attributes + let mut mesh = Mesh::from(shape::Cube { size: 1.0 }); + mesh.vertex_layout_mut() + .push(Mesh::ATTRIBUTE_COLOR, VertexFormat::Float32x4); + mesh.set_attribute(Mesh::ATTRIBUTE_COLOR, cube_vertex_colors()); commands.spawn().insert_bundle(( - meshes.add(Mesh::from(shape::Cube { size: 1.0 })), + meshes.add(mesh), Transform::from_xyz(0.0, 0.5, 0.0), GlobalTransform::default(), Visibility::default(), @@ -56,6 +63,48 @@ fn setup( }); } +fn rotate_mesh(time: Res