Off center projection

Posted by N0xus on Game Development See other posts from Game Development or by N0xus
Published on 2012-09-13T10:37:38Z Indexed on 2012/09/13 15:52 UTC
Read the original article Hit count: 385

Filed under:
|

I'm trying to implement the code that was freely given by a very kind developer at the following link:

http://forum.unity3d.com/threads/142383-Code-sample-Off-Center-Projection-Code-for-VR-CAVE-or-just-for-fun

Right now, all I'm trying to do is bring it in on one camera, but I have a few issues. My class, looks as follows:

using UnityEngine;
using System.Collections;

public class PerspectiveOffCenter : MonoBehaviour 
{

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }


    public static Matrix4x4 GeneralizedPerspectiveProjection(Vector3 pa, Vector3 pb, Vector3 pc, Vector3 pe, float near, float far)
    {
        Vector3 va, vb, vc;
        Vector3 vr, vu, vn;

        float left, right, bottom, top, eyedistance;

        Matrix4x4 transformMatrix;
        Matrix4x4 projectionM;
        Matrix4x4 eyeTranslateM;
        Matrix4x4 finalProjection;

        ///Calculate the orthonormal for the screen (the screen coordinate system
        vr = pb - pa;
        vr.Normalize();
        vu = pc - pa;
        vu.Normalize();
        vn = Vector3.Cross(vr, vu);
        vn.Normalize(); 

        //Calculate the vector from eye (pe) to screen corners (pa, pb, pc)
        va = pa-pe;
        vb = pb-pe;
        vc = pc-pe;

        //Get the distance;; from the eye to the screen plane
        eyedistance = -(Vector3.Dot(va, vn));

        //Get the varaibles for the off center projection
        left    = (Vector3.Dot(vr, va)*near)/eyedistance;
        right   = (Vector3.Dot(vr, vb)*near)/eyedistance;
        bottom  = (Vector3.Dot(vu, va)*near)/eyedistance;
        top     = (Vector3.Dot(vu, vc)*near)/eyedistance;

        //Get this projection
        projectionM = PerspectiveOffCenter(left, right, bottom, top, near, far);

        //Fill in the transform matrix
        transformMatrix = new Matrix4x4();

        transformMatrix[0, 0] = vr.x;
        transformMatrix[0, 1] = vr.y;
        transformMatrix[0, 2] = vr.z;
        transformMatrix[0, 3] = 0;

        transformMatrix[1, 0] = vu.x;
        transformMatrix[1, 1] = vu.y;
        transformMatrix[1, 2] = vu.z;
        transformMatrix[1, 3] = 0;

        transformMatrix[2, 0] = vn.x;
        transformMatrix[2, 1] = vn.y;
        transformMatrix[2, 2] = vn.z;
        transformMatrix[2, 3] = 0;

        transformMatrix[3, 0] = 0;
        transformMatrix[3, 1] = 0;
        transformMatrix[3, 2] = 0;
        transformMatrix[3, 3] = 1;

        //Now for the eye transform
        eyeTranslateM = new Matrix4x4();

        eyeTranslateM[0, 0] = 1;
        eyeTranslateM[0, 1] = 0;
        eyeTranslateM[0, 2] = 0;
        eyeTranslateM[0, 3] = -pe.x;

        eyeTranslateM[1, 0] = 0;
        eyeTranslateM[1, 1] = 1;
        eyeTranslateM[1, 2] = 0;
        eyeTranslateM[1, 3] = -pe.y;

        eyeTranslateM[2, 0] = 0;
        eyeTranslateM[2, 1] = 0;
        eyeTranslateM[2, 2] = 1;
        eyeTranslateM[2, 3] = -pe.z;

        eyeTranslateM[3, 0] = 0;
        eyeTranslateM[3, 1] = 0;
        eyeTranslateM[3, 2] = 0;
        eyeTranslateM[3, 3] = 1f;



        //Multiply all together
        finalProjection = new Matrix4x4();
        finalProjection = Matrix4x4.identity * projectionM*transformMatrix*eyeTranslateM;

        //finally return
        return finalProjection;
    }

    // Update is called once per frame

    public void FixedUpdate () 
    {
        Camera cam = camera;

        //calculate projection
        Matrix4x4 genProjection = GeneralizedPerspectiveProjection( new Vector3(0,1,0), new Vector3(1,1,0), new Vector3(0,0,0), new Vector3(0,0,0), cam.nearClipPlane, cam.farClipPlane);
                                                                //(BottomLeftCorner, BottomRightCorner, TopLeftCorner, trackerPosition, cam.nearClipPlane, cam.farClipPlane);
        cam.projectionMatrix = genProjection;   

    }   
}

My error lies in projectionM = PerspectiveOffCenter(left, right, bottom, top, near, far);

The debugger states:

Expression denotes a `type', where a 'variable', 'value' or 'method group' was expected.

Thus, I changed the line to read: projectionM = new PerspectiveOffCenter(left, right, bottom, top, near, far);

But then the error is changed to:

The type 'PerspectiveOffCenter' does not contain a constructor that takes '6' arguments.

For reasons that are obvious.

So, finally, I changed the line to read: projectionM = new GeneralizedPerspectiveProjection(left, right, bottom, top, near, far);

And the error I get is:

is a 'method' but a 'type' was expected.

With this last error, I'm not sure what it is I should do / missing. Can anyone see what it is that I'm missing to fix this error?

© Game Development or respective owner

Related posts about unity

Related posts about projection