Skip to content

Commit 1a85fb5

Browse files
committed
Fix mesh2d_manual example (#4037)
# Objective Fixes #4036 ## Solution - Use `VertexBufferLayout::from_vertex_formats` - Actually put a u32 into `ATTRIBUTE_COLOR` and convert it in the shader I'm not 100% sure about the color stuff. It seems like `ATTRIBUTE_COLOR` has been `Uint32` this whole time, but this example previously worked with `[f32; 4]` somehow, perhaps because the vertex layout was manually specified. Let me know if that can be improved, or feel free to close for an alternative fix.
1 parent 6ce8e50 commit 1a85fb5

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

examples/2d/mesh2d_manual.rs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use bevy::{
1111
BlendState, ColorTargetState, ColorWrites, Face, FragmentState, FrontFace,
1212
MultisampleState, PolygonMode, PrimitiveState, PrimitiveTopology, RenderPipelineCache,
1313
RenderPipelineDescriptor, SpecializedPipeline, SpecializedPipelines, TextureFormat,
14-
VertexAttribute, VertexBufferLayout, VertexFormat, VertexState, VertexStepMode,
14+
VertexBufferLayout, VertexFormat, VertexState, VertexStepMode,
1515
},
1616
texture::BevyDefault,
1717
view::VisibleEntities,
@@ -70,8 +70,8 @@ fn star(
7070
// Set the position attribute
7171
star.insert_attribute(Mesh::ATTRIBUTE_POSITION, v_pos);
7272
// 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]);
7575
star.insert_attribute(Mesh::ATTRIBUTE_COLOR, v_color);
7676

7777
// Now, we specify the indices of the vertex that are going to compose the
@@ -131,24 +131,15 @@ impl SpecializedPipeline for ColoredMesh2dPipeline {
131131
fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
132132
// Customize how to store the meshes' vertex attributes in the vertex buffer
133133
// 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,
143137
// Color
144-
VertexAttribute {
145-
format: VertexFormat::Float32x4,
146-
offset: 0,
147-
shader_location: 1,
148-
},
138+
VertexFormat::Uint32,
149139
];
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);
152143

153144
RenderPipelineDescriptor {
154145
vertex: VertexState {
@@ -157,11 +148,7 @@ impl SpecializedPipeline for ColoredMesh2dPipeline {
157148
entry_point: "vertex".into(),
158149
shader_defs: Vec::new(),
159150
// 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],
165152
},
166153
fragment: Some(FragmentState {
167154
// Use our custom shader
@@ -227,13 +214,13 @@ var<uniform> mesh: Mesh2d;
227214
// The structure of the vertex buffer is as specified in `specialize()`
228215
struct Vertex {
229216
[[location(0)]] position: vec3<f32>;
230-
[[location(1)]] color: vec4<f32>;
217+
[[location(1)]] color: u32;
231218
};
232219
233220
struct VertexOutput {
234221
// The vertex shader must set the on-screen position of the vertex
235222
[[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
237224
[[location(0)]] color: vec4<f32>;
238225
};
239226
@@ -243,7 +230,8 @@ fn vertex(vertex: Vertex) -> VertexOutput {
243230
var out: VertexOutput;
244231
// Project the world position of the mesh into screen position
245232
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;
247235
return out;
248236
}
249237

0 commit comments

Comments
 (0)