123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- using System;
- using System.Collections;
- using System.IO;
- using System.Runtime.InteropServices;
- using UnityEngine;
- using Valve.VR;
- public class SteamVR_Render : MonoBehaviour
- {
- public static EVREye eye { get; private set; }
- public static SteamVR_Render instance
- {
- get
- {
- if (SteamVR_Render._instance == null)
- {
- SteamVR_Render._instance = UnityEngine.Object.FindObjectOfType<SteamVR_Render>();
- if (SteamVR_Render._instance == null)
- {
- SteamVR_Render._instance = new GameObject("[SteamVR]").AddComponent<SteamVR_Render>();
- }
- }
- return SteamVR_Render._instance;
- }
- }
- private void OnDestroy()
- {
- SteamVR_Render._instance = null;
- }
- private void OnApplicationQuit()
- {
- SteamVR_Render.isQuitting = true;
- SteamVR.SafeDispose();
- }
- public static void Add(SteamVR_Camera vrcam)
- {
- if (!SteamVR_Render.isQuitting)
- {
- SteamVR_Render.instance.AddInternal(vrcam);
- }
- }
- public static void Remove(SteamVR_Camera vrcam)
- {
- if (!SteamVR_Render.isQuitting && SteamVR_Render._instance != null)
- {
- SteamVR_Render.instance.RemoveInternal(vrcam);
- }
- }
- public static SteamVR_Camera Top()
- {
- if (!SteamVR_Render.isQuitting)
- {
- return SteamVR_Render.instance.TopInternal();
- }
- return null;
- }
- private void AddInternal(SteamVR_Camera vrcam)
- {
- Camera component = vrcam.GetComponent<Camera>();
- int num = this.cameras.Length;
- SteamVR_Camera[] array = new SteamVR_Camera[num + 1];
- int num2 = 0;
- for (int i = 0; i < num; i++)
- {
- Camera component2 = this.cameras[i].GetComponent<Camera>();
- if (i == num2 && component2.depth > component.depth)
- {
- array[num2++] = vrcam;
- }
- array[num2++] = this.cameras[i];
- }
- if (num2 == num)
- {
- array[num2] = vrcam;
- }
- this.cameras = array;
- }
- private void RemoveInternal(SteamVR_Camera vrcam)
- {
- int num = this.cameras.Length;
- int num2 = 0;
- for (int i = 0; i < num; i++)
- {
- SteamVR_Camera x = this.cameras[i];
- if (x == vrcam)
- {
- num2++;
- }
- }
- if (num2 == 0)
- {
- return;
- }
- SteamVR_Camera[] array = new SteamVR_Camera[num - num2];
- int num3 = 0;
- for (int j = 0; j < num; j++)
- {
- SteamVR_Camera steamVR_Camera = this.cameras[j];
- if (steamVR_Camera != vrcam)
- {
- array[num3++] = steamVR_Camera;
- }
- }
- this.cameras = array;
- }
- private SteamVR_Camera TopInternal()
- {
- if (this.cameras.Length > 0)
- {
- return this.cameras[this.cameras.Length - 1];
- }
- return null;
- }
- public static bool pauseRendering
- {
- get
- {
- return SteamVR_Render._pauseRendering;
- }
- set
- {
- SteamVR_Render._pauseRendering = value;
- CVRCompositor compositor = OpenVR.Compositor;
- if (compositor != null)
- {
- compositor.SuspendRendering(value);
- }
- }
- }
- private IEnumerator RenderLoop()
- {
- for (;;)
- {
- yield return new 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;
- }
- private void RenderExternalCamera()
- {
- if (this.externalCamera == null)
- {
- return;
- }
- if (!this.externalCamera.gameObject.activeInHierarchy)
- {
- return;
- }
- int num = (int)Mathf.Max(this.externalCamera.config.frameSkip, 0f);
- if (Time.frameCount % (num + 1) != 0)
- {
- return;
- }
- this.externalCamera.AttachToCamera(this.TopInternal());
- this.externalCamera.RenderNear();
- this.externalCamera.RenderFar();
- }
- private void OnInputFocus(params object[] args)
- {
- bool flag = (bool)args[0];
- if (flag)
- {
- if (this.pauseGameWhenDashboardIsVisible)
- {
- Time.timeScale = this.timeScale;
- }
- SteamVR_Camera.sceneResolutionScale = this.sceneResolutionScale;
- }
- else
- {
- if (this.pauseGameWhenDashboardIsVisible)
- {
- this.timeScale = Time.timeScale;
- Time.timeScale = 0f;
- }
- this.sceneResolutionScale = SteamVR_Camera.sceneResolutionScale;
- SteamVR_Camera.sceneResolutionScale = 0.5f;
- }
- }
- private void OnQuit(params object[] args)
- {
- Application.Quit();
- }
- private void OnEnable()
- {
- base.StartCoroutine("RenderLoop");
- SteamVR_Utils.Event.Listen("input_focus", new SteamVR_Utils.Event.Handler(this.OnInputFocus));
- SteamVR_Utils.Event.Listen("Quit", new SteamVR_Utils.Event.Handler(this.OnQuit));
- }
- private void OnDisable()
- {
- base.StopAllCoroutines();
- SteamVR_Utils.Event.Remove("input_focus", new SteamVR_Utils.Event.Handler(this.OnInputFocus));
- SteamVR_Utils.Event.Remove("Quit", new SteamVR_Utils.Event.Handler(this.OnQuit));
- }
- private void Awake()
- {
- if (this.externalCamera == null && File.Exists(this.externalCameraConfigPath))
- {
- GameObject original = Resources.Load<GameObject>("SteamVR_ExternalCamera");
- GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(original);
- gameObject.gameObject.name = "External Camera";
- this.externalCamera = gameObject.transform.GetChild(0).GetComponent<SteamVR_ExternalCamera>();
- this.externalCamera.configPath = this.externalCameraConfigPath;
- this.externalCamera.ReadConfig();
- }
- }
- private void FixedUpdate()
- {
- }
- private void Update()
- {
- if (this.poseUpdater == null)
- {
- this.poseUpdater = new GameObject("poseUpdater")
- {
- transform =
- {
- parent = base.transform
- }
- }.AddComponent<SteamVR_UpdatePoses>();
- }
- SteamVR_Controller.Update();
- CVRSystem system = OpenVR.System;
- if (system != null)
- {
- VREvent_t vrevent_t = default(VREvent_t);
- uint uncbVREvent = (uint)Marshal.SizeOf(typeof(VREvent_t));
- for (int i = 0; i < 64; i++)
- {
- if (!system.PollNextEvent(ref vrevent_t, uncbVREvent))
- {
- break;
- }
- EVREventType eventType = (EVREventType)vrevent_t.eventType;
- if (eventType != EVREventType.VREvent_InputFocusCaptured)
- {
- if (eventType != EVREventType.VREvent_InputFocusReleased)
- {
- if (eventType != EVREventType.VREvent_HideRenderModels)
- {
- if (eventType != EVREventType.VREvent_ShowRenderModels)
- {
- string name = Enum.GetName(typeof(EVREventType), vrevent_t.eventType);
- if (name != null)
- {
- SteamVR_Utils.Event.Send(name.Substring(8), new object[]
- {
- vrevent_t
- });
- }
- }
- else
- {
- SteamVR_Utils.Event.Send("hide_render_models", new object[]
- {
- false
- });
- }
- }
- else
- {
- SteamVR_Utils.Event.Send("hide_render_models", new object[]
- {
- true
- });
- }
- }
- else if (vrevent_t.data.process.pid == 0U)
- {
- SteamVR_Utils.Event.Send("input_focus", new object[]
- {
- true
- });
- }
- }
- else if (vrevent_t.data.process.oldPid == 0U)
- {
- SteamVR_Utils.Event.Send("input_focus", new object[]
- {
- false
- });
- }
- }
- }
- Application.targetFrameRate = -1;
- Application.runInBackground = true;
- QualitySettings.maxQueuedFrames = -1;
- QualitySettings.vSyncCount = 0;
- if (this.lockPhysicsUpdateRateToRenderFrequency && Time.timeScale > 0f)
- {
- SteamVR instance = SteamVR.instance;
- if (instance != null)
- {
- Compositor_FrameTiming compositor_FrameTiming = default(Compositor_FrameTiming);
- compositor_FrameTiming.m_nSize = (uint)Marshal.SizeOf(typeof(Compositor_FrameTiming));
- instance.compositor.GetFrameTiming(ref compositor_FrameTiming, 0U);
- Time.fixedDeltaTime = Time.timeScale / instance.hmd_DisplayFrequency;
- Time.maximumDeltaTime = Time.fixedDeltaTime * compositor_FrameTiming.m_nNumFramePresents;
- }
- }
- }
- public bool pauseGameWhenDashboardIsVisible = true;
- public bool lockPhysicsUpdateRateToRenderFrequency = true;
- public SteamVR_ExternalCamera externalCamera;
- public string externalCameraConfigPath = "externalcamera.cfg";
- public ETrackingUniverseOrigin trackingSpace = ETrackingUniverseOrigin.TrackingUniverseStanding;
- private static SteamVR_Render _instance;
- private static bool isQuitting;
- private SteamVR_Camera[] cameras = new SteamVR_Camera[0];
- public TrackedDevicePose_t[] poses = new TrackedDevicePose_t[16];
- public TrackedDevicePose_t[] gamePoses = new TrackedDevicePose_t[0];
- private static bool _pauseRendering;
- private float sceneResolutionScale = 1f;
- private float timeScale = 1f;
- private SteamVR_UpdatePoses poseUpdater;
- }
|