Example #1
0
        private static EVROverlayError ProcessOverlay1(CVROverlay overlay, ref ulong overlayHandle, ref bool overlayVisible, bool dashboardVisible, uint overlayIndex, DateTime nextOverlay)
        {
            var err = EVROverlayError.None;

            if (overlayHandle == 0)
            {
                err = overlay.FindOverlay("VRCX1", ref overlayHandle);
                if (err != EVROverlayError.None)
                {
                    if (err != EVROverlayError.UnknownOverlay)
                    {
                        return(err);
                    }
                    overlayVisible = false;
                    err            = overlay.CreateOverlay("VRCX1", "VRCX1", ref overlayHandle);
                    if (err != EVROverlayError.None)
                    {
                        return(err);
                    }
                    err = overlay.SetOverlayAlpha(overlayHandle, 0.9f);
                    if (err != EVROverlayError.None)
                    {
                        return(err);
                    }
                    err = overlay.SetOverlayWidthInMeters(overlayHandle, 1f);
                    if (err != EVROverlayError.None)
                    {
                        return(err);
                    }
                    err = overlay.SetOverlayInputMethod(overlayHandle, VROverlayInputMethod.None);
                    if (err != EVROverlayError.None)
                    {
                        return(err);
                    }
                }
            }

            if (overlayIndex != OpenVR.k_unTrackedDeviceIndexInvalid)
            {
                // http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices
                // Scaling-Rotation-Translation
                var m = Matrix.Scaling(0.25f);
                m *= Matrix.RotationX(m_Rotation[0]);
                m *= Matrix.RotationY(m_Rotation[1]);
                m *= Matrix.RotationZ(m_Rotation[2]);
                m *= Matrix.Translation(m_Translation[0], m_Translation[1], m_Translation[2]);
                var hm34 = new HmdMatrix34_t
                {
                    m0  = m.M11,
                    m1  = m.M21,
                    m2  = m.M31,
                    m3  = m.M41,
                    m4  = m.M12,
                    m5  = m.M22,
                    m6  = m.M32,
                    m7  = m.M42,
                    m8  = m.M13,
                    m9  = m.M23,
                    m10 = m.M33,
                    m11 = m.M43,
                };
                err = overlay.SetOverlayTransformTrackedDeviceRelative(overlayHandle, overlayIndex, ref hm34);
                if (err != EVROverlayError.None)
                {
                    return(err);
                }
            }

            if (!dashboardVisible &&
                DateTime.UtcNow.CompareTo(nextOverlay) <= 0)
            {
                var texture = new Texture_t
                {
                    handle = m_Texture1.NativePointer
                };
                err = overlay.SetOverlayTexture(overlayHandle, ref texture);
                if (err != EVROverlayError.None)
                {
                    return(err);
                }
                if (!overlayVisible)
                {
                    err = overlay.ShowOverlay(overlayHandle);
                    if (err != EVROverlayError.None)
                    {
                        return(err);
                    }
                    overlayVisible = true;
                }
            }
            else if (overlayVisible)
            {
                err = overlay.HideOverlay(overlayHandle);
                if (err != EVROverlayError.None)
                {
                    return(err);
                }
                overlayVisible = false;
            }

            return(err);
        }
Example #2
0
        private static EVROverlayError ProcessOverlay2(CVROverlay overlay, ref ulong overlayHandle, ref bool overlayVisible, bool dashboardVisible)
        {
            var err = EVROverlayError.None;

            if (overlayHandle == 0)
            {
                err = overlay.FindOverlay("VRCX2", ref overlayHandle);
                if (err != EVROverlayError.None)
                {
                    if (err != EVROverlayError.UnknownOverlay)
                    {
                        return(err);
                    }
                    overlayVisible = false;
                    err            = overlay.CreateOverlay("VRCX2", "VRCX2", ref overlayHandle);
                    if (err != EVROverlayError.None)
                    {
                        return(err);
                    }
                    err = overlay.SetOverlayAlpha(overlayHandle, 0.9f);
                    if (err != EVROverlayError.None)
                    {
                        return(err);
                    }
                    err = overlay.SetOverlayWidthInMeters(overlayHandle, 1f);
                    if (err != EVROverlayError.None)
                    {
                        return(err);
                    }
                    err = overlay.SetOverlayInputMethod(overlayHandle, VROverlayInputMethod.None);
                    if (err != EVROverlayError.None)
                    {
                        return(err);
                    }
                    var m = Matrix.Scaling(1f);
                    m *= Matrix.Translation(0, -0.3f, -1.5f);
                    var hm34 = new HmdMatrix34_t
                    {
                        m0  = m.M11,
                        m1  = m.M21,
                        m2  = m.M31,
                        m3  = m.M41,
                        m4  = m.M12,
                        m5  = m.M22,
                        m6  = m.M32,
                        m7  = m.M42,
                        m8  = m.M13,
                        m9  = m.M23,
                        m10 = m.M33,
                        m11 = m.M43,
                    };
                    err = overlay.SetOverlayTransformTrackedDeviceRelative(overlayHandle, OpenVR.k_unTrackedDeviceIndex_Hmd, ref hm34);
                    if (err != EVROverlayError.None)
                    {
                        return(err);
                    }
                }
            }

            if (!dashboardVisible)
            {
                var texture = new Texture_t
                {
                    handle = m_Texture2.NativePointer
                };
                err = overlay.SetOverlayTexture(overlayHandle, ref texture);
                if (err != EVROverlayError.None)
                {
                    return(err);
                }
                if (!overlayVisible)
                {
                    err = overlay.ShowOverlay(overlayHandle);
                    if (err != EVROverlayError.None)
                    {
                        return(err);
                    }
                    overlayVisible = true;
                }
            }
            else if (overlayVisible)
            {
                err = overlay.HideOverlay(overlayHandle);
                if (err != EVROverlayError.None)
                {
                    return(err);
                }
                overlayVisible = false;
            }

            return(err);
        }
Example #3
0
        private static EVROverlayError ProcessDashboard(CVROverlay overlay, ref ulong dashboardHandle, bool dashboardVisible)
        {
            var err = EVROverlayError.None;

            if (dashboardHandle == 0)
            {
                err = overlay.FindOverlay("VRCX", ref dashboardHandle);
                if (err != EVROverlayError.None)
                {
                    if (err != EVROverlayError.UnknownOverlay)
                    {
                        return(err);
                    }
                    ulong handle = 0;
                    err = overlay.CreateDashboardOverlay("VRCX", "VRCX", ref dashboardHandle, ref handle);
                    if (err != EVROverlayError.None)
                    {
                        return(err);
                    }
                    err = overlay.SetOverlayWidthInMeters(dashboardHandle, 1.5f);
                    if (err != EVROverlayError.None)
                    {
                        return(err);
                    }
                    err = overlay.SetOverlayInputMethod(dashboardHandle, VROverlayInputMethod.Mouse);
                    if (err != EVROverlayError.None)
                    {
                        return(err);
                    }
                }
            }

            var e = new VREvent_t();

            while (overlay.PollNextOverlayEvent(dashboardHandle, ref e, (uint)Marshal.SizeOf(e)))
            {
                var type = (EVREventType)e.eventType;
                if (type == EVREventType.VREvent_MouseMove)
                {
                    var m = e.data.mouse;
                    var s = m_Browser1.Size;
                    m_Browser1.GetBrowserHost().SendMouseMoveEvent((int)(m.x * s.Width), s.Height - (int)(m.y * s.Height), false, CefEventFlags.None);
                }
                else if (type == EVREventType.VREvent_MouseButtonDown)
                {
                    var m = e.data.mouse;
                    var s = m_Browser1.Size;
                    m_Browser1.GetBrowserHost().SendMouseClickEvent((int)(m.x * s.Width), s.Height - (int)(m.y * s.Height), MouseButtonType.Left, false, 1, CefEventFlags.LeftMouseButton);
                }
                else if (type == EVREventType.VREvent_MouseButtonUp)
                {
                    var m = e.data.mouse;
                    var s = m_Browser1.Size;
                    m_Browser1.GetBrowserHost().SendMouseClickEvent((int)(m.x * s.Width), s.Height - (int)(m.y * s.Height), MouseButtonType.Left, true, 1, CefEventFlags.None);
                }
            }

            if (dashboardVisible)
            {
                var texture = new Texture_t
                {
                    handle = m_Texture1.NativePointer
                };
                err = overlay.SetOverlayTexture(dashboardHandle, ref texture);
                if (err != EVROverlayError.None)
                {
                    return(err);
                }
            }

            return(err);
        }
Example #4
0
        private bool StartupOVR()
        {
            EVRInitError error = EVRInitError.Driver_Failed;

            vr = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Overlay);

            if (error != EVRInitError.None)
            {
                Debug.WriteLine("OpenVR Init Error: " + error.ToString());
                return(false);
            }

            overlay = OpenVR.Overlay;

            EVROverlayError overlayError = overlay.CreateOverlay("Jstf_ovr_cards", "OpenVR Cards System Overlay", ref overlayHandle);

            if (overlayError != EVROverlayError.None)
            {
                OpenVR.Shutdown();
                Debug.WriteLine("OpenVR Overlay Error: " + overlayError.ToString());
                return(false);
            }

            /*overlayError = overlay.SetOverlayFromFile(overlayHandle, AssetsPath + "overlay.png");
             * if (overlayError != EVROverlayError.None)
             * {
             *      CleanupOVR();
             *      Debug.WriteLine("OpenVR Overlay Error: " + overlayError.ToString());
             *      return false;
             * }*/

            overlayError = overlay.SetOverlayInputMethod(overlayHandle, VROverlayInputMethod.Mouse);
            if (overlayError != EVROverlayError.None)
            {
                CleanupOVR();
                Debug.WriteLine("OpenVR Overlay Error: " + overlayError.ToString());
                return(false);
            }

            overlayTexture = new Texture_t
            {
                eType       = ETextureType.OpenGL,
                eColorSpace = EColorSpace.Auto,
                handle      = (IntPtr)textureId
            };

            overlayError = overlay.SetOverlayTexture(overlayHandle, ref overlayTexture);
            if (overlayError != EVROverlayError.None)
            {
                CleanupOVR();
                Debug.WriteLine("OpenVR Overlay Error: " + overlayError.ToString());
                return(false);
            }

            HmdId = OpenVR.k_unTrackedDeviceIndex_Hmd;

            for (uint i = HmdId + 1; i < OpenVR.k_unMaxTrackedDeviceCount; i++)
            {
                if (vr.IsTrackedDeviceConnected(i))
                {
                    ETrackedDeviceClass cls = vr.GetTrackedDeviceClass(i);
                    if (cls == ETrackedDeviceClass.Controller)
                    {
                        ETrackedControllerRole rl = vr.GetControllerRoleForTrackedDeviceIndex(i);
                        if (rl == ETrackedControllerRole.LeftHand)
                        {
                            LeftControllerId = i;
                            Debug.WriteLine("Found Left Controller");
                        }
                        else if (rl == ETrackedControllerRole.RightHand)
                        {
                            RightControllerId = i;
                            Debug.WriteLine("Found Right Controller");
                        }
                    }
                }
            }

            mat = new HmdMatrix34_t
            {
                m0  = 1,
                m1  = 0,
                m2  = 0,
                m3  = 0f,
                m4  = 0,
                m5  = 1,
                m6  = 0,
                m7  = 0.1f,
                m8  = 0,
                m9  = 0,
                m10 = 1,
                m11 = 0f
            };



            overlayError = overlay.SetOverlayTransformTrackedDeviceRelative(overlayHandle, RightControllerId, ref mat);
            if (overlayError != EVROverlayError.None)
            {
                Debug.WriteLine("Cannot bind overlay to Tracked device.");
                Debug.WriteLine("Error: " + overlayError.ToString());
                CleanupOVR();
                return(false);
            }
            overlayError = overlay.SetOverlayWidthInMeters(overlayHandle, 0.2f);
            if (overlayError != EVROverlayError.None)
            {
                Debug.WriteLine("Cannot set overlay size.");
                Debug.WriteLine("Error: " + overlayError.ToString());
                CleanupOVR();
                return(false);
            }

            overlayError = overlay.SetOverlayAlpha(overlayHandle, 1);
            if (overlayError != EVROverlayError.None)
            {
                Debug.WriteLine("Cannot set overlay alpha.");
                Debug.WriteLine("Error: " + overlayError.ToString());
                CleanupOVR();
                return(false);
            }

            overlayError = overlay.SetOverlayColor(overlayHandle, 1, 1, 1);
            if (overlayError != EVROverlayError.None)
            {
                Debug.WriteLine("Cannot set overlay color.");
                Debug.WriteLine("Error: " + overlayError.ToString());
                CleanupOVR();
                return(false);
            }
#if DEBUG
            Debug.WriteLine("OpenVR Startup Complete");
#endif
            return(true);
        }
Example #5
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.textureType;
            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 = 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 = 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);
        }
    }
Example #6
0
    void Start()
    {
        var openVRError  = EVRInitError.None;
        var overlayError = EVROverlayError.None;

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

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

        //マウス(Dashboardのときのみ有効)
        overlay.SetOverlayInputMethod(overlayHandle, VROverlayInputMethod.Mouse);

        //オーバーレイに渡すテクスチャ種類の設定
        OverlayTextureBounds = new VRTextureBounds_t();
        var isOpenGL = SystemInfo.graphicsDeviceVersion.Contains("OpenGL");

        if (isOpenGL)
        {
            //pGLuintTexture
            overlayTexture.eType   = ETextureType.OpenGL;
            thumbnailTexture.eType = ETextureType.OpenGL;
            //上下反転しない
            OverlayTextureBounds.uMin = 1;
            OverlayTextureBounds.vMin = 0;
            OverlayTextureBounds.uMax = 1;
            OverlayTextureBounds.vMax = 0;
            overlay.SetOverlayTextureBounds(thumbnailHandle, ref OverlayTextureBounds);
        }
        else
        {
            //pTexture
            overlayTexture.eType   = ETextureType.DirectX;
            thumbnailTexture.eType = ETextureType.DirectX;
            //上下反転する
            OverlayTextureBounds.uMin = 0;
            OverlayTextureBounds.vMin = 1;
            OverlayTextureBounds.uMax = 1;
            OverlayTextureBounds.vMax = 0;
            overlay.SetOverlayTextureBounds(overlayHandle, ref OverlayTextureBounds);
        }

        //サムネイルテクスチャが存在するなら
        if (thumbnailTextureInput != null)
        {
            //サムネイルテクスチャの情報を取得
            var thumWidth  = thumbnailTextureInput.width;
            var thumHeight = thumbnailTextureInput.height;
            //作業用テクスチャの領域を確保
            thumbnailTexture2D = new Texture2D(thumWidth, thumHeight, TextureFormat.RGBA32, false);

            //力技でコピーして圧縮を解除
            if (isOpenGL)
            {
                for (int y = 0; y < thumHeight; y++)
                {
                    for (int x = 0; x < thumWidth; x++)
                    {
                        thumbnailTexture2D.SetPixel(x, y, thumbnailTextureInput.GetPixel(x, y));
                    }
                }
            }
            else
            {
                //DirectXは上下反転
                for (int y = 0; y < thumHeight; y++)
                {
                    for (int x = 0; x < thumWidth; x++)
                    {
                        thumbnailTexture2D.SetPixel(x, thumHeight - y - 1, thumbnailTextureInput.GetPixel(x, y));
                    }
                }
            }

            //操作を反映
            thumbnailTexture2D.Apply();
            //ネイティブテクスチャを取得して設定
            thumbnailTexture.handle = thumbnailTexture2D.GetNativeTexturePtr();
            //サムネイル利用可能フラグを立てる
            thumbnailAvailable = true;
        }

        Debug.Log(Tag + "初期化完了しました");
    }
Example #7
0
 /**
  * Set overlay's input method
  */
 public void SetInputMethod(VROverlayInputMethod inputMethod)
 {
     ReportError(overlay.SetOverlayInputMethod(handle, inputMethod));
 }