Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Stopping Camera Vertical Compression

Discussion in 'Scripting' started by Paden, Mar 8, 2015.

  1. Paden

    Paden

    Joined:
    Sep 13, 2013
    Posts:
    13
    So I am working on a project that has two cameras: one is the main camera, and one is black and white. The B&W camera is rendering on top of the main camera. The user will be able to dynamically resize the B&W camera (from the center), making different percentages of the image black and white.

    When modifying the Viewport Rect of the camera, I can get the width and x values to change properly without any image distortion.



    However, when I change the height or y values, the image is compressed.



    I've tried everything and I can't figure out how to NOT get it to compress like that. I looked into custom projection matrices, but I do not understand how they work.

    Any help is appreciated. Thanks!
     
  2. derPuppeteer

    derPuppeteer

    Joined:
    Mar 21, 2014
    Posts:
    23
    Hi
    This is because the Camera keeps the fov (Field of view). You will have to change it according to the portion of the screen you want to have bw. And then rotate the camera a bit if you want to keep your current approach.

    I do not know your game but you may not need 2 cameras:
    If you simply want to have a rect on the screen bw you could create a postprocessing effect for that.
    Just modify a bw effect so that it only changes pixels that are in the specified area.
     
  3. Paden

    Paden

    Joined:
    Sep 13, 2013
    Posts:
    13
    Thanks for the reply!

    I need two cameras because the user will have the ability to zoom in with the B&W camera (so...change the FoV). I'm making a game to be used for a study where users will be taking pictures in the game.

    Would you happen to know the formula to figure out how to maintain the same FoV as the Viewport Rect values change?
     
  4. derPuppeteer

    derPuppeteer

    Joined:
    Mar 21, 2014
    Posts:
    23
    If you are not using a custom pers matrix this is the best you can get:
    (DISCLAIMER: As far as I know!)
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class fixViewFovRot : MonoBehaviour {
    6.  
    7.     public Camera mainCam;
    8.     public Camera bwCam;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.         Rect r = bwCam.rect;
    18.         r.width = Mathf.Clamp01(r.width);
    19.         r.height = Mathf.Clamp01(r.height);
    20.         r.x = Mathf.Clamp(r.x,0f,1f-r.width);
    21.         r.y = Mathf.Clamp(r.y,0f,1f-r.height);
    22.         bwCam.rect = new Rect(r.x,r.y,r.width,r.height);
    23.  
    24.         bwCam.transform.position = mainCam.transform.position;
    25.         bwCam.fieldOfView = mainCam.fieldOfView * bwCam.rect.height;
    26.         bwCam.aspect = (mainCam.fieldOfView * mainCam.aspect * bwCam.rect.width) / bwCam.fieldOfView;
    27.      
    28.         bwCam.transform.rotation = Quaternion.AngleAxis(
    29.             -(mainCam.fieldOfView * (1f - bwCam.rect.height))
    30.             * (bwCam.rect.y / (1f - bwCam.rect.height) - 0.5f)
    31.             , mainCam.transform.right) *
    32.  
    33.             Quaternion.AngleAxis(
    34.             (mainCam.fieldOfView * mainCam.aspect * (1f - bwCam.rect.width))
    35.             * (bwCam.rect.x / (1f - bwCam.rect.width) - 0.5f)
    36.             , mainCam.transform.up) *
    37.             mainCam.transform.rotation;
    38.     }
    39. }
    40.  
    41.  
    I am not really good with persp. matrices so I am prob. not able to help you. I will take a look at it but I no promises.
     
  5. Paden

    Paden

    Joined:
    Sep 13, 2013
    Posts:
    13
    Thanks!

    I had most of this in my code already (not the EXACT same, but close). The FoV seems to be a bit off with this formula, but it works good enough for our testing.

    Thanks again!
     
  6. derPuppeteer

    derPuppeteer

    Joined:
    Mar 21, 2014
    Posts:
    23
    Hi!
    I do not think this is a fov problem, but rather because the camera near/far planes do not have the same orientation anymore. Which is why you would need a custom proj. matrix to fix that.