How can I select/highlight mesh face under mouse cursor?
Asked Answered
B

4

0

Hello,

I import a mesh into Godot from Blender and I would like to, as I move around the mouse hovering above the mesh, be able to see the triangle (maybe even quad if this is possible) under the mouse cursor highlighted in green.

What are some of the key components to make this work? Is it a combination of raycasting and shaders? Not looking for a complete solution but more a high level overview of the thought process.

Thank you very much!

Bricklaying answered 20/11, 2022 at 10:45 Comment(0)
I
0

There are two solutions I've used in the past:

You can either use MeshDataTool to get information about a mesh, then give the triangles to some functions in the Geometry singleton to find out which one is hovered.

The second approach is more complicated, but more performant: You create a new mesh where every triangle has a different vertex color. Then you put it into a Viewport with the same transform as the real thing. If you sample the pixel where the cursor is, you get the triangle ID.

To actually render the highlight, you could dynamically create a new mesh or give the triangle position / ID to a shader and let it handle the rest.
Some code examples are here, but I think understanding is more future-proof than copy-pasting in this case. I have different selection types in the linked addon. There is also one for quads.

Impressionism answered 20/11, 2022 at 12:44 Comment(0)
S
0

Bricklaying as I move around the mouse hovering above the mesh, be able to see the triangle (maybe even quad if this is possible) under the mouse cursor highlighted in green.

Do you mean in game or in editor? If former, see Impressionism comment above. If in editor then you would likely also need to read up on the documentation for how to develop addons/editor plugins.

Symer answered 20/11, 2022 at 15:32 Comment(0)
B
0

Impressionism Thanks, I really appreciate your taking the time to guide me in the right direction.

Bricklaying answered 20/11, 2022 at 20:9 Comment(0)
B
0

Symer Thanks - Indeed I would like to implement this feature in game so I will follow Jummit's tips.

Bricklaying answered 20/11, 2022 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.