aevyrie / bevy_mod_picking

Picking and pointer events for Bevy.
https://crates.io/crates/bevy_mod_picking
Apache License 2.0
784 stars 172 forks source link

Click handlers not firing on mesh #344

Closed mahulst closed 3 months ago

mahulst commented 3 months ago

Issue already resolved, only filing it so others are aware of it if they encounter the same problem.

I added a click handler to a mesh that never fired:

commands
  .spawn((
      MaterialMesh2dBundle {
          mesh: mesh_handle.clone().into(),
          ..default()
      },
      On::<Pointer<Click>>::run(click_handler),
  ));

Turns out the mesh created from code and before picking I only needed it to exist while rendering:

    Mesh::new(
        PrimitiveTopology::TriangleList,
        RenderAssetUsages::RENDER_WORLD,
    )

Picking does need it in the MAIN_WORLD though, so it should be

    Mesh::new(
        PrimitiveTopology::TriangleList,
        RenderAssetUsages::all(),
    )
aevyrie commented 3 months ago

To add more context, picking doesn't need this, the mod_raycast backend for picking does. If you are using a picking shader as the backend, you would not need the mesh to stay loaded on the CPU.

mattlennon3 commented 3 months ago

Thankyou @mahulst for posting here. I have been losing my mind as to why my click event wasn't firing! 🙏 👏

I was using RaycastPickable as part of my bundle, I am just coming back to this project from a few months prior and bumped versions etc. I'm not sure why I had it originally.

I've removed it now and things work as expected!

I appreciate the answer too Aevyrie, although I'm not 100% clear. I am not sure what I lose by removing the RaycastPickable.

Also I was confused how I was supposed to add the RenderAssetUsages::all() to my mesh bundle.

e.g:

// Where does this fit in?
    let mesh = Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::all());

  // This was the previous bundle:
    let bundle = MaterialMesh2dBundle {
        mesh: meshes.add(Circle::new(radius)).into(),
        material: materials.add(ColorMaterial {
            color: colour.into(),
            texture: None,
        }),
        transform: *transform,
        ..default()
    }

Not that I need to now I've removed the RaycastPickable!

aevyrie commented 3 months ago

although I'm not 100% clear. I am not sure what I lose by removing the RaycastPickable.

This is documented:

https://docs.rs/bevy_picking_raycast/0.20.0/bevy_picking_raycast/struct.RaycastPickable.html

You likely inherited this from an old version of the plugin, and did not remove it when it became optional.

Also I was confused how I was supposed to add the RenderAssetUsages::all() to my mesh bundle.

You don't need to worry about this, by default, bevy sets to all. https://docs.rs/bevy/latest/bevy/render/render_asset/struct.RenderAssetUsages.html#method.default

This issue only applies if you set render usages to render world only.