- Home /
How do you determine if an object is visible to the camera?
I am making a run-time level editor.
One of the features is selecting objects in a region by clicking and dragging.
There are several tutorials on this, but none of them take into account if the object is visible or not.
I want it to work like the selection does in Blender. All of the visible objects are selected, but not ones that are occluded:
My idea was to iterate through all of the objects in the selection and only select the ones visible to the camera. But I don't know how to do this.
I can't use ray-casting because I would need a ray for every pixel on the screen, and that would cause some lag.
And I can't use Renderer.isVisible or OnBecameVisible() because they don't check for occlusion.
Is there some way to check if an object is being rendered onscreen?
Answer by Pakillottk · Aug 05, 2019 at 08:44 PM
Hi. A simple, and very common, way to solve this is by implementing Color Selection (http://www.opengl-tutorial.org/miscellaneous/clicking-on-objects/picking-with-an-opengl-hack/). Instead of programmatically calculate ray-intersections, distances and so on... you simply render the scene twice: one to the screen, the other to a texture where each selectable object it's rendered with an unique color. So then you can make a lookup of that texture and recover the ID/s of the visible object/s that match your selection boundaries.
That's a cool solution!
But I'm a bit worried since the site you linked advises against this method due to the performance hit.
Rendering the frame takes time, and the ReadPixels function used to get the color information is slow too.
In the example, the object-identification frame is rendered every time the mouse is clicked.
So maybe I can optimize this by rendering the frame and reading the pixels only once (after each time the user finishes moving the camera or an object).
This would also allow the process to be run asynchronously and hide any delay.
Since it doesn't look like its possible to see the z-buffer of the camera, I'll try this out and report back.
P.S. would native plugins be useful for this at all?
Sure rendering the scene twice hurt performance but as you say, you can only render that way whenever you need to make a selection and only for a single frame. Unless you are handling really complex scenes (in which case the performance would be bad anyways) it shouldn't be noticeable. You can find some frame breakdowns of popular modern games (i.e. Resident Evil 2 Remake) and you'll see how many times the scene is renderer into different framebuffers behind the scenes. In unity, I suggest that you make a second camera, with a RenderTexture as output, and attach a script that only enables it when other script order it (something like: ColorSelectCamera.RenderInSelection$$anonymous$$ode()) and then once the frame is fully rendered the camera turns off again (therefore, the performance hit will be $$anonymous$$imal). After that, you can read from the RenderTexture to make the real selection. The way to render the objects could be using ShaderReplacement when rendering with the second camera. The shader would simply outputs the object's color id.