Skip to content

Commit

Permalink
Fix custom pipeline in mesh2d_manual rendering other meshes (bevyengi…
Browse files Browse the repository at this point in the history
…ne#11477)

# Objective

Fixes bevyengine#11476

## Solution

Give the pipeline its own "mesh2d instances hashmap."

Pretty sure this is a good fix, but I am not super familiar with this
code so a rendering expert should take a look.

> your fix in the pull request works brilliantly for me too.
> -- _Discord user who pointed out bug_
  • Loading branch information
rparrett authored Apr 25, 2024
1 parent ade70b3 commit a1adba1
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions examples/2d/mesh2d_manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ use bevy::{
},
sprite::{
extract_mesh2d, DrawMesh2d, Material2dBindGroupId, Mesh2dHandle, Mesh2dPipeline,
Mesh2dPipelineKey, Mesh2dTransforms, MeshFlags, RenderMesh2dInstance,
RenderMesh2dInstances, SetMesh2dBindGroup, SetMesh2dViewBindGroup, WithMesh2d,
Mesh2dPipelineKey, Mesh2dTransforms, MeshFlags, RenderMesh2dInstance, SetMesh2dBindGroup,
SetMesh2dViewBindGroup, WithMesh2d,
},
utils::EntityHashMap,
};
use std::f32::consts::PI;

Expand Down Expand Up @@ -269,6 +270,10 @@ pub struct ColoredMesh2dPlugin;
pub const COLORED_MESH2D_SHADER_HANDLE: Handle<Shader> =
Handle::weak_from_u128(13828845428412094821);

/// Our custom pipeline needs its own instance storage
#[derive(Resource, Deref, DerefMut, Default)]
pub struct RenderColoredMesh2dInstances(EntityHashMap<Entity, RenderMesh2dInstance>);

impl Plugin for ColoredMesh2dPlugin {
fn build(&self, app: &mut App) {
// Load our custom shader
Expand All @@ -283,6 +288,7 @@ impl Plugin for ColoredMesh2dPlugin {
.unwrap()
.add_render_command::<Transparent2d, DrawColoredMesh2d>()
.init_resource::<SpecializedRenderPipelines<ColoredMesh2dPipeline>>()
.init_resource::<RenderColoredMesh2dInstances>()
.add_systems(
ExtractSchedule,
extract_colored_mesh2d.after(extract_mesh2d),
Expand All @@ -307,7 +313,7 @@ pub fn extract_colored_mesh2d(
query: Extract<
Query<(Entity, &ViewVisibility, &GlobalTransform, &Mesh2dHandle), With<ColoredMesh2d>>,
>,
mut render_mesh_instances: ResMut<RenderMesh2dInstances>,
mut render_mesh_instances: ResMut<RenderColoredMesh2dInstances>,
) {
let mut values = Vec::with_capacity(*previous_len);
for (entity, view_visibility, transform, handle) in &query {
Expand Down Expand Up @@ -344,7 +350,7 @@ pub fn queue_colored_mesh2d(
pipeline_cache: Res<PipelineCache>,
msaa: Res<Msaa>,
render_meshes: Res<RenderAssets<GpuMesh>>,
render_mesh_instances: Res<RenderMesh2dInstances>,
render_mesh_instances: Res<RenderColoredMesh2dInstances>,
mut views: Query<(
&VisibleEntities,
&mut SortedRenderPhase<Transparent2d>,
Expand Down

0 comments on commit a1adba1

Please sign in to comment.