Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to fix the aspect ratio in VR...

Discussion in 'AR/VR (XR) Discussion' started by justin_kasowski, Aug 18, 2020.

  1. justin_kasowski

    justin_kasowski

    Joined:
    Jan 14, 2020
    Posts:
    45
    The following code works for fixing the camera aspect ratio in non VR but it breaks when VR is in use (specifically an HTC Vive Pro Eye).


    Code (CSharp):
    1. using UnityEngine;
    2. public class CameraFixedAspectRatio : MonoBehaviour
    3. {
    4.     private Camera mainCam = new Camera();
    5.  
    6.     public float targetAspectRatio = 1f;
    7.     private void Start()
    8.     {
    9.         mainCam = BackendResourceManager.Instance.mainCamera;
    10.     }
    11.    
    12.     private void Update()
    13.     {
    14.         float w = Screen.width;
    15.         float h = Screen.height;
    16.         float a = w / h;
    17.         Rect r;
    18.         if (a > targetAspectRatio)
    19.         {
    20.             float tw = h * targetAspectRatio;
    21.             float o = (w - tw) * 0.5f;
    22.             r = new Rect(o,0,tw,h);
    23.         }
    24.         else
    25.         {
    26.             float th = w / targetAspectRatio;
    27.             float o = (h - th) * 0.5f;
    28.             r = new Rect(0, o, w, th);
    29.         }
    30.         mainCam.pixelRect = r;
    31.  
    32.     }
    33. }
    I'm building a simulation and need the x-resolution and y-resolution to match... Ideally, I'd like to only render an 800x800 screen to the headset. Is this possible? Thanks in advance!
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    Why do you want to "fix" the aspect ratio of the headset?
    And also doing it in every frame.
     
  3. Gruguir

    Gruguir

    Joined:
    Nov 30, 2010
    Posts:
    340
    You can't AFAIK, but maybe you could use render textures?
     
  4. justin_kasowski

    justin_kasowski

    Joined:
    Jan 14, 2020
    Posts:
    45
    I'm running a simulation of bionic vision and a limitation of the software we're using to pre-calculate neuronal information is a fixed aspect ratio.
     
  5. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    So you basically have some precalculated image data, and want to watch it in 3D. Why not just put it in a Texture at the size of choice in front of your camera in the scene? There are a lot of tutorials for viewing images on flat, or curved meshes.