Beispiel #1
0
    protected void Init()
    {
        if (RenderMaterial == null)
        {
            RenderMaterial = Resources.Load("OVRLensCorrectionMat", typeof(Material)) as Material;

            if (RenderMaterial == null)
            {
                MiddleVRTools.Log(-1, "[RiftCamera] Failed to load OVRLensCorrectionMat.");
            }
        }

        vrObject oculusDriver = MiddleVR.VRKernel.GetObject("OculusRift Tracker Driver");

        if (oculusDriver == null)
        {
            MiddleVRTools.Log(0, "[RiftCamera] No Oculus Rift driver found!");
            return;
        }

        float DistortionScale = oculusDriver.GetProperty("DistortionScale").GetFloat();
        float AR = oculusDriver.GetProperty("AspectRatio").GetFloat();
        float K0 = oculusDriver.GetProperty("K0").GetFloat();
        float K1 = oculusDriver.GetProperty("K1").GetFloat();
        float K2 = oculusDriver.GetProperty("K2").GetFloat();
        float K3 = oculusDriver.GetProperty("K3").GetFloat();

        float OffsetLensLeft  = oculusDriver.GetProperty("OffsetLensLeft").GetFloat();
        float OffsetLensRight = oculusDriver.GetProperty("OffsetLensRight").GetFloat();

        //print("DistortionScale: " + DistortionScale);
        //print("AspectRatio: " + AR);

        //float LensSeparationDistance = oculusDriver.GetProperty("LensSeparationDistance").GetFloat();

        // Get the distortion scale and aspect ratio to use when calculating distortion shader
        float distortionScale = 1.0f / DistortionScale;
        float aspectRatio     = AR;

        float LensOffsetLeft  = OffsetLensLeft;
        float LensOffsetRight = OffsetLensRight;

        // These values are different in the SDK World Demo; Unity renders each camera to a buffer
        // that is normalized, so we will respect this rule when calculating the distortion inputs
        float NormalizedWidth  = 1.0f;
        float NormalizedHeight = 1.0f;

        _Scale.x        = (NormalizedWidth / 2.0f) * distortionScale;
        _Scale.y        = (NormalizedHeight / 2.0f) * distortionScale * aspectRatio;
        _ScaleIn.x      = (2.0f / NormalizedWidth);
        _ScaleIn.y      = (2.0f / NormalizedHeight) / aspectRatio;
        _HmdWarpParam.x = K0;
        _HmdWarpParam.y = K1;
        _HmdWarpParam.z = K2;
        _HmdWarpParam.w = K3;

        if (name.Contains("Left"))
        {
            float lensOffset = 0.5f + (LensOffsetLeft * 0.5f);
            _Center.x = lensOffset;
        }

        if (name.Contains("Right"))
        {
            float lensOffset = 0.5f + (LensOffsetRight * 0.5f);
            _Center.x = lensOffset;
        }

        _Center.y = 0.5f;

        if (RenderMaterial != null)
        {
            SetMaterialProperties();
        }
        else
        {
            print("NOMAT");
        }

        IsInit = true;
    }