Skip to content

Commit 62319a6

Browse files
author
Yuval Boger
committed
Merge pull request #37 from DuFF14/LatencyUpdates
Latency improvements. Read display descriptor file from disk as opposed to being hard-coded
2 parents ffe1b17 + 09dc6fa commit 62319a6

6 files changed

Lines changed: 113 additions & 23 deletions

File tree

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,22 @@ void OnEnable()
102102
void FixedUpdate()
103103
{
104104
EnsureStarted();
105+
//Debug.Log("ClientKit FixedUpdate: frame # " + Time.frameCount + " " + Time.time);
106+
contextObject.update();
107+
}
108+
109+
//may seem superfluous. the goal here is to update the client more often to make sure we have the most recent tracker data
110+
//this helps reduce latency
111+
void Update()
112+
{
113+
contextObject.update();
114+
}
115+
//may seem superfluous. the goal here is to update the client more often to make sure we have the most recent tracker data
116+
//this helps reduce latency
117+
void LateUpdate()
118+
{
105119
contextObject.update();
106120
}
107-
108121
void Stop()
109122
{
110123
if (null != contextObject)

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

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using UnityEngine;
2323
using Newtonsoft.Json;
2424
using System.IO;
25+
using System.Collections;
2526

2627
namespace OSVR
2728
{
@@ -35,21 +36,49 @@ namespace Unity
3536
/// </summary>
3637
public class DisplayInterface : MonoBehaviour
3738
{
39+
const string HmdJsonFileName = "hmd.json"; //hardcoded filename of hmd config in Data folder
3840
private string _deviceDescriptorJson; //a string that is the JSON file to be parsed
3941
public TextAsset JsonDescriptorFile; //drop the json file into this slot in the Unity inspector
42+
public bool Initialized
43+
{
44+
get { return _initialized; }
45+
}
46+
private bool _initialized = false; //flag set when _deviceDescriptorJson has data
47+
4048
void Awake()
4149
{
42-
if (JsonDescriptorFile != null)
50+
//check to see if "hmd.json" is provided in the Data folder
51+
//if so, load it
52+
string filePath = Application.dataPath + "/" + HmdJsonFileName;
53+
if (System.IO.File.Exists(filePath))
4354
{
44-
_deviceDescriptorJson = JsonDescriptorFile.text; //read JSON file directly from Unity if provided
55+
StartCoroutine(LoadJsonFile(filePath));
4556
}
46-
else
57+
else //if not, load the json file provided in the Unity editor
4758
{
48-
_deviceDescriptorJson = ClientKit.instance.context.getStringParameter("/display"); //otherwise read from /display
49-
}
59+
if (JsonDescriptorFile != null)
60+
{
61+
_deviceDescriptorJson = JsonDescriptorFile.text; //read JSON file directly from Unity if provided
62+
}
63+
else
64+
{
65+
_deviceDescriptorJson = ClientKit.instance.context.getStringParameter("/display"); //otherwise read from /display
66+
}
67+
_initialized = true;
68+
}
69+
}
70+
71+
//coroutine for loading an external json config file
72+
//this could be more generic, but I'm not sure we will be loading external files
73+
//this will eventually go away anyway when we get display config data from /display
74+
private IEnumerator LoadJsonFile(string filePath)
75+
{
76+
WWW jsonFile = new WWW("file://" + filePath);
77+
yield return jsonFile;
78+
_initialized = true;
79+
_deviceDescriptorJson = jsonFile.text;
5080
}
5181

52-
5382

5483
/// <summary>
5584
/// This function will parse the device parameters from a device descriptor json file.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,30 @@ namespace Unity
3232
/// </summary>
3333
public class PoseInterface : InterfaceGameObject
3434
{
35+
//private int currentFrame = 0;
36+
//private int frameCount = 0;
3537
new void Start()
3638
{
3739
osvrInterface.RegisterCallback(callback);
40+
//currentFrame = Time.frameCount;
41+
//frameCount = 0;
3842
}
3943

4044
private void callback(string source, Vector3 position, Quaternion rotation)
4145
{
4246
transform.localPosition = position;
4347
transform.localRotation = rotation;
48+
//keeping this here for now for debugging purposes
49+
/*if(currentFrame != Time.frameCount)
50+
{
51+
Debug.Log("Time.frameCount = " + currentFrame + ", Time.time = " + Time.time + ". Callbacks per frame = " + frameCount);
52+
frameCount = 0;
53+
currentFrame = Time.frameCount;
54+
}
55+
else
56+
{
57+
frameCount++;
58+
}*/
4459
}
4560
}
4661
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ public class VREye : MonoBehaviour
3737
{
3838
#region Private Variables
3939
private Camera _camera;
40+
private ClientKit clientKit;
4041
#endregion
4142
#region Public Variables
4243
public Eye eye;
4344
public Camera Camera { get { return _camera; } set { _camera = value; } }
4445
[HideInInspector]
4546
public Transform cachedTransform;
47+
4648
#endregion
4749

4850
#region Init
@@ -90,6 +92,10 @@ public void SetEyeRoll(float rollAmount)
9092
#region Private Methods
9193
void Init()
9294
{
95+
if(clientKit == null)
96+
{
97+
clientKit = GameObject.FindObjectOfType<ClientKit>();
98+
}
9399
//cache:
94100
cachedTransform = transform;
95101

@@ -122,6 +128,14 @@ private void SetViewportRects()
122128
break;
123129
}
124130
}
131+
132+
//Called after a camera finishes rendering the scene.
133+
//the goal here is to update the client often to make sure we have the most recent tracker data
134+
//this helps reduce latency
135+
void OnPostRender()
136+
{
137+
clientKit.context.update();
138+
}
125139
#endregion
126140

127141

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

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class VRHead : MonoBehaviour
5353
private Camera _camera;
5454
private DeviceDescriptor _deviceDescriptor;
5555
private OsvrDistortion _distortionEffect;
56+
private bool _initDisplayInterface = false;
5657
#endregion
5758

5859
#region Init
@@ -65,23 +66,24 @@ void Start()
6566
{
6667
_distortionEffect.enabled = (viewMode == ViewMode.mono);
6768
}
68-
GetDeviceDescription();
69-
MatchEyes(); //copy camera properties to each eye
70-
//rotate each eye based on overlap percent, must do this after match eyes
71-
if (_deviceDescriptor != null)
69+
70+
//update VRHead with info from the display interface if it has been initialized
71+
//it might not be initialized if it is still loading/parsing a display json file
72+
//in that case, we will try to initialize asap in the update function
73+
if (GetComponent<DisplayInterface>().Initialized)
7274
{
73-
SetEyeRotation(_deviceDescriptor.OverlapPercent, _deviceDescriptor.MonocularHorizontal);
74-
SetEyeRoll(_deviceDescriptor.LeftRoll, _deviceDescriptor.RightRoll);
75-
}
76-
77-
78-
79-
}
75+
UpdateDisplayInterface();
76+
}
77+
}
8078
#endregion
8179

8280
#region Loop
8381
void Update()
8482
{
83+
if(!_initDisplayInterface && GetComponent<DisplayInterface>().Initialized)
84+
{
85+
UpdateDisplayInterface();
86+
}
8587
UpdateStereoAmount();
8688
UpdateViewMode();
8789
}
@@ -91,6 +93,18 @@ void Update()
9193
#endregion
9294

9395
#region Private Methods
96+
private void UpdateDisplayInterface()
97+
{
98+
GetDeviceDescription();
99+
MatchEyes(); //copy camera properties to each eye
100+
//rotate each eye based on overlap percent, must do this after match eyes
101+
if (_deviceDescriptor != null)
102+
{
103+
SetEyeRotation(_deviceDescriptor.OverlapPercent, _deviceDescriptor.MonocularHorizontal);
104+
SetEyeRoll(_deviceDescriptor.LeftRoll, _deviceDescriptor.RightRoll);
105+
}
106+
_initDisplayInterface = true;
107+
}
94108
void UpdateViewMode()
95109
{
96110
if (Time.realtimeSinceStartup < 100 || _previousViewMode != viewMode)
@@ -170,6 +184,8 @@ void Init()
170184

171185
//60 FPS whenever possible:
172186
Application.targetFrameRate = 60;
187+
188+
_initDisplayInterface = false;
173189
}
174190

175191
/// <summary>
@@ -178,10 +194,10 @@ void Init()
178194
/// </summary>
179195
private void GetDeviceDescription()
180196
{
181-
_deviceDescriptor = GetComponent<DisplayInterface>().GetDeviceDescription();
182-
Debug.Log(_deviceDescriptor.ToString());
197+
_deviceDescriptor = GetComponent<DisplayInterface>().GetDeviceDescription();
183198
if (_deviceDescriptor != null)
184199
{
200+
Debug.Log(_deviceDescriptor.ToString());
185201
switch (_deviceDescriptor.DisplayMode)
186202
{
187203
case "full_screen":

OSVR-Unity/CHANGES.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
This is an abbreviated changelog for the OSVR Unity Plugin.
44

55
Use git for a full changelog.
6-
##Recent Changes (updated 18-March-2015)
7-
##Distortion (20-March-2015)
6+
##Recent Changes (updated 28-March-2015)
7+
##External Json File v0.1-56-gf2d8bab (20-March-2015)
8+
- The JSON file containing display configuration can now be read from a file at runtime. To do this, add a config file "hmd.json" to the _Data folder that is created in a build. The format of the JSON file has not changed at all. It is the same file you would drag-and-drop onto the DisplayInterface component on your VRDisplayTracked prefab. If "hmd.json" does not exist in the Data folder (and it won't by default unless you put it there), then the plugin will look for the JSON file assigned in the DisplayInterface component in your scene. If that also has not been assigned, it will fall back to reading OSVR's /display parameter (this will eventually be the default).
9+
10+
##Distortion v0.1-55-g046c709(20-March-2015)
811
- Unity 4 Free limitations. The new distortion shader will not work with Unity 4 Free version. It will work with Unity 4 Pro or Unity 5. If you are using Unity 4 Free, everything will still work but there won't be any distortion. This is due to the distortion shader using a render to texture pass.
912
- Fixed Timestep was changed from 0.2 to 0.01667. This is how often FixedUpdate() gets called. The change should result in a more comfortable VR experience, but make sure it doesn't break any physics in your game.
1013
- Changed the default quality settings. Turned on 4x AA and disabled shadows.
1114

12-
##Unity 5 Update (updated 18-March-2015)
15+
##Unity 5 Update v0.1-55-g046c709 (updated 18-March-2015)
1316
- Refactored VRHead.cs and VREye.cs to be Unity 5-friendly (mostly instances of "camera" that needed to become GetComponent<Camera>()). Upgrading from Unity 4 to Unity 5 should be seamless.
1417
- VRDisplayTracked.prefab has been updated to use the json descriptor file for the HDK by default. If you are using another HMD, be sure to set the corresponding json file on the VRDisplayTracked prefab's DisplayInterface script.
1518
- Fixed issue with stereo overlap and roll for HMDs with multiple video inputs.

0 commit comments

Comments
 (0)