Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.5 KiB

  1. // Frames Per Second Canvas|Prefabs|0030
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. /// <summary>
  7. /// Provides a frames per second text element to the HMD view. To use the prefab it must be placed into the scene then the headset camera needs attaching to the canvas:
  8. /// </summary>
  9. /// <remarks>
  10. /// **Prefab Usage:**
  11. /// * Place the `VRTK/Prefabs/FramesPerSecondCanvas/FramesPerSecondCanvas` prefab in the scene hierarchy.
  12. ///
  13. /// > This script is largely based on the script at: http://talesfromtherift.com/vr-fps-counter/ So all credit to Peter Koch for his work. Twitter: @peterept
  14. /// </remarks>
  15. /// <example>
  16. /// `VRTK/Examples/018_CameraRig_FramesPerSecondCounter` displays the frames per second in the centre of the headset view. Pressing the trigger generates a new sphere and pressing the touchpad generates ten new spheres. Eventually when lots of spheres are present the FPS will drop and demonstrate the prefab.
  17. /// </example>
  18. public class VRTK_FramesPerSecondViewer : MonoBehaviour
  19. {
  20. [Tooltip("Toggles whether the FPS text is visible.")]
  21. public bool displayFPS = true;
  22. [Tooltip("The frames per second deemed acceptable that is used as the benchmark to change the FPS text colour.")]
  23. public int targetFPS = 90;
  24. [Tooltip("The size of the font the FPS is displayed in.")]
  25. public int fontSize = 32;
  26. [Tooltip("The position of the FPS text within the headset view.")]
  27. public Vector3 position = Vector3.zero;
  28. [Tooltip("The colour of the FPS text when the frames per second are within reasonable limits of the Target FPS.")]
  29. public Color goodColor = Color.green;
  30. [Tooltip("The colour of the FPS text when the frames per second are falling short of reasonable limits of the Target FPS.")]
  31. public Color warnColor = Color.yellow;
  32. [Tooltip("The colour of the FPS text when the frames per second are at an unreasonable level of the Target FPS.")]
  33. public Color badColor = Color.red;
  34. protected const float updateInterval = 0.5f;
  35. protected int framesCount;
  36. protected float framesTime;
  37. protected Canvas canvas;
  38. protected Text text;
  39. protected virtual void OnEnable()
  40. {
  41. VRTK_SDKManager.SubscribeLoadedSetupChanged(LoadedSetupChanged);
  42. InitCanvas();
  43. }
  44. protected virtual void OnDisable()
  45. {
  46. if (!gameObject.activeSelf)
  47. {
  48. VRTK_SDKManager.UnsubscribeLoadedSetupChanged(LoadedSetupChanged);
  49. }
  50. }
  51. protected virtual void Update()
  52. {
  53. framesCount++;
  54. framesTime += Time.unscaledDeltaTime;
  55. if (framesTime > updateInterval)
  56. {
  57. if (text != null)
  58. {
  59. if (displayFPS)
  60. {
  61. float fps = framesCount / framesTime;
  62. text.text = string.Format("{0:F2} FPS", fps);
  63. text.color = (fps > (targetFPS - 5) ? goodColor :
  64. (fps > (targetFPS - 30) ? warnColor :
  65. badColor));
  66. }
  67. else
  68. {
  69. text.text = "";
  70. }
  71. }
  72. framesCount = 0;
  73. framesTime = 0;
  74. }
  75. }
  76. protected virtual void LoadedSetupChanged(VRTK_SDKManager sender, VRTK_SDKManager.LoadedSetupChangeEventArgs e)
  77. {
  78. if (this != null && VRTK_SDKManager.ValidInstance() && gameObject.activeInHierarchy)
  79. {
  80. SetCanvasCamera();
  81. }
  82. }
  83. protected virtual void InitCanvas()
  84. {
  85. canvas = transform.GetComponentInParent<Canvas>();
  86. text = GetComponent<Text>();
  87. if (canvas != null)
  88. {
  89. canvas.planeDistance = 0.5f;
  90. }
  91. if (text != null)
  92. {
  93. text.fontSize = fontSize;
  94. text.transform.localPosition = position;
  95. }
  96. SetCanvasCamera();
  97. }
  98. protected virtual void SetCanvasCamera()
  99. {
  100. Transform sdkCamera = VRTK_DeviceFinder.HeadsetCamera();
  101. if (sdkCamera != null)
  102. {
  103. canvas.worldCamera = sdkCamera.GetComponent<Camera>();
  104. }
  105. }
  106. }
  107. }