Search Unity

This script gives you objects screen size in pixels

Discussion in 'Scripting' started by aubergine, May 14, 2010.

  1. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Here is a script that gives you an objects bounding size in pixels. So, you can use it to calculate lod or anything else like that.

    usage: pixelSize variable is what you need, make a circle texture and put it in, and move your object around to see the size of it.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AngleT : MonoBehaviour {
    5.    
    6.     public Transform target;
    7.     public Texture2D texture;
    8.    
    9.     private float distance;
    10.     private float diameter;
    11.     private float angularSize;
    12.     private float pixelSize;
    13.     private Vector3 scrPos;
    14.    
    15.     void Start () {
    16.         diameter = target.collider.bounds.extents.magnitude;
    17.     }
    18.    
    19.     void Update () {
    20.         distance = Vector3.Distance(target.position, Camera.main.transform.position);
    21.         angularSize = (diameter / distance) * Mathf.Rad2Deg;
    22.         pixelSize = ((angularSize * Screen.height) / Camera.main.fieldOfView);
    23.         scrPos = Camera.main.WorldToScreenPoint (target.position);
    24.     }
    25.    
    26.     void OnGUI () {
    27.         GUI.DrawTexture (new Rect(scrPos.x-pixelSize/2, Screen.height-scrPos.y-pixelSize/2, pixelSize, pixelSize), texture);
    28.     }
    29. }
     
    PrimalCoder and MirzaZee like this.
  2. dbp

    dbp

    Joined:
    Mar 3, 2010
    Posts:
    324
    nice, thanks for sharing this, i have seen many people asking for something like this before
     
  3. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,374
    A slight addition, and it should be a bit faster because it only uses one division:

    Code (CSharp):
    1.  
    2. //Get the screen size of an object in pixels, given its distance and diameter.
    3. float DistanceAndDiameterToPixelSize(float distance, float diameter){
    4.        
    5.     float pixelSize = (diameter * Mathf.Rad2Deg * Screen.height) / (distance * Camera.main.fieldOfView);
    6.     return pixelSize;
    7. }
    8.  
    9. //Get the distance of an object, given its screen size in pixels and diameter.
    10. float PixelSizeAndDiameterToDistance(float pixelSize, float diameter){
    11.  
    12.     float distance = (diameter * Mathf.Rad2Deg * Screen.height) / (pixelSize * Camera.main.fieldOfView);
    13.     return distance;
    14. }
    15.  
    16. //Get the diameter of an object, given its screen size in pixels and distance.
    17. float PixelSizeAndDistanceToDiameter(float pixelSize, float distance){
    18.  
    19.     float diameter = (pixelSize * distance * Camera.main.fieldOfView) / (Mathf.Rad2Deg * Screen.height);
    20.     return diameter;
    21. }
    22.  
     
    Last edited: May 10, 2015