Skip to content

Commit 1911ed7

Browse files
committed
Add a new vertex attribute to the pipelined custom shader example
1 parent bafe868 commit 1911ed7

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

assets/shaders/custom.wgsl

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ struct Vertex {
1818
[[location(0)]] position: vec3<f32>;
1919
[[location(1)]] normal: vec3<f32>;
2020
[[location(2)]] uv: vec2<f32>;
21+
[[location(3)]] color: vec4<f32>;
2122
};
2223

2324
struct VertexOutput {
2425
[[builtin(position)]] clip_position: vec4<f32>;
26+
[[location(0)]] color: vec4<f32>;
2527
};
2628

2729
[[stage(vertex)]]
@@ -30,6 +32,7 @@ fn vertex(vertex: Vertex) -> VertexOutput {
3032

3133
var out: VertexOutput;
3234
out.clip_position = view.view_proj * world_position;
35+
out.color = vertex.color;
3336
return out;
3437
}
3538

@@ -40,7 +43,11 @@ struct CustomMaterial {
4043
[[group(2), binding(0)]]
4144
var<uniform> material: CustomMaterial;
4245

46+
struct FragmentInput {
47+
[[location(0)]] color: vec4<f32>;
48+
};
49+
4350
[[stage(fragment)]]
44-
fn fragment() -> [[location(0)]] vec4<f32> {
45-
return material.color;
51+
fn fragment(fragment: FragmentInput) -> [[location(0)]] vec4<f32> {
52+
return (material.color + fragment.color) / 2.0;
4653
}

examples/shader/custom_shader_pipelined.rs

+51-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use bevy::{
2+
core::Time,
23
core_pipeline::Transparent3d,
34
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
45
ecs::{
56
prelude::*,
67
system::{lifetimeless::*, SystemParamItem},
78
},
8-
math::{Vec3, Vec4},
9+
math::{Quat, Vec3, Vec4},
910
pbr2::{
1011
DrawMesh, MeshUniform, PbrPipeline, PbrPipelineKey, SetMeshViewBindGroup,
1112
SetTransformBindGroup,
@@ -38,6 +39,7 @@ fn main() {
3839
.add_plugin(LogDiagnosticsPlugin::default())
3940
.add_plugin(CustomMaterialPlugin)
4041
.add_startup_system(setup)
42+
.add_system(rotate_mesh)
4143
.run();
4244
}
4345

@@ -47,9 +49,13 @@ fn setup(
4749
mut meshes: ResMut<Assets<Mesh>>,
4850
mut materials: ResMut<Assets<CustomMaterial>>,
4951
) {
50-
// cube
52+
// cube with custom vertex attributes
53+
let mut mesh = Mesh::from(shape::Cube { size: 1.0 });
54+
mesh.vertex_layout_mut()
55+
.push(Mesh::ATTRIBUTE_COLOR, VertexFormat::Float32x4);
56+
mesh.set_attribute(Mesh::ATTRIBUTE_COLOR, cube_vertex_colors());
5157
commands.spawn().insert_bundle((
52-
meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
58+
meshes.add(mesh),
5359
Transform::from_xyz(0.0, 0.5, 0.0),
5460
GlobalTransform::default(),
5561
Visibility::default(),
@@ -66,6 +72,48 @@ fn setup(
6672
});
6773
}
6874

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

0 commit comments

Comments
 (0)