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