Skip to content

Commit 329db5d

Browse files
committed
Fix mesh2d_manual example
1 parent fb02b84 commit 329db5d

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

examples/2d/mesh2d_manual.rs

Lines changed: 13 additions & 26 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![bytemuck::cast([0 as u8, 0 as u8, 0 as u8, 255 as u8])];
74+
v_color.extend_from_slice(&[bytemuck::cast([255 as u8, 255 as u8, 0 as u8, 255 as u8]); 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,7 +214,7 @@ 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 {
@@ -243,7 +230,7 @@ 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+
out.color = vec4<f32>((vec4<u32>(vertex.color) >> vec4<u32>(0u, 8u, 16u, 24u)) & vec4<u32>(255u)) / 255.0;
247234
return out;
248235
}
249236

0 commit comments

Comments
 (0)