aevyrie / bevy_mod_raycast

A little mesh raycasting plugin for Bevy
https://crates.io/crates/bevy_mod_raycast
MIT License
320 stars 93 forks source link

Unable to get indices of raycast hit on indexed mesh where multiple triangles start on same index. #113

Open connorgmeehan opened 4 months ago

connorgmeehan commented 4 months ago

Description

When raycasting against and indexed mesh I am unable to differentiate between two vertices when both vertices start at the same index. I'm trying to use triangle_index to lookup some attribute data but it looks like, for indexed meshes, triangle_index contains the index after its already been mapped meaning I can't derive the index of the other verts in the tri.

Extra, problem specific, details in case the context is helpful.

I'm trying to get some attribute data from a mesh that I'm raycasting against. I was hoping to use the `triangle_index` field to derive the indices in the attribute I needed to lookup. I'm having an issue where `triangle_index` is not changing, despite raycasting different triangles. You can see in the attached images I am hitting two different triangles (see the value of `intersection_data.triangle[1]`) but the `triangle_index` field remains the same between both tris. The second log is my (incorrectly) derived indices to lookup the attribute with. The final log is my attempt at calculating the position attribute at the hit location using my (incorrectly) derived indices, the position attribute buffer and the barycentric coordinates. It should be equal to the #### Hit 1 (Top Left): ![image](https://github.com/aevyrie/bevy_mod_raycast/assets/7402063/1e8af801-21af-4e9a-9804-d7659acd97ae) #### Hit 2 (Bottom Right): ![image](https://github.com/aevyrie/bevy_mod_raycast/assets/7402063/120323d8-aa6b-4ee9-82d9-3076ebb9af17)

Solution?

Not 100% certain but it seems like the solution is we'll need to return all 3 triangle indices as [usize; 3], growing the struct by 2 usize. I'm happy to PR if this is suitable but I worry I might be missing something as well. Can you let me know your thoughts? And thank you for maintaining this great library :)

connorgmeehan commented 4 months ago

https://github.com/aevyrie/bevy_mod_raycast/pull/103#issuecomment-1975386832

I found this PR + proposed solution but it's not working for my mesh where both vertices start with the same index. If I use mesh.duplicate_vertices() I can get attribute data but it'd be nice if I didn't have to.

Here's the index field for reference, the mesh is a simple rectangle generated through lyon_tesselation so changing the way meshes generate is a much bigger task.

values: Float32x3([[100.0, -25.0, 0.0], [100.0, 25.0, 0.0], [0.0, -25.0, 0.0], [0.0, 25.0, 0.0]])
indices: Some(U32([3, 2, 0, 3, 0, 1]))

Let me know thoughts on solution and if you want me to PR :)

brookman commented 2 months ago

Hi @connorgmeehan

Maybe I'm just too dense but I'm still struggling with the same issue. I have never found out how to access the other two vertex indices in an indexed mesh. The field is called triangle_index but in fact only contains the first vertex_index and there seems to be no information about which triangle the vertex belongs to (could be multiple). I'm still using my fork and have updated to Bevy 0.14 now. In case you want to use it:

bevy_mod_raycast = { git = "https://github.com/brookman/bevy_mod_raycast", rev = "2be456f65c0737ce41b43b4756f0c05f4a1856a0" }