@@ -11,7 +11,7 @@ use bevy::{
11
11
BlendState , ColorTargetState , ColorWrites , Face , FragmentState , FrontFace ,
12
12
MultisampleState , PolygonMode , PrimitiveState , PrimitiveTopology , RenderPipelineCache ,
13
13
RenderPipelineDescriptor , SpecializedPipeline , SpecializedPipelines , TextureFormat ,
14
- VertexAttribute , VertexBufferLayout , VertexFormat , VertexState , VertexStepMode ,
14
+ VertexBufferLayout , VertexFormat , VertexState , VertexStepMode ,
15
15
} ,
16
16
texture:: BevyDefault ,
17
17
view:: VisibleEntities ,
@@ -70,8 +70,8 @@ fn star(
70
70
// Set the position attribute
71
71
star. insert_attribute ( Mesh :: ATTRIBUTE_POSITION , v_pos) ;
72
72
// And a RGB color attribute as well
73
- let mut v_color = vec ! [ [ 0.0 , 0.0 , 0.0 , 1.0 ] ] ;
74
- v_color. extend_from_slice ( & [ [ 1.0 , 1.0 , 0.0 , 1.0 ] ; 10 ] ) ;
73
+ let mut v_color: Vec < u32 > = vec ! [ Color :: BLACK . as_linear_rgba_u32 ( ) ] ;
74
+ v_color. extend_from_slice ( & [ Color :: YELLOW . as_linear_rgba_u32 ( ) ; 10 ] ) ;
75
75
star. insert_attribute ( Mesh :: ATTRIBUTE_COLOR , v_color) ;
76
76
77
77
// Now, we specify the indices of the vertex that are going to compose the
@@ -131,24 +131,15 @@ impl SpecializedPipeline for ColoredMesh2dPipeline {
131
131
fn specialize ( & self , key : Self :: Key ) -> RenderPipelineDescriptor {
132
132
// Customize how to store the meshes' vertex attributes in the vertex buffer
133
133
// Our meshes only have position and color
134
- let vertex_attributes = vec ! [
135
- // Position (GOTCHA! Vertex_Position isn't first in the buffer due to how Mesh sorts attributes (alphabetically))
136
- VertexAttribute {
137
- format: VertexFormat :: Float32x3 ,
138
- // this offset is the size of the color attribute, which is stored first
139
- offset: 16 ,
140
- // position is available at location 0 in the shader
141
- shader_location: 0 ,
142
- } ,
134
+ let formats = vec ! [
135
+ // Position
136
+ VertexFormat :: Float32x3 ,
143
137
// Color
144
- VertexAttribute {
145
- format: VertexFormat :: Float32x4 ,
146
- offset: 0 ,
147
- shader_location: 1 ,
148
- } ,
138
+ VertexFormat :: Uint32 ,
149
139
] ;
150
- // This is the sum of the size of position and color attributes (12 + 16 = 28)
151
- let vertex_array_stride = 28 ;
140
+
141
+ let vertex_layout =
142
+ VertexBufferLayout :: from_vertex_formats ( VertexStepMode :: Vertex , formats) ;
152
143
153
144
RenderPipelineDescriptor {
154
145
vertex : VertexState {
@@ -157,11 +148,7 @@ impl SpecializedPipeline for ColoredMesh2dPipeline {
157
148
entry_point : "vertex" . into ( ) ,
158
149
shader_defs : Vec :: new ( ) ,
159
150
// Use our custom vertex buffer
160
- buffers : vec ! [ VertexBufferLayout {
161
- array_stride: vertex_array_stride,
162
- step_mode: VertexStepMode :: Vertex ,
163
- attributes: vertex_attributes,
164
- } ] ,
151
+ buffers : vec ! [ vertex_layout] ,
165
152
} ,
166
153
fragment : Some ( FragmentState {
167
154
// Use our custom shader
@@ -227,13 +214,13 @@ var<uniform> mesh: Mesh2d;
227
214
// The structure of the vertex buffer is as specified in `specialize()`
228
215
struct Vertex {
229
216
[[location(0)]] position: vec3<f32>;
230
- [[location(1)]] color: vec4<f32> ;
217
+ [[location(1)]] color: u32 ;
231
218
};
232
219
233
220
struct VertexOutput {
234
221
// The vertex shader must set the on-screen position of the vertex
235
222
[[builtin(position)]] clip_position: vec4<f32>;
236
- // We pass the vertex color to the framgent shader in location 0
223
+ // We pass the vertex color to the fragment shader in location 0
237
224
[[location(0)]] color: vec4<f32>;
238
225
};
239
226
@@ -243,7 +230,8 @@ fn vertex(vertex: Vertex) -> VertexOutput {
243
230
var out: VertexOutput;
244
231
// Project the world position of the mesh into screen position
245
232
out.clip_position = view.view_proj * mesh.model * vec4<f32>(vertex.position, 1.0);
246
- out.color = vertex.color;
233
+ // Unpack the `u32` from the vertex buffer into the `vec4<f32>` used by the fragment shader
234
+ out.color = vec4<f32>((vec4<u32>(vertex.color) >> vec4<u32>(0u, 8u, 16u, 24u)) & vec4<u32>(255u)) / 255.0;
247
235
return out;
248
236
}
249
237
0 commit comments