Skip to content

Commit d58274d

Browse files
committed
Add a new vertex attribute to the pipelined custom shader example
1 parent ef32b02 commit d58274d

File tree

2 files changed

+85
-5
lines changed

2 files changed

+85
-5
lines changed

assets/shaders/custom_material.wgsl

+34-2
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/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, MeshPipeline, MeshPipelineKey, MeshUniform, SetMeshBindGroup,
1112
SetMeshViewBindGroup,
@@ -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)