Exemplo n.º 1
0
        public static Result <int, string> RightControllerIndex(this CVRSystem hmd)
        {
            if (hmd == null)
            {
                throw new ArgumentNullException(nameof(hmd));
            }
            int?rightContIndex =
                hmd.DevicesInClass(ETrackedDeviceClass.Controller)
                .SingleOrDefault(i => hmd.GetControllerRoleForTrackedDeviceIndex((uint)i) == ETrackedControllerRole.RightHand);

            return(rightContIndex == null ? Result <int, string> .Err("No right controller") : rightContIndex.Value);
        }
Exemplo n.º 2
0
        private static void SteamVr()
        {
            Console.WriteLine("Starting");
            EVRInitError initError = EVRInitError.None;
            CVRSystem    cvrSystem = OpenVR.Init(ref initError, EVRApplicationType.VRApplication_Utility);

            Console.WriteLine("Error: " + initError.ToString());
            if (cvrSystem == null)
            {
                Console.WriteLine("Error!");
            }
            while (true)
            {
                Thread.Sleep(1);
                TrackedDevicePose_t[] trackedDevicePose = new TrackedDevicePose_t[OpenVR.k_unMaxTrackedDeviceCount];
                cvrSystem.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseRawAndUncalibrated, 0f, trackedDevicePose);
                VRControllerState_t controllerState = new VRControllerState_t();
                cvrSystem.GetControllerState(1, ref controllerState,
                                             (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRControllerState_t)));
                int  trigger   = controllerState.rAxis1.x > 0.9f ? 1 : 0;
                bool topButtom = (controllerState.ulButtonPressed & (1ul << (int)EVRButtonId.k_EButton_ApplicationMenu)) != 0;

                TrackedDevicePose_t pose           = trackedDevicePose[1];
                ETrackingResult     trackingResult = pose.eTrackingResult;
                HmdMatrix34_t       hmdMatrix      = pose.mDeviceToAbsoluteTracking;
                Position            pos            = new Position(hmdMatrix);
                Rotation            rot            = new Rotation(hmdMatrix);
                Console.WriteLine($"Position: {pos} Rotation: {rot} trigger {trigger} app {topButtom}");
                foreach (Socket client in _clients.ToArray())
                {
                    try
                    {
                        client.Send(Encoding.ASCII.GetBytes($"S{pos.ToData()} {rot.ToData()} {trigger} {(topButtom ? 1 : 0)}E"));
                    }
                    catch (Exception)
                    {
                        _clients.Remove(client);
                    }
                }
            }
            for (int i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++)
            {
                if (cvrSystem?.IsTrackedDeviceConnected((uint)i) ?? false)
                {
                    ETrackedDeviceClass deviceClass = cvrSystem.GetTrackedDeviceClass((uint)i);
                    Console.WriteLine($"index: {i} is {deviceClass}");
                }
            }
            Console.ReadLine();
            OpenVR.Shutdown();
            Console.WriteLine("Shut down");
            Console.ReadLine();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Instantiate and initialize a new <see cref="Application"/>.
        /// Internally, this will initialize the OpenVR API with the specified
        /// <paramref name="type"/> and <paramref name="startupInfo"/>.
        /// </summary>
        ///
        /// <param name="type"></param>
        /// <param name="startupInfo"></param>
        public Application(ApplicationType type, string startupInfo = "")
        {
            // Attempt to initialize a new OpenVR context
            EVRInitError err = EVRInitError.None;

            OVRSystem = OpenVR.Init(ref err, (EVRApplicationType)type, startupInfo);

            if (err != EVRInitError.None)
            {
                throw new OpenVRSystemException <EVRInitError>("An error occurred while initializing the OpenVR runtime.", err);
            }
        }
        public VrPositioningService()
        {
            EVRInitError evrInitError = default;

            Task.Run(() =>
            {
                // TODO: make vr position in combination work properly with audio emitter/listener
                _vrSystem   = OpenVR.Init(ref evrInitError, EVRApplicationType.VRApplication_Other);
                IsVrEnabled = evrInitError == EVRInitError.None;
                //_isVrEnabled = true;
            });
        }
Exemplo n.º 5
0
 public VrTrackedDevices(CVRSystem ivr)
 {
     this.vr          = ivr;
     AllDevices       = new Dictionary <int, VrTrackedDevice>();
     IndexesByClasses = new Dictionary <string, List <int> >
     {
         { "HMD", new List <int>() },
         { "Controller", new List <int>() },
         { "Tracker", new List <int>() },
         { "Lighthouse", new List <int>() }
     };
 }
    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());
            ApplicationQuit();
            return;
        }

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

        //オーバーレイの大きさ設定(幅のみ。高さはテクスチャの比から自動計算される)
        var width = 2.0f;

        overlay.SetOverlayWidthInMeters(overlayHandle, width);
        //オーバーレイの透明度を設定
        var alpha = 0.5f;

        overlay.SetOverlayAlpha(overlayHandle, alpha);

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

        if (isOpenGL)
        {
            //pGLuintTexture
            overlayTexture.eType = ETextureType.OpenGL;
            //上下反転しない
            textureYflip = 1;
        }
        else
        {
            //pTexture
            overlayTexture.eType = ETextureType.DirectX;
            //上下反転する
            textureYflip = -1;
        }
        Debug.Log(Tag + "初期化完了しました");
    }
Exemplo n.º 7
0
        string GetStringProperty(CVRSystem system, uint deviceId, ETrackedDeviceProperty prop)
        {
            var error  = ETrackedPropertyError.TrackedProp_Success;
            var result = new System.Text.StringBuilder();

            var capacity = system.GetStringTrackedDeviceProperty(deviceId, prop, result, 64, ref error);

            if (capacity > 0)
            {
                return(result.ToString());
            }
            return(string.Empty);
        }
Exemplo n.º 8
0
    void Start()
    {
        // define an error
        var error = EVRInitError.None;

        // Initialize OpenVR with Application Type as other
        vrSystem = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Other);

        // If there is an error, handle the error
        if (error != EVRInitError.None)
        {
        }
    }
        public static string GetControllerProperty(int deviceIndex, ETrackedDeviceProperty p)
        {
            var sbType = new StringBuilder(1000);
            ETrackedPropertyError err    = ETrackedPropertyError.TrackedProp_Success;
            CVRSystem             system = OpenVR.System;

            if (system == null)
            {
                return("SteamVR not yet initialized");
            }
            system.GetStringTrackedDeviceProperty((uint)deviceIndex, p, sbType, 1000, ref err);
            return(sbType.ToString());
        }
Exemplo n.º 10
0
    private void OnDeviceConnected(params object[] args)
    {
        int num = (int)args[0];

        if (num != (int)this.index)
        {
            return;
        }
        base.GetComponent <MeshFilter>().mesh = null;
        bool flag = (bool)args[1];

        if (flag)
        {
            CVRSystem system = OpenVR.System;
            if (system != null && system.GetTrackedDeviceClass((uint)num) == ETrackedDeviceClass.TrackingReference)
            {
                ETrackedPropertyError eTrackedPropertyError = ETrackedPropertyError.TrackedProp_Success;
                float floatTrackedDeviceProperty            = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_FieldOfViewLeftDegrees_Float, ref eTrackedPropertyError);
                if (eTrackedPropertyError == ETrackedPropertyError.TrackedProp_Success)
                {
                    this.fovLeft = floatTrackedDeviceProperty;
                }
                floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_FieldOfViewRightDegrees_Float, ref eTrackedPropertyError);
                if (eTrackedPropertyError == ETrackedPropertyError.TrackedProp_Success)
                {
                    this.fovRight = floatTrackedDeviceProperty;
                }
                floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_FieldOfViewTopDegrees_Float, ref eTrackedPropertyError);
                if (eTrackedPropertyError == ETrackedPropertyError.TrackedProp_Success)
                {
                    this.fovTop = floatTrackedDeviceProperty;
                }
                floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_FieldOfViewBottomDegrees_Float, ref eTrackedPropertyError);
                if (eTrackedPropertyError == ETrackedPropertyError.TrackedProp_Success)
                {
                    this.fovBottom = floatTrackedDeviceProperty;
                }
                floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_TrackingRangeMinimumMeters_Float, ref eTrackedPropertyError);
                if (eTrackedPropertyError == ETrackedPropertyError.TrackedProp_Success)
                {
                    this.nearZ = floatTrackedDeviceProperty;
                }
                floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_TrackingRangeMaximumMeters_Float, ref eTrackedPropertyError);
                if (eTrackedPropertyError == ETrackedPropertyError.TrackedProp_Success)
                {
                    this.farZ = floatTrackedDeviceProperty;
                }
                this.UpdateModel();
            }
        }
    }
Exemplo n.º 11
0
 public bool ResetVR()
 {
     if (_VRSystem != null)
     {
         OpenVR.Shutdown();
         _VRSystem = null;
     }
     if (InitVR())
     {
         Show();
         return(true);
     }
     return(false);
 }
Exemplo n.º 12
0
        public override void Init()
        {
            base.Init();

            // make sure OpenVR is init right away
            EVRInitError e = EVRInitError.None;

            system = OpenVR.System;
            if (system == null)
            {
                system = OpenVR.Init(ref e);
            }
            Debug.Log("OpenVR version: " + system.GetRuntimeVersion());
        }
Exemplo n.º 13
0
        public OpenVRInputObservable(CVRSystem hmd)
        {
            this.hmd = hmd;

            var eventLauncher = new OpenVREventLauncher(hmd);

            InputEvents =
                from evt in Observable.FromEventPattern <OpenVREventArgs>(eventLauncher, nameof(eventLauncher.OpenVREvent))
                let evtType                       = evt.EventArgs.EventType
                                         let data = evt.EventArgs.EventData
                                                    where ControllerEvents.Contains(evtType)
                                                    where evtType == EVREventType.VREvent_ButtonPress || evtType == EVREventType.VREvent_ButtonUnpress
                                                    select new OpenVRControllerInput(hmd, evt.EventArgs.DeviceIndex, evtType, data.controller);
        }
Exemplo n.º 14
0
        bool OpenVRInit()
        {
            var error = EVRInitError.None;

            vrSystem = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Scene);
            if (error != EVRInitError.None || !OpenVR.IsHmdPresent())
            {
                ModConsole.Error("Error in VR init");
                ModConsole.Print(error);
                return(false);
            }

            return(true);
        }
Exemplo n.º 15
0
        public static string GetTrackedDeviceManufacturerString(uint deviceId)
        {
            CVRSystem hmd = SteamVR.instance.hmd;

            var error    = ETrackedPropertyError.TrackedProp_Success;
            var capacity = hmd.GetStringTrackedDeviceProperty(deviceId, ETrackedDeviceProperty.Prop_ManufacturerName_String, null, 0, ref error);

            if (capacity > 1)
            {
                var result = new System.Text.StringBuilder((int)capacity);
                hmd.GetStringTrackedDeviceProperty(deviceId, ETrackedDeviceProperty.Prop_ManufacturerName_String, result, capacity, ref error);
                return(result.ToString());
            }
            return(null);
        }
Exemplo n.º 16
0
 public VrHandler()
 {
     _vrControls   = new VrControls();
     _vrSystem     = OpenVR.System;
     _vrCompositor = OpenVR.Compositor;
     _vrSystem.GetRecommendedRenderTargetSize(ref _width, ref _height);
     _renderPose = new TrackedDevicePose_t[1];
     _gamePose   = new TrackedDevicePose_t[1];
     //var error = default(ETrackedPropertyError);
     //var displayFrequency = _vrSystem.GetFloatTrackedDeviceProperty(0, ETrackedDeviceProperty.Prop_DisplayFrequency_Float, ref error);
     //Check(error);
     //_frameDuration = 1.0f / displayFrequency;
     //_vsyncToPhotons = _vrSystem.GetFloatTrackedDeviceProperty(0, ETrackedDeviceProperty.Prop_SecondsFromVsyncToPhotons_Float, ref error);
     //Check(error);
 }
Exemplo n.º 17
0
        public static void Init()
        {
            EVRInitError error  = EVRInitError.None;
            CVRSystem    system = OpenVR.Init(ref error);

            if (error != EVRInitError.None)
            {
                throw new OpenVRInitException(error);
            }

            if (system == null)
            {
                throw new OpenVRInitException("OpenVR.Init returned null");
            }
        }
Exemplo n.º 18
0
        public override void Evaluate(int SpreadMax, CVRSystem system)
        {
            if ((FHandleIn.IsChanged || FColorSpaceIn.IsChanged) && FHandleIn[0] > 0)
            {
                FTexture = new Texture_t()
                {
                    handle      = new IntPtr(FHandleIn[0]),
                    eType       = ETextureType.DirectX,
                    eColorSpace = FColorSpaceIn[0]
                };
            }

            //set tex
            VRTextureBounds_t boundsL;
            VRTextureBounds_t boundsR;

            if (FIsOUIn[0])
            {
                boundsL = FOUTexBoundsL;
                boundsR = FOUTexBoundsR;
            }
            else
            {
                boundsL = FSBSTexBoundsL;
                boundsR = FSBSTexBoundsR;
            }

            var compositor = OpenVR.Compositor;
            var error      = compositor.Submit(EVREye.Eye_Left, ref FTexture, ref boundsL, EVRSubmitFlags.Submit_Default);

            SetStatus(error);
            if (error != EVRCompositorError.None)
            {
                return;
            }
            error = compositor.Submit(EVREye.Eye_Right, ref FTexture, ref boundsR, EVRSubmitFlags.Submit_Default);
            SetStatus(error);
            if (error != EVRCompositorError.None)
            {
                return;
            }

            //let OpenVR know we are done for this frame
            if (FPostPresentHandoff[0])
            {
                compositor.PostPresentHandoff();
            }
        }