public static void Init(CefSettings settings = null) { Overlays = new List <WebKitOverlay>(); if (settings == null) { settings = new CefSettings(); } AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; gw = new GameWindow(300, 30); // Invisible GL Context GL.Enable(EnableCap.Texture2D); Cef.Initialize(settings); InitOpenVR(); _system = OpenVR.System; _compositor = OpenVR.Compositor; _overlay = OpenVR.Overlay; _applications = OpenVR.Applications; _controllerManager = new SteamVR_ControllerManager(); Console.WriteLine("SteamVR_WebKit Initialised"); _initialised = true; }
// Token: 0x06005F5A RID: 24410 RVA: 0x00217CD0 File Offset: 0x002160D0 private IEnumerator RenderLoop() { while (Application.isPlaying) { yield return(this.waitForEndOfFrame); if (!SteamVR_Render.pauseRendering) { CVRCompositor compositor = OpenVR.Compositor; if (compositor != null) { if (!compositor.CanRenderScene()) { continue; } compositor.SetTrackingSpace(this.trackingSpace); } SteamVR_Overlay overlay = SteamVR_Overlay.instance; if (overlay != null) { overlay.UpdateOverlay(); } this.RenderExternalCamera(); } } yield break; }
/// <summary> /// Internal start call for VR setup. /// </summary> public void Start() { uint w = 0; uint h = 0; VR.GetRecommendedRenderTargetSize(ref w, ref h); if (w <= 0 || h <= 0) { throw new Exception("Failed to start VR: Invalid render target size!"); } w *= 2; TheClient.Engine3D.MainView.Generate(TheClient.Engine3D, (int)w, (int)h); TheClient.Engine3D.MainView.GenerateFBO(); StringBuilder val = new StringBuilder(256); ETrackedPropertyError errx = ETrackedPropertyError.TrackedProp_Success; VR.GetStringTrackedDeviceProperty(OpenVR.k_unTrackedDeviceIndex_Hmd, ETrackedDeviceProperty.Prop_TrackingSystemName_String, val, 256, ref errx); SysConsole.Output(OutputType.INIT, "Switching to VR mode: " + w + "/" + h + "... " + val.ToString()); VRModel = val.ToString(); Compositor = OpenVR.Compositor; Compositor.SetTrackingSpace(ETrackingUniverseOrigin.TrackingUniverseStanding); Compositor.CompositorBringToFront(); LeftTexture = new VRControllerTextureEngine(); RightTexture = new VRControllerTextureEngine(); LeftTexture.BaseTexture = TheClient.Textures.GetTexture("vr/controller/vive_circle_left"); RightTexture.BaseTexture = TheClient.Textures.GetTexture("vr/controller/vive_circle_right"); LeftTexture.GenerateFirst(); RightTexture.GenerateFirst(); RightControllerModel = TheClient.Models.GetModel("vr/controller/vive"); LeftControllerModel = TheClient.Models.GetModel("vr/controller/vive"); }
public static Compositor_CumulativeStats GetCumulativeStats(this CVRCompositor compositor) { Compositor_CumulativeStats pStats = default(Compositor_CumulativeStats); OpenVR.Compositor.GetCumulativeStats(ref pStats, CUMULATIVESTATS_SIZE); return(pStats); }
// Token: 0x06005F92 RID: 24466 RVA: 0x00219C10 File Offset: 0x00218010 private void Update() { if (this.text != null) { if (Input.GetKeyDown(KeyCode.I)) { this.text.enabled = !this.text.enabled; } if (this.text.enabled) { CVRCompositor compositor = OpenVR.Compositor; if (compositor != null) { Compositor_FrameTiming compositor_FrameTiming = default(Compositor_FrameTiming); compositor_FrameTiming.m_nSize = (uint)Marshal.SizeOf(typeof(Compositor_FrameTiming)); compositor.GetFrameTiming(ref compositor_FrameTiming, 0u); double flSystemTimeInSeconds = compositor_FrameTiming.m_flSystemTimeInSeconds; if (flSystemTimeInSeconds > this.lastUpdate) { double num = (this.lastUpdate <= 0.0) ? 0.0 : (1.0 / (flSystemTimeInSeconds - this.lastUpdate)); this.lastUpdate = flSystemTimeInSeconds; this.text.text = string.Format("framerate: {0:N0}\ndropped frames: {1}", num, (int)compositor_FrameTiming.m_nNumDroppedFrames); } else { this.lastUpdate = flSystemTimeInSeconds; } } } } }
public static Compositor_FrameTiming GetFrameTiming(this CVRCompositor compositor, uint unFramesAgo) { Compositor_FrameTiming pTiming = default(Compositor_FrameTiming); pTiming.m_nSize = FRAMETIMING_SIZE; OpenVR.Compositor.GetFrameTiming(ref pTiming, unFramesAgo); return(pTiming); }
// Token: 0x06000DF1 RID: 3569 RVA: 0x00058C84 File Offset: 0x00056E84 public static void ClearOverride() { CVRCompositor compositor = OpenVR.Compositor; if (compositor != null) { compositor.ClearSkyboxOverride(); } }
// Token: 0x06000D82 RID: 3458 RVA: 0x00054D60 File Offset: 0x00052F60 public static void View(Color newColor, float duration) { CVRCompositor compositor = OpenVR.Compositor; if (compositor != null) { compositor.FadeToColor(duration, newColor.r, newColor.g, newColor.b, newColor.a, false); } }
// Token: 0x06005F63 RID: 24419 RVA: 0x002180DC File Offset: 0x002164DC public void UpdatePoses() { CVRCompositor compositor = OpenVR.Compositor; if (compositor != null) { compositor.GetLastPoses(this.poses, this.gamePoses); SteamVR_Events.NewPoses.Send(this.poses); SteamVR_Events.NewPosesApplied.Send(); } }
public static void Init(CefSettings settings = null) { Overlays = new List <WebKitOverlay>(); if (settings == null) { settings = new CefSettings(); } gw = new GameWindow(300, 30); // Invisible GL Context GL.Enable(EnableCap.Texture2D); if (Cef.IsInitialized) { Cef.Shutdown(); } Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null); bool tryAgain = true; while (tryAgain && !_doStop) { try { InitOpenVR(); tryAgain = false; } catch (Exception e) { Log(e.Message); Log("Trying again in 3 seconds"); Thread.Sleep(3000); } } if (_doStop) { CefShutdown(); return; } _system = OpenVR.System; _compositor = OpenVR.Compositor; _overlay = OpenVR.Overlay; _applications = OpenVR.Applications; _controllerManager = new SteamVR_ControllerManager(); SteamVR_WebKit.Log("SteamVR_WebKit Initialised"); _initialised = true; }
internal VRSystem() { if (!OpenVR.IsRuntimeInstalled()) { throw new Exception("VR Runtime not installed"); } if (!OpenVR.IsHmdPresent()) { throw new Exception("HMD not found"); } EVRInitError error = EVRInitError.None; vrContext = OpenVR.Init(ref error); if (error != EVRInitError.None) { Console.WriteLine(error); } compositor = OpenVR.Compositor; RenderModel = OpenVR.GetGenericInterface(OpenVR.IVRRenderModels_Version, ref error); if (RenderModel == IntPtr.Zero) { ; } { Console.WriteLine(error); } controllerSystem = new VRControllerSystem(vrContext); leftTexture = new Texture_t(); leftTexture.eType = ETextureType.OpenGL; leftTexture.eColorSpace = EColorSpace.Gamma; rightTexture = new Texture_t(); rightTexture.eType = ETextureType.OpenGL; rightTexture.eColorSpace = EColorSpace.Gamma; vrContext.GetRecommendedRenderTargetSize(ref height, ref width); bound = new VRTextureBounds_t(); bound.uMin = 0; bound.vMin = 0; bound.vMax = 1; bound.uMax = 1; /* * var errProp = new ETrackedPropertyError(); * var str = new StringBuilder(); * vrContext.GetStringTrackedDeviceProperty(OpenVR.k_unTrackedDeviceIndex_Hmd, ETrackedDeviceProperty.Prop_TrackingSystemName_String, str, 100, ref errProp); */ }
private void OnPreCull() { CVRCompositor compositor = OpenVR.Compositor; if (compositor != null) { SteamVR_Render instance = SteamVR_Render.instance; compositor.GetLastPoses(instance.poses, instance.gamePoses); SteamVR_Utils.Event.Send("new_poses", new object[] { instance.poses }); SteamVR_Utils.Event.Send("new_poses_applied", new object[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); }
private void SubmitTexture(CVRCompositor compositor, GPUTexture colorTex, EVREye eye) { Texture_t texT; var renderer = GPUDevice.RendererType; if (renderer == RendererType.DirectX10 || renderer == RendererType.DirectX10_1 || renderer == RendererType.DirectX11) { texT.handle = colorTex.NativePtr; texT.eColorSpace = EColorSpace.Gamma; texT.eType = ETextureType.DirectX; } else { throw new Exception($"Renderer '{renderer}' is not yet supported"); } /*if (rt == RendererType.DirectX12) * { * texT.handle = colorTex.NativePtr; * texT.eColorSpace = EColorSpace.Gamma; * texT.eType = ETextureType.DirectX12; * } * * if(rt == RendererType.Vulkan) * { * texT.handle = colorTex.NativePtr; * texT.eColorSpace = EColorSpace.Gamma; * texT.eType = ETextureType.Vulkan; * }*/ VRTextureBounds_t boundsT; boundsT.uMin = 0; boundsT.uMax = 1; boundsT.vMin = 0; boundsT.vMax = 1; EVRCompositorError compositorError = EVRCompositorError.None; compositorError = compositor.Submit(eye, ref texT, ref boundsT, EVRSubmitFlags.Submit_Default); if (compositorError != EVRCompositorError.None) { throw new Exception($"Failed to submit to the OpenVR Compositor: {compositorError}"); } }
void Awake() { compositor = OpenVR.Compositor; if (text == null) { text = GetComponent <Text>(); text.enabled = false; } if (fadeDuration > 0) { SteamVR_Fade.Start(fadeColor, 0); SteamVR_Fade.Start(Color.clear, fadeDuration); } }
public OpenVRContext(VRContextOptions options) { _options = options; EVRInitError initError = EVRInitError.None; _vrSystem = OVR.Init(ref initError, EVRApplicationType.VRApplication_Scene); if (initError != EVRInitError.None) { throw new Exception($"Failed to initialize OpenVR: {OVR.GetStringForHmdError(initError)}"); } _compositor = OVR.Compositor; if (_compositor == null) { throw new Exception("Failed to access the OpenVR Compositor."); } }
// Token: 0x06000DF0 RID: 3568 RVA: 0x00058BA4 File Offset: 0x00056DA4 public static void SetOverride(Texture front = null, Texture back = null, Texture left = null, Texture right = null, Texture top = null, Texture bottom = null) { CVRCompositor compositor = OpenVR.Compositor; if (compositor != null) { Texture[] array = new Texture[] { front, back, left, right, top, bottom }; Texture_t[] array2 = new Texture_t[6]; for (int i = 0; i < 6; i++) { array2[i].handle = ((array[i] != null) ? array[i].GetNativeTexturePtr() : IntPtr.Zero); array2[i].eType = SteamVR.instance.textureType; array2[i].eColorSpace = EColorSpace.Auto; } EVRCompositorError evrcompositorError = compositor.SetSkyboxOverride(array2); if (evrcompositorError != EVRCompositorError.None) { Debug.LogError("Failed to set skybox override with error: " + evrcompositorError); if (evrcompositorError == EVRCompositorError.TextureIsOnWrongDevice) { Debug.Log("Set your graphics driver to use the same video card as the headset is plugged into for Unity."); return; } if (evrcompositorError == EVRCompositorError.TextureUsesUnsupportedFormat) { Debug.Log("Ensure skybox textures are not compressed and have no mipmaps."); } } } }
//----------------------コンポジターfps取得------------------- public double GetCompositorFrameTime() { CVRCompositor cVRCompositor = OpenVR.Compositor; if (cVRCompositor == null) { return(float.NaN); } //現在 Compositor_FrameTiming pTiming0 = new Compositor_FrameTiming(); pTiming0.m_nSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(Compositor_FrameTiming)); if (!cVRCompositor.GetFrameTiming(ref pTiming0, 0)) { return(float.NaN); } double frameTime0 = pTiming0.m_flTotalRenderGpuMs; double fps = 1f / frameTime0; return(frameTime0); }
private void Update() { if (Input.anyKeyDown) { print("Got Key Down, Count: " + ++_count + ", Player " + (!_hasPaused ? "Paused" : "Playing")); if (Input.GetKeyDown(KeyCode.Escape)) { print("Got Key Down, Code: Escape"); _hasPaused = true; _toAnotherScene = true; enabled = false; ConfigurationManager.Test = (int)_count; SceneManager.LoadScene(ConfigurationManager.MenuSceneName, LoadSceneMode.Single); return; } _hasPaused = !_hasPaused; } if (UseVrDevice && _compositor == null) { _compositor = SteamVR.instance.compositor; } var frameTiming = new Compositor_FrameTiming { m_nSize = (uint)Marshal.SizeOf(typeof(Compositor_FrameTiming)) }; if (!UseVrDevice || _compositor != null && _compositor.GetFrameTiming(ref frameTiming, 0)) { if (frameTiming.m_nFrameIndex == _lastVrFrameIndex) { return; } _lastVrFrameIndex = frameTiming.m_nFrameIndex; } }
public void Start() { uint w = 0; uint h = 0; VR.GetRecommendedRenderTargetSize(ref w, ref h); if (w <= 0 || h <= 0) { throw new Exception("Failed to start VR: Invalid render target size!"); } w *= 2; TheClient.MainWorldView.Generate(TheClient, (int)w, (int)h); TheClient.MainWorldView.GenerateFBO(); SysConsole.Output(OutputType.INFO, "Switching to VR mode: " + w + "/" + h); Compositor = OpenVR.Compositor; Compositor.SetTrackingSpace(ETrackingUniverseOrigin.TrackingUniverseStanding); Compositor.CompositorBringToFront(); LeftTexture = new VRControllerTextureEngine(); RightTexture = new VRControllerTextureEngine(); LeftTexture.BaseTexture = TheClient.Textures.GetTexture("vr/controller/vive_circle_left"); RightTexture.BaseTexture = TheClient.Textures.GetTexture("vr/controller/vive_circle_right"); LeftTexture.GenerateFirst(); RightTexture.GenerateFirst(); }
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); } // Hook up the render thread event. 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)) }; 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); }
internal static void Enable() { var initError = EVRInitError.None; System = OpenVR.Init(ref initError); if (initError != EVRInitError.None) { return; } Compositor = OpenVR.Compositor; Compositor.CompositorBringToFront(); Compositor.FadeGrid(5.0f, false); var count = OpenVR.k_unMaxTrackedDeviceCount; CurrentPoses = new TrackedDevicePose_t[count]; NextPoses = new TrackedDevicePose_t[count]; Controllers = new List <uint>(); ControllerModels = new RenderModel_t[count]; ControllerTextures = new RenderModel_TextureMap_t[count]; ControllerTextureViews = new ShaderResourceView[count]; ControllerVertexBuffers = new SharpDX.Direct3D11.Buffer[count]; ControllerIndexBuffers = new SharpDX.Direct3D11.Buffer[count]; ControllerVertexBufferBindings = new VertexBufferBinding[count]; ControllerEmitters = new Emitter[count]; ControllerVoices = new SourceVoice[count]; for (uint device = 0; device < count; device++) { var deviceClass = System.GetTrackedDeviceClass(device); switch (deviceClass) { case ETrackedDeviceClass.HMD: Headset = device; break; case ETrackedDeviceClass.Controller: Controllers.Add(device); break; } } uint width = 0; uint height = 0; System.GetRecommendedRenderTargetSize(ref width, ref height); headsetSize = new Size((int)width, (int)height); windowSize = new Size(960, 540); var leftEyeProjection = Convert(System.GetProjectionMatrix(EVREye.Eye_Left, 0.01f, 1000.0f)); var rightEyeProjection = Convert(System.GetProjectionMatrix(EVREye.Eye_Right, 0.01f, 1000.0f)); var leftEyeView = Convert(System.GetEyeToHeadTransform(EVREye.Eye_Left)); var rightEyeView = Convert(System.GetEyeToHeadTransform(EVREye.Eye_Right)); foreach (var controller in Controllers) { var modelName = new StringBuilder(255, 255); var propertyError = ETrackedPropertyError.TrackedProp_Success; var length = System.GetStringTrackedDeviceProperty(controller, ETrackedDeviceProperty.Prop_RenderModelName_String, modelName, 255, ref propertyError); if (propertyError == ETrackedPropertyError.TrackedProp_Success) { var modelName2 = modelName.ToString(); while (true) { var pointer = IntPtr.Zero; var modelError = EVRRenderModelError.None; modelError = OpenVR.RenderModels.LoadRenderModel_Async(modelName2, ref pointer); if (modelError == EVRRenderModelError.Loading) { continue; } if (modelError == EVRRenderModelError.None) { var renderModel = global::System.Runtime.InteropServices.Marshal.PtrToStructure <RenderModel_t>(pointer); ControllerModels[controller] = renderModel; break; } } while (true) { var pointer = IntPtr.Zero; var textureError = EVRRenderModelError.None; textureError = OpenVR.RenderModels.LoadTexture_Async(ControllerModels[controller].diffuseTextureId, ref pointer); if (textureError == EVRRenderModelError.Loading) { continue; } if (textureError == EVRRenderModelError.None) { var texture = global::System.Runtime.InteropServices.Marshal.PtrToStructure <RenderModel_TextureMap_t>(pointer); ControllerTextures[controller] = texture; break; } } } } int adapterIndex = 0; System.GetDXGIOutputInfo(ref adapterIndex); Thread = new Thread(Run); Thread.Start(); }
private void setup() { //init VR System and check for errors var error = EVRInitError.None; vrSystem = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Scene); if ((int)error != (int)EVRInitError.None) { log("KerbalVrPlugin started."); } else { err(error.ToString()); } //rendervalues ######################################################### // Setup render values uint w = 0, h = 0; vrSystem.GetRecommendedRenderTargetSize(ref w, ref h); float sceneWidth = (float)w; float sceneHeight = (float)h; float l_left = 0.0f, l_right = 0.0f, l_top = 0.0f, l_bottom = 0.0f; vrSystem.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; vrSystem.GetProjectionRaw(EVREye.Eye_Right, ref r_left, ref r_right, ref r_top, ref r_bottom); Vector2 tanHalfFov = new Vector2(Mathf.Max(-l_left, l_right, -r_left, r_right), Mathf.Max(-l_top, l_bottom, -r_top, r_bottom)); //Setup rendertextures hmdLeftEyeTexture = new Texture_t(); hmdLeftEyeTexture.eColorSpace = EColorSpace.Auto; hmdRightEyeTexture = new Texture_t(); hmdRightEyeTexture.eColorSpace = EColorSpace.Auto; //select Texture Type depending on RenderAPI (Currently only DirectX11 is tested) switch (SystemInfo.graphicsDeviceType) { case UnityEngine.Rendering.GraphicsDeviceType.OpenGL2: log("OpenGL2"); hmdLeftEyeTexture.eType = ETextureType.OpenGL; hmdRightEyeTexture.eType = ETextureType.OpenGL; break; case UnityEngine.Rendering.GraphicsDeviceType.OpenGLCore: log("OpenCore"); hmdLeftEyeTexture.eType = ETextureType.OpenGL; hmdRightEyeTexture.eType = ETextureType.OpenGL; break; case UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2: log("OpenGLES2"); hmdLeftEyeTexture.eType = ETextureType.OpenGL; hmdRightEyeTexture.eType = ETextureType.OpenGL; break; case UnityEngine.Rendering.GraphicsDeviceType.OpenGLES3: log("OpenGLES3"); hmdLeftEyeTexture.eType = ETextureType.OpenGL; hmdRightEyeTexture.eType = ETextureType.OpenGL; break; case UnityEngine.Rendering.GraphicsDeviceType.Direct3D9: log("Direct3D9"); warn("DirectX 9 mode not Supported! There be Dragons!"); hmdLeftEyeTexture.eType = ETextureType.DirectX; hmdRightEyeTexture.eType = ETextureType.DirectX; break; case UnityEngine.Rendering.GraphicsDeviceType.Direct3D11: log("Direct3D11"); hmdLeftEyeTexture.eType = ETextureType.DirectX; hmdRightEyeTexture.eType = ETextureType.DirectX; break; case UnityEngine.Rendering.GraphicsDeviceType.Direct3D12: log("Direct3D12"); warn("DirectX 12 mode not implemented! There be Dragons!"); hmdLeftEyeTexture.eType = ETextureType.DirectX12; hmdRightEyeTexture.eType = ETextureType.DirectX12; break; default: throw (new Exception(SystemInfo.graphicsDeviceType.ToString() + " not supported")); } vrCompositor = OpenVR.Compositor; if (!vrCompositor.CanRenderScene()) { err("can not render scene"); } }
static void Main() { var initError = EVRInitError.None; system = OpenVR.Init(ref initError); if (initError != EVRInitError.None) { return; } compositor = OpenVR.Compositor; compositor.CompositorBringToFront(); compositor.FadeGrid(5.0f, false); count = OpenVR.k_unMaxTrackedDeviceCount; currentPoses = new TrackedDevicePose_t[count]; nextPoses = new TrackedDevicePose_t[count]; controllers = new List <uint>(); controllerModels = new RenderModel_t[count]; controllerTextures = new RenderModel_TextureMap_t[count]; controllerTextureViews = new ShaderResourceView[count]; controllerVertexBuffers = new SharpDX.Direct3D11.Buffer[count]; controllerIndexBuffers = new SharpDX.Direct3D11.Buffer[count]; controllerVertexBufferBindings = new VertexBufferBinding[count]; for (uint device = 0; device < count; device++) { var deviceClass = system.GetTrackedDeviceClass(device); switch (deviceClass) { case ETrackedDeviceClass.HMD: headset = device; break; case ETrackedDeviceClass.Controller: controllers.Add(device); break; } } uint width = 0; uint height = 0; system.GetRecommendedRenderTargetSize(ref width, ref height); headsetSize = new Size((int)width, (int)height); windowSize = new Size(960, 540); var leftEyeProjection = Convert(system.GetProjectionMatrix(EVREye.Eye_Left, 0.01f, 1000.0f)); var rightEyeProjection = Convert(system.GetProjectionMatrix(EVREye.Eye_Right, 0.01f, 1000.0f)); var leftEyeView = Convert(system.GetEyeToHeadTransform(EVREye.Eye_Left)); var rightEyeView = Convert(system.GetEyeToHeadTransform(EVREye.Eye_Right)); foreach (var controller in controllers) { var modelName = new StringBuilder(255, 255); var propertyError = ETrackedPropertyError.TrackedProp_Success; var length = system.GetStringTrackedDeviceProperty(controller, ETrackedDeviceProperty.Prop_RenderModelName_String, modelName, 255, ref propertyError); if (propertyError == ETrackedPropertyError.TrackedProp_Success) { var modelName2 = modelName.ToString(); while (true) { var pointer = IntPtr.Zero; var modelError = EVRRenderModelError.None; modelError = OpenVR.RenderModels.LoadRenderModel_Async(modelName2, ref pointer); if (modelError == EVRRenderModelError.Loading) { continue; } if (modelError == EVRRenderModelError.None) { var renderModel = System.Runtime.InteropServices.Marshal.PtrToStructure <RenderModel_t>(pointer); controllerModels[controller] = renderModel; break; } } while (true) { var pointer = IntPtr.Zero; var textureError = EVRRenderModelError.None; textureError = OpenVR.RenderModels.LoadTexture_Async(controllerModels[controller].diffuseTextureId, ref pointer); if (textureError == EVRRenderModelError.Loading) { continue; } if (textureError == EVRRenderModelError.None) { var texture = System.Runtime.InteropServices.Marshal.PtrToStructure <RenderModel_TextureMap_t>(pointer); controllerTextures[controller] = texture; break; } } } } int adapterIndex = 0; system.GetDXGIOutputInfo(ref adapterIndex); using (var form = new Form()) using (var factory = new Factory4()) { form.ClientSize = windowSize; var adapter = factory.GetAdapter(adapterIndex); var swapChainDescription = new SwapChainDescription { BufferCount = 1, Flags = SwapChainFlags.None, IsWindowed = true, ModeDescription = new ModeDescription { Format = Format.B8G8R8A8_UNorm, Width = form.ClientSize.Width, Height = form.ClientSize.Height, RefreshRate = new Rational(60, 1) }, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; SharpDX.Direct3D11.Device.CreateWithSwapChain(adapter, DeviceCreationFlags.None, swapChainDescription, out device, out swapChain); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.None); context = device.ImmediateContext; using (var backBuffer = swapChain.GetBackBuffer <Texture2D>(0)) backBufferView = new RenderTargetView(device, backBuffer); var depthBufferDescription = new Texture2DDescription { Format = Format.D16_UNorm, ArraySize = 1, MipLevels = 1, Width = form.ClientSize.Width, Height = form.ClientSize.Height, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }; using (var depthBuffer = new Texture2D(device, depthBufferDescription)) depthStencilView = new DepthStencilView(device, depthBuffer); // Create Eye Textures var eyeTextureDescription = new Texture2DDescription { ArraySize = 1, BindFlags = BindFlags.RenderTarget, CpuAccessFlags = CpuAccessFlags.None, Format = Format.B8G8R8A8_UNorm, Width = headsetSize.Width, Height = headsetSize.Height, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; var leftEyeTexture = new Texture2D(device, eyeTextureDescription); var rightEyeTexture = new Texture2D(device, eyeTextureDescription); var leftEyeTextureView = new RenderTargetView(device, leftEyeTexture); var rightEyeTextureView = new RenderTargetView(device, rightEyeTexture); // Create Eye Depth Buffer eyeTextureDescription.BindFlags = BindFlags.DepthStencil; eyeTextureDescription.Format = Format.D32_Float; var eyeDepth = new Texture2D(device, eyeTextureDescription); var eyeDepthView = new DepthStencilView(device, eyeDepth); Shapes.Cube.Load(device); Shapes.Sphere.Load(device); Shaders.Position.Load(device); Shaders.Normal.Load(device); Shaders.NormalTexture.Load(device); // Load Controller Models foreach (var controller in controllers) { var model = controllerModels[controller]; controllerVertexBuffers[controller] = new SharpDX.Direct3D11.Buffer(device, model.rVertexData, new BufferDescription { BindFlags = BindFlags.VertexBuffer, SizeInBytes = (int)model.unVertexCount * 32 }); controllerVertexBufferBindings[controller] = new VertexBufferBinding(controllerVertexBuffers[controller], 32, 0); controllerIndexBuffers[controller] = new SharpDX.Direct3D11.Buffer(device, model.rIndexData, new BufferDescription { BindFlags = BindFlags.IndexBuffer, SizeInBytes = (int)model.unTriangleCount * 3 * 2 }); var texture = controllerTextures[controller]; using (var texture2d = new Texture2D(device, new Texture2DDescription { ArraySize = 1, BindFlags = BindFlags.ShaderResource, Format = Format.R8G8B8A8_UNorm, Width = texture.unWidth, Height = texture.unHeight, MipLevels = 1, SampleDescription = new SampleDescription(1, 0) }, new DataRectangle(texture.rubTextureMapData, texture.unWidth * 4))) controllerTextureViews[controller] = new ShaderResourceView(device, texture2d); } worldViewProjectionBuffer = new SharpDX.Direct3D11.Buffer(device, Utilities.SizeOf <Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0); var rasterizerStateDescription = RasterizerStateDescription.Default(); //rasterizerStateDescription.FillMode = FillMode.Wireframe; rasterizerStateDescription.IsFrontCounterClockwise = true; //rasterizerStateDescription.CullMode = CullMode.None; rasterizerState = new RasterizerState(device, rasterizerStateDescription); var blendStateDescription = BlendStateDescription.Default(); blendStateDescription.RenderTarget[0].BlendOperation = BlendOperation.Add; blendStateDescription.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha; blendStateDescription.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha; blendStateDescription.RenderTarget[0].IsBlendEnabled = false; blendState = new BlendState(device, blendStateDescription); var depthStateDescription = DepthStencilStateDescription.Default(); depthStateDescription.DepthComparison = Comparison.LessEqual; depthStateDescription.IsDepthEnabled = true; depthStateDescription.IsStencilEnabled = false; depthStencilState = new DepthStencilState(device, depthStateDescription); var samplerStateDescription = SamplerStateDescription.Default(); samplerStateDescription.Filter = Filter.MinMagMipLinear; samplerStateDescription.AddressU = TextureAddressMode.Wrap; samplerStateDescription.AddressV = TextureAddressMode.Wrap; samplerState = new SamplerState(device, samplerStateDescription); startTime = DateTime.Now; frame = 0; windowSize = form.ClientSize; backgroundColor = new RawColor4(0.1f, 0.1f, 0.1f, 1); var vrEvent = new VREvent_t(); var eventSize = (uint)Utilities.SizeOf <VREvent_t>(); head = Matrix.Identity; // Initialize Audio var audioSamples = 1024; var audioDevices = DirectSoundCapture.GetDevices(); //var audioCapture = new DirectSoundCapture(devices.OrderByDescending(d => d.Description.Contains("Mic")).First().DriverGuid); var audioCapture = new DirectSoundCapture(audioDevices.OrderByDescending(d => d.Description.Contains("Mix")).First().DriverGuid); var audioFormat = new WaveFormat(); var audioLength = audioFormat.ConvertLatencyToByteSize(24); var audioData = new byte[audioLength]; var audioPosition = 0; var leftWaveForm = new float[1024 * 8]; var rightWaveForm = new float[1024 * 8]; for (var sample = 0; sample < 1024; sample++) { leftWaveForm[(sample * 8) + 0] = -1.0f + ((float)sample / 512.0f); rightWaveForm[(sample * 8) + 0] = -1.0f + ((float)sample / 512.0f); } var audioBuffer = new CaptureBuffer(audioCapture, new CaptureBufferDescription { BufferBytes = audioLength, Format = audioFormat }); audioBuffer.Start(true); var waveFormBufferDescription = new BufferDescription { BindFlags = BindFlags.VertexBuffer, SizeInBytes = leftWaveForm.Length * sizeof(float), CpuAccessFlags = CpuAccessFlags.Write, Usage = ResourceUsage.Dynamic }; var leftWaveFormVertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, leftWaveForm, waveFormBufferDescription); var rightWaveFormVertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, rightWaveForm, waveFormBufferDescription); var leftWaveFormVertexBufferBinding = new VertexBufferBinding(leftWaveFormVertexBuffer, 8 * sizeof(float), 0); var rightWaveFormVertexBufferBinding = new VertexBufferBinding(rightWaveFormVertexBuffer, 8 * sizeof(float), 0); RenderLoop.Run(form, () => { if (audioBuffer.CurrentCapturePosition != audioBuffer.CurrentRealPosition) { audioBuffer.Read(audioData, 0, audioData.Length, 0, LockFlags.None); for (var sample = 0; sample < 1024; sample++) { leftWaveForm[(sample * 8) + 1] = -BitConverter.ToInt16(audioData, sample * 4) / (float)short.MinValue; rightWaveForm[(sample * 8) + 1] = -BitConverter.ToInt16(audioData, (sample * 4) + 2) / (float)short.MinValue; } DataStream stream; context.MapSubresource(leftWaveFormVertexBuffer, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out stream); stream.WriteRange(leftWaveForm); context.UnmapSubresource(leftWaveFormVertexBuffer, 0); stream.Dispose(); context.MapSubresource(rightWaveFormVertexBuffer, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out stream); stream.WriteRange(rightWaveForm); context.UnmapSubresource(rightWaveFormVertexBuffer, 0); stream.Dispose(); } audioPosition += 8; if (audioPosition >= leftWaveForm.Length) { audioPosition = 0; } while (system.PollNextEvent(ref vrEvent, eventSize)) { switch ((EVREventType)vrEvent.eventType) { case EVREventType.VREvent_TrackedDeviceActivated: var controller = vrEvent.trackedDeviceIndex; controllers.Add(controller); var modelName = new StringBuilder(255, 255); var propertyError = ETrackedPropertyError.TrackedProp_Success; var length = system.GetStringTrackedDeviceProperty(controller, ETrackedDeviceProperty.Prop_RenderModelName_String, modelName, 255, ref propertyError); if (propertyError == ETrackedPropertyError.TrackedProp_Success) { var modelName2 = modelName.ToString(); while (true) { var pointer = IntPtr.Zero; var modelError = EVRRenderModelError.None; modelError = OpenVR.RenderModels.LoadRenderModel_Async(modelName2, ref pointer); if (modelError == EVRRenderModelError.Loading) { continue; } if (modelError == EVRRenderModelError.None) { var renderModel = System.Runtime.InteropServices.Marshal.PtrToStructure <RenderModel_t>(pointer); controllerModels[controller] = renderModel; // Load Controller Model var model = controllerModels[controller]; controllerVertexBuffers[controller] = new SharpDX.Direct3D11.Buffer(device, model.rVertexData, new BufferDescription { BindFlags = BindFlags.VertexBuffer, SizeInBytes = (int)model.unVertexCount * 32 }); controllerVertexBufferBindings[controller] = new VertexBufferBinding(controllerVertexBuffers[controller], 32, 0); controllerIndexBuffers[controller] = new SharpDX.Direct3D11.Buffer(device, model.rIndexData, new BufferDescription { BindFlags = BindFlags.IndexBuffer, SizeInBytes = (int)model.unTriangleCount * 3 * 2 }); break; } } while (true) { var pointer = IntPtr.Zero; var textureError = EVRRenderModelError.None; textureError = OpenVR.RenderModels.LoadTexture_Async(controllerModels[controller].diffuseTextureId, ref pointer); if (textureError == EVRRenderModelError.Loading) { continue; } if (textureError == EVRRenderModelError.None) { var textureMap = System.Runtime.InteropServices.Marshal.PtrToStructure <RenderModel_TextureMap_t>(pointer); controllerTextures[controller] = textureMap; using (var texture2d = new Texture2D(device, new Texture2DDescription { ArraySize = 1, BindFlags = BindFlags.ShaderResource, Format = Format.R8G8B8A8_UNorm, Width = textureMap.unWidth, Height = textureMap.unHeight, MipLevels = 1, SampleDescription = new SampleDescription(1, 0) }, new DataRectangle(textureMap.rubTextureMapData, textureMap.unWidth * 4))) controllerTextureViews[controller] = new ShaderResourceView(device, texture2d); break; } } } break; case EVREventType.VREvent_TrackedDeviceDeactivated: controllers.RemoveAll(c => c == vrEvent.trackedDeviceIndex); break; default: System.Diagnostics.Debug.WriteLine((EVREventType)vrEvent.eventType); break; } } if (form.ClientSize != windowSize) { Utilities.Dispose(ref backBufferView); if (form.ClientSize.Width != 0 && form.ClientSize.Height != 0) { swapChain.ResizeBuffers(1, form.ClientSize.Width, form.ClientSize.Height, Format.B8G8R8A8_UNorm, SwapChainFlags.None); using (var backBuffer = swapChain.GetBackBuffer <Texture2D>(0)) backBufferView = new RenderTargetView(device, backBuffer); } windowSize = form.ClientSize; } // Update Device Tracking compositor.WaitGetPoses(currentPoses, nextPoses); if (currentPoses[headset].bPoseIsValid) { Convert(ref currentPoses[headset].mDeviceToAbsoluteTracking, ref head); } foreach (var controller in controllers) { var controllerMatrix = Matrix.Identity; Convert(ref currentPoses[controller].mDeviceToAbsoluteTracking, ref controllerMatrix); } // Render Left Eye context.Rasterizer.SetViewport(0, 0, headsetSize.Width, headsetSize.Height); context.OutputMerger.SetTargets(eyeDepthView, leftEyeTextureView); context.OutputMerger.SetDepthStencilState(depthStencilState); context.ClearRenderTargetView(leftEyeTextureView, backgroundColor); context.ClearDepthStencilView(eyeDepthView, DepthStencilClearFlags.Depth, 1.0f, 0); Shaders.Normal.Apply(context); context.Rasterizer.State = rasterizerState; context.OutputMerger.SetBlendState(blendState); context.OutputMerger.SetDepthStencilState(depthStencilState); context.PixelShader.SetSampler(0, samplerState); var ratio = (float)headsetSize.Width / (float)headsetSize.Height; var projection = leftEyeProjection; var view = Matrix.Invert(leftEyeView * head); var world = Matrix.Scaling(1.0f + (Math.Abs(leftWaveForm[audioPosition + 1]) * 0.1f)) * Matrix.Translation(0, 1.0f, 0); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer); //Shapes.Cube.Begin(context); //Shapes.Cube.Draw(context); Shapes.Sphere.Begin(context); Shapes.Sphere.Draw(context); // Draw Controllers context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; Shaders.NormalTexture.Apply(context); context.PixelShader.SetSampler(0, samplerState); foreach (var controller in controllers) { context.InputAssembler.SetVertexBuffers(0, controllerVertexBufferBindings[controller]); context.InputAssembler.SetIndexBuffer(controllerIndexBuffers[controller], Format.R16_UInt, 0); context.PixelShader.SetShaderResource(0, controllerTextureViews[controller]); Convert(ref currentPoses[controller].mDeviceToAbsoluteTracking, ref world); world = Matrix.Scaling(1.0f + (Math.Abs(leftWaveForm[audioPosition + 1]) * 0.5f)) * world; worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer); context.DrawIndexed((int)controllerModels[controller].unTriangleCount * 3 * 4, 0, 0); } // Draw Waveforms Shaders.Position.Apply(context); world = Matrix.Scaling(100, 2.5f, 1) * Matrix.Translation(0, 1, 1); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineStrip; context.InputAssembler.SetVertexBuffers(0, leftWaveFormVertexBufferBinding); context.Draw(1024, 0); world = Matrix.Scaling(100, 2.5f, 1) * Matrix.Translation(0, 1, -1); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.InputAssembler.SetVertexBuffers(0, rightWaveFormVertexBufferBinding); context.Draw(1024, 0); // Present Left Eye var texture = new Texture_t { eType = ETextureType.DirectX, eColorSpace = EColorSpace.Gamma, handle = leftEyeTextureView.Resource.NativePointer }; var bounds = new VRTextureBounds_t { uMin = 0.0f, uMax = 1.0f, vMin = 0.0f, vMax = 1.0f, }; var submitError = compositor.Submit(EVREye.Eye_Left, ref texture, ref bounds, EVRSubmitFlags.Submit_Default); if (submitError != EVRCompositorError.None) { System.Diagnostics.Debug.WriteLine(submitError); } // Render Right Eye context.Rasterizer.SetViewport(0, 0, headsetSize.Width, headsetSize.Height); context.OutputMerger.SetTargets(eyeDepthView, rightEyeTextureView); context.OutputMerger.SetDepthStencilState(depthStencilState); context.ClearRenderTargetView(rightEyeTextureView, backgroundColor); context.ClearDepthStencilView(eyeDepthView, DepthStencilClearFlags.Depth, 1.0f, 0); Shaders.Normal.Apply(context); context.Rasterizer.State = rasterizerState; context.OutputMerger.SetBlendState(blendState); context.OutputMerger.SetDepthStencilState(depthStencilState); context.PixelShader.SetSampler(0, samplerState); projection = rightEyeProjection; view = Matrix.Invert(rightEyeView * head); world = Matrix.Scaling(1.0f + (Math.Abs(leftWaveForm[audioPosition + 1]) * 0.1f)) * Matrix.Translation(0, 1.0f, 0); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer); //Shapes.Cube.Begin(context); //Shapes.Cube.Draw(context); Shapes.Sphere.Begin(context); Shapes.Sphere.Draw(context); // Draw Controllers context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; Shaders.NormalTexture.Apply(context); context.PixelShader.SetSampler(0, samplerState); foreach (var controller in controllers) { context.InputAssembler.SetVertexBuffers(0, controllerVertexBufferBindings[controller]); context.InputAssembler.SetIndexBuffer(controllerIndexBuffers[controller], Format.R16_UInt, 0); context.PixelShader.SetShaderResource(0, controllerTextureViews[controller]); Convert(ref currentPoses[controller].mDeviceToAbsoluteTracking, ref world); world = Matrix.Scaling(1.0f + (Math.Abs(leftWaveForm[audioPosition + 1]) * 0.5f)) * world; worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer); context.DrawIndexed((int)controllerModels[controller].unTriangleCount * 3 * 4, 0, 0); } // Draw Waveforms Shaders.Position.Apply(context); world = Matrix.Scaling(100, 2.5f, 1) * Matrix.Translation(0, 1, 1); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineStrip; context.InputAssembler.SetVertexBuffers(0, leftWaveFormVertexBufferBinding); context.Draw(1024, 0); world = Matrix.Scaling(100, 2.5f, 1) * Matrix.Translation(0, 1, -1); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.InputAssembler.SetVertexBuffers(0, rightWaveFormVertexBufferBinding); context.Draw(1024, 0); // Present Right Eye texture.handle = rightEyeTextureView.Resource.NativePointer; submitError = compositor.Submit(EVREye.Eye_Right, ref texture, ref bounds, EVRSubmitFlags.Submit_Default); if (submitError != EVRCompositorError.None) { System.Diagnostics.Debug.WriteLine(submitError); } // Render Window context.Rasterizer.SetViewport(0, 0, windowSize.Width, windowSize.Height); context.OutputMerger.SetTargets(depthStencilView, backBufferView); context.OutputMerger.SetDepthStencilState(depthStencilState); context.ClearRenderTargetView(backBufferView, backgroundColor); context.ClearDepthStencilView(depthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0); Shaders.Normal.Apply(context); context.Rasterizer.State = rasterizerState; context.OutputMerger.SetBlendState(blendState); context.OutputMerger.SetDepthStencilState(depthStencilState); context.PixelShader.SetSampler(0, samplerState); ratio = (float)form.ClientSize.Width / (float)form.ClientSize.Height; projection = Matrix.PerspectiveFovRH(3.14f / 3.0f, ratio, 0.01f, 1000); view = Matrix.Invert(head); world = Matrix.Scaling(1.0f + (Math.Abs(leftWaveForm[audioPosition + 1]) * 0.1f)) * Matrix.Translation(0, 1.0f, 0); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer); //Shapes.Cube.Begin(context); //Shapes.Cube.Draw(context); Shapes.Sphere.Begin(context); Shapes.Sphere.Draw(context); // Draw Controllers context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; Shaders.NormalTexture.Apply(context); context.PixelShader.SetSampler(0, samplerState); foreach (var controller in controllers) { context.InputAssembler.SetVertexBuffers(0, controllerVertexBufferBindings[controller]); context.InputAssembler.SetIndexBuffer(controllerIndexBuffers[controller], Format.R16_UInt, 0); Convert(ref currentPoses[controller].mDeviceToAbsoluteTracking, ref world); world = Matrix.Scaling(1.0f + (Math.Abs(leftWaveForm[audioPosition + 1]) * 0.5f)) * world; worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer); context.DrawIndexed((int)controllerModels[controller].unTriangleCount * 3 * 4, 0, 0); } // Draw Waveforms Shaders.Position.Apply(context); world = Matrix.Scaling(100, 2.5f, 1) * Matrix.Translation(0, 1, 1); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineStrip; context.InputAssembler.SetVertexBuffers(0, leftWaveFormVertexBufferBinding); context.Draw(1024, 0); world = Matrix.Scaling(100, 2.5f, 1) * Matrix.Translation(0, 1, -1); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.InputAssembler.SetVertexBuffers(0, rightWaveFormVertexBufferBinding); context.Draw(1024, 0); // Show Backbuffer swapChain.Present(0, PresentFlags.None); }); } }
/// <summary> /// Initialize HMD using OpenVR API calls. /// </summary> /// <returns>True on success, false otherwise. Errors logged.</returns> bool InitHMD() { bool retVal = false; // return if HMD has already been initialized if (hmdIsInitialized) { return(true); } bool is64bit = (IntPtr.Size == 8); string mypath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); log("OpenVR path set to " + Path.Combine(mypath, is64bit ? "win64" : "win32")); SetDllDirectory(Path.Combine(mypath, is64bit ? "win64" : "win32")); // check if HMD is connected on the system retVal = OpenVR.IsHmdPresent(); if (!retVal) { err("HMD not found on this system."); return(retVal); } // check if SteamVR runtime is installed. // For this plugin, MAKE SURE IT IS ALREADY RUNNING. retVal = OpenVR.IsRuntimeInstalled(); if (!retVal) { err("SteamVR runtime not found on this system."); return(retVal); } // initialize HMD EVRInitError hmdInitErrorCode = EVRInitError.None; vrSystem = OpenVR.Init(ref hmdInitErrorCode, EVRApplicationType.VRApplication_Scene); // return if failure retVal = (hmdInitErrorCode == EVRInitError.None); if (!retVal) { err("Failed to initialize HMD. Init returned: " + OpenVR.GetStringForHmdError(hmdInitErrorCode)); return(retVal); } else { log("OpenVR.Init passed."); } // reset "seated position" and capture initial position. this means you should hold the HMD in // the position you would like to consider "seated", before running this code. ResetInitialHmdPosition(); // initialize Compositor vrCompositor = OpenVR.Compositor; // initialize render textures (for displaying on HMD) uint renderTextureWidth = 0; uint renderTextureHeight = 0; vrSystem.GetRecommendedRenderTargetSize(ref renderTextureWidth, ref renderTextureHeight); //renderTextureWidth /= 2; //renderTextureHeight /= 2; log("Render Texture size: " + renderTextureWidth + " x " + renderTextureHeight); hmdLeftEyeRenderTexture = new RenderTexture((int)renderTextureWidth, (int)renderTextureHeight, 24, RenderTextureFormat.ARGB32); hmdLeftEyeRenderTexture.Create(); hmdRightEyeRenderTexture = new RenderTexture((int)renderTextureWidth, (int)renderTextureHeight, 24, RenderTextureFormat.ARGB32); hmdRightEyeRenderTexture.Create(); hmdLeftEyeTexture.handle = hmdLeftEyeRenderTexture.GetNativeTexturePtr(); hmdLeftEyeTexture.eColorSpace = EColorSpace.Auto; hmdRightEyeTexture.handle = hmdRightEyeRenderTexture.GetNativeTexturePtr(); hmdRightEyeTexture.eColorSpace = EColorSpace.Auto; switch (SystemInfo.graphicsDeviceType) { case UnityEngine.Rendering.GraphicsDeviceType.OpenGL2: case UnityEngine.Rendering.GraphicsDeviceType.OpenGLCore: case UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2: case UnityEngine.Rendering.GraphicsDeviceType.OpenGLES3: hmdLeftEyeTexture.eType = EGraphicsAPIConvention.API_OpenGL; hmdRightEyeTexture.eType = EGraphicsAPIConvention.API_OpenGL; break; //doesnt work in unity 5.4 with current SteamVR (12/2016) case UnityEngine.Rendering.GraphicsDeviceType.Direct3D9: throw (new Exception("DirectX9 not supported")); case UnityEngine.Rendering.GraphicsDeviceType.Direct3D11: hmdLeftEyeTexture.eType = EGraphicsAPIConvention.API_DirectX; hmdRightEyeTexture.eType = EGraphicsAPIConvention.API_DirectX; break; default: throw (new Exception(SystemInfo.graphicsDeviceType.ToString() + " not supported")); } // Set rendering bounds on texture to render? // I assume min=0.0 and max=1.0 renders to the full extent of the texture hmdTextureBounds.uMin = 0.0f; hmdTextureBounds.uMax = 1.0f; hmdTextureBounds.vMin = 0.0f; hmdTextureBounds.vMax = 1.0f; // TODO: Need to understand better how to create render targets and incorporate hidden area mask mesh foreach (Camera camera in Camera.allCameras) { log("KSP Camera: " + camera.name); } // search for camera objects to render foreach (string cameraName in cameraNamesToRender) { foreach (Camera camera in Camera.allCameras) { if (cameraName.Equals(camera.name)) { float nearClipPlane = (camera.name.Equals(cameraNames[3])) ? 0.05f : camera.nearClipPlane; HmdMatrix44_t projLeft = vrSystem.GetProjectionMatrix(EVREye.Eye_Left, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_OpenGL); HmdMatrix44_t projRight = vrSystem.GetProjectionMatrix(EVREye.Eye_Right, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_OpenGL); //HmdMatrix44_t projLeft = vrSystem.GetProjectionMatrix(EVREye.Eye_Left, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_DirectX); // this doesn't seem to work //HmdMatrix44_t projRight = vrSystem.GetProjectionMatrix(EVREye.Eye_Right, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_DirectX); // this doesn't seem to work camerasToRender.Add(new CameraProperties(camera, camera.projectionMatrix, MathUtils.Matrix4x4_OpenVr2UnityFormat(ref projLeft), MathUtils.Matrix4x4_OpenVr2UnityFormat(ref projRight))); break; } } } // detect controllers for (uint idx = 0; idx < OpenVR.k_unMaxTrackedDeviceCount; idx++) { if ((ctrlIndexLeft == 0) && (vrSystem.GetTrackedDeviceClass(idx) == ETrackedDeviceClass.Controller)) { ctrlIndexLeft = idx; } else if ((ctrlIndexRight == 0) && (vrSystem.GetTrackedDeviceClass(idx) == ETrackedDeviceClass.Controller)) { ctrlIndexRight = idx; } } bool ctrlFocusCaptured = vrSystem.CaptureInputFocus(); if (!ctrlFocusCaptured) { warn("Controller input focus was not captured"); } initTmr.Start(); return(retVal); }
public void InitRendering(UI ui) { lock (_drawLock) { if (ui == null) { return; } _ui = ui; ResizeRedraw = true; var initError = EVRInitError.None; system = OpenVR.Init(ref initError); if (initError != EVRInitError.None) { throw new Exception("Not Available"); } compositor = OpenVR.Compositor; compositor.CompositorBringToFront(); compositor.FadeGrid(5.0f, false); count = OpenVR.k_unMaxTrackedDeviceCount; currentPoses = new TrackedDevicePose_t[count]; nextPoses = new TrackedDevicePose_t[count]; controllers = new List <uint>(); controllerModels = new RenderModel_t[count]; controllerTextures = new RenderModel_TextureMap_t[count]; controllerTextureViews = new ShaderResourceView[count]; controllerVertexBuffers = new SharpDX.Direct3D11.Buffer[count]; controllerIndexBuffers = new SharpDX.Direct3D11.Buffer[count]; controllerVertexBufferBindings = new VertexBufferBinding[count]; for (uint device = 0; device < count; device++) { var deviceClass = system.GetTrackedDeviceClass(device); switch (deviceClass) { case ETrackedDeviceClass.HMD: headset = device; break; case ETrackedDeviceClass.Controller: controllers.Add(device); break; } } uint width = 0; uint height = 0; system.GetRecommendedRenderTargetSize(ref width, ref height); headsetSize = new Size((int)width, (int)height); windowSize = new Size(UI.GameWidth, UI.GameHeight); leftEyeProjection = Convert(system.GetProjectionMatrix(EVREye.Eye_Left, 0.01f, 1000.0f)); rightEyeProjection = Convert(system.GetProjectionMatrix(EVREye.Eye_Right, 0.01f, 1000.0f)); leftEyeView = Convert(system.GetEyeToHeadTransform(EVREye.Eye_Left)); rightEyeView = Convert(system.GetEyeToHeadTransform(EVREye.Eye_Right)); foreach (var controller in controllers) { var modelName = new StringBuilder(255, 255); var propertyError = ETrackedPropertyError.TrackedProp_Success; var length = system.GetStringTrackedDeviceProperty(controller, ETrackedDeviceProperty.Prop_RenderModelName_String, modelName, 255, ref propertyError); if (propertyError == ETrackedPropertyError.TrackedProp_Success) { var modelName2 = modelName.ToString(); while (true) { var pointer = IntPtr.Zero; var modelError = EVRRenderModelError.None; modelError = OpenVR.RenderModels.LoadRenderModel_Async(modelName2, ref pointer); if (modelError == EVRRenderModelError.Loading) { continue; } if (modelError == EVRRenderModelError.None) { var renderModel = System.Runtime.InteropServices.Marshal.PtrToStructure <RenderModel_t>(pointer); controllerModels[controller] = renderModel; break; } } while (true) { var pointer = IntPtr.Zero; var textureError = EVRRenderModelError.None; textureError = OpenVR.RenderModels.LoadTexture_Async(controllerModels[controller].diffuseTextureId, ref pointer); if (textureError == EVRRenderModelError.Loading) { continue; } if (textureError == EVRRenderModelError.None) { var texture = System.Runtime.InteropServices.Marshal.PtrToStructure <RenderModel_TextureMap_t>(pointer); controllerTextures[controller] = texture; break; } } } } int adapterIndex = 0; system.GetDXGIOutputInfo(ref adapterIndex); using (var factory = new SharpDX.DXGI.Factory4()) { var adapter = factory.GetAdapter(adapterIndex); var swapChainDescription = new SwapChainDescription { BufferCount = 1, Flags = SwapChainFlags.None, IsWindowed = true, ModeDescription = new ModeDescription { Format = Format.B8G8R8A8_UNorm, Width = windowSize.Width, Height = windowSize.Height, RefreshRate = new Rational(60, 1) }, OutputHandle = this.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; SharpDX.Direct3D11.Device.CreateWithSwapChain(adapter, DeviceCreationFlags.None, swapChainDescription, out device, out swapChain); factory.MakeWindowAssociation(this.Handle, WindowAssociationFlags.None); context = device.ImmediateContext; using (var backBuffer = swapChain.GetBackBuffer <Texture2D>(0)) backBufferView = new RenderTargetView(device, backBuffer); var depthBufferDescription = new Texture2DDescription { Format = Format.D16_UNorm, ArraySize = 1, MipLevels = 1, Width = windowSize.Width, Height = windowSize.Height, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }; using (var depthBuffer = new Texture2D(device, depthBufferDescription)) depthStencilView = new DepthStencilView(device, depthBuffer); // Create Eye Textures var eyeTextureDescription = new Texture2DDescription { ArraySize = 1, BindFlags = BindFlags.RenderTarget, CpuAccessFlags = CpuAccessFlags.None, Format = Format.B8G8R8A8_UNorm, Width = headsetSize.Width, Height = headsetSize.Height, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; leftEyeTexture = new Texture2D(device, eyeTextureDescription); rightEyeTexture = new Texture2D(device, eyeTextureDescription); leftEyeTextureView = new RenderTargetView(device, leftEyeTexture); rightEyeTextureView = new RenderTargetView(device, rightEyeTexture); // Create Eye Depth Buffer eyeTextureDescription.BindFlags = BindFlags.DepthStencil; eyeTextureDescription.Format = Format.D32_Float; eyeDepth = new Texture2D(device, eyeTextureDescription); eyeDepthView = new DepthStencilView(device, eyeDepth); Shapes.Cube.Load(device); Shapes.Sphere.Load(device); Shaders.Normal.Load(device); Shaders.NormalTexture.Load(device); // Load Controller Models foreach (var controller in controllers) { var model = controllerModels[controller]; controllerVertexBuffers[controller] = new SharpDX.Direct3D11.Buffer(device, model.rVertexData, new BufferDescription { BindFlags = BindFlags.VertexBuffer, SizeInBytes = (int)model.unVertexCount * 32 }); controllerVertexBufferBindings[controller] = new VertexBufferBinding(controllerVertexBuffers[controller], 32, 0); controllerIndexBuffers[controller] = new SharpDX.Direct3D11.Buffer(device, model.rIndexData, new BufferDescription { BindFlags = BindFlags.IndexBuffer, SizeInBytes = (int)model.unTriangleCount * 3 * 2 }); var texture = controllerTextures[controller]; using (var texture2d = new Texture2D(device, new Texture2DDescription { ArraySize = 1, BindFlags = BindFlags.ShaderResource, Format = Format.R8G8B8A8_UNorm, Width = texture.unWidth, Height = texture.unHeight, MipLevels = 1, SampleDescription = new SampleDescription(1, 0) }, new DataRectangle(texture.rubTextureMapData, texture.unWidth * 4))) controllerTextureViews[controller] = new ShaderResourceView(device, texture2d); } shaderParameterBuffer = new SharpDX.Direct3D11.Buffer(device, Utilities.SizeOf <Shaders.Parameters>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0); var rasterizerStateDescription = RasterizerStateDescription.Default(); //rasterizerStateDescription.FillMode = FillMode.Wireframe; rasterizerStateDescription.IsFrontCounterClockwise = true; //rasterizerStateDescription.CullMode = CullMode.None; rasterizerState = new RasterizerState(device, rasterizerStateDescription); var blendStateDescription = BlendStateDescription.Default(); blendStateDescription.RenderTarget[0].BlendOperation = BlendOperation.Add; blendStateDescription.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha; blendStateDescription.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha; blendStateDescription.RenderTarget[0].IsBlendEnabled = false; blendState = new BlendState(device, blendStateDescription); var depthStateDescription = DepthStencilStateDescription.Default(); depthStateDescription.DepthComparison = Comparison.LessEqual; depthStateDescription.IsDepthEnabled = true; depthStateDescription.IsStencilEnabled = false; depthStencilState = new DepthStencilState(device, depthStateDescription); var samplerStateDescription = SamplerStateDescription.Default(); samplerStateDescription.Filter = Filter.MinMagMipLinear; samplerStateDescription.AddressU = TextureAddressMode.Wrap; samplerStateDescription.AddressV = TextureAddressMode.Wrap; samplerState = new SamplerState(device, samplerStateDescription); startTime = DateTime.Now; frame = 0; //windowSize = ClientSize; backgroundColor = new RawColor4(0.1f, 0.1f, 0.1f, 1); head = Matrix.Identity; _ui.ready = true; } } }
private void SubmitTexture(CVRCompositor compositor, Texture colorTex, EVREye eye) { Texture_t texT; if (_gd.GetD3D11Info(out BackendInfoD3D11 d3dInfo)) { texT.eColorSpace = EColorSpace.Gamma; texT.eType = ETextureType.DirectX; texT.handle = d3dInfo.GetTexturePointer(colorTex); } else if (_gd.GetOpenGLInfo(out BackendInfoOpenGL openglInfo)) { texT.eColorSpace = EColorSpace.Gamma; texT.eType = ETextureType.OpenGL; texT.handle = (IntPtr)openglInfo.GetTextureName(colorTex); } else if (_gd.GetVulkanInfo(out BackendInfoVulkan vkInfo)) { vkInfo.TransitionImageLayout(colorTex, (uint)Vulkan.VkImageLayout.TransferSrcOptimal); VRVulkanTextureData_t vkTexData; vkTexData.m_nImage = vkInfo.GetVkImage(colorTex); vkTexData.m_pDevice = vkInfo.Device; vkTexData.m_pPhysicalDevice = vkInfo.PhysicalDevice; vkTexData.m_pInstance = vkInfo.Instance; vkTexData.m_pQueue = vkInfo.GraphicsQueue; vkTexData.m_nQueueFamilyIndex = vkInfo.GraphicsQueueFamilyIndex; vkTexData.m_nWidth = colorTex.Width; vkTexData.m_nHeight = colorTex.Height; vkTexData.m_nFormat = (uint)VkFormats.VdToVkPixelFormat( colorTex.Format, (colorTex.Usage & TextureUsage.DepthStencil) != 0); vkTexData.m_nSampleCount = GetSampleCount(colorTex.SampleCount); texT.eColorSpace = EColorSpace.Gamma; texT.eType = ETextureType.Vulkan; unsafe { texT.handle = (IntPtr)(&vkTexData); } } else { throw new NotSupportedException(); } VRTextureBounds_t boundsT; boundsT.uMin = 0; boundsT.uMax = 1; boundsT.vMin = 0; boundsT.vMax = 1; EVRCompositorError compositorError = EVRCompositorError.None; if (_gd.GetOpenGLInfo(out BackendInfoOpenGL glInfo)) { glInfo.ExecuteOnGLThread(() => { compositorError = compositor.Submit(eye, ref texT, ref boundsT, EVRSubmitFlags.Submit_Default); }); } else { compositorError = compositor.Submit(eye, ref texT, ref boundsT, EVRSubmitFlags.Submit_Default); } if (compositorError != EVRCompositorError.None) { throw new VeldridException($"Failed to submit to the OpenVR Compositor: {compositorError}"); } }
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(); } }
static void Main() { var initError = EVRInitError.None; system = OpenVR.Init(ref initError); if (initError != EVRInitError.None) { return; } compositor = OpenVR.Compositor; compositor.CompositorBringToFront(); compositor.FadeGrid(5.0f, false); count = OpenVR.k_unMaxTrackedDeviceCount; currentPoses = new TrackedDevicePose_t[count]; nextPoses = new TrackedDevicePose_t[count]; controllers = new List <uint>(); controllerModels = new RenderModel_t[count]; controllerTextures = new RenderModel_TextureMap_t[count]; controllerTextureViews = new ShaderResourceView[count]; controllerVertexBuffers = new SharpDX.Direct3D11.Buffer[count]; controllerIndexBuffers = new SharpDX.Direct3D11.Buffer[count]; controllerVertexBufferBindings = new VertexBufferBinding[count]; controllerEmitters = new Emitter[count]; controllerVoices = new SourceVoice[count]; for (uint device = 0; device < count; device++) { var deviceClass = system.GetTrackedDeviceClass(device); switch (deviceClass) { case ETrackedDeviceClass.HMD: headset = device; break; case ETrackedDeviceClass.Controller: controllers.Add(device); break; } } uint width = 0; uint height = 0; system.GetRecommendedRenderTargetSize(ref width, ref height); headsetSize = new Size((int)width, (int)height); windowSize = new Size(960, 540); var leftEyeProjection = Convert(system.GetProjectionMatrix(EVREye.Eye_Left, 0.01f, 1000.0f)); var rightEyeProjection = Convert(system.GetProjectionMatrix(EVREye.Eye_Right, 0.01f, 1000.0f)); var leftEyeView = Convert(system.GetEyeToHeadTransform(EVREye.Eye_Left)); var rightEyeView = Convert(system.GetEyeToHeadTransform(EVREye.Eye_Right)); foreach (var controller in controllers) { var modelName = new StringBuilder(255, 255); var propertyError = ETrackedPropertyError.TrackedProp_Success; var length = system.GetStringTrackedDeviceProperty(controller, ETrackedDeviceProperty.Prop_RenderModelName_String, modelName, 255, ref propertyError); if (propertyError == ETrackedPropertyError.TrackedProp_Success) { var modelName2 = modelName.ToString(); while (true) { var pointer = IntPtr.Zero; var modelError = EVRRenderModelError.None; modelError = OpenVR.RenderModels.LoadRenderModel_Async(modelName2, ref pointer); if (modelError == EVRRenderModelError.Loading) { continue; } if (modelError == EVRRenderModelError.None) { var renderModel = System.Runtime.InteropServices.Marshal.PtrToStructure <RenderModel_t>(pointer); controllerModels[controller] = renderModel; break; } } while (true) { var pointer = IntPtr.Zero; var textureError = EVRRenderModelError.None; textureError = OpenVR.RenderModels.LoadTexture_Async(controllerModels[controller].diffuseTextureId, ref pointer); if (textureError == EVRRenderModelError.Loading) { continue; } if (textureError == EVRRenderModelError.None) { var texture = System.Runtime.InteropServices.Marshal.PtrToStructure <RenderModel_TextureMap_t>(pointer); controllerTextures[controller] = texture; break; } } } } int adapterIndex = 0; system.GetDXGIOutputInfo(ref adapterIndex); using (var form = new Form()) using (var factory = new Factory4()) { form.ClientSize = windowSize; var adapter = factory.GetAdapter(adapterIndex); var swapChainDescription = new SwapChainDescription { BufferCount = 1, Flags = SwapChainFlags.None, IsWindowed = true, ModeDescription = new ModeDescription { Format = Format.B8G8R8A8_UNorm, Width = form.ClientSize.Width, Height = form.ClientSize.Height, RefreshRate = new Rational(60, 1) }, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; SharpDX.Direct3D11.Device.CreateWithSwapChain(adapter, DeviceCreationFlags.None, swapChainDescription, out device, out swapChain); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.None); context = device.ImmediateContext; using (var backBuffer = swapChain.GetBackBuffer <Texture2D>(0)) backBufferView = new RenderTargetView(device, backBuffer); var depthBufferDescription = new Texture2DDescription { Format = Format.D16_UNorm, ArraySize = 1, MipLevels = 1, Width = form.ClientSize.Width, Height = form.ClientSize.Height, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }; using (var depthBuffer = new Texture2D(device, depthBufferDescription)) depthStencilView = new DepthStencilView(device, depthBuffer); // Create Eye Textures var eyeTextureDescription = new Texture2DDescription { ArraySize = 1, BindFlags = BindFlags.RenderTarget, CpuAccessFlags = CpuAccessFlags.None, Format = Format.B8G8R8A8_UNorm, Width = headsetSize.Width, Height = headsetSize.Height, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; var leftEyeTexture = new Texture2D(device, eyeTextureDescription); var rightEyeTexture = new Texture2D(device, eyeTextureDescription); var leftEyeTextureView = new RenderTargetView(device, leftEyeTexture); var rightEyeTextureView = new RenderTargetView(device, rightEyeTexture); // Create Eye Depth Buffer eyeTextureDescription.BindFlags = BindFlags.DepthStencil; eyeTextureDescription.Format = Format.D32_Float; var eyeDepth = new Texture2D(device, eyeTextureDescription); var eyeDepthView = new DepthStencilView(device, eyeDepth); Shapes.Cube.Load(device); Shapes.Sphere.Load(device); Shaders.Load(device); // Load Controller Models foreach (var controller in controllers) { var model = controllerModels[controller]; controllerVertexBuffers[controller] = new SharpDX.Direct3D11.Buffer(device, model.rVertexData, new BufferDescription { BindFlags = BindFlags.VertexBuffer, SizeInBytes = (int)model.unVertexCount * 32 }); controllerVertexBufferBindings[controller] = new VertexBufferBinding(controllerVertexBuffers[controller], 32, 0); controllerIndexBuffers[controller] = new SharpDX.Direct3D11.Buffer(device, model.rIndexData, new BufferDescription { BindFlags = BindFlags.IndexBuffer, SizeInBytes = (int)model.unTriangleCount * 3 * 2 }); var texture = controllerTextures[controller]; using (var texture2d = new Texture2D(device, new Texture2DDescription { ArraySize = 1, BindFlags = BindFlags.ShaderResource, Format = Format.R8G8B8A8_UNorm, Width = texture.unWidth, Height = texture.unHeight, MipLevels = 1, SampleDescription = new SampleDescription(1, 0) }, new DataRectangle(texture.rubTextureMapData, texture.unWidth * 4))) controllerTextureViews[controller] = new ShaderResourceView(device, texture2d); } var controllerVertexShaderByteCode = SharpDX.D3DCompiler.ShaderBytecode.Compile(Properties.Resources.NormalTextureShader, "VS", "vs_5_0"); controllerVertexShader = new VertexShader(device, controllerVertexShaderByteCode); controllerPixelShader = new PixelShader(device, SharpDX.D3DCompiler.ShaderBytecode.Compile(Properties.Resources.NormalTextureShader, "PS", "ps_5_0")); var controllerLayout = new InputLayout(device, SharpDX.D3DCompiler.ShaderSignature.GetInputSignature(controllerVertexShaderByteCode), new InputElement[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32_Float, 12, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 24, 0) }); worldViewProjectionBuffer = new SharpDX.Direct3D11.Buffer(device, Utilities.SizeOf <Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0); var rasterizerStateDescription = RasterizerStateDescription.Default(); //rasterizerStateDescription.FillMode = FillMode.Wireframe; rasterizerStateDescription.IsFrontCounterClockwise = true; //rasterizerStateDescription.CullMode = CullMode.None; rasterizerState = new RasterizerState(device, rasterizerStateDescription); var blendStateDescription = BlendStateDescription.Default(); blendStateDescription.RenderTarget[0].BlendOperation = BlendOperation.Add; blendStateDescription.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha; blendStateDescription.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha; blendStateDescription.RenderTarget[0].IsBlendEnabled = false; blendState = new BlendState(device, blendStateDescription); var depthStateDescription = DepthStencilStateDescription.Default(); depthStateDescription.DepthComparison = Comparison.LessEqual; depthStateDescription.IsDepthEnabled = true; depthStateDescription.IsStencilEnabled = false; depthStencilState = new DepthStencilState(device, depthStateDescription); var samplerStateDescription = SamplerStateDescription.Default(); samplerStateDescription.Filter = Filter.MinMagMipLinear; samplerStateDescription.AddressU = TextureAddressMode.Wrap; samplerStateDescription.AddressV = TextureAddressMode.Wrap; samplerState = new SamplerState(device, samplerStateDescription); startTime = DateTime.Now; frame = 0; windowSize = form.ClientSize; backgroundColor = new RawColor4(0.1f, 0.1f, 0.1f, 1); var vrEvent = new VREvent_t(); var eventSize = (uint)Utilities.SizeOf <VREvent_t>(); head = Matrix.Identity; // Initialize Audio audio = new XAudio2(); var voice = new MasteringVoice(audio); audio3d = new X3DAudio(Speakers.Stereo); foreach (var controller in controllers) { controllerEmitters[controller] = new Emitter { ChannelCount = 1, CurveDistanceScaler = 0.15f, OrientFront = Vector3.ForwardLH, OrientTop = Vector3.Up, Position = new Vector3(0, 0, 1000), //Velocity = Vector3.Zero }; } listener = new Listener { OrientFront = Vector3.ForwardLH, OrientTop = Vector3.Up, Position = new Vector3(0, 0, 1000) }; var audioFormat = new WaveFormat(44100, 32, 1); //var audioSource = new SourceVoice(audio, audioFormat); var audioBufferSize = audioFormat.ConvertLatencyToByteSize(1000); var audioStream = new DataStream(audioBufferSize, true, true); var audioSamples = audioBufferSize / audioFormat.BlockAlign; var random = new Random(); for (var sample = 0; sample < audioSamples; sample++) { audioStream.Write((float)random.NextFloat(-1, 1)); } audioStream.Position = 0; var audioBuffer = new AudioBuffer { Stream = audioStream, AudioBytes = audioBufferSize, LoopCount = 255 }; var audioSettings = new DspSettings(1, 2); foreach (var controller in controllers) { var audioSource = new SourceVoice(audio, audioFormat); audioSource.SubmitSourceBuffer(audioBuffer, null); audio3d.Calculate(listener, controllerEmitters[controller], CalculateFlags.Matrix, audioSettings); audioSource.SetOutputMatrix(1, 2, audioSettings.MatrixCoefficients); audioSource.Start(); controllerVoices[controller] = audioSource; } RenderLoop.Run(form, () => { while (system.PollNextEvent(ref vrEvent, eventSize)) { switch ((EVREventType)vrEvent.eventType) { case EVREventType.VREvent_TrackedDeviceActivated: var controller = vrEvent.trackedDeviceIndex; controllers.Add(controller); var modelName = new StringBuilder(255, 255); var propertyError = ETrackedPropertyError.TrackedProp_Success; var length = system.GetStringTrackedDeviceProperty(controller, ETrackedDeviceProperty.Prop_RenderModelName_String, modelName, 255, ref propertyError); if (propertyError == ETrackedPropertyError.TrackedProp_Success) { var modelName2 = modelName.ToString(); while (true) { var pointer = IntPtr.Zero; var modelError = EVRRenderModelError.None; modelError = OpenVR.RenderModels.LoadRenderModel_Async(modelName2, ref pointer); if (modelError == EVRRenderModelError.Loading) { continue; } if (modelError == EVRRenderModelError.None) { var renderModel = System.Runtime.InteropServices.Marshal.PtrToStructure <RenderModel_t>(pointer); controllerModels[controller] = renderModel; // Load Controller Model var model = controllerModels[controller]; controllerVertexBuffers[controller] = new SharpDX.Direct3D11.Buffer(device, model.rVertexData, new BufferDescription { BindFlags = BindFlags.VertexBuffer, SizeInBytes = (int)model.unVertexCount * 32 }); controllerVertexBufferBindings[controller] = new VertexBufferBinding(controllerVertexBuffers[controller], 32, 0); controllerIndexBuffers[controller] = new SharpDX.Direct3D11.Buffer(device, model.rIndexData, new BufferDescription { BindFlags = BindFlags.IndexBuffer, SizeInBytes = (int)model.unTriangleCount * 3 * 2 }); break; } } while (true) { var pointer = IntPtr.Zero; var textureError = EVRRenderModelError.None; textureError = OpenVR.RenderModels.LoadTexture_Async(controllerModels[controller].diffuseTextureId, ref pointer); if (textureError == EVRRenderModelError.Loading) { continue; } if (textureError == EVRRenderModelError.None) { var textureMap = System.Runtime.InteropServices.Marshal.PtrToStructure <RenderModel_TextureMap_t>(pointer); controllerTextures[controller] = textureMap; using (var texture2d = new Texture2D(device, new Texture2DDescription { ArraySize = 1, BindFlags = BindFlags.ShaderResource, Format = Format.R8G8B8A8_UNorm, Width = textureMap.unWidth, Height = textureMap.unHeight, MipLevels = 1, SampleDescription = new SampleDescription(1, 0) }, new DataRectangle(textureMap.rubTextureMapData, textureMap.unWidth * 4))) controllerTextureViews[controller] = new ShaderResourceView(device, texture2d); break; } } controllerEmitters[controller] = new Emitter { ChannelCount = 1, CurveDistanceScaler = 0.15f, OrientFront = Vector3.ForwardLH, OrientTop = Vector3.Up, Position = new Vector3(0, 0, 1000), //Velocity = Vector3.Zero }; var audioSource = new SourceVoice(audio, audioFormat); audioSource.SubmitSourceBuffer(audioBuffer, null); audio3d.Calculate(listener, controllerEmitters[controller], CalculateFlags.Matrix, audioSettings); audioSource.SetOutputMatrix(1, 2, audioSettings.MatrixCoefficients); audioSource.Start(); controllerVoices[controller] = audioSource; } break; case EVREventType.VREvent_TrackedDeviceDeactivated: controllers.RemoveAll(c => c == vrEvent.trackedDeviceIndex); break; default: System.Diagnostics.Debug.WriteLine((EVREventType)vrEvent.eventType); break; } } if (form.ClientSize != windowSize) { Utilities.Dispose(ref backBufferView); if (form.ClientSize.Width != 0 && form.ClientSize.Height != 0) { swapChain.ResizeBuffers(1, form.ClientSize.Width, form.ClientSize.Height, Format.B8G8R8A8_UNorm, SwapChainFlags.None); using (var backBuffer = swapChain.GetBackBuffer <Texture2D>(0)) backBufferView = new RenderTargetView(device, backBuffer); } windowSize = form.ClientSize; } // Update Device Tracking compositor.WaitGetPoses(currentPoses, nextPoses); if (currentPoses[headset].bPoseIsValid) { Convert(ref currentPoses[headset].mDeviceToAbsoluteTracking, ref head); // Update Audio Listener listener.Position = head.TranslationVector * new Vector3(1, 1, -1); listener.OrientFront = head.Forward * new Vector3(1, 1, -1); listener.OrientTop = head.Up * new Vector3(1, 1, -1); } foreach (var controller in controllers) { var controllerMatrix = Matrix.Identity; Convert(ref currentPoses[controller].mDeviceToAbsoluteTracking, ref controllerMatrix); var position = controllerMatrix.TranslationVector * new Vector3(1, 1, -1); controllerEmitters[controller].Position = position; audio3d.Calculate(listener, controllerEmitters[controller], CalculateFlags.Matrix, audioSettings); controllerVoices[controller].SetOutputMatrix(1, 2, audioSettings.MatrixCoefficients); } // Render Left Eye context.Rasterizer.SetViewport(0, 0, headsetSize.Width, headsetSize.Height); context.OutputMerger.SetTargets(eyeDepthView, leftEyeTextureView); context.OutputMerger.SetDepthStencilState(depthStencilState); context.ClearRenderTargetView(leftEyeTextureView, backgroundColor); context.ClearDepthStencilView(eyeDepthView, DepthStencilClearFlags.Depth, 1.0f, 0); Shaders.Apply(context); context.Rasterizer.State = rasterizerState; context.OutputMerger.SetBlendState(blendState); context.OutputMerger.SetDepthStencilState(depthStencilState); context.PixelShader.SetSampler(0, samplerState); var ratio = (float)headsetSize.Width / (float)headsetSize.Height; var projection = leftEyeProjection; var view = Matrix.Invert(leftEyeView * head); var world = Matrix.Scaling(0.5f) * Matrix.Translation(0, 1.0f, 0); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer); //Shapes.Cube.Begin(context); //Shapes.Cube.Draw(context); Shapes.Sphere.Begin(context); Shapes.Sphere.Draw(context); // Draw Controllers context.InputAssembler.InputLayout = controllerLayout; context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; context.VertexShader.Set(controllerVertexShader); context.PixelShader.Set(controllerPixelShader); context.GeometryShader.Set(null); context.DomainShader.Set(null); context.HullShader.Set(null); context.PixelShader.SetSampler(0, samplerState); foreach (var controller in controllers) { context.InputAssembler.SetVertexBuffers(0, controllerVertexBufferBindings[controller]); context.InputAssembler.SetIndexBuffer(controllerIndexBuffers[controller], Format.R16_UInt, 0); context.PixelShader.SetShaderResource(0, controllerTextureViews[controller]); Convert(ref currentPoses[controller].mDeviceToAbsoluteTracking, ref world); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer); context.DrawIndexed((int)controllerModels[controller].unTriangleCount * 3 * 4, 0, 0); } var texture = new Texture_t { eType = ETextureType.DirectX, eColorSpace = EColorSpace.Gamma, handle = leftEyeTextureView.Resource.NativePointer }; var bounds = new VRTextureBounds_t { uMin = 0.0f, uMax = 1.0f, vMin = 0.0f, vMax = 1.0f, }; var submitError = compositor.Submit(EVREye.Eye_Left, ref texture, ref bounds, EVRSubmitFlags.Submit_Default); if (submitError != EVRCompositorError.None) { System.Diagnostics.Debug.WriteLine(submitError); } // Render Right Eye context.Rasterizer.SetViewport(0, 0, headsetSize.Width, headsetSize.Height); context.OutputMerger.SetTargets(eyeDepthView, rightEyeTextureView); context.OutputMerger.SetDepthStencilState(depthStencilState); context.ClearRenderTargetView(rightEyeTextureView, backgroundColor); context.ClearDepthStencilView(eyeDepthView, DepthStencilClearFlags.Depth, 1.0f, 0); Shaders.Apply(context); context.Rasterizer.State = rasterizerState; context.OutputMerger.SetBlendState(blendState); context.OutputMerger.SetDepthStencilState(depthStencilState); context.PixelShader.SetSampler(0, samplerState); projection = rightEyeProjection; view = Matrix.Invert(rightEyeView * head); world = Matrix.Scaling(0.5f) * Matrix.Translation(0, 1.0f, 0); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer); //Shapes.Cube.Begin(context); //Shapes.Cube.Draw(context); Shapes.Sphere.Begin(context); Shapes.Sphere.Draw(context); // Draw Controllers context.InputAssembler.InputLayout = controllerLayout; context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; context.VertexShader.Set(controllerVertexShader); context.PixelShader.Set(controllerPixelShader); context.GeometryShader.Set(null); context.DomainShader.Set(null); context.HullShader.Set(null); context.PixelShader.SetSampler(0, samplerState); foreach (var controller in controllers) { context.InputAssembler.SetVertexBuffers(0, controllerVertexBufferBindings[controller]); context.InputAssembler.SetIndexBuffer(controllerIndexBuffers[controller], Format.R16_UInt, 0); context.PixelShader.SetShaderResource(0, controllerTextureViews[controller]); Convert(ref currentPoses[controller].mDeviceToAbsoluteTracking, ref world); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer); context.DrawIndexed((int)controllerModels[controller].unTriangleCount * 3 * 4, 0, 0); } texture.handle = rightEyeTextureView.Resource.NativePointer; submitError = compositor.Submit(EVREye.Eye_Right, ref texture, ref bounds, EVRSubmitFlags.Submit_Default); if (submitError != EVRCompositorError.None) { System.Diagnostics.Debug.WriteLine(submitError); } // Render Window context.Rasterizer.SetViewport(0, 0, windowSize.Width, windowSize.Height); context.OutputMerger.SetTargets(depthStencilView, backBufferView); context.OutputMerger.SetDepthStencilState(depthStencilState); context.ClearRenderTargetView(backBufferView, backgroundColor); context.ClearDepthStencilView(depthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0); Shaders.Apply(context); context.Rasterizer.State = rasterizerState; context.OutputMerger.SetBlendState(blendState); context.OutputMerger.SetDepthStencilState(depthStencilState); context.PixelShader.SetSampler(0, samplerState); ratio = (float)form.ClientSize.Width / (float)form.ClientSize.Height; projection = Matrix.PerspectiveFovRH(3.14F / 3.0F, ratio, 0.01f, 1000); view = Matrix.Invert(head); world = Matrix.Scaling(0.5f) * Matrix.Translation(0, 1.0f, 0); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer); //Shapes.Cube.Begin(context); //Shapes.Cube.Draw(context); Shapes.Sphere.Begin(context); Shapes.Sphere.Draw(context); // Draw Controllers context.InputAssembler.InputLayout = controllerLayout; context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; context.VertexShader.Set(controllerVertexShader); context.PixelShader.Set(controllerPixelShader); context.GeometryShader.Set(null); context.DomainShader.Set(null); context.HullShader.Set(null); context.PixelShader.SetSampler(0, samplerState); foreach (var controller in controllers) { context.InputAssembler.SetVertexBuffers(0, controllerVertexBufferBindings[controller]); context.InputAssembler.SetIndexBuffer(controllerIndexBuffers[controller], Format.R16_UInt, 0); Convert(ref currentPoses[controller].mDeviceToAbsoluteTracking, ref world); worldViewProjection = world * view * projection; context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer); context.DrawIndexed((int)controllerModels[controller].unTriangleCount * 3 * 4, 0, 0); } // Show Backbuffer swapChain.Present(0, PresentFlags.None); }); audio.Dispose(); } }
// Token: 0x06005F2C RID: 24364 RVA: 0x00215578 File Offset: 0x00213978 private IEnumerator LoadLevel() { if (this.loadingScreen != null && this.loadingScreenDistance > 0f) { SteamVR_Controller.Device hmd = SteamVR_Controller.Input(0); while (!hmd.hasTracking) { yield return(null); } SteamVR_Utils.RigidTransform tloading = hmd.transform; tloading.rot = Quaternion.Euler(0f, tloading.rot.eulerAngles.y, 0f); tloading.pos += tloading.rot * new Vector3(0f, 0f, this.loadingScreenDistance); Transform t = (!(this.loadingScreenTransform != null)) ? base.transform : this.loadingScreenTransform; t.position = tloading.pos; t.rotation = tloading.rot; } SteamVR_LoadLevel._active = this; SteamVR_Events.Loading.Send(true); if (this.loadingScreenFadeInTime > 0f) { this.fadeRate = 1f / this.loadingScreenFadeInTime; } else { this.alpha = 1f; } CVROverlay overlay = OpenVR.Overlay; if (this.loadingScreen != null && overlay != null) { this.loadingScreenOverlayHandle = this.GetOverlayHandle("loadingScreen", (!(this.loadingScreenTransform != null)) ? base.transform : this.loadingScreenTransform, this.loadingScreenWidthInMeters); if (this.loadingScreenOverlayHandle != 0UL) { Texture_t texture_t = default(Texture_t); texture_t.handle = this.loadingScreen.GetNativeTexturePtr(); texture_t.eType = SteamVR.instance.textureType; texture_t.eColorSpace = EColorSpace.Auto; overlay.SetOverlayTexture(this.loadingScreenOverlayHandle, ref texture_t); } } bool fadedForeground = false; SteamVR_Events.LoadingFadeOut.Send(this.fadeOutTime); CVRCompositor compositor = OpenVR.Compositor; if (compositor != null) { if (this.front != null) { SteamVR_Skybox.SetOverride(this.front, this.back, this.left, this.right, this.top, this.bottom); compositor.FadeGrid(this.fadeOutTime, true); yield return(new WaitForSeconds(this.fadeOutTime)); } else if (this.backgroundColor != Color.clear) { if (this.showGrid) { compositor.FadeToColor(0f, this.backgroundColor.r, this.backgroundColor.g, this.backgroundColor.b, this.backgroundColor.a, true); compositor.FadeGrid(this.fadeOutTime, true); yield return(new WaitForSeconds(this.fadeOutTime)); } else { compositor.FadeToColor(this.fadeOutTime, this.backgroundColor.r, this.backgroundColor.g, this.backgroundColor.b, this.backgroundColor.a, false); yield return(new WaitForSeconds(this.fadeOutTime + 0.1f)); compositor.FadeGrid(0f, true); fadedForeground = true; } } } SteamVR_Render.pauseRendering = true; while (this.alpha < 1f) { yield return(null); } base.transform.parent = null; UnityEngine.Object.DontDestroyOnLoad(base.gameObject); if (!string.IsNullOrEmpty(this.internalProcessPath)) { UnityEngine.Debug.Log("Launching external application..."); CVRApplications applications = OpenVR.Applications; if (applications == null) { UnityEngine.Debug.Log("Failed to get OpenVR.Applications interface!"); } else { string currentDirectory = Directory.GetCurrentDirectory(); string text = Path.Combine(currentDirectory, this.internalProcessPath); UnityEngine.Debug.Log("LaunchingInternalProcess"); UnityEngine.Debug.Log("ExternalAppPath = " + this.internalProcessPath); UnityEngine.Debug.Log("FullPath = " + text); UnityEngine.Debug.Log("ExternalAppArgs = " + this.internalProcessArgs); UnityEngine.Debug.Log("WorkingDirectory = " + currentDirectory); EVRApplicationError evrapplicationError = applications.LaunchInternalProcess(text, this.internalProcessArgs, currentDirectory); UnityEngine.Debug.Log("LaunchInternalProcessError: " + evrapplicationError); Process.GetCurrentProcess().Kill(); } } else { LoadSceneMode mode = (!this.loadAdditive) ? LoadSceneMode.Single : LoadSceneMode.Additive; if (this.loadAsync) { Application.backgroundLoadingPriority = ThreadPriority.Low; this.async = SceneManager.LoadSceneAsync(this.levelName, mode); while (!this.async.isDone) { yield return(null); } } else { SceneManager.LoadScene(this.levelName, mode); } } yield return(null); GC.Collect(); yield return(null); Shader.WarmupAllShaders(); yield return(new WaitForSeconds(this.postLoadSettleTime)); SteamVR_Render.pauseRendering = false; if (this.loadingScreenFadeOutTime > 0f) { this.fadeRate = -1f / this.loadingScreenFadeOutTime; } else { this.alpha = 0f; } SteamVR_Events.LoadingFadeIn.Send(this.fadeInTime); if (compositor != null) { if (fadedForeground) { compositor.FadeGrid(0f, false); compositor.FadeToColor(this.fadeInTime, 0f, 0f, 0f, 0f, false); yield return(new WaitForSeconds(this.fadeInTime)); } else { compositor.FadeGrid(this.fadeInTime, false); yield return(new WaitForSeconds(this.fadeInTime)); if (this.front != null) { SteamVR_Skybox.ClearOverride(); } } } while (this.alpha > 0f) { yield return(null); } if (overlay != null) { if (this.progressBarOverlayHandle != 0UL) { overlay.HideOverlay(this.progressBarOverlayHandle); } if (this.loadingScreenOverlayHandle != 0UL) { overlay.HideOverlay(this.loadingScreenOverlayHandle); } } UnityEngine.Object.Destroy(base.gameObject); SteamVR_LoadLevel._active = null; SteamVR_Events.Loading.Send(false); yield break; }