コード例 #1
0
    //--------------------------------------------------------------------------

    //初期化処理
    private void Start()
    {
#pragma warning disable 0219
        string Tag = "[" + this.GetType().Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(); //クラス名とメソッド名を自動取得
#pragma warning restore 0219
        Debug.Log(Tag + "Begin");

        var openVRError  = EVRInitError.None;
        var overlayError = EVROverlayError.None;
        error   = false;
        oldshow = !show;

        //フレームレートを90fpsにする。(しないと無限に早くなることがある)
        Application.targetFrameRate = 90;
        Debug.Log(Tag + "Set Frame Rate 90");

        //JSON読み込み
        Debug.Log(Tag + "Load JSON");
        loadJSON();

        InstanceKeyText.text = "";
        //複数起動用キー
        if (Environment.GetCommandLineArgs().Length >= 3)
        {
            Debug.Log(Environment.GetCommandLineArgs()[1]);
            Debug.Log(Environment.GetCommandLineArgs()[2]);

            //引数に合わせて一時的にKeyを変更する
            if (Environment.GetCommandLineArgs()[1] == "overlaykey")
            {
                config.OverlayKeyName      = Environment.GetCommandLineArgs()[2];
                config.OverlayFriendlyName = Environment.GetCommandLineArgs()[2];

                //サブインスタンス表示
                InstanceKeyText.text = config.OverlayKeyName;
            }
        }

        //表示用
        OverlayKeyName      = config.OverlayKeyName;
        OverlayFriendlyName = config.OverlayFriendlyName;


        //OpenVRの初期化
        openvr = OpenVR.Init(ref openVRError, EVRApplicationType.VRApplication_Overlay);
        if (openVRError != EVRInitError.None)
        {
            Debug.LogError(Tag + "OpenVRの初期化に失敗." + openVRError.ToString());

            //エラーを告知し5秒後に終了
            DOVirtual.DelayedCall(5f, () => {
                ApplicationQuit();
            });

            ProcessError();
            return;
        }

        //オーバーレイ機能の初期化
        overlay      = OpenVR.Overlay;
        overlayError = overlay.CreateOverlay(config.OverlayKeyName, config.OverlayFriendlyName, ref overlayHandle);
        if (overlayError != EVROverlayError.None)
        {
            Debug.LogError(Tag + "Overlayの初期化に失敗. " + overlayError.ToString());

            //エラーを告知し5秒後に終了
            DOVirtual.DelayedCall(5f, () => {
                ApplicationQuit();
            });

            ProcessError();
            return;
        }

        //オーバーレイに渡すテクスチャ種類の設定
        var OverlayTextureBounds = new VRTextureBounds_t();
        var isOpenGL             = SystemInfo.graphicsDeviceVersion.Contains("OpenGL");
        if (isOpenGL)
        {
            //pGLuintTexture
            overlayTexture.eType = ETextureType.OpenGL;
            //上下反転しない
            OverlayTextureBounds.uMin = 1;
            OverlayTextureBounds.vMin = 0;
            OverlayTextureBounds.uMax = 1;
            OverlayTextureBounds.vMax = 0;
            overlay.SetOverlayTextureBounds(overlayHandle, ref OverlayTextureBounds);
        }
        else
        {
            //pTexture
            overlayTexture.eType = ETextureType.DirectX;
            //上下反転する
            OverlayTextureBounds.uMin = 0;
            OverlayTextureBounds.vMin = 1;
            OverlayTextureBounds.uMax = 1;
            OverlayTextureBounds.vMax = 0;
            overlay.SetOverlayTextureBounds(overlayHandle, ref OverlayTextureBounds);
        }

        //--------
        showDevices();

        Debug.Log(Tag + "初期化完了しました");
    }
コード例 #2
0
    public void UpdateOverlay()
    {
        CVROverlay overlay = OpenVR.Overlay;

        if (overlay == null)
        {
            return;
        }
        if (this.texture != null)
        {
            EVROverlayError eVROverlayError = overlay.ShowOverlay(this.handle);
            if ((eVROverlayError == EVROverlayError.InvalidHandle || eVROverlayError == EVROverlayError.UnknownOverlay) && overlay.FindOverlay(SteamVR_Overlay.key, ref this.handle) != EVROverlayError.None)
            {
                return;
            }
            Texture_t texture_t = default(Texture_t);
            texture_t.handle      = this.texture.GetNativeTexturePtr();
            texture_t.eType       = SteamVR.instance.graphicsAPI;
            texture_t.eColorSpace = EColorSpace.Auto;
            overlay.SetOverlayTexture(this.handle, ref texture_t);
            overlay.SetOverlayAlpha(this.handle, this.alpha);
            overlay.SetOverlayWidthInMeters(this.handle, this.scale);
            overlay.SetOverlayAutoCurveDistanceRangeInMeters(this.handle, this.curvedRange.x, this.curvedRange.y);
            VRTextureBounds_t vRTextureBounds_t = default(VRTextureBounds_t);
            vRTextureBounds_t.uMin = (0f + this.uvOffset.x) * this.uvOffset.z;
            vRTextureBounds_t.vMin = (1f + this.uvOffset.y) * this.uvOffset.w;
            vRTextureBounds_t.uMax = (1f + this.uvOffset.x) * this.uvOffset.z;
            vRTextureBounds_t.vMax = (0f + this.uvOffset.y) * this.uvOffset.w;
            overlay.SetOverlayTextureBounds(this.handle, ref vRTextureBounds_t);
            HmdVector2_t hmdVector2_t = default(HmdVector2_t);
            hmdVector2_t.v0 = this.mouseScale.x;
            hmdVector2_t.v1 = this.mouseScale.y;
            overlay.SetOverlayMouseScale(this.handle, ref hmdVector2_t);
            SteamVR_Camera steamVR_Camera = SteamVR_Render.Top();
            if (steamVR_Camera != null && steamVR_Camera.origin != null)
            {
                SteamVR_Utils.RigidTransform rigidTransform = new SteamVR_Utils.RigidTransform(steamVR_Camera.origin, base.transform);
                rigidTransform.pos.x = rigidTransform.pos.x / steamVR_Camera.origin.localScale.x;
                rigidTransform.pos.y = rigidTransform.pos.y / steamVR_Camera.origin.localScale.y;
                rigidTransform.pos.z = rigidTransform.pos.z / steamVR_Camera.origin.localScale.z;
                rigidTransform.pos.z = rigidTransform.pos.z + this.distance;
                HmdMatrix34_t hmdMatrix34_t = rigidTransform.ToHmdMatrix34();
                overlay.SetOverlayTransformAbsolute(this.handle, SteamVR_Render.instance.trackingSpace, ref hmdMatrix34_t);
            }
            overlay.SetOverlayInputMethod(this.handle, this.inputMethod);
            if (this.curved || this.antialias)
            {
                this.highquality = true;
            }
            if (this.highquality)
            {
                overlay.SetHighQualityOverlay(this.handle);
                overlay.SetOverlayFlag(this.handle, VROverlayFlags.Curved, this.curved);
                overlay.SetOverlayFlag(this.handle, VROverlayFlags.RGSS4X, this.antialias);
            }
            else if (overlay.GetHighQualityOverlay() == this.handle)
            {
                overlay.SetHighQualityOverlay(0uL);
            }
        }
        else
        {
            overlay.HideOverlay(this.handle);
        }
    }
コード例 #3
0
ファイル: SteamVR.cs プロジェクト: JamesBear/SteamVRProject
    private SteamVR(System.IntPtr pHmd, System.IntPtr pCompositor, System.IntPtr pOverlay)
    {
        hmd = new CVRSystem(pHmd);
        Debug.Log("Connected to " + hmd_TrackingSystemName + ":" + hmd_SerialNumber);

        compositor = new CVRCompositor(pCompositor);
        overlay    = new CVROverlay(pOverlay);

        // Setup render values
        uint w = 0, h = 0;

        hmd.GetRecommendedRenderTargetSize(ref w, ref h);
        sceneWidth  = (float)w;
        sceneHeight = (float)h;

        float l_left = 0.0f, l_right = 0.0f, l_top = 0.0f, l_bottom = 0.0f;

        hmd.GetProjectionRaw(EVREye.Eye_Left, ref l_left, ref l_right, ref l_top, ref l_bottom);

        float r_left = 0.0f, r_right = 0.0f, r_top = 0.0f, r_bottom = 0.0f;

        hmd.GetProjectionRaw(EVREye.Eye_Right, ref r_left, ref r_right, ref r_top, ref r_bottom);

        tanHalfFov = new Vector2(
            Mathf.Max(-l_left, l_right, -r_left, r_right),
            Mathf.Max(-l_top, l_bottom, -r_top, r_bottom));

        textureBounds = new VRTextureBounds_t[2];

        textureBounds[0].uMin = 0.5f + 0.5f * l_left / tanHalfFov.x;
        textureBounds[0].uMax = 0.5f + 0.5f * l_right / tanHalfFov.x;
        textureBounds[0].vMin = 0.5f - 0.5f * l_bottom / tanHalfFov.y;
        textureBounds[0].vMax = 0.5f - 0.5f * l_top / tanHalfFov.y;

        textureBounds[1].uMin = 0.5f + 0.5f * r_left / tanHalfFov.x;
        textureBounds[1].uMax = 0.5f + 0.5f * r_right / tanHalfFov.x;
        textureBounds[1].vMin = 0.5f - 0.5f * r_bottom / tanHalfFov.y;
        textureBounds[1].vMax = 0.5f - 0.5f * r_top / tanHalfFov.y;

        Unity.SetSubmitParams(textureBounds[0], textureBounds[1], EVRSubmitFlags.Submit_Default);

        // Grow the recommended size to account for the overlapping fov
        sceneWidth  = sceneWidth / Mathf.Max(textureBounds[0].uMax - textureBounds[0].uMin, textureBounds[1].uMax - textureBounds[1].uMin);
        sceneHeight = sceneHeight / Mathf.Max(textureBounds[0].vMax - textureBounds[0].vMin, textureBounds[1].vMax - textureBounds[1].vMin);

        aspect      = tanHalfFov.x / tanHalfFov.y;
        fieldOfView = 2.0f * Mathf.Atan(tanHalfFov.y) * Mathf.Rad2Deg;

        eyes = new SteamVR_Utils.RigidTransform[] {
            new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(EVREye.Eye_Left)),
            new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(EVREye.Eye_Right))
        };

        if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL"))
        {
            graphicsAPI = EGraphicsAPIConvention.API_OpenGL;
        }
        else
        {
            graphicsAPI = EGraphicsAPIConvention.API_DirectX;
        }

        SteamVR_Utils.Event.Listen("initializing", OnInitializing);
        SteamVR_Utils.Event.Listen("calibrating", OnCalibrating);
        SteamVR_Utils.Event.Listen("out_of_range", OnOutOfRange);
        SteamVR_Utils.Event.Listen("device_connected", OnDeviceConnected);
        SteamVR_Utils.Event.Listen("new_poses", OnNewPoses);
    }
コード例 #4
0
    private SteamVR(System.IntPtr pHmd, System.IntPtr pCompositor, System.IntPtr pOverlay)
    {
        hmd = new CVRSystem(pHmd);
        Debug.Log("Connected to " + hmd_TrackingSystemName + ":" + hmd_SerialNumber);

        compositor = new CVRCompositor(pCompositor);
        overlay    = new CVROverlay(pOverlay);

        var device = new UnityGraphicsDevice();

        GetUnityGraphicsDevice(ref device);
        switch (device.type)
        {
        case GfxDeviceRenderer.kGfxRendererD3D11:
            compositor.SetGraphicsDevice(Compositor_DeviceType.D3D11, device.ptr);
            break;

        case GfxDeviceRenderer.kGfxRendererOpenGL:
            compositor.SetGraphicsDevice(Compositor_DeviceType.OpenGL, device.ptr);
            break;

        default:
            throw new System.Exception("Unsupported device type.");
        }

        var capacity = compositor.GetLastError(null, 0);

        if (capacity > 1)
        {
            var result = new System.Text.StringBuilder((int)capacity);
            compositor.GetLastError(result, capacity);
            Debug.Log("Compositor - " + result);
        }

        // Register for a callback if our graphics device goes away, so we can properly clean up.
        var resetDelegate = new UnityResetDelegate(SteamVR.SafeDispose);

        callbackHandle = GCHandle.Alloc(resetDelegate);
        SetUnityResetCallback(Marshal.GetFunctionPointerForDelegate(resetDelegate));

        // Hook up the render thread present event just in case we wind up needing to use this.
        var error = HmdError.None;

        SetUnityRenderCallback(OpenVR.GetGenericInterface(IVRHmdDistortPresent_Version, ref error));

        // Setup render values
        uint w = 0, h = 0;

        hmd.GetRecommendedRenderTargetSize(ref w, ref h);
        sceneWidth  = (float)w;
        sceneHeight = (float)h;

        float l_left = 0.0f, l_right = 0.0f, l_top = 0.0f, l_bottom = 0.0f;

        hmd.GetProjectionRaw(Hmd_Eye.Eye_Left, ref l_left, ref l_right, ref l_top, ref l_bottom);

        float r_left = 0.0f, r_right = 0.0f, r_top = 0.0f, r_bottom = 0.0f;

        hmd.GetProjectionRaw(Hmd_Eye.Eye_Right, ref r_left, ref r_right, ref r_top, ref r_bottom);

        tanHalfFov = new Vector2(
            Mathf.Max(-l_left, l_right, -r_left, r_right),
            Mathf.Max(-l_top, l_bottom, -r_top, r_bottom));

        textureBounds = new VRTextureBounds_t[2];

        textureBounds[0].uMin = 0.5f + 0.5f * l_left / tanHalfFov.x;
        textureBounds[0].uMax = 0.5f + 0.5f * l_right / tanHalfFov.x;
        textureBounds[0].vMin = 0.5f - 0.5f * l_bottom / tanHalfFov.y;
        textureBounds[0].vMax = 0.5f - 0.5f * l_top / tanHalfFov.y;

        textureBounds[1].uMin = 0.5f + 0.5f * r_left / tanHalfFov.x;
        textureBounds[1].uMax = 0.5f + 0.5f * r_right / tanHalfFov.x;
        textureBounds[1].vMin = 0.5f - 0.5f * r_bottom / tanHalfFov.y;
        textureBounds[1].vMax = 0.5f - 0.5f * r_top / tanHalfFov.y;

        // Grow the recommended size to account for the overlapping fov
        sceneWidth  = sceneWidth / Mathf.Max(textureBounds[0].uMax - textureBounds[0].uMin, textureBounds[1].uMax - textureBounds[1].uMin);
        sceneHeight = sceneHeight / Mathf.Max(textureBounds[0].vMax - textureBounds[0].vMin, textureBounds[1].vMax - textureBounds[1].vMin);

        aspect      = tanHalfFov.x / tanHalfFov.y;
        fieldOfView = 2.0f * Mathf.Atan(tanHalfFov.y) * Mathf.Rad2Deg;

        eyes = new SteamVR_Utils.RigidTransform[] {
            new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(Hmd_Eye.Eye_Left)),
            new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(Hmd_Eye.Eye_Right))
        };

        SteamVR_Utils.Event.Listen("initializing", OnInitializing);
        SteamVR_Utils.Event.Listen("calibrating", OnCalibrating);
        SteamVR_Utils.Event.Listen("out_of_range", OnOutOfRange);
        SteamVR_Utils.Event.Listen("device_connected", OnDeviceConnected);
        SteamVR_Utils.Event.Listen("new_poses", OnNewPoses);
    }