How to calculate screen area coverage of a 3D object

friends i want to know what % of the screen does the object occupy?
it’s 3d object.
help me.

For a true pixel percentage of an irregular shaped object, you can render the object to a texture against some know background color that would not be in the object. Rendering to a texture can be done in Pro using a RenderTexture. For Unity Free, you wold have to render only the object and a background for a single frame and use Texture2D.ReadPixels() to get the screen pixels. Once you have the texture you can count the number of pixels that are not background and divide the result by the total number of pixels on a screen.

If you can live with an approximation, you can take corners of the bounds of the mesh, convert to world positions (Transform.TransformPoint()), then convert the result to Screen or Viewport points. Taking the minimum and maximum x and y values will give an XY axes aligned bounding rect that can be used to calculate a percentage. Here is an example script:

#pragma strict
 
function Update() {
	var p = CalcScreenPercentage();
 	Debug.Log("Object uses " + p + " of the screen");
}

function CalcScreenPercentage() {

	var minX = Mathf.Infinity;
	var minY = Mathf.Infinity;
	var maxX = -Mathf.Infinity;
	var maxY = -Mathf.Infinity;
	
    var bounds = GetComponent.<MeshFilter>().mesh.bounds; 
    var v3Center = bounds.center;
    var v3Extents = bounds.extents;
    
    var corners : Vector3[] = new Vector3[8];

    corners[0]  = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top left corner
    corners[1]  = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top right corner
    corners[2]  = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom left corner
    corners[3]  = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom right corner
    corners[4]  = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top left corner
    corners[5]  = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top right corner
    corners[6]  = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom left corner
    corners[7]  = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom right corner
    
    for (var i = 0; i < corners.Length; i++) {
    	var corner = transform.TransformPoint(corners*);*
  • corner = Camera.main.WorldToScreenPoint(corner);*

  • if (corner.x > maxX) maxX = corner.x;*

  • if (corner.x < minX) minX = corner.x;*

  • if (corner.y > maxY) maxY = corner.y;*

  • if (corner.y < minY) minY = corner.y;*

  • minX = Mathf.Clamp(minX, 0, Screen.width);*

  • maxX = Mathf.Clamp(maxX, 0, Screen.width);*

  • minY = Mathf.Clamp(minY, 0, Screen.height);*

  • maxY = Mathf.Clamp(maxY, 0, Screen.height);*
    }

  • var width = maxX - minX;*

  • var height = maxY - minY;*
    _ var area = width * height;_
    _ var percentage = area / (Screen.width * Screen.height) * 100.0;_

  • return percentage;*
    }

You can RayCast from different screen points to find one point which is point on your object. Then from that point we can find bounds of the object on the screen moving ray cast vector horizontally and vertically. Code for this:

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {
	private bool isVisibleFromCamera = false;
	public GameObject anObject;
	private Camera cam;
	private Plane[] planes;
	public float step = 10f;

	void Start() {
		cam = Camera.main;
		planes = GeometryUtility.CalculateFrustumPlanes(cam);
	}
	
	void Update() {
		if (GeometryUtility.TestPlanesAABB(planes, anObject.collider.bounds))
			isVisibleFromCamera = true;
		else
			isVisibleFromCamera = false;
	}
	
	float CalculateBounds() {
		if(isVisibleFromCamera) { //is anObject is visible from mainCamera
			Vector2 point = new Vector2(Random.Range(0, Screen.width), Random.Range(0, Screen.height));
			Ray ray = Camera.main.ScreenPointToRay(point);
			RaycastHit hit;
			bool found = false;
			while(!found) { //calculating point of anObject on the screen
				if (Physics.Raycast(ray, out hit, 100)) {
					if (hit.collider.gameObject == anObject) 
						found = true;
				}
				else {
					point = new Vector2(Random.Range(0, Screen.width), Random.Range(0, Screen.height));
					ray = Camera.main.ScreenPointToRay(point);
				}
			}
			//now Vector2 point is point of the anObject 
			//time to find screen bounds

			float leftBound, rightBound, topBound, bottomBound;
			leftBound = rightBound = point.x;
			topBound = bottomBound = point.y;
			bool boundFound = false;
			while(!boundFound) {
				boundFound = true;
				leftBound -= step;
				point = new Vector2(leftBound, point.y);
				ray = Camera.main.ScreenPointToRay(point);
				if (Physics.Raycast(ray, out hit, 100)) {
					if (hit.collider.gameObject == anObject) 
						boundFound = false;
				}
			}
			boundFound = false;
			while(!boundFound) {
				boundFound = true;
				rightBound += step;
				point = new Vector2(rightBound, point.y);
				ray = Camera.main.ScreenPointToRay(point);
				if (Physics.Raycast(ray, out hit, 100)) {
					if (hit.collider.gameObject == anObject) 
						boundFound = false;
				}
			}
			boundFound = false;
			while(!boundFound) {
				boundFound = true;
				topBound -= step;
				point = new Vector2(point.x, topBound);
				ray = Camera.main.ScreenPointToRay(point);
				if (Physics.Raycast(ray, out hit, 100)) {
					if (hit.collider.gameObject == anObject) 
						boundFound = false;
				}
			}
			boundFound = false;
			while(!boundFound) {
				boundFound = true;
				bottomBound += step;
				point = new Vector2(point.x, bottomBound);
				ray = Camera.main.ScreenPointToRay(point);
				if (Physics.Raycast(ray, out hit, 100)) {
					if (hit.collider.gameObject == anObject) 
						boundFound = false;
				}
			}
			//now we have bounds calculated

			float screenArea = Screen.width * Screen.height;
		float objectArea = (rightBound - leftBound) * (bottomBound - topBound);
		return objectArea / screenArea * 100;
		} 
	}
}