Search Unity

Calculate orthographic size

Discussion in 'Scripting' started by MartijnDekker, Feb 3, 2017.

  1. MartijnDekker

    MartijnDekker

    Joined:
    Feb 13, 2015
    Posts:
    8
    Hi,

    I'm trying to calculate the correct orthographic size.

    My camera is rotate : 30,45,0
    I have created a quad with the size of 20
    The orthographic size should be around 10.6 to fill the screen with the quad.

    But how can i calculate this value??

    upload_2017-2-3_9-32-26.png
     
    dylan-hart likes this.
  2. villevli

    villevli

    Joined:
    Jan 19, 2016
    Posts:
    89
    I made a component which may help you:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. public class CameraSizeFit : MonoBehaviour
    5. {
    6.     public Vector2 minSize = new Vector2(10, 5);
    7.     public bool fitToObject;
    8.     public bool lookAt;
    9.     public GameObject fitObject;
    10.  
    11.     private Camera cam;
    12.     private new Transform transform;
    13.  
    14.     void Awake ()
    15.     {
    16.         cam = GetComponent<Camera>();
    17.         transform = GetComponent<Transform>();
    18.     }
    19.  
    20.     void Update ()
    21.     {
    22.         if (lookAt && fitObject)
    23.             transform.LookAt(fitObject.transform.position);
    24.  
    25.         var minSize = EvaluateSize();
    26.         cam.orthographicSize = CalculateOrtographicSize(minSize);
    27.     }
    28.  
    29.  
    30.     private Vector2 EvaluateSize()
    31.     {
    32.         Vector2 size = minSize;
    33.  
    34.         if (fitToObject && fitObject) {
    35.             Bounds bounds;
    36.             if (FetchObjectBounds(fitObject, out bounds)) {
    37.                 CalculateSizeFromBounds(bounds, ref size);
    38.             }
    39.         }
    40.  
    41.         return size;
    42.     }
    43.  
    44.  
    45.     private float CalculateOrtographicSize(Vector2 size)
    46.     {
    47.         float aspect = Screen.width / (float)Screen.height; // not using cam.aspect because it is not always updated immediately
    48.         float height = size.x / aspect;
    49.         if (height < size.y) height = size.y;
    50.         return height / 2;
    51.     }
    52.  
    53.  
    54.     // returns false if object bounds could not be determined
    55.     private bool FetchObjectBounds(GameObject obj, out Bounds bounds)
    56.     {
    57.         var renderer = obj.GetComponent<Renderer>();
    58.         if (renderer) {
    59.             bounds = renderer.bounds;
    60.             return true;
    61.         }
    62.  
    63.         var collider = obj.GetComponent<Collider>();
    64.         if (collider) {
    65.             bounds = collider.bounds;
    66.             return true;
    67.         }
    68.  
    69.         bounds = new Bounds();
    70.         return false;
    71.     }
    72.  
    73.  
    74.     // returns false if calculated size was not valid
    75.     private bool CalculateSizeFromBounds(Bounds bounds, ref Vector2 size)
    76.     {
    77.         bounds = TransformBounds(bounds);
    78.         if (bounds.size.x > float.Epsilon && bounds.size.y > float.Epsilon) {
    79.             size = bounds.size;
    80.             return true;
    81.         }
    82.         return false;
    83.     }
    84.  
    85.  
    86.     private static Vector3[] extentPoints = new Vector3[8]; // array reused by TransformBounds method, don't do this in a multithreaded application
    87.  
    88.     private Bounds TransformBounds(Bounds bounds)
    89.     {
    90.         Vector3 cen = bounds.center;
    91.         Vector3 ext = bounds.extents;
    92.         extentPoints[0] = transform.InverseTransformPoint(new Vector3(cen.x - ext.x, cen.y - ext.y, cen.z - ext.z));
    93.         extentPoints[1] = transform.InverseTransformPoint(new Vector3(cen.x + ext.x, cen.y - ext.y, cen.z - ext.z));
    94.         extentPoints[2] = transform.InverseTransformPoint(new Vector3(cen.x - ext.x, cen.y - ext.y, cen.z + ext.z));
    95.         extentPoints[3] = transform.InverseTransformPoint(new Vector3(cen.x + ext.x, cen.y - ext.y, cen.z + ext.z));
    96.         extentPoints[4] = transform.InverseTransformPoint(new Vector3(cen.x - ext.x, cen.y + ext.y, cen.z - ext.z));
    97.         extentPoints[5] = transform.InverseTransformPoint(new Vector3(cen.x + ext.x, cen.y + ext.y, cen.z - ext.z));
    98.         extentPoints[6] = transform.InverseTransformPoint(new Vector3(cen.x - ext.x, cen.y + ext.y, cen.z + ext.z));
    99.         extentPoints[7] = transform.InverseTransformPoint(new Vector3(cen.x + ext.x, cen.y + ext.y, cen.z + ext.z));
    100.  
    101.         Vector3 min = extentPoints[0];
    102.         Vector3 max = extentPoints[0];
    103.         foreach (Vector3 v in extentPoints) {
    104.             min = Vector3.Min(min, v);
    105.             max = Vector3.Max(max, v);
    106.         }
    107.  
    108.         bounds.SetMinMax(min, max);
    109.         return bounds;
    110.     }
    111.  
    112. }
    113.  
     
  3. zchek

    zchek

    Joined:
    Dec 8, 2014
    Posts:
    7
    Great script! Helps me make my old school point and click adventure game with non moving backgrounds.
     
    EZaca likes this.