diff --git a/assets/shaders/custom.wgsl b/assets/shaders/custom.wgsl index 8fef36d30388d2..c9578be247a98a 100644 --- a/assets/shaders/custom.wgsl +++ b/assets/shaders/custom.wgsl @@ -18,10 +18,12 @@ 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; }; [[stage(vertex)]] @@ -30,6 +32,7 @@ fn vertex(vertex: Vertex) -> VertexOutput { var out: VertexOutput; out.clip_position = view.view_proj * world_position; + out.color = vertex.color; return out; } @@ -40,7 +43,11 @@ struct CustomMaterial { [[group(2), 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/custom_shader_pipelined.rs b/examples/shader/custom_shader_pipelined.rs index 16d1949f35e458..21a327f96e703a 100644 --- a/examples/shader/custom_shader_pipelined.rs +++ b/examples/shader/custom_shader_pipelined.rs @@ -1,11 +1,12 @@ use bevy::{ + core::Time, core_pipeline::Transparent3d, diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, ecs::{ prelude::*, system::{lifetimeless::*, SystemParamItem}, }, - math::{Vec3, Vec4}, + math::{Quat, Vec3, Vec4}, pbr2::{ DrawMesh, MeshUniform, PbrPipeline, PbrPipelineKey, SetMeshViewBindGroup, SetTransformBindGroup, @@ -38,6 +39,7 @@ fn main() { .add_plugin(LogDiagnosticsPlugin::default()) .add_plugin(CustomMaterialPlugin) .add_startup_system(setup) + .add_system(rotate_mesh) .run(); } @@ -47,9 +49,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(), @@ -66,6 +72,48 @@ fn setup( }); } +fn rotate_mesh(time: Res