Skip to content

Commit f6ab7c2

Browse files
committed
Add a new vertex attribute to the pipelined custom shader example
1 parent 2daa5c2 commit f6ab7c2

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed

assets/shaders/custom_material.wgsl

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
1+
#import bevy_pbr::mesh_view_bind_group
2+
#import bevy_pbr::mesh_struct
3+
4+
struct Vertex {
5+
[[location(0)]] position: vec3<f32>;
6+
[[location(1)]] normal: vec3<f32>;
7+
[[location(2)]] uv: vec2<f32>;
8+
[[location(3)]] color: vec4<f32>;
9+
};
10+
11+
struct VertexOutput {
12+
[[builtin(position)]] clip_position: vec4<f32>;
13+
[[location(0)]] color: vec4<f32>;
14+
};
15+
16+
[[group(2), binding(0)]]
17+
var<uniform> mesh: Mesh;
18+
19+
[[stage(vertex)]]
20+
fn vertex(vertex: Vertex) -> VertexOutput {
21+
let world_position = mesh.model * vec4<f32>(vertex.position, 1.0);
22+
23+
var out: VertexOutput;
24+
out.clip_position = view.view_proj * world_position;
25+
out.color = vertex.color;
26+
return out;
27+
}
28+
129
[[block]]
230
struct CustomMaterial {
331
color: vec4<f32>;
432
};
533
[[group(1), binding(0)]]
634
var<uniform> material: CustomMaterial;
735

36+
struct FragmentInput {
37+
[[location(0)]] color: vec4<f32>;
38+
};
39+
840
[[stage(fragment)]]
9-
fn fragment() -> [[location(0)]] vec4<f32> {
10-
return material.color;
41+
fn fragment(fragment: FragmentInput) -> [[location(0)]] vec4<f32> {
42+
return (material.color + fragment.color) / 2.0;
1143
}

examples/shader/shader_material.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use bevy::{
2+
core::Time,
23
core_pipeline::Transparent3d,
34
ecs::system::{lifetimeless::*, SystemParamItem},
5+
math::Quat,
46
pbr::{
57
DrawMesh, MeshPipeline, MeshPipelineFlags, MeshPipelineKey, MeshUniform, SetMeshBindGroup,
68
SetMeshViewBindGroup,
@@ -28,6 +30,7 @@ fn main() {
2830
.add_plugins(DefaultPlugins)
2931
.add_plugin(CustomMaterialPlugin)
3032
.add_startup_system(setup)
33+
.add_system(rotate_mesh)
3134
.run();
3235
}
3336

@@ -37,9 +40,13 @@ fn setup(
3740
mut meshes: ResMut<Assets<Mesh>>,
3841
mut materials: ResMut<Assets<CustomMaterial>>,
3942
) {
40-
// cube
43+
// cube with custom vertex attributes
44+
let mut mesh = Mesh::from(shape::Cube { size: 1.0 });
45+
mesh.vertex_layout_mut()
46+
.push(Mesh::ATTRIBUTE_COLOR, VertexFormat::Float32x4);
47+
mesh.set_attribute(Mesh::ATTRIBUTE_COLOR, cube_vertex_colors());
4148
commands.spawn().insert_bundle((
42-
meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
49+
meshes.add(mesh),
4350
Transform::from_xyz(0.0, 0.5, 0.0),
4451
GlobalTransform::default(),
4552
Visibility::default(),
@@ -56,6 +63,48 @@ fn setup(
5663
});
5764
}
5865

66+
fn rotate_mesh(time: Res<Time>, mut query: Query<&mut Transform, With<Handle<Mesh>>>) {
67+
let angle = std::f32::consts::TAU / 12.0;
68+
let mut transform = query.single_mut();
69+
transform.rotate(Quat::from_rotation_x(angle * time.delta_seconds()));
70+
transform.rotate(Quat::from_rotation_y(angle * time.delta_seconds()));
71+
}
72+
73+
fn cube_vertex_colors() -> Vec<[f32; 4]> {
74+
vec![
75+
// Front
76+
[0.0, 0.5, 0.0, 1.0], // Green
77+
[0.5, 0.0, 0.0, 1.0], // Red
78+
[0.0, 0.5, 0.5, 1.0], // Cyan
79+
[0.5, 0.0, 0.5, 1.0], // Magenta
80+
// Back
81+
[0.5, 0.5, 0.0, 1.0], // Yellow
82+
[0.0, 0.0, 0.0, 1.0], // Black
83+
[0.5, 0.5, 0.5, 1.0], // White
84+
[0.0, 0.0, 0.5, 1.0], // Blue
85+
// Right
86+
[0.5, 0.5, 0.5, 1.0], // White
87+
[0.0, 0.0, 0.0, 1.0], // Black
88+
[0.0, 0.5, 0.5, 1.0], // Cyan
89+
[0.5, 0.0, 0.0, 1.0], // Red
90+
// Left
91+
[0.0, 0.5, 0.0, 1.0], // Green
92+
[0.5, 0.0, 0.5, 1.0], // Magenta
93+
[0.5, 0.5, 0.0, 1.0], // Yellow
94+
[0.0, 0.0, 0.5, 1.0], // Blue
95+
// Top
96+
[0.0, 0.0, 0.0, 1.0], // Black
97+
[0.5, 0.5, 0.0, 1.0], // Yellow
98+
[0.5, 0.0, 0.5, 1.0], // Magenta
99+
[0.0, 0.5, 0.5, 1.0], // Cyan
100+
// Bottom
101+
[0.5, 0.0, 0.0, 1.0], // Red
102+
[0.0, 0.5, 0.0, 1.0], // Green
103+
[0.0, 0.0, 0.5, 1.0], // Blue
104+
[0.5, 0.5, 0.5, 1.0], // White
105+
]
106+
}
107+
59108
#[derive(Debug, Clone, TypeUuid)]
60109
#[uuid = "4ee9c363-1124-4113-890e-199d81b00281"]
61110
pub struct CustomMaterial {

0 commit comments

Comments
 (0)