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.

116 lines
2.4 KiB

  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Sets cubemap to use in the compositor.
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using Valve.VR;
  8. public class SteamVR_Skybox : MonoBehaviour
  9. {
  10. // Note: Unity's Left and Right Skybox shader variables are switched.
  11. public Texture front, back, left, right, top, bottom;
  12. public enum CellSize
  13. {
  14. x1024, x64, x32, x16, x8
  15. }
  16. public CellSize StereoCellSize = CellSize.x32;
  17. public float StereoIpdMm = 64.0f;
  18. public void SetTextureByIndex(int i, Texture t)
  19. {
  20. switch (i)
  21. {
  22. case 0:
  23. front = t;
  24. break;
  25. case 1:
  26. back = t;
  27. break;
  28. case 2:
  29. left = t;
  30. break;
  31. case 3:
  32. right = t;
  33. break;
  34. case 4:
  35. top = t;
  36. break;
  37. case 5:
  38. bottom = t;
  39. break;
  40. }
  41. }
  42. public Texture GetTextureByIndex(int i)
  43. {
  44. switch (i)
  45. {
  46. case 0:
  47. return front;
  48. case 1:
  49. return back;
  50. case 2:
  51. return left;
  52. case 3:
  53. return right;
  54. case 4:
  55. return top;
  56. case 5:
  57. return bottom;
  58. }
  59. return null;
  60. }
  61. static public void SetOverride(
  62. Texture front = null,
  63. Texture back = null,
  64. Texture left = null,
  65. Texture right = null,
  66. Texture top = null,
  67. Texture bottom = null )
  68. {
  69. var compositor = OpenVR.Compositor;
  70. if (compositor != null)
  71. {
  72. var handles = new Texture[] { front, back, left, right, top, bottom };
  73. var textures = new Texture_t[6];
  74. for (int i = 0; i < 6; i++)
  75. {
  76. textures[i].handle = (handles[i] != null) ? handles[i].GetNativeTexturePtr() : System.IntPtr.Zero;
  77. textures[i].eType = SteamVR.instance.textureType;
  78. textures[i].eColorSpace = EColorSpace.Auto;
  79. }
  80. var error = compositor.SetSkyboxOverride(textures);
  81. if (error != EVRCompositorError.None)
  82. {
  83. Debug.LogError("Failed to set skybox override with error: " + error);
  84. if (error == EVRCompositorError.TextureIsOnWrongDevice)
  85. Debug.Log("Set your graphics driver to use the same video card as the headset is plugged into for Unity.");
  86. else if (error == EVRCompositorError.TextureUsesUnsupportedFormat)
  87. Debug.Log("Ensure skybox textures are not compressed and have no mipmaps.");
  88. }
  89. }
  90. }
  91. static public void ClearOverride()
  92. {
  93. var compositor = OpenVR.Compositor;
  94. if (compositor != null)
  95. compositor.ClearSkyboxOverride();
  96. }
  97. void OnEnable()
  98. {
  99. SetOverride(front, back, left, right, top, bottom);
  100. }
  101. void OnDisable()
  102. {
  103. ClearOverride();
  104. }
  105. }