Skip to content

Commit 9a325ec

Browse files
committed
Added VRSurface[] and surfacecount variable. Added UpdateSurfaces and CreateSurfaces functions.
1 parent 507c08d commit 9a325ec

1 file changed

Lines changed: 86 additions & 7 deletions

File tree

OSVR-Unity/Assets/OSVRUnity/src/VREye.cs

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ namespace Unity
3232
{
3333
public class VREye : MonoBehaviour
3434
{
35-
#region Private Variables
36-
private VRSurface _surface; //the surface associated with this eye
35+
public const int NUM_SURFACES = 1;
36+
37+
#region Private Variables
3738
private VRViewer _viewer; //the viewer associated with this eye
39+
private VRSurface[] _surfaces; //the surfaces associated with this eye
40+
private uint _surfaceCount;
3841
private uint _eyeIndex;
3942

4043
#endregion
@@ -44,11 +47,8 @@ public uint EyeIndex
4447
get { return _eyeIndex; }
4548
set { _eyeIndex = value; }
4649
}
47-
public VRSurface Surface
48-
{
49-
get { return _surface; }
50-
set { _surface = value; }
51-
}
50+
public VRSurface[] Surfaces { get { return _surfaces; } }
51+
public uint SurfaceCount { get { return _surfaceCount; } }
5252
public VRViewer Viewer
5353
{
5454
get { return _viewer; }
@@ -83,6 +83,85 @@ public void UpdateEyePose(OSVR.ClientKit.Pose3 eyePose)
8383
cachedTransform.localPosition = Math.ConvertPosition(eyePose.translation);
8484
cachedTransform.localRotation = Math.ConvertOrientation(eyePose.rotation);
8585
}
86+
87+
//For each Surface, update viewing parameters and render the surface
88+
public void UpdateSurfaces()
89+
{
90+
//for each surface
91+
for (uint surfaceIndex = 0; surfaceIndex < SurfaceCount; surfaceIndex++)
92+
{
93+
//get the eye's surface
94+
VRSurface surface = Surfaces[surfaceIndex];
95+
96+
//get viewport from ClientKit and set surface viewport
97+
OSVR.ClientKit.Viewport viewport = Viewer.DisplayController.DisplayConfig.GetRelativeViewportForViewerEyeSurface(
98+
Viewer.ViewerIndex, (byte)_eyeIndex, surfaceIndex);
99+
100+
surface.SetViewport(Math.ConvertViewport(viewport));
101+
102+
//get projection matrix from ClientKit and set surface projection matrix
103+
OSVR.ClientKit.Matrix44f projMatrix = Viewer.DisplayController.DisplayConfig.GetProjectionMatrixForViewerEyeSurfacef(
104+
Viewer.ViewerIndex, (byte)_eyeIndex, surfaceIndex,
105+
surface.Camera.nearClipPlane, surface.Camera.farClipPlane, OSVR.ClientKit.MatrixConventionsFlags.ColMajor);
106+
107+
surface.SetProjectionMatrix(Math.ConvertMatrix(projMatrix));
108+
109+
//render the surface
110+
surface.Render();
111+
}
112+
}
113+
114+
//Create this Eye's VRSurfaces.
115+
//Each VRSurface has a camera component which controls rendering for the VREye
116+
public void CreateSurfaces(uint surfaceCount)
117+
{
118+
_surfaceCount = surfaceCount;
119+
_surfaces = new VRSurface[_surfaceCount];
120+
if (surfaceCount != NUM_SURFACES)
121+
{
122+
Debug.LogError("Eye" + _eyeIndex + " has " + surfaceCount + " surfaces, but " +
123+
"this implementation requires exactly one surface per eye.");
124+
return;
125+
}
126+
//loop through surfaces because at some point we could support eyes with multiple surfaces
127+
//but this implementation currently supports exactly one
128+
for (uint surfaceIndex = 0; surfaceIndex < surfaceCount; surfaceIndex++)
129+
{
130+
GameObject surfaceGameObject = new GameObject("Surface");
131+
VRSurface surface = surfaceGameObject.AddComponent<VRSurface>();
132+
surface.Eye = this;
133+
surface.Camera = surfaceGameObject.GetComponent<Camera>(); //VRSurface has camera component by default
134+
CopyCamera(Viewer.DisplayController.Camera, surface.Camera); //copy camera properties from the "dummy" camera to surface camera
135+
surface.Camera.enabled = false; //disabled so we can control rendering manually
136+
surfaceGameObject.transform.parent = this.transform; //surface is child of Eye
137+
surfaceGameObject.transform.localPosition = Vector3.zero;
138+
Surfaces[surfaceIndex] = surface;
139+
140+
//distortion
141+
bool useDistortion = Viewer.DisplayController.DisplayConfig.DoesViewerEyeSurfaceWantDistortion(Viewer.ViewerIndex, (byte)_eyeIndex, surfaceIndex);
142+
if(useDistortion)
143+
{
144+
//@todo figure out which type of distortion to use
145+
//right now, there is only one option, SurfaceRadialDistortion
146+
//get distortion parameters
147+
OSVR.ClientKit.RadialDistortionParameters distortionParameters =
148+
Viewer.DisplayController.DisplayConfig.GetViewerEyeSurfaceRadialDistortion(
149+
Viewer.ViewerIndex, (byte)_eyeIndex, surfaceIndex);
150+
151+
surface.SetDistortion(distortionParameters);
152+
}
153+
}
154+
}
155+
156+
//helper method that copies camera properties from one camera to another
157+
//copies from srcCamera to destCamera
158+
private void CopyCamera(Camera srcCamera, Camera destCamera)
159+
{
160+
//Copy the camera properties.
161+
destCamera.CopyFrom(srcCamera);
162+
163+
//@todo Copy other components attached to the DisplayController?
164+
}
86165
}
87166
}
88167
}

0 commit comments

Comments
 (0)