Skip to content

Commit f2d8bab

Browse files
committed
Modified DisplayInterface to look for json in _Data folder at runtime.
If it's not there, load json from Unity scene as usual.
1 parent 046c709 commit f2d8bab

2 files changed

Lines changed: 66 additions & 19 deletions

File tree

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

Lines changed: 37 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,20 +36,51 @@ namespace Unity
3536
/// </summary>
3637
public class DisplayInterface : MonoBehaviour
3738
{
39+
const string HmdJsonFileName = "hmd.json";
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+
43+
public bool Initialized
44+
{
45+
get { return _initialized; }
46+
}
47+
private bool _initialized = false;
48+
4049
void Awake()
4150
{
42-
if (JsonDescriptorFile != null)
51+
//check to see if "hmd.json" is provided in the Data folder
52+
//if so, load it
53+
string filePath = Application.dataPath + "/" + HmdJsonFileName;
54+
if (System.IO.File.Exists(filePath))
4355
{
44-
_deviceDescriptorJson = JsonDescriptorFile.text; //read JSON file directly from Unity if provided
45-
}
46-
else
56+
StartCoroutine(LoadJsonFile(filePath));
57+
}
58+
else //if not, load the json file provided in the Unity editor
4759
{
48-
_deviceDescriptorJson = ClientKit.instance.context.getStringParameter("/display"); //otherwise read from /display
60+
61+
if (JsonDescriptorFile != null)
62+
{
63+
_deviceDescriptorJson = JsonDescriptorFile.text; //read JSON file directly from Unity if provided
64+
}
65+
else
66+
{
67+
_deviceDescriptorJson = ClientKit.instance.context.getStringParameter("/display"); //otherwise read from /display
68+
}
69+
_initialized = true;
4970
}
71+
72+
5073
}
5174

75+
private IEnumerator LoadJsonFile(string filePath)
76+
{
77+
WWW jsonFile = new WWW("file://"+filePath);
78+
yield return jsonFile;
79+
_initialized = true;
80+
_deviceDescriptorJson = jsonFile.text;
81+
}
82+
83+
5284

5385

5486
/// <summary>
@@ -72,7 +104,6 @@ public DeviceDescriptor GetDeviceDescription()
72104
//create a device descriptor object for storing the parsed json in an object
73105
DeviceDescriptor deviceDescriptor;
74106
JsonTextReader reader;
75-
76107

77108
reader = new JsonTextReader(new StringReader(_deviceDescriptorJson));
78109
if(reader != null)

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":

0 commit comments

Comments
 (0)