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/custom_shader_pipelined.rs b/examples/shader/custom_shader_pipelined.rs index ce2ca1a908d718..e63b4dd587f91f 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, MeshPipeline, MeshPipelineFlags, MeshPipelineKey, MeshUniform, SetMeshBindGroup, SetMeshViewBindGroup, @@ -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