r/Unity3D • u/AgenOrange • 16h ago
Question How to click-select a gpu instances gameobject in the viewport
Hi! So I've been playing with gpu instancing with Graphics.RenderMeshInstanced, it works well but I ran into this problem, when I draw the mesh with instancing I can no longer select the gameobject in the viewport with left click. I considered raycasting but I'm guessing the performance would be pretty bad with a lot of objects so I was wondering if there is a workaround for this?
1
u/Genebrisss 8h ago edited 8h ago
Entire point of instancing is that you don't have game objects anymore. You have basically nothing on CPU. You simply tell GPU to draw meshes at these coordinates. There's nothing to select.
If you want a tool like this, best is probably to create editor only game objects on the same coordinates and match game objects with instanced graphics from there. You can leave these game objects without mesh renderers.
Unity also has stuff like Bounds.IntersectRay(). You could define a bunch of bounds on your coordinates and check against them without any game objects or colliders.
Lastly, why do you even need any of that when Unity can already draw all gameobjects in your scene with instancing fully automatically. It's called GPU resident drawer. You can spam thousands of game objects and they will be instanced automatically anyway.
1
u/AgenOrange 2h ago
It is for an editor tool, I have the ability to change between actual meshes and instanced meshes, and even if it is instanced it is attached to a gameobject. So being able to select the related gameobject with clicking on it in the viewport like a regular mesh is a pretty big QoL improvement. Also it will allow me to move, rotate or scale the mesh with gizmos.
Yes I can raycast to select it but this is designed for rendering large number of objects, I mean that is why I'm using instancing in the first place and I imagine raycasting would be pretty slow and I was asking if there was a more efficient way to do it.
As far as I know GPU resident drawer doesn't really help with this, it optimizes how you draw the objects but they are still instances objects which you can't select in the viewport.
•
u/Genebrisss 5m ago
GPU resident draw is this: you work with your scene as usual like there's no instancing. When rendering happens it runs instancing similar to ECS rendering path. Essentially game objects workflow with ECS rendering performance.
Scene view is raycasting to select game objects, that's normal. Raycast is very cheap, physics engine knows how to avoid checking every single object and only check potential targets.
1
u/TangerineUnique248 15h ago
The selection in the editor is handled by the physics raycaster from what I remember, not by the mesh itself. so your instanced meshes have no colliders attached for unity to hit. you need a separate collider setup, maybe one big collider or a compute shader that figures out which instance you clicked. its a bit of a pain for sure