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