- Home /
Visibility of objects
Hello. How to define visibility of object. For example as on a screen the cube behind a green wall has to be defined as invisible. Something such is used in the Occlusion Culling technology.
Your question is confusing. If your camera is placed where the green arrow is, the cube would be invisible, because there's a green wall in the way...?
Just to add to @tanoshimi's answer.... And you won't see the cube get's invisible in the editor, because Occlusion Culling works on the actual build of the game. So even if you do see the cube in the Editor, when you play, it will be invisible when you build the game and the camera is at that position. So you don't have to hide it manually...
@tanoshimi The camera is located where the cut-off cone. The camera looks at a green wall. She sees a green wall, but doesn't see a white cube as it is blocked by a green wall. I need a script, whether which would define the object is covered with other object. At once I will tell: Raycast not an exit as objects have no colliders.
Answer by tanoshimi · Jun 05, 2015 at 07:41 AM
With the information in your further comments, it sounds like you want OnWillRenderObject: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnWillRenderObject.html
Call that function in a script attached to the cube and it will tell you which cameras will render it. If the function doesn't get called for a particular camera, the object is "invisible" to that camera.
Unfortunately not, this function is also caused even when one object of overlap by another. The same problems at the OnBecameVisible() function.
@tanoshimi Unfortunately not, this function is also caused even when one object of overlap by another. The same problems at the OnBecameVisible() function.
(please don't post comments as answers).Ok, it sounds like you'll need to add colliders and use regular raycasting then.
Give them colliders and set them to IsTrigger if you don't want them to block physics-based objects, such as rigidbodies. Trigger colliders will still register RayCasts.
Answer by AmbroiseRabier · Mar 04, 2018 at 01:38 PM
tanoshimi answer helped me. You cannot made it with OnBecameVisible and OnBecameInvisible, because they get called only when the objects change visibility, that they are many or one camera having the object in view do not change anything. OnWillRenderObject are only called on gameobject having a renderer attached. (childrens do not counts). I tested it with occlusion culling (don't forget you have to bake it in Unity in the occlusion culling panel) and it work. In case you are moving the MeshRenderer you have a checkbox 'Dynamic Occluded' that should be setted to true (only beginning from unity 2017.2, on version bellow it is by default true).
This bellow is WIP, but should help understanding:
private List<Camera> cameraSeeingMe;
private Array<Camera> cameraSeeingMeBuffer;
private void OnWillRenderObject() {
// OnWillRenderObject is called even when editor pause, better avoid duplicate
if (!isRenderingMe.Exists((e)=> e == Camera.current))
cameraSeeingMe.Add(Camera.current);
}
private void Update() {
cameraSeeingMeBuffer = cameraSeeingMe.ToArray();
cameraSeeingMe.Clear()
}
public bool isSeenBy(Camera cam) {
foreach (var otherCam in cameraSeeingMe) {
if (cam == otherCam ) {
return true;
}
}
return false;
}
You can add a gizmo to see what happen. We use the buffer because OnDrawGizmos get called immediately after OnWillRenderObject, and not after Update. (without buffer some gizmos won't be draw on some camera)
private void OnDrawGizmos() {
Gizmos.color = Color.yellow;
foreach (var cam in cameraSeeingMeBuffer) {
Gizmos.DrawLine(transform.position, cam.transform.position);
}
}
[Edit]: Few corrections after more testing.
Answer by knudsjef · Jun 08, 2015 at 07:40 AM
You could make a bool called Visible or something and then use a raycast and see if anything is in the way. If it is, Visible = false; Else Visible = true;
Your answer
Follow this Question
Related Questions
I want to put main camera floating 0 Answers
Animated camera problems 0 Answers
WorldToScreenPoint 0 Answers
Please Help me converting this to C# 3 Answers
Natural camera sway 1 Answer