示例#1
0
    /// <summary>
    /// 设置相机参数
    /// Note :当且仅当 不存在camera时候才会调用
    /// </summary>
    /// <param name="controller"></param>
    /// <param name="parx"></param>
    /// <param name="pary"></param>
    public void CopyCameraAndMakeSideBySide(PicoVREyeManager controller,
                                            float parx, float pary)
    {
        // camera.CopyFrom(controller.GetComponent<Camera>());
        camera.cullingMask = InitCullingMask;
        Camera headCamera = controller.GetComponent <Camera>();

        camera.aspect      = headCamera.aspect;
        camera.rect        = headCamera.rect;
        camera.fieldOfView = headCamera.fieldOfView;
#if !ANDROID_DEVICE && !IOS_DEVICE
        float ipd = PicoVRManager.SDK.picoVRProfile.device.devLenses.separation * controller.stereoMultiplier;
        transform.localPosition = (eye == PicoVRManager.Eye.LeftEye ? -ipd / 2 : ipd / 2) * Vector3.right;
        transform.localRotation = Quaternion.identity;
        transform.localScale    = Vector3.one;
        Rect    rect   = camera.rect;
        Vector2 center = rect.center;
        center.x    = Mathf.Lerp(center.x, 0.5f, Mathf.Clamp01(controller.stereoPaddingX));
        center.y    = Mathf.Lerp(center.y, 0.5f, Mathf.Clamp01(controller.stereoPaddingY));
        rect.center = center;
        float width = Mathf.SmoothStep(-0.5f, 0.5f, (rect.width + 1) / 2);
        rect.x    += (rect.width - width) / 2;
        rect.width = width;
        rect.x    *= (0.5f - rect.width) / (1 - rect.width);
        if (eye == PicoVRManager.Eye.RightEye)
        {
            rect.x += 0.5f; // Move to right half of the screen.
        }
        float parallax = Mathf.Clamp01(controller.screenParallax);
        if (controller.GetComponent <Camera>().rect.width < 1 && parallax > 0)
        {
            rect.x -= parx / 4 * parallax; // Extra factor of 1/2 because of side-by-side stereo.
            rect.y -= pary / 2 * parallax;
        }
#endif
#if ANDROID_DEVICE || IOS_DEVICE
        Rect rect = new Rect(0, 0, 1, 1);
#endif
        camera.rect = rect;
    }
示例#2
0
    /// <summary>
    /// 设置相关参数
    /// </summary>
    private void Setup()
    {
        if (controller == null)
        {
            return;
        }
#if UNITY_EDITOR
        if (PicoVRManager.SDK.IsVREditorDebug)
        {
            transform.localPosition = PicoVRManager.SDK.EyeOffset(eye);
            camera.aspect           = 1.0f;
            Rect rect = new Rect(0, 0, 1, 1);
            camera.fieldOfView = PicoVRManager.SDK.eyeFov;
            camera.rect        = rect;
        }
        else
        {
            var monoCamera = controller.GetComponent <Camera>();

            Matrix4x4 proj = PicoVRManager.SDK.Projection(eye);

            CopyCameraAndMakeSideBySide(controller, proj[0, 2], proj[1, 2]);

            float lerp = Mathf.Clamp01(controller.matchByZoom) * Mathf.Clamp01(controller.matchMonoFOV);

            float monoProj11 = monoCamera.projectionMatrix[1, 1];
            float zoom       = 1;
            if (proj[1, 1] != 0 && monoProj11 != 0 && proj[1, 1] != 0)
            {
                zoom = 1 / Mathf.Lerp(1 / proj[1, 1], 1 / monoProj11, lerp) / proj[1, 1];
            }
            proj[0, 0] *= zoom;
            proj[1, 1] *= zoom;

            float ipdScale;
            float eyeOffset;
            controller.ComputeStereoAdjustment(proj[1, 1], transform.lossyScale.z,
                                               out ipdScale, out eyeOffset);

            transform.localPosition = ipdScale * PicoVRManager.SDK.EyeOffset(eye) +
                                      eyeOffset * Vector3.forward;


            // Set up the eye's projection.
            float near = monoCamera.nearClipPlane;
            float far  = monoCamera.farClipPlane;
            FixProjection(ref proj, near, far, ipdScale);
            camera.projectionMatrix = proj;

            if (Application.isEditor)
            {
                camera.fieldOfView = 2 * Mathf.Atan(1 / proj[1, 1]) * Mathf.Rad2Deg;
                Matrix4x4 realProj = PicoVRManager.SDK.UndistortedProjection(eye);
                FixProjection(ref realProj, near, far, ipdScale);

                Vector4 projvec = new Vector4(proj[0, 0] / zoom, proj[1, 1] / zoom,
                                              proj[0, 2] - 1, proj[1, 2] - 1) / 2;
                Vector4 unprojvec = new Vector4(realProj[0, 0], realProj[1, 1],
                                                realProj[0, 2] - 1, realProj[1, 2] - 1) / 2;
                Shader.SetGlobalVector("_Projection", projvec);
                Shader.SetGlobalVector("_Unprojection", unprojvec);
                PicoVRConfigProfile p = PicoVRManager.SDK.picoVRProfile;

                float distortionFactor = 0.0241425f;
                Shader.SetGlobalVector("_Distortion1",
                                       new Vector4(p.device.devDistortion.k1, p.device.devDistortion.k2, p.device.devDistortion.k3, distortionFactor));
                Shader.SetGlobalVector("_Distortion2",
                                       new Vector4(p.device.devDistortion.k4, p.device.devDistortion.k5, p.device.devDistortion.k6));
            }

            if (controller.StereoScreen == null)
            {
                Rect rect = camera.rect;

                if (Application.isEditor)
                {
                    Rect view = PicoVRManager.SDK.EyeRect(eye);
                    if (eye == PicoVRManager.Eye.RightEye)
                    {
                        rect.x -= 0.5f;
                    }
                    rect.width  *= 2 * view.width;
                    rect.x       = view.x + 2 * rect.x * view.width;
                    rect.height *= view.height;
                    rect.y       = view.y + rect.y * view.height;
                }
                camera.rect = rect;
            }
        }
#else
        // NOTE : Android && PC && IOS using the same setting !!!!!

        transform.localPosition = PicoVRManager.SDK.EyeOffset(eye);
        camera.aspect           = 1.0f;
        Rect rect = new Rect(0, 0, 1, 1);
        camera.fieldOfView = PicoVRManager.SDK.eyeFov;
        camera.rect        = rect;
#endif
    }