SteamVR_Skybox.cs 3.6 KB

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