Skip to content

Commit

Permalink
Fix mesh2d_manual example
Browse files Browse the repository at this point in the history
  • Loading branch information
rparrett authored and omarbassam88 committed Feb 26, 2022
1 parent b697e73 commit 13f02ca
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions examples/2d/mesh2d_manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bevy::{
BlendState, ColorTargetState, ColorWrites, Face, FragmentState, FrontFace,
MultisampleState, PolygonMode, PrimitiveState, PrimitiveTopology, RenderPipelineCache,
RenderPipelineDescriptor, SpecializedPipeline, SpecializedPipelines, TextureFormat,
VertexAttribute, VertexBufferLayout, VertexFormat, VertexState, VertexStepMode,
VertexBufferLayout, VertexFormat, VertexState, VertexStepMode,
},
texture::BevyDefault,
view::VisibleEntities,
Expand Down Expand Up @@ -70,8 +70,8 @@ fn star(
// Set the position attribute
star.insert_attribute(Mesh::ATTRIBUTE_POSITION, v_pos);
// And a RGB color attribute as well
let mut v_color = vec![[0.0, 0.0, 0.0, 1.0]];
v_color.extend_from_slice(&[[1.0, 1.0, 0.0, 1.0]; 10]);
let mut v_color: Vec<u32> = vec![bytemuck::cast([0 as u8, 0 as u8, 0 as u8, 255 as u8])];
v_color.extend_from_slice(&[bytemuck::cast([255 as u8, 255 as u8, 0 as u8, 255 as u8]); 10]);
star.insert_attribute(Mesh::ATTRIBUTE_COLOR, v_color);

// Now, we specify the indices of the vertex that are going to compose the
Expand Down Expand Up @@ -131,24 +131,15 @@ impl SpecializedPipeline for ColoredMesh2dPipeline {
fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
// Customize how to store the meshes' vertex attributes in the vertex buffer
// Our meshes only have position and color
let vertex_attributes = vec![
// Position (GOTCHA! Vertex_Position isn't first in the buffer due to how Mesh sorts attributes (alphabetically))
VertexAttribute {
format: VertexFormat::Float32x3,
// this offset is the size of the color attribute, which is stored first
offset: 16,
// position is available at location 0 in the shader
shader_location: 0,
},
let formats = vec![
// Position
VertexFormat::Float32x3,
// Color
VertexAttribute {
format: VertexFormat::Float32x4,
offset: 0,
shader_location: 1,
},
VertexFormat::Uint32,
];
// This is the sum of the size of position and color attributes (12 + 16 = 28)
let vertex_array_stride = 28;

let vertex_layout =
VertexBufferLayout::from_vertex_formats(VertexStepMode::Vertex, formats);

RenderPipelineDescriptor {
vertex: VertexState {
Expand All @@ -157,11 +148,7 @@ impl SpecializedPipeline for ColoredMesh2dPipeline {
entry_point: "vertex".into(),
shader_defs: Vec::new(),
// Use our custom vertex buffer
buffers: vec![VertexBufferLayout {
array_stride: vertex_array_stride,
step_mode: VertexStepMode::Vertex,
attributes: vertex_attributes,
}],
buffers: vec![vertex_layout],
},
fragment: Some(FragmentState {
// Use our custom shader
Expand Down Expand Up @@ -227,7 +214,7 @@ var<uniform> mesh: Mesh2d;
// The structure of the vertex buffer is as specified in `specialize()`
struct Vertex {
[[location(0)]] position: vec3<f32>;
[[location(1)]] color: vec4<f32>;
[[location(1)]] color: u32;
};
struct VertexOutput {
Expand All @@ -243,7 +230,7 @@ fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
// Project the world position of the mesh into screen position
out.clip_position = view.view_proj * mesh.model * vec4<f32>(vertex.position, 1.0);
out.color = vertex.color;
out.color = vec4<f32>((vec4<u32>(vertex.color) >> vec4<u32>(0u, 8u, 16u, 24u)) & vec4<u32>(255u)) / 255.0;
return out;
}
Expand Down

0 comments on commit 13f02ca

Please sign in to comment.