Hi,
New to Unity. Trying to get the list vertices for each object in the camera view. But not getting the correct result. Using bounding check i.e.
Vector3 tmp_screen_point = cam.WorldToViewportPoint(transform.TransformPoint(verts[vert]));
if (tmp_screen_point.z > 0 && (tmp_screen_point.x > 0 && tmp_screen_point.x < 1) && (tmp_screen_point.y > 0 && tmp_screen_point.y < 1))
and Ray cast method (Rays from the Camera). Using Raycast to exclude occluded vertices. Here is my code snippet:
void Start()
{
//Get the list of all active objetcs in the scene
MeshFilter[] object_mesh_list = FindObjectsOfType<MeshFilter>();
Camera cam = GameObject.Find("Main Camera").GetComponent<Camera>();
Vector3 camera_position = cam.transform.position;
Transform obj = transform;
//Debug.Log(camera_position);
RaycastHit ray_hit = new RaycastHit();
for (int i = 0; i < object_mesh_list.Length; i++)
{
Mesh obj_mesh = object_mesh_list*.mesh;*
Vector3[] verts = obj_mesh.vertices;
List in_frame_verts_raycast = new List();
List in_frame_verts_bound = new List();
for (int vert = 0; vert < verts.Length; vert++)
{
//Bounding Method
Vector3 tmp_screen_point = cam.WorldToViewportPoint(transform.TransformPoint(verts[vert]));
if (tmp_screen_point.z > 0 && (tmp_screen_point.x > 0 && tmp_screen_point.x < 1) && (tmp_screen_point.y > 0 && tmp_screen_point.y < 1))
{
in_frame_verts_bound.Add(verts[vert]);
}
//Camrea Ray cast method
Ray ray = cam.ViewportPointToRay(cam.WorldToViewportPoint(transform.TransformPoint(verts[vert])));
if (Physics.Raycast(ray, out ray_hit))
{
in_frame_verts_raycast.Add(verts[vert]);
}
}
Debug.Log(“Raycast Method = Gameobject " + object_mesh_list*.gameObject.name + " has total of: " + verts.Length.ToString() + " vertice and " + in_frame_verts_raycast.Count.ToString() + " are visible to camera”);
Debug.Log("Bound Method = Gameobject " + object_mesh_list.gameObject.name + " has total of: " + verts.Length.ToString() + " vertice and " + in_frame_verts_bound.Count.ToString() + " are visible to camera");*
}
}
and following screen shots show the output. Ray-cast method seems to working but not correct. Only 3 vertices of the quad are visible but it says all 4 are visible.
Imag1:
[157220-1-1.png|157220]
Now rotate camera and resits no vertices are visible.
[157221-2-1.jpg*|157221]*
–Thanks
any suggestion or pointer to fix the issue.
*
*