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

Is there anyway to do perspective warping with Unity's cameras?

Discussion in 'Editor & General Support' started by brisklemonade53, Mar 11, 2013.

  1. brisklemonade53

    brisklemonade53

    Joined:
    Mar 6, 2013
    Posts:
    6
    Hi all, I am new to Unity, and I am trying to figure out if I can use the Unity camera to simulate an eyepoint perspective. That is, I want to treat my monitor like a window frame for viewing my scene. For example, (eventually I will include headtracking with the Kinect), you move your head from side to side and the corresponding eyepoint shifts accordingly, but the window frame itself does not do this. Basically, just look out of any window and move your head sideways, up/down, etc. and see how the corresponding scene outside your window changes. That is the effect I am trying to set up in my game, but I keep researching and can't find any solutions. Any guidance is much appreciated! Thanks!
     
  2. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    This is possible by setting the Camera.projectionMatrix to a custom projection matrix. That property gives you full control over the projection matrix used for rendering. You will need to calculate an off-axis projection matrix to get this working, so some mathematics is required. I recommend searching for "off-axis projection matrix" and "stereo rendering". You are not trying to do stereo, but your problem is similar to setting up projection for a single eye in a stereo display setup.

    This post about stereo in Unity has some code you can re-use:
    http://forum.unity3d.com/threads/11775-Stereo-Anagylph-Rendering-HELP!!!-)

    Particularly the PerspectiveOffCenter(...) function as posted by Monark at the end of the first page in that thread is of interest to you.
     
  3. brisklemonade53

    brisklemonade53

    Joined:
    Mar 6, 2013
    Posts:
    6
    Cool beans, thanks for the help! I actually will be using a stereoscopic display later in my setup.
     
  4. brisklemonade53

    brisklemonade53

    Joined:
    Mar 6, 2013
    Posts:
    6
    I have been trying to set up a custom projection matrix that merely shows the same view my camera has by default, but everytime I run the program, all I can see is blue, I am pretty sure this has something to do with how I define top, bottom, left, and right, but I am not exactly sure, any suggestions?
     
  5. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    I usually debug this kind of thing by drawing gizmos for the different points and vectors in my calculations (the default camera gizmos no longer work when you use a custom projection matrix!). This usually reveals where my logic failed :p. I have used off-axis projection in several situations before, so this is definitely something that works once you get the maths right for your situation.
     
  6. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @brisklemonade53, any luck on implementing this? I'm trying the same thing, but can't quite get it working. Would you mind sharing your implementation?