I whant to make ui elements for some 3d objects. Its ui elements must hide if special 3d objects not visible in camera. Screenshot by Lightshot.
I mad ui elements in canvas. But I dont`t know how detect some 3d objects visible or not in camera
I don’t have experience in this, but these might help:
https://docs.unity3d.com/ScriptReference/Renderer.OnBecameInvisible.html
https://docs.unity3d.com/ScriptReference/Renderer.OnBecameVisible.html
It`s help in detect visible gameobjects by camera, but not detect is gameobject behind come other gameobjects
Maybe if you raycast from camera towards object with UI:
https://docs.unity3d.com/Manual/CameraRays.html
And then you compare object wih UI and object the ray hit. If they aren’t the same, you hide UI.
Raycast from camera to the object, and if the ray hits something before hitting the object, assume the object is not visible.
Thank you. But it`s work if objects full obstructed other objects. If big object and befor its small objects raycast may return small objecst
If it’s getting that complicated, why not make UI a part of 3d scene by moving canvas to worldspace?
Now when object is obstructed by a bigger object entirely, UI will also not be visible. You will, however, have to update UI’s Z position to object’s distance to camera on every frame if you have a moving camera (or object).
Edit:
And today I learned neginfinity has blocked me =D
Uh, fire multiple raycasts to cover object’s visible surface in a grid, and if they all hit something else, assume the object is not visible.
–edit–
It is possible to detect visibility with pixel-perfect precision, of course, but it will involve using compute shaders and for most practical purposes is an overkill.
It’s old-school, but one method is to render a colour-keyed version of the scene to a texture, then use that texture to decide what’s visible.