Search Unity

Mimic custom projection matrix with physical camera properties

Discussion in 'General Graphics' started by barbelot, Mar 4, 2019.

  1. barbelot

    barbelot

    Joined:
    Jul 18, 2017
    Posts:
    38
    Hello,

    I have a projection area in my scene defined by 4 points (the 4 corners of the area). I am trying to create a camera that will always look at exactly at this area, wherever it is. I used a custom projection projection matrix as shown in these examples : https://docs.unity3d.com/ScriptReference/Camera-projectionMatrix.html and https://en.wikibooks.org/wiki/Cg_Programming/Unity/Projection_for_Virtual_Reality.

    While this works very well in forward rendering, it creates lighting problems in deferred rendering as mentionned here.

    To avoid these problems I thought of using a physical camera with lens shift instead since this seems to work even in deferred. To do this I was wondering if someone had information on how the physical parameters of the camera are used when computing the projection matrix, especially the lens shift.

    I tried manually and ended up on the formulas below but these does not seems to work when the camera get far from the screen. It is a fairly good approximation when the camera is roughly above the screen though.

    This is a simplified example of my setup in one dimension.

    upload_2019-3-4_17-28-10.png

    Since I am no expert on physical cameras I'd love some help on how to compute those parameters from the screen and camera positions.

    I also noticed that when assigning a custom projection matrix to a physical camera, Unity automatically disable the physical parameters and was wondering why.

    Thanks for any help !
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The physical camera option calculates a custom projection matrix based on the physical camera settings. When you set your own custom projection matrix you're overriding the one the physical camera properties setup, so it gets disabled.

    For calculating your own lens shifted matrix, you might try looking at the projection matrix documentation you linked to. If you use a left & right or top & bottom that's at the "screen plane", you can calculate the near plane equivalent values by multiplying the value by (nearPlane / screenPos.z) where screenPos.z is the camera space depth from the camera to the screen plane (basically the E to P length in the above diagram). You can do that for all 4 edges, but you might have problems with your pixel aspect ratio if you're not careful.
     
  3. barbelot

    barbelot

    Joined:
    Jul 18, 2017
    Posts:
    38
    Thanks for the answer, it makes sense.

    My idea was to compute the physical camera parameters from the screen position instead of the projection matrix directly but I finally managed to get the custom projection matrix working in every settings. Thanks again!
     
  4. wechat_os_Qy069UGeemtLYHpBD8UPgX8Iw

    wechat_os_Qy069UGeemtLYHpBD8UPgX8Iw

    Joined:
    Mar 20, 2021
    Posts:
    1
    Hi, have you successfully calculated the custom projection matrix? be deeply grateful:)
     
  5. barbelot

    barbelot

    Joined:
    Jul 18, 2017
    Posts:
    38
    Hello, yes, I ended up using the custom projection similarly to the examples I linked in my first post.