OvrAvatarSDKManager.cs
14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
using UnityEngine;
using Oculus.Avatar;
using System;
using System.Collections.Generic;
public delegate void specificationCallback(IntPtr specification);
public delegate void assetLoadedCallback(OvrAvatarAsset asset);
public delegate void combinedMeshLoadedCallback(IntPtr asset);
public class OvrAvatarSDKManager : MonoBehaviour
{
private static OvrAvatarSDKManager _instance;
private bool initialized = false;
private Dictionary<ulong, HashSet<specificationCallback>> specificationCallbacks;
private Dictionary<ulong, HashSet<assetLoadedCallback>> assetLoadedCallbacks;
private Dictionary<IntPtr, combinedMeshLoadedCallback> combinedMeshLoadedCallbacks;
private Dictionary<UInt64, OvrAvatarAsset> assetCache;
private OvrAvatarTextureCopyManager textureCopyManager;
public ovrAvatarLogLevel LoggingLevel = ovrAvatarLogLevel.Info;
private Queue<AvatarSpecRequestParams> avatarSpecificationQueue;
private List<int> loadingAvatars;
private bool avatarSpecRequestAvailable = true;
private float lastDispatchedAvatarSpecRequestTime = 0f;
private const float AVATAR_SPEC_REQUEST_TIMEOUT = 5f;
#if AVATAR_DEBUG
private ovrAvatarDebugContext debugContext = ovrAvatarDebugContext.None;
#endif
public struct AvatarSpecRequestParams
{
public UInt64 _userId;
public specificationCallback _callback;
public bool _useCombinedMesh;
public ovrAvatarAssetLevelOfDetail _lod;
public bool _forceMobileTextureFormat;
public ovrAvatarLookAndFeelVersion _lookVersion;
public ovrAvatarLookAndFeelVersion _fallbackVersion;
public bool _enableExpressive;
public AvatarSpecRequestParams(
UInt64 userId,
specificationCallback callback,
bool useCombinedMesh,
ovrAvatarAssetLevelOfDetail lod,
bool forceMobileTextureFormat,
ovrAvatarLookAndFeelVersion lookVersion,
ovrAvatarLookAndFeelVersion fallbackVersion,
bool enableExpressive)
{
_userId = userId;
_callback = callback;
_useCombinedMesh = useCombinedMesh;
_lod = lod;
_forceMobileTextureFormat = forceMobileTextureFormat;
_lookVersion = lookVersion;
_fallbackVersion = fallbackVersion;
_enableExpressive = enableExpressive;
}
}
public static OvrAvatarSDKManager Instance
{
get
{
if (_instance == null)
{
_instance = GameObject.FindObjectOfType<OvrAvatarSDKManager>();
if (_instance == null)
{
GameObject manager = new GameObject("OvrAvatarSDKManager");
_instance = manager.AddComponent<OvrAvatarSDKManager>();
_instance.textureCopyManager = manager.AddComponent<OvrAvatarTextureCopyManager>();
_instance.initialized = _instance.Initialize();
}
}
return _instance.initialized ? _instance : null;
}
}
private bool Initialize()
{
CAPI.Initialize();
string appId = GetAppId();
if (appId == "")
{
AvatarLogger.LogError("No Oculus App ID has been provided for target platform. " +
"Go to Oculus Avatar > Edit Configuration to supply one", OvrAvatarSettings.Instance);
appId = "0";
}
#if UNITY_ANDROID && !UNITY_EDITOR
#if AVATAR_XPLAT
CAPI.ovrAvatar_Initialize(appId);
#else
CAPI.ovrAvatar_InitializeAndroidUnity(appId);
#endif
#else
CAPI.ovrAvatar_Initialize(appId);
CAPI.SendEvent("initialize", appId);
#endif
specificationCallbacks = new Dictionary<UInt64, HashSet<specificationCallback>>();
assetLoadedCallbacks = new Dictionary<UInt64, HashSet<assetLoadedCallback>>();
combinedMeshLoadedCallbacks = new Dictionary<IntPtr, combinedMeshLoadedCallback>();
assetCache = new Dictionary<ulong, OvrAvatarAsset>();
avatarSpecificationQueue = new Queue<AvatarSpecRequestParams>();
loadingAvatars = new List<int>();
CAPI.ovrAvatar_SetLoggingLevel(LoggingLevel);
CAPI.ovrAvatar_RegisterLoggingCallback(CAPI.LoggingCallback);
#if AVATAR_DEBUG
CAPI.ovrAvatar_SetDebugDrawContext((uint)debugContext);
#endif
return true;
}
void OnDestroy()
{
CAPI.Shutdown();
CAPI.ovrAvatar_RegisterLoggingCallback(null);
CAPI.ovrAvatar_Shutdown();
}
void Update()
{
if (Instance == null)
{
return;
}
#if AVATAR_DEBUG
// Call before ovrAvatarMessage_Pop which flushes the state
CAPI.ovrAvatar_DrawDebugLines();
#endif
// Dispatch waiting avatar spec request
if (avatarSpecificationQueue.Count > 0 &&
(avatarSpecRequestAvailable ||
Time.time - lastDispatchedAvatarSpecRequestTime >= AVATAR_SPEC_REQUEST_TIMEOUT))
{
avatarSpecRequestAvailable = false;
AvatarSpecRequestParams avatarSpec = avatarSpecificationQueue.Dequeue();
DispatchAvatarSpecificationRequest(avatarSpec);
lastDispatchedAvatarSpecRequestTime = Time.time;
AvatarLogger.Log("Avatar spec request dispatched: " + avatarSpec._userId);
}
IntPtr message = CAPI.ovrAvatarMessage_Pop();
if (message == IntPtr.Zero)
{
return;
}
ovrAvatarMessageType messageType = CAPI.ovrAvatarMessage_GetType(message);
switch (messageType)
{
case ovrAvatarMessageType.AssetLoaded:
{
ovrAvatarMessage_AssetLoaded assetMessage = CAPI.ovrAvatarMessage_GetAssetLoaded(message);
IntPtr asset = assetMessage.asset;
UInt64 assetID = assetMessage.assetID;
ovrAvatarAssetType assetType = CAPI.ovrAvatarAsset_GetType(asset);
OvrAvatarAsset assetData = null;
IntPtr avatarOwner = IntPtr.Zero;
switch (assetType)
{
case ovrAvatarAssetType.Mesh:
assetData = new OvrAvatarAssetMesh(assetID, asset, ovrAvatarAssetType.Mesh);
break;
case ovrAvatarAssetType.Texture:
assetData = new OvrAvatarAssetTexture(assetID, asset);
break;
case ovrAvatarAssetType.Material:
assetData = new OvrAvatarAssetMaterial(assetID, asset);
break;
case ovrAvatarAssetType.CombinedMesh:
avatarOwner = CAPI.ovrAvatarAsset_GetAvatar(asset);
assetData = new OvrAvatarAssetMesh(assetID, asset, ovrAvatarAssetType.CombinedMesh);
break;
case ovrAvatarAssetType.FailedLoad:
AvatarLogger.LogWarning("Asset failed to load from SDK " + assetID);
break;
default:
throw new NotImplementedException(string.Format("Unsupported asset type format {0}", assetType.ToString()));
}
HashSet<assetLoadedCallback> callbackSet;
if (assetType == ovrAvatarAssetType.CombinedMesh)
{
if (!assetCache.ContainsKey(assetID))
{
assetCache.Add(assetID, assetData);
}
combinedMeshLoadedCallback callback;
if (combinedMeshLoadedCallbacks.TryGetValue(avatarOwner, out callback))
{
callback(asset);
combinedMeshLoadedCallbacks.Remove(avatarOwner);
}
else
{
AvatarLogger.LogWarning("Loaded a combined mesh with no owner: " + assetMessage.assetID);
}
}
else
{
if (assetData != null && assetLoadedCallbacks.TryGetValue(assetMessage.assetID, out callbackSet))
{
assetCache.Add(assetID, assetData);
foreach (var callback in callbackSet)
{
callback(assetData);
}
assetLoadedCallbacks.Remove(assetMessage.assetID);
}
}
break;
}
case ovrAvatarMessageType.AvatarSpecification:
{
avatarSpecRequestAvailable = true;
ovrAvatarMessage_AvatarSpecification spec = CAPI.ovrAvatarMessage_GetAvatarSpecification(message);
HashSet<specificationCallback> callbackSet;
if (specificationCallbacks.TryGetValue(spec.oculusUserID, out callbackSet))
{
foreach (var callback in callbackSet)
{
callback(spec.avatarSpec);
}
specificationCallbacks.Remove(spec.oculusUserID);
}
else
{
AvatarLogger.LogWarning("Error, got an avatar specification callback from a user id we don't have a record for: " + spec.oculusUserID);
}
break;
}
default:
throw new NotImplementedException("Unhandled ovrAvatarMessageType: " + messageType);
}
CAPI.ovrAvatarMessage_Free(message);
}
public bool IsAvatarSpecWaiting()
{
return avatarSpecificationQueue.Count > 0;
}
public bool IsAvatarLoading()
{
return loadingAvatars.Count > 0;
}
// Add avatar gameobject ID to loading list to keep track of loading avatars
public void AddLoadingAvatar(int gameobjectID)
{
loadingAvatars.Add(gameobjectID);
}
// Remove avatar gameobject ID from loading list
public void RemoveLoadingAvatar(int gameobjectID)
{
loadingAvatars.Remove(gameobjectID);
}
// Request an avatar specification to be loaded by adding to the queue.
// Requests are dispatched in Update().
public void RequestAvatarSpecification(AvatarSpecRequestParams avatarSpecRequest)
{
avatarSpecificationQueue.Enqueue(avatarSpecRequest);
AvatarLogger.Log("Avatar spec request queued: " + avatarSpecRequest._userId.ToString());
}
private void DispatchAvatarSpecificationRequest(AvatarSpecRequestParams avatarSpecRequest)
{
textureCopyManager.CheckFallbackTextureSet(avatarSpecRequest._lod);
CAPI.ovrAvatar_SetForceASTCTextures(avatarSpecRequest._forceMobileTextureFormat);
HashSet<specificationCallback> callbackSet;
if (!specificationCallbacks.TryGetValue(avatarSpecRequest._userId, out callbackSet))
{
callbackSet = new HashSet<specificationCallback>();
specificationCallbacks.Add(avatarSpecRequest._userId, callbackSet);
IntPtr specRequest = CAPI.ovrAvatarSpecificationRequest_Create(avatarSpecRequest._userId);
CAPI.ovrAvatarSpecificationRequest_SetLookAndFeelVersion(specRequest, avatarSpecRequest._lookVersion);
CAPI.ovrAvatarSpecificationRequest_SetFallbackLookAndFeelVersion(specRequest, avatarSpecRequest._fallbackVersion);
CAPI.ovrAvatarSpecificationRequest_SetLevelOfDetail(specRequest, avatarSpecRequest._lod);
CAPI.ovrAvatarSpecificationRequest_SetCombineMeshes(specRequest, avatarSpecRequest._useCombinedMesh);
CAPI.ovrAvatarSpecificationRequest_SetExpressiveFlag(specRequest, avatarSpecRequest._enableExpressive);
CAPI.ovrAvatar_RequestAvatarSpecificationFromSpecRequest(specRequest);
CAPI.ovrAvatarSpecificationRequest_Destroy(specRequest);
}
callbackSet.Add(avatarSpecRequest._callback);
}
public void BeginLoadingAsset(
UInt64 assetId,
ovrAvatarAssetLevelOfDetail lod,
assetLoadedCallback callback)
{
HashSet<assetLoadedCallback> callbackSet;
if (!assetLoadedCallbacks.TryGetValue(assetId, out callbackSet))
{
callbackSet = new HashSet<assetLoadedCallback>();
assetLoadedCallbacks.Add(assetId, callbackSet);
}
AvatarLogger.Log("Loading Asset ID: " + assetId);
CAPI.ovrAvatarAsset_BeginLoadingLOD(assetId, lod);
callbackSet.Add(callback);
}
public void RegisterCombinedMeshCallback(
IntPtr sdkAvatar,
combinedMeshLoadedCallback callback)
{
combinedMeshLoadedCallback currentCallback;
if (!combinedMeshLoadedCallbacks.TryGetValue(sdkAvatar, out currentCallback))
{
combinedMeshLoadedCallbacks.Add(sdkAvatar, callback);
}
else
{
throw new Exception("Adding second combind mesh callback for same avatar");
}
}
public OvrAvatarAsset GetAsset(UInt64 assetId)
{
OvrAvatarAsset asset;
if (assetCache.TryGetValue(assetId, out asset))
{
return asset;
}
else
{
return null;
}
}
public void DeleteAssetFromCache(UInt64 assetId)
{
if (assetCache.ContainsKey(assetId))
{
assetCache.Remove(assetId);
}
}
public string GetAppId()
{
return UnityEngine.Application.platform == RuntimePlatform.Android ?
OvrAvatarSettings.MobileAppID : OvrAvatarSettings.AppID;
}
public OvrAvatarTextureCopyManager GetTextureCopyManager()
{
if (textureCopyManager != null)
{
return textureCopyManager;
}
else
{
return null;
}
}
}