SteamVR_Skybox.cs 2.6 KB

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