Skip to content

Commit 16e219f

Browse files
mockersfameknite
authored andcommitted
fix example mesh2d_manual (bevyengine#9941)
# Objective - After bevyengine#9903, example `mesh2d_manual` doesn't render anything ## Solution - Fix the example using the new `RenderMesh2dInstances`
1 parent 4fdb382 commit 16e219f

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

crates/bevy_sprite/src/mesh2d/mesh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl From<&Mesh2dTransforms> for Mesh2dUniform {
178178
// NOTE: These must match the bit flags in bevy_sprite/src/mesh2d/mesh2d.wgsl!
179179
bitflags::bitflags! {
180180
#[repr(transparent)]
181-
struct MeshFlags: u32 {
181+
pub struct MeshFlags: u32 {
182182
const NONE = 0;
183183
const UNINITIALIZED = 0xFFFF;
184184
}

examples/2d/mesh2d_manual.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! pipeline for 2d meshes.
33
//! It doesn't use the [`Material2d`] abstraction, but changes the vertex buffer to include vertex color.
44
//! Check out the "mesh2d" example for simpler / higher level 2d meshes.
5+
#![allow(clippy::type_complexity)]
56

67
use bevy::{
78
core_pipeline::core_2d::Transparent2d,
@@ -21,8 +22,9 @@ use bevy::{
2122
Extract, Render, RenderApp, RenderSet,
2223
},
2324
sprite::{
24-
DrawMesh2d, Mesh2dHandle, Mesh2dPipeline, Mesh2dPipelineKey, Mesh2dTransforms,
25-
SetMesh2dBindGroup, SetMesh2dViewBindGroup,
25+
extract_mesh2d, DrawMesh2d, Material2dBindGroupId, Mesh2dHandle, Mesh2dPipeline,
26+
Mesh2dPipelineKey, Mesh2dTransforms, MeshFlags, RenderMesh2dInstance,
27+
RenderMesh2dInstances, SetMesh2dBindGroup, SetMesh2dViewBindGroup,
2628
},
2729
utils::FloatOrd,
2830
};
@@ -280,7 +282,10 @@ impl Plugin for ColoredMesh2dPlugin {
280282
.unwrap()
281283
.add_render_command::<Transparent2d, DrawColoredMesh2d>()
282284
.init_resource::<SpecializedRenderPipelines<ColoredMesh2dPipeline>>()
283-
.add_systems(ExtractSchedule, extract_colored_mesh2d)
285+
.add_systems(
286+
ExtractSchedule,
287+
extract_colored_mesh2d.after(extract_mesh2d),
288+
)
284289
.add_systems(Render, queue_colored_mesh2d.in_set(RenderSet::QueueMeshes));
285290
}
286291

@@ -298,14 +303,32 @@ pub fn extract_colored_mesh2d(
298303
mut previous_len: Local<usize>,
299304
// When extracting, you must use `Extract` to mark the `SystemParam`s
300305
// which should be taken from the main world.
301-
query: Extract<Query<(Entity, &ViewVisibility), With<ColoredMesh2d>>>,
306+
query: Extract<
307+
Query<(Entity, &ViewVisibility, &GlobalTransform, &Mesh2dHandle), With<ColoredMesh2d>>,
308+
>,
309+
mut render_mesh_instances: ResMut<RenderMesh2dInstances>,
302310
) {
303311
let mut values = Vec::with_capacity(*previous_len);
304-
for (entity, view_visibility) in &query {
312+
for (entity, view_visibility, transform, handle) in &query {
305313
if !view_visibility.get() {
306314
continue;
307315
}
316+
317+
let transforms = Mesh2dTransforms {
318+
transform: (&transform.affine()).into(),
319+
flags: MeshFlags::empty().bits(),
320+
};
321+
308322
values.push((entity, ColoredMesh2d));
323+
render_mesh_instances.insert(
324+
entity,
325+
RenderMesh2dInstance {
326+
mesh_asset_id: handle.0.id(),
327+
transforms,
328+
material_bind_group_id: Material2dBindGroupId::default(),
329+
automatic_batching: false,
330+
},
331+
);
309332
}
310333
*previous_len = values.len();
311334
commands.insert_or_spawn_batch(values);
@@ -320,14 +343,14 @@ pub fn queue_colored_mesh2d(
320343
pipeline_cache: Res<PipelineCache>,
321344
msaa: Res<Msaa>,
322345
render_meshes: Res<RenderAssets<Mesh>>,
323-
colored_mesh2d: Query<(&Mesh2dHandle, &Mesh2dTransforms), With<ColoredMesh2d>>,
346+
render_mesh_instances: Res<RenderMesh2dInstances>,
324347
mut views: Query<(
325348
&VisibleEntities,
326349
&mut RenderPhase<Transparent2d>,
327350
&ExtractedView,
328351
)>,
329352
) {
330-
if colored_mesh2d.is_empty() {
353+
if render_mesh_instances.is_empty() {
331354
return;
332355
}
333356
// Iterate each view (a camera is a view)
@@ -339,10 +362,12 @@ pub fn queue_colored_mesh2d(
339362

340363
// Queue all entities visible to that view
341364
for visible_entity in &visible_entities.entities {
342-
if let Ok((mesh2d_handle, mesh2d_transforms)) = colored_mesh2d.get(*visible_entity) {
365+
if let Some(mesh_instance) = render_mesh_instances.get(visible_entity) {
366+
let mesh2d_handle = mesh_instance.mesh_asset_id;
367+
let mesh2d_transforms = &mesh_instance.transforms;
343368
// Get our specialized pipeline
344369
let mut mesh2d_key = mesh_key;
345-
if let Some(mesh) = render_meshes.get(&mesh2d_handle.0) {
370+
if let Some(mesh) = render_meshes.get(mesh2d_handle) {
346371
mesh2d_key |=
347372
Mesh2dPipelineKey::from_primitive_topology(mesh.primitive_topology);
348373
}

0 commit comments

Comments
 (0)