2828namespace OSVR
2929{
3030 namespace Unity
31- {
32- [ RequireComponent ( typeof ( Camera ) ) ]
31+ {
3332 public class VRViewer : MonoBehaviour
34- {
35- #region Public Variables
36- public Camera Camera
37- {
38- get
39- {
40- if ( _camera == null )
41- {
42- _camera = GetComponent < Camera > ( ) ;
43- }
44- return _camera ;
45- }
46- set { _camera = value ; }
47- }
33+ {
34+ #region Public Variables
4835 public DisplayController DisplayController { get { return _displayController ; } set { _displayController = value ; } }
36+ public VREye [ ] Eyes { get { return _eyes ; } }
37+ public uint EyeCount { get { return _eyeCount ; } }
38+ public uint ViewerIndex { get { return _viewerIndex ; } set { _viewerIndex = value ; } }
4939 [ HideInInspector ]
5040 public Transform cachedTransform ;
5141 #endregion
5242
5343 #region Private Variables
5444 private DisplayController _displayController ;
55- private Camera _camera ;
56- private bool _disabledCamera = true ;
45+ private VREye [ ] _eyes ;
46+ private uint _eyeCount ;
47+ private uint _viewerIndex ;
48+
5749 #endregion
5850
5951 void Awake ( )
@@ -67,15 +59,24 @@ void Init()
6759 cachedTransform = transform ;
6860 }
6961
70- void OnEnable ( )
71- {
72- StartCoroutine ( "EndOfFrame" ) ;
73- }
74-
75- void OnDisable ( )
62+ //Creates the Eyes of this Viewer
63+ public void CreateEyes ( uint eyeCount )
7664 {
77- StopCoroutine ( "EndOfFrame" ) ;
78- }
65+ _eyeCount = eyeCount ; //cache the number of eyes this viewer controls
66+ _eyes = new VREye [ _eyeCount ] ;
67+ for ( uint eyeIndex = 0 ; eyeIndex < _eyeCount ; eyeIndex ++ )
68+ {
69+ GameObject eyeGameObject = new GameObject ( "Eye" + eyeIndex ) ; //add an eye gameobject to the scene
70+ VREye eye = eyeGameObject . AddComponent < VREye > ( ) ; //add the VReye component
71+ eye . Viewer = this ; //ASSUME THERE IS ONLY ONE VIEWER
72+ eye . EyeIndex = eyeIndex ; //set the eye's index
73+ eyeGameObject . transform . parent = _displayController . transform ; //child of DisplayController
74+ eyeGameObject . transform . localPosition = Vector3 . zero ;
75+ _eyes [ eyeIndex ] = eye ;
76+ uint eyeSurfaceCount = DisplayController . DisplayConfig . GetNumSurfacesForViewerEye ( ViewerIndex , ( byte ) eyeIndex ) ;
77+ eye . CreateSurfaces ( eyeSurfaceCount ) ;
78+ }
79+ }
7980
8081 //Updates the position and rotation of the head
8182 public void UpdateViewerHeadPose ( OSVR . ClientKit . Pose3 headPose )
@@ -84,64 +85,22 @@ public void UpdateViewerHeadPose(OSVR.ClientKit.Pose3 headPose)
8485 cachedTransform . localRotation = Math . ConvertOrientation ( headPose . rotation ) ;
8586 }
8687
87- //Culling determines which objects are visible to the camera. OnPreCull is called just before this process.
88- void OnPreCull ( )
88+ //Update the pose of each eye, then update and render each eye's surfaces
89+ public void UpdateEyes ( )
8990 {
90- //update the client
91- _displayController . UpdateClient ( ) ;
92-
93- // Disable dummy camera during rendering
94- // Enable after frame ends
95- _camera . enabled = false ;
96-
97- //update the viewer's head pose
98- UpdateViewerHeadPose ( _displayController . DisplayConfig . GetViewerPose ( DisplayController . DEFAULT_VIEWER ) ) ;
99-
100- //render each eye camera (each surface)
101- //assumes one surface per eye
102- for ( int i = 0 ; i < _displayController . EyeCount ; i ++ )
91+ for ( uint eyeIndex = 0 ; eyeIndex < EyeCount ; eyeIndex ++ )
10392 {
104- //update the eye pose
105- VREye eye = _displayController . Eyes [ i ] ;
106- eye . UpdateEyePose ( _displayController . DisplayConfig . GetViewerEyePose ( DisplayController . DEFAULT_VIEWER , ( byte ) i ) ) ;
107-
108- //get the eye's surface
109- VRSurface surface = eye . Surface ;
110-
111- //get viewport from ClientKit and set surface viewport
112- OSVR . ClientKit . Viewport viewport = _displayController . DisplayConfig . GetRelativeViewportForViewerEyeSurface (
113- DisplayController . DEFAULT_VIEWER , ( byte ) i , DisplayController . DEFAULT_SURFACE ) ;
114-
115- surface . SetViewport ( Math . ConvertViewport ( viewport ) ) ;
116-
117- //get projection matrix from ClientKit and set surface projection matrix
118- OSVR . ClientKit . Matrix44f projMatrix = _displayController . DisplayConfig . GetProjectionMatrixForViewerEyeSurfacef (
119- DisplayController . DEFAULT_VIEWER , ( byte ) i , DisplayController . DEFAULT_SURFACE ,
120- _camera . nearClipPlane , _camera . farClipPlane , OSVR . ClientKit . MatrixConventionsFlags . ColMajor ) ;
121-
122- surface . SetProjectionMatrix ( Math . ConvertMatrix ( projMatrix ) ) ;
123-
124- //render the surface
125- surface . Render ( ) ;
126- }
93+ //update the client
94+ DisplayController . UpdateClient ( ) ;
12795
128- // Remember to reenable.
129- _disabledCamera = true ;
130- }
96+ //update the eye pose
97+ VREye eye = Eyes [ eyeIndex ] ;
98+ eye . UpdateEyePose ( _displayController . DisplayConfig . GetViewerEyePose ( ViewerIndex , ( byte ) eyeIndex ) ) ;
13199
132- IEnumerator EndOfFrame ( )
133- {
134- while ( true )
135- {
136- //if we disabled the dummy camera, enable it here
137- if ( _disabledCamera )
138- {
139- Camera . enabled = true ;
140- _disabledCamera = false ;
141- }
142- yield return new WaitForEndOfFrame ( ) ;
100+ //update the eye's surfaces, includes a call to Render the surface
101+ eye . UpdateSurfaces ( ) ;
143102 }
144- }
103+ }
145104 }
146105 }
147106}
0 commit comments