Ejemplo n.º 1
0
    public void Init(SteamVR_Camera tracker, SteamVR.Hmd_Eye eye, float depth)
    {
        this.tracker = tracker;
        this.eye     = eye;

        var hmd = SteamVR.IHmd.instance;
        var i   = (int)eye;

        uint x = 0, y = 0, w = 0, h = 0;

        hmd.GetEyeOutputViewport(eye, ref x, ref y, ref w, ref h);

        overlaySettings.kernel = new Vector3[] {         // AA sub-pixel sampling (2x2 RGSS)
            new Vector3(0.125f / w, -0.375f / h, 0),
            new Vector3(0.375f / w, 0.125f / h, 0),
            new Vector3(-0.125f / w, 0.375f / h, 0),
            new Vector3(-0.375f / w, -0.125f / h, 0)
        };

        float left = 0.0f, right = 0.0f, top = 0.0f, bottom = 0.0f;

        hmd.GetProjectionRaw(eye, ref left, ref right, ref top, ref bottom);

        var   camera = GetComponent <Camera>();
        float zFar   = camera.farClipPlane;
        float zNear  = camera.nearClipPlane;

        var m = Matrix4x4.identity;

        float idx = 1.0f / (right - left);
        float idy = 1.0f / (bottom - top);
        float idz = 1.0f / (zFar - zNear);
        float sx  = right + left;
        float sy  = bottom + top;

        m[0, 0] = 2 * idx; m[0, 1] = 0;     m[0, 2] = sx * idx;    m[0, 3] = 0;
        m[1, 0] = 0;     m[1, 1] = 2 * idy; m[1, 2] = sy * idy;    m[1, 3] = 0;
        m[2, 0] = 0;     m[2, 1] = 0;     m[2, 2] = -zFar * idz; m[2, 3] = -zFar * zNear * idz;
        m[3, 0] = 0;     m[3, 1] = 0;     m[3, 2] = -1.0f;     m[3, 3] = 0;

        camera.projectionMatrix = m;
        camera.depth            = depth;                                // enforce rendering order
        camera.eventMask        = 0;                                    // disable mouse events
        camera.orthographic     = false;                                // force perspective
        camera.aspect           = (float)w / h;

        // Use fov of projection matrix (which is grown to account for non-linear undistort)
        camera.fieldOfView = (Mathf.Atan(bottom) - Mathf.Atan(top)) * Mathf.Rad2Deg;

        // Copy and clear clearFlags to avoid duplicate work.
        if ((int)clearFlags == 0)
        {
            clearFlags        = camera.clearFlags;
            camera.clearFlags = CameraClearFlags.Nothing;
        }

        camera.targetTexture = SteamVR_Camera.sceneTexture;

        overlaySettings.invProj = m.inverse;

        transform.parent     = tracker.offset;
        transform.localScale = Vector3.one;

        var t = new SteamVR_Utils.RigidTransform(hmd.GetHeadFromEyePose(eye));

        transform.localPosition = t.pos;
        transform.localRotation = t.rot;

        if (distortMesh[i] == null)
        {
            var viewportWidth  = SteamVR_Camera.viewportTexture.width;
            var viewportHeight = SteamVR_Camera.viewportTexture.height;

            var eyeViewport = new Rect(
                2.0f * x / viewportWidth - 1.0f,
                2.0f * y / viewportHeight - 1.0f,
                2.0f * w / viewportWidth,
                2.0f * h / viewportHeight);

            distortMesh[i] = SteamVR_Utils.CreateDistortMesh(hmd, eye, 32, 32, eyeViewport);
        }
    }