public static object CallSystemFn(SystemFn fn, params object[] args) { object result = null; if (SteamVR.active) { result = fn(SteamVR.instance.hmd, args); } else { var error = HmdError.None; var pSystem = OpenVR.Init(ref error); if (pSystem != System.IntPtr.Zero && error == HmdError.None) { // Make sure we're using the proper version pSystem = OpenVR.GetGenericInterface(OpenVR.IVRSystem_Version, ref error); if (pSystem != System.IntPtr.Zero && error == HmdError.None) { var system = new CVRSystem(pSystem); result = fn(system, args); } OpenVR.Shutdown(); } } return result; }
Matrix4 GetHMDMatrixProjectionEye(ref Valve.VR.CVRSystem HMD, Valve.VR.EVREye eye) { if (HMD == null) { return(new Matrix4()); } Valve.VR.HmdMatrix44_t M = HMD.GetProjectionMatrix(eye, mNearClip, mFarClip); return(UtilOld.steamVRMatrixToMatrix4(M)); }
Matrix4 GetHMDMatrixPoseEye(ref Valve.VR.CVRSystem HMD, Valve.VR.EVREye eye) { if (HMD == null) { return(new Matrix4()); } Valve.VR.HmdMatrix34_t M = HMD.GetEyeToHeadTransform(eye); return(UtilOld.steamVRMatrixToMatrix4(M).Inverted()); }
bool SetupStereoRenderTargets(ref Valve.VR.CVRSystem HMD) { if (HMD == null) { return(false); } HMD.GetRecommendedRenderTargetSize(ref vrRenderWidth, ref vrRenderHeight); CreateFrameBuffer((int)vrRenderWidth, (int)vrRenderHeight, out leftEyeDesc); CreateFrameBuffer((int)vrRenderWidth, (int)vrRenderHeight, out rightEyeDesc); return(true); }
public void Clear() { this.m_pVRSystem = null; this.m_pVRChaperone = null; this.m_pVRChaperoneSetup = null; this.m_pVRCompositor = null; this.m_pVROverlay = null; this.m_pVRRenderModels = null; this.m_pVRExtendedDisplay = null; this.m_pVRSettings = null; this.m_pVRApplications = null; }
public void Update(CVRSystem system, uint id) { if (id == 999) return; //is pressed system.GetControllerState(id, ref m_controllerState); //was pressed ulong buttonsChanged = m_previousButtons ^ m_controllerState.ulButtonPressed; m_buttonPressedAcc |= buttonsChanged & m_controllerState.ulButtonPressed; //was released m_buttonReleasedAcc |= buttonsChanged & ~m_controllerState.ulButtonPressed; m_previousButtons = m_controllerState.ulButtonPressed; }
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); }
public CVRSystem VRSystem() { this.CheckClear(); if (this.m_pVRSystem == null) { EVRInitError eVRInitError = EVRInitError.None; IntPtr genericInterface = OpenVRInterop.GetGenericInterface("FnTable:IVRSystem_011", ref eVRInitError); if (genericInterface != IntPtr.Zero && eVRInitError == EVRInitError.None) { this.m_pVRSystem = new CVRSystem(genericInterface); } } return(this.m_pVRSystem); }
public VrRenderer(ref Valve.VR.CVRSystem HMD, ref Scene scene, uint mRenderWidth, uint mRenderHeight) { mHMD = HMD; mScene = scene; SetupStereoRenderTargets(ref mHMD); //1344 * 1600 vrRenderWidth = mRenderWidth; vrRenderHeight = mRenderHeight; //ovrvision if (enableAR) { ovrvision_controller = new OvrvisionController(ref mHMD, ref mScene); ovrvision_controller.initOVrvision(); } }
private OpenVRScene(float nearClip, float farClip, CVRSystem hmd, int leftIndex, int rightIndex) { // set up the hmd this.hmd = hmd; NearClip = nearClip; FarClip = farClip; leftEyeProj = hmd.GetProjectionMatrix(EVREye.Eye_Left, nearClip, farClip, EGraphicsAPIConvention.API_OpenGL).ToGLMatrix4(); rightEyeProj = hmd.GetProjectionMatrix(EVREye.Eye_Right, nearClip, farClip, EGraphicsAPIConvention.API_OpenGL).ToGLMatrix4(); leftEyeView = hmd.GetEyeToHeadTransform(EVREye.Eye_Left).ToGLMatrix4().Inverted(); rightEyeView = hmd.GetEyeToHeadTransform(EVREye.Eye_Right).ToGLMatrix4().Inverted(); leftHandControllerIndex = leftIndex; rightHandControllerIndex = rightIndex; OpenVRInput = new OpenVRInputObservable(hmd); }
public OpenVRControllerInput(CVRSystem hmd, int index, EVREventType eventType, VREvent_Controller_t contEvent) { State = eventType == EVREventType.VREvent_ButtonPress ? InputState.Start : InputState.Finish; // CONTROLLER KEY BINDINGS ARE HERE switch ((EVRButtonId)contEvent.button) { case EVRButtonId.k_EButton_System: // used by system, dont handle break; case EVRButtonId.k_EButton_ApplicationMenu: Input = InputType.Pause; break; case EVRButtonId.k_EButton_Grip: break; case EVRButtonId.k_EButton_DPad_Left: break; case EVRButtonId.k_EButton_DPad_Up: break; case EVRButtonId.k_EButton_DPad_Right: break; case EVRButtonId.k_EButton_DPad_Down: break; case EVRButtonId.k_EButton_A: break; case EVRButtonId.k_EButton_Axis2: break; case EVRButtonId.k_EButton_Axis3: break; case EVRButtonId.k_EButton_Axis4: break; case EVRButtonId.k_EButton_SteamVR_Touchpad: Input = hmd.ControllerState(index).rAxis0.y > 0 ? InputType.SpeedUp : InputType.SlowDown; break; case EVRButtonId.k_EButton_SteamVR_Trigger: Input = InputType.PrimarySelect; break; case EVRButtonId.k_EButton_Max: // should never fire break; default: break; } }
public static object CallSystemFn(SystemFn fn, params object[] args) { bool initOpenVR = (!SteamVR.active && !SteamVR.usingNativeSupport); if (initOpenVR) { EVRInitError error = EVRInitError.None; OpenVR.Init(ref error, EVRApplicationType.VRApplication_Utility); } CVRSystem system = OpenVR.System; object result = (system != null) ? fn(system, args) : null; if (initOpenVR) { OpenVR.Shutdown(); } return(result); }
public OpenVREventLauncher(CVRSystem hmd) { this.hmd = hmd; timer = new Timer(PollEvents, null, 0, PollIntervalMilis); // automatically starts the timer }
public CVRSystem VRSystem() { this.CheckClear(); if (this.m_pVRSystem == null) { EVRInitError eVRInitError = EVRInitError.None; IntPtr genericInterface = OpenVRInterop.GetGenericInterface("FnTable:IVRSystem_011", ref eVRInitError); if (genericInterface != IntPtr.Zero && eVRInitError == EVRInitError.None) { this.m_pVRSystem = new CVRSystem(genericInterface); } } return this.m_pVRSystem; }
public OvrvisionController(ref Valve.VR.CVRSystem HMD, ref Scene scene) { mHMD = HMD; mScene = scene; }
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 capacity = compositor.GetLastError(null, 0); if (capacity > 1) { var result = new System.Text.StringBuilder((int)capacity); compositor.GetLastError(result, capacity); Debug.Log("Compositor - " + result); } // 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; Unity.SetSubmitParams(textureBounds[0], textureBounds[1], VRSubmitFlags_t.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(Hmd_Eye.Eye_Left)), new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(Hmd_Eye.Eye_Right)) }; if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL")) graphicsAPI = GraphicsAPIConvention.API_OpenGL; else graphicsAPI = GraphicsAPIConvention.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); }
public Scene(ref Rhino.RhinoDoc doc, ref Valve.VR.CVRSystem hmd) { rhinoDoc = doc; mHMD = hmd; }
public CVRSystem VRSystem() { CheckClear(); if (m_pVRSystem == null) { var eError = EVRInitError.None; var pInterface = OpenVRInterop.GetGenericInterface(FnTable_Prefix+IVRSystem_Version, ref eError); if (pInterface != IntPtr.Zero && eError == EVRInitError.None) m_pVRSystem = new CVRSystem(pInterface); } return m_pVRSystem; }
public MyOpenVR() { if (true) { m_viewHMD = Matrix.Identity; m_headsetPosD = MatrixD.Identity; m_c1pos = Matrix.Identity; m_c2pos = Matrix.Identity; //IntPtr Handle = MyRender11.LoadLibrary(@"D:\KeenSWH.VR\Sandbox\Sources\SpaceEngineers\bin\x64\Debug\Bin64\openvr_api.dll"); //IntPtr Handle = LoadLibrary(@"C:\Program Files (x86)\Steam\SteamApps\common\SteamVR\bin\win64\openvr_api.dll"); IntPtr Handle = LoadLibrary(@"openvr_api.dll"); //IntPtr Handle = MyRender11.LoadLibrary(@"c:\Program Files (x86)\Steam\bin\openvr_api.dll");//err code 193 - not a valid Win32 application //Assembly.LoadFile(@"D:\KeenSWH\Sandbox\Sources\SpaceEngineers\bin\x64\Debug\Bin64\openvr_api.dll"); if (Handle == IntPtr.Zero) { int errorCode = Marshal.GetLastWin32Error(); throw new Exception(string.Format("Failed to load library (ErrorCode: {0})", errorCode)); } EVRInitError error = EVRInitError.None; IntPtr ptr = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Scene); if (error == EVRInitError.None) { m_vrSystem = new CVRSystem(ptr); uint sizeX=0,sizeY=0; m_vrSystem.GetRecommendedRenderTargetSize(ref sizeX, ref sizeY); m_vrSystem.SetDisplayVisibility(true); ETrackedPropertyError pError=0; m_ipd_2=0.5f*m_vrSystem.GetFloatTrackedDeviceProperty(0,ETrackedDeviceProperty.Prop_UserIpdMeters_Float,ref pError); SetIPD(m_ipd_2*2); IntPtr pointer = OpenVR.GetGenericInterface(OpenVR.IVRCompositor_Version, ref error); if (error == EVRInitError.None) { m_vrCompositor = new CVRCompositor(pointer); m_vrCompositor.CompositorBringToFront(); m_vrCompositor.ShowMirrorWindow(); m_openVR = this; } else { var errString = OpenVR.GetStringForHmdError(error); Log.WriteLineAndConsole(errString); Debug.Fail("No compositor interface"); throw new Exception(errString); } } else { var errString = OpenVR.GetStringForHmdError(error); Log.WriteLineAndConsole(errString); //Debug.Fail("OpenVR init failed"); throw new Exception(errString); } InitOverlays(); } }
public void Clear() { m_pVRSystem = null; m_pVRChaperone = null; m_pVRChaperoneSetup = null; m_pVRCompositor = null; m_pVROverlay = null; m_pVRRenderModels = null; m_pVRExtendedDisplay = null; m_pVRSettings = null; m_pVRApplications = null; }
private void Update() { if (!SteamVR.active) { return; } this.UpdatePoses(); CVRSystem system = OpenVR.System; if (system != null) { VREvent_t vrevent_t = default(VREvent_t); uint uncbVREvent = (uint)Marshal.SizeOf(typeof(VREvent_t)); int num = 0; while (num < 64 && system.PollNextEvent(ref vrevent_t, uncbVREvent)) { EVREventType eventType = (EVREventType)vrevent_t.eventType; if (eventType <= EVREventType.VREvent_InputFocusReleased) { if (eventType != EVREventType.VREvent_InputFocusCaptured) { if (eventType != EVREventType.VREvent_InputFocusReleased) { goto IL_CA; } if (vrevent_t.data.process.pid == 0u) { SteamVR_Events.InputFocus.Send(true); } } else if (vrevent_t.data.process.oldPid == 0u) { SteamVR_Events.InputFocus.Send(false); } } else if (eventType != EVREventType.VREvent_HideRenderModels) { if (eventType != EVREventType.VREvent_ShowRenderModels) { goto IL_CA; } SteamVR_Events.HideRenderModels.Send(false); } else { SteamVR_Events.HideRenderModels.Send(true); } IL_C4: num++; continue; IL_CA: SteamVR_Events.System((EVREventType)vrevent_t.eventType).Send(vrevent_t); goto IL_C4; } } Application.targetFrameRate = -1; Application.runInBackground = true; QualitySettings.maxQueuedFrames = -1; QualitySettings.vSyncCount = 0; if (SteamVR.settings.lockPhysicsUpdateRateToRenderFrequency && Time.timeScale > 0f) { SteamVR instance = SteamVR.instance; if (instance != null) { Time.fixedDeltaTime = Time.timeScale / instance.hmd_DisplayFrequency; } } }
private static SteamVR CreateInstance() { SteamVR.initializedState = SteamVR.InitializedStates.Initializing; try { EVRInitError evrinitError = EVRInitError.None; OpenVR.GetGenericInterface("IVRCompositor_022", ref evrinitError); OpenVR.Init(ref evrinitError, EVRApplicationType.VRApplication_Scene, ""); CVRSystem system = OpenVR.System; string manifestFile = SteamVR.GetManifestFile(); EVRApplicationError evrapplicationError = OpenVR.Applications.AddApplicationManifest(manifestFile, true); if (evrapplicationError != EVRApplicationError.None) { UnityEngine.Debug.LogError("<b>[SteamVR_Standalone]</b> Error adding vr manifest file: " + evrapplicationError.ToString()); } int id = Process.GetCurrentProcess().Id; OpenVR.Applications.IdentifyApplication((uint)id, SteamVR_Settings.instance.editorAppKey); UnityEngine.Debug.Log("Is HMD here? " + OpenVR.IsHmdPresent().ToString()); if (evrinitError != EVRInitError.None) { SteamVR.initializedState = SteamVR.InitializedStates.InitializeFailure; SteamVR.ReportError(evrinitError); SteamVR.ReportGeneralErrors(); SteamVR_Events.Initializing.Send(false); return(null); } OpenVR.GetGenericInterface("IVROverlay_021", ref evrinitError); if (evrinitError != EVRInitError.None) { SteamVR.initializedState = SteamVR.InitializedStates.InitializeFailure; SteamVR.ReportError(evrinitError); SteamVR_Events.Initializing.Send(false); return(null); } OpenVR.GetGenericInterface("IVRInput_007", ref evrinitError); if (evrinitError != EVRInitError.None) { SteamVR.initializedState = SteamVR.InitializedStates.InitializeFailure; SteamVR.ReportError(evrinitError); SteamVR_Events.Initializing.Send(false); return(null); } SteamVR.settings = SteamVR_Settings.instance; if (Application.isEditor) { SteamVR.IdentifyEditorApplication(true); } SteamVR_Input.IdentifyActionsFile(true); if (SteamVR_Settings.instance.inputUpdateMode != SteamVR_UpdateModes.Nothing || SteamVR_Settings.instance.poseUpdateMode != SteamVR_UpdateModes.Nothing) { SteamVR_Input.Initialize(false); } } catch (Exception arg) { UnityEngine.Debug.LogError("<b>[SteamVR_Standalone]</b> " + arg); SteamVR_Events.Initializing.Send(false); return(null); } SteamVR._enabled = true; SteamVR.initializedState = SteamVR.InitializedStates.InitializeSuccess; SteamVR_Events.Initializing.Send(true); return(new SteamVR()); }
private SteamVR(System.IntPtr pHmd, System.IntPtr pCompositor) { hmd = new CVRSystem(pHmd); Debug.Log("Connected to " + hmd_TrackingSystemName + ":" + hmd_SerialNumber); compositor = new CVRCompositor(pCompositor); 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 Compositor_TextureBounds[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; 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); }