| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.Assertions; |
| | 5 | |
|
| | 6 | | namespace DCL.Components |
| | 7 | | { |
| | 8 | | public class RendereableAssetLoadHelper |
| | 9 | | { |
| | 10 | | public event Action<Rendereable> OnSuccessEvent; |
| | 11 | | public event Action<Exception> OnFailEvent; |
| | 12 | |
|
| | 13 | | public enum LoadingType |
| | 14 | | { |
| | 15 | | ASSET_BUNDLE_WITH_GLTF_FALLBACK, |
| | 16 | | ASSET_BUNDLE_WITH_OLD_GLTF_FALLBACK, |
| | 17 | | ASSET_BUNDLE_ONLY, |
| | 18 | | GLTF_ONLY, |
| | 19 | | OLD_GLTF, |
| | 20 | | DEFAULT |
| | 21 | | } |
| | 22 | |
|
| | 23 | | private const string AB_GO_NAME_PREFIX = "AB:"; |
| | 24 | | private const string GLTF_GO_NAME_PREFIX = "GLTF:"; |
| | 25 | | private const string GLTFAST_GO_NAME_PREFIX = "GLTFast:"; |
| | 26 | | private const string FROM_ASSET_BUNDLE_TAG = "FromAssetBundle"; |
| | 27 | | private const string FROM_RAW_GLTF_TAG = "FromRawGLTF"; |
| | 28 | |
|
| | 29 | | public static bool VERBOSE = false; |
| | 30 | | public static bool useCustomContentServerUrl = false; |
| | 31 | | public static string customContentServerUrl; |
| | 32 | | public static LoadingType defaultLoadingType = LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK; |
| | 33 | |
|
| 82 | 34 | | public AssetPromiseSettings_Rendering settings = new (); |
| 298 | 35 | | public Rendereable loadedAsset { get; protected set; } |
| | 36 | |
|
| | 37 | | private readonly string bundlesContentUrl; |
| | 38 | | private readonly Func<bool> IsGltFastEnabled; |
| | 39 | | private readonly ContentProvider contentProvider; |
| | 40 | | private AssetPromise_GLTF gltfPromise; |
| | 41 | | private AssetPromise_GLTFast_Instance gltfastPromise; |
| | 42 | | private AssetPromise_AB_GameObject abPromise; |
| | 43 | |
|
| | 44 | | public bool isFinished |
| | 45 | | { |
| | 46 | | get |
| | 47 | | { |
| 73 | 48 | | if (gltfPromise != null) |
| 73 | 49 | | return gltfPromise.state == AssetPromiseState.FINISHED; |
| | 50 | |
|
| 0 | 51 | | if (abPromise != null) |
| 0 | 52 | | return abPromise.state == AssetPromiseState.FINISHED; |
| | 53 | |
|
| 0 | 54 | | return true; |
| | 55 | | } |
| | 56 | | } |
| | 57 | |
|
| | 58 | | #if UNITY_EDITOR |
| | 59 | | public override string ToString() |
| | 60 | | { |
| 0 | 61 | | float loadTime = Mathf.Min(loadFinishTime, Time.realtimeSinceStartup) - loadStartTime; |
| | 62 | |
|
| 0 | 63 | | string result = "not loading"; |
| | 64 | |
|
| 0 | 65 | | if (gltfPromise != null) |
| | 66 | | { |
| 0 | 67 | | result = $"GLTF -> promise state = {gltfPromise.state} ({loadTime} load time)... waiting promises = {Ass |
| | 68 | |
|
| 0 | 69 | | if (gltfPromise.state == AssetPromiseState.WAITING) { result += $"\nmaster promise state... is blocked.. |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | if (abPromise != null) { result = $"ASSET BUNDLE -> promise state = {abPromise.ToString()} ({loadTime} load |
| | 73 | |
|
| 0 | 74 | | return result; |
| | 75 | | } |
| | 76 | |
|
| | 77 | | float loadStartTime = 0; |
| 82 | 78 | | float loadFinishTime = float.MaxValue; |
| | 79 | | #endif |
| | 80 | |
|
| 82 | 81 | | public RendereableAssetLoadHelper(ContentProvider contentProvider, string bundlesContentUrl, Func<bool> isGltFas |
| | 82 | | { |
| 82 | 83 | | this.contentProvider = contentProvider; |
| 82 | 84 | | this.bundlesContentUrl = bundlesContentUrl; |
| 82 | 85 | | this.IsGltFastEnabled = isGltFastEnabled; |
| 82 | 86 | | } |
| | 87 | |
|
| | 88 | | public void Load(string targetUrl, LoadingType forcedLoadingType = LoadingType.DEFAULT) |
| | 89 | | { |
| 88 | 90 | | Assert.IsFalse(string.IsNullOrEmpty(targetUrl), "url is null!!"); |
| | 91 | | #if UNITY_EDITOR |
| 88 | 92 | | loadStartTime = Time.realtimeSinceStartup; |
| | 93 | | #endif |
| | 94 | |
|
| 88 | 95 | | LoadingType finalLoadingType = forcedLoadingType == LoadingType.DEFAULT ? defaultLoadingType : forcedLoading |
| | 96 | |
|
| | 97 | | switch (finalLoadingType) |
| | 98 | | { |
| | 99 | | case LoadingType.ASSET_BUNDLE_ONLY: |
| 0 | 100 | | LoadAssetBundle(targetUrl, OnSuccessEvent, OnFailEvent); |
| 0 | 101 | | break; |
| | 102 | | case LoadingType.GLTF_ONLY: |
| 8 | 103 | | ProxyLoadGltf(targetUrl); |
| 8 | 104 | | break; |
| | 105 | | case LoadingType.OLD_GLTF: |
| 0 | 106 | | LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent); |
| 0 | 107 | | break; |
| | 108 | | case LoadingType.DEFAULT: |
| | 109 | | case LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK: |
| 160 | 110 | | LoadAssetBundle(targetUrl, OnSuccessEvent, exception => ProxyLoadGltf(targetUrl)); |
| 80 | 111 | | break; |
| | 112 | | case LoadingType.ASSET_BUNDLE_WITH_OLD_GLTF_FALLBACK: |
| 0 | 113 | | LoadAssetBundle(targetUrl, OnSuccessEvent, exception => LoadGltf(targetUrl, OnSuccessEvent, OnFailEv |
| | 114 | | break; |
| | 115 | | } |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | private void ProxyLoadGltf(string targetUrl) |
| | 119 | | { |
| 88 | 120 | | if (IsGltFastEnabled()) |
| 0 | 121 | | LoadGLTFast(targetUrl, OnSuccessEvent, _ => |
| | 122 | | { |
| 0 | 123 | | Debug.LogError($"GLTFast failed to load for {targetUrl} so we are going to fallback into old gltf"); |
| 0 | 124 | | LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent); |
| 0 | 125 | | }); |
| | 126 | | else |
| 88 | 127 | | LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent); |
| 88 | 128 | | } |
| | 129 | |
|
| | 130 | | public void Unload() |
| | 131 | | { |
| 70 | 132 | | UnloadAB(); |
| 70 | 133 | | UnloadGLTF(); |
| 70 | 134 | | UnloadGLTFast(); |
| 70 | 135 | | } |
| | 136 | |
|
| | 137 | | void UnloadAB() |
| | 138 | | { |
| 70 | 139 | | if (abPromise != null) { AssetPromiseKeeper_AB_GameObject.i.Forget(abPromise); } |
| 70 | 140 | | } |
| | 141 | |
|
| | 142 | | void UnloadGLTF() |
| | 143 | | { |
| 146 | 144 | | if (gltfPromise != null) { AssetPromiseKeeper_GLTF.i.Forget(gltfPromise); } |
| 76 | 145 | | } |
| | 146 | |
|
| | 147 | | void UnloadGLTFast() |
| | 148 | | { |
| 70 | 149 | | if (gltfastPromise != null) { AssetPromiseKeeper_GLTFast_Instance.i.Forget(gltfastPromise); } |
| 70 | 150 | | } |
| | 151 | |
|
| | 152 | | void LoadAssetBundle(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail) |
| | 153 | | { |
| 80 | 154 | | if (abPromise != null) |
| | 155 | | { |
| 0 | 156 | | UnloadAB(); |
| | 157 | |
|
| 0 | 158 | | if (VERBOSE) |
| 0 | 159 | | Debug.Log("Forgetting not null promise..." + targetUrl); |
| | 160 | | } |
| | 161 | |
|
| 80 | 162 | | string bundlesBaseUrl = useCustomContentServerUrl ? customContentServerUrl : bundlesContentUrl; |
| | 163 | |
|
| 80 | 164 | | if (string.IsNullOrEmpty(bundlesBaseUrl)) |
| | 165 | | { |
| 80 | 166 | | OnFailWrapper(OnFail, new Exception("bundlesBaseUrl is null")); |
| 80 | 167 | | return; |
| | 168 | | } |
| | 169 | |
|
| 0 | 170 | | if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash)) |
| | 171 | | { |
| 0 | 172 | | OnFailWrapper(OnFail, new Exception($"Content url does not contains {targetUrl}")); |
| 0 | 173 | | return; |
| | 174 | | } |
| | 175 | |
|
| 0 | 176 | | abPromise = new AssetPromise_AB_GameObject(bundlesBaseUrl, hash); |
| 0 | 177 | | abPromise.settings = this.settings; |
| | 178 | |
|
| 0 | 179 | | abPromise.OnSuccessEvent += (x) => |
| | 180 | | { |
| | 181 | | #if UNITY_EDITOR |
| 0 | 182 | | x.container.name = AB_GO_NAME_PREFIX + x.container.name; |
| | 183 | | #endif |
| 0 | 184 | | var r = new Rendereable() |
| | 185 | | { |
| | 186 | | container = x.container, |
| | 187 | | totalTriangleCount = x.totalTriangleCount, |
| | 188 | | meshes = x.meshes, |
| | 189 | | renderers = x.renderers, |
| | 190 | | materials = x.materials, |
| | 191 | | textures = x.textures, |
| | 192 | | meshToTriangleCount = x.meshToTriangleCount, |
| | 193 | | animationClipSize = x.animationClipSize, |
| | 194 | | animationClips = x.animationClips, |
| | 195 | | meshDataSize = x.meshDataSize |
| | 196 | | }; |
| | 197 | |
|
| 0 | 198 | | foreach (var someRenderer in r.renderers) { someRenderer.tag = FROM_ASSET_BUNDLE_TAG; } |
| | 199 | |
|
| 0 | 200 | | OnSuccessWrapper(r, OnSuccess); |
| 0 | 201 | | }; |
| | 202 | |
|
| 0 | 203 | | abPromise.OnFailEvent += (x, exception) => OnFailWrapper(OnFail, exception); |
| | 204 | |
|
| 0 | 205 | | AssetPromiseKeeper_AB_GameObject.i.Keep(abPromise); |
| 0 | 206 | | } |
| | 207 | |
|
| | 208 | | void LoadGltf(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail) |
| | 209 | | { |
| 88 | 210 | | if (gltfPromise != null) |
| | 211 | | { |
| 6 | 212 | | UnloadGLTF(); |
| | 213 | |
|
| 6 | 214 | | if (VERBOSE) |
| 0 | 215 | | Debug.Log("Forgetting not null promise... " + targetUrl); |
| | 216 | | } |
| | 217 | |
|
| 88 | 218 | | if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash)) |
| | 219 | | { |
| 0 | 220 | | OnFailWrapper(OnFail, new Exception($"Content provider does not contains url {targetUrl}")); |
| 0 | 221 | | return; |
| | 222 | | } |
| | 223 | |
|
| 88 | 224 | | gltfPromise = new AssetPromise_GLTF(contentProvider, targetUrl, hash); |
| 88 | 225 | | gltfPromise.settings = settings; |
| | 226 | |
|
| 88 | 227 | | gltfPromise.OnSuccessEvent += (Asset_GLTF x) => |
| | 228 | | { |
| | 229 | | #if UNITY_EDITOR |
| 81 | 230 | | x.container.name = GLTF_GO_NAME_PREFIX + x.container.name; |
| | 231 | | #endif |
| 81 | 232 | | var r = new Rendereable |
| | 233 | | { |
| | 234 | | container = x.container, |
| | 235 | | totalTriangleCount = x.totalTriangleCount, |
| | 236 | | meshes = x.meshes, |
| | 237 | | renderers = x.renderers, |
| | 238 | | materials = x.materials, |
| | 239 | | textures = x.textures, |
| | 240 | | meshToTriangleCount = x.meshToTriangleCount, |
| | 241 | | animationClipSize = x.animationClipSize, |
| | 242 | | meshDataSize = x.meshDataSize, |
| | 243 | | animationClips = x.animationClips |
| | 244 | | }; |
| | 245 | |
|
| 723 | 246 | | foreach (var someRenderer in r.renderers) { someRenderer.tag = FROM_RAW_GLTF_TAG; } |
| | 247 | |
|
| 81 | 248 | | OnSuccessWrapper(r, OnSuccess); |
| 81 | 249 | | }; |
| | 250 | |
|
| 91 | 251 | | gltfPromise.OnFailEvent += (asset, exception) => OnFailWrapper(OnFail, exception); |
| | 252 | |
|
| 88 | 253 | | AssetPromiseKeeper_GLTF.i.Keep(gltfPromise); |
| 88 | 254 | | } |
| | 255 | |
|
| | 256 | | private void LoadGLTFast(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail) |
| | 257 | | { |
| 0 | 258 | | if (gltfastPromise != null) |
| | 259 | | { |
| 0 | 260 | | UnloadGLTFast(); |
| | 261 | |
|
| 0 | 262 | | if (VERBOSE) |
| 0 | 263 | | Debug.Log("Forgetting not null promise... " + targetUrl); |
| | 264 | | } |
| | 265 | |
|
| 0 | 266 | | if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash)) |
| | 267 | | { |
| 0 | 268 | | OnFailWrapper(OnFail, new Exception($"Content provider does not contains url {targetUrl}")); |
| 0 | 269 | | return; |
| | 270 | | } |
| | 271 | |
|
| 0 | 272 | | gltfastPromise = new AssetPromise_GLTFast_Instance(targetUrl, hash, Environment.i.platform.webRequest, conte |
| | 273 | |
|
| 0 | 274 | | gltfastPromise.OnSuccessEvent += (Asset_GLTFast_Instance x) => |
| | 275 | | { |
| | 276 | | #if UNITY_EDITOR |
| 0 | 277 | | x.container.name = GLTFAST_GO_NAME_PREFIX + x.container.name; |
| | 278 | | #endif |
| 0 | 279 | | Rendereable r = x.ToRendereable(); |
| | 280 | |
|
| 0 | 281 | | OnSuccessWrapper(r, OnSuccess); |
| 0 | 282 | | }; |
| | 283 | |
|
| 0 | 284 | | gltfastPromise.OnFailEvent += (asset, exception) => |
| | 285 | | { |
| 0 | 286 | | Debug.LogException(exception); |
| 0 | 287 | | OnFailWrapper(OnFail, exception); |
| 0 | 288 | | }; |
| | 289 | |
|
| 0 | 290 | | AssetPromiseKeeper_GLTFast_Instance.i.Keep(gltfastPromise); |
| 0 | 291 | | } |
| | 292 | |
|
| | 293 | | private void OnFailWrapper(Action<Exception> OnFail, Exception exception) |
| | 294 | | { |
| | 295 | | #if UNITY_EDITOR |
| 83 | 296 | | loadFinishTime = Time.realtimeSinceStartup; |
| | 297 | | #endif |
| | 298 | |
|
| 83 | 299 | | OnFail?.Invoke(exception); |
| 83 | 300 | | ClearEvents(); |
| 83 | 301 | | } |
| | 302 | |
|
| | 303 | | private void OnSuccessWrapper(Rendereable loadedAsset, Action<Rendereable> OnSuccess) |
| | 304 | | { |
| | 305 | | #if UNITY_EDITOR |
| 81 | 306 | | loadFinishTime = Time.realtimeSinceStartup; |
| | 307 | | #endif |
| 81 | 308 | | if (VERBOSE) |
| | 309 | | { |
| 0 | 310 | | if (gltfPromise != null) |
| 0 | 311 | | Debug.Log($"GLTF Load(): target URL -> {gltfPromise.GetId()}. Success!"); |
| | 312 | | else |
| 0 | 313 | | Debug.Log($"AB Load(): target URL -> {abPromise.hash}. Success!"); |
| | 314 | | } |
| | 315 | |
|
| 81 | 316 | | this.loadedAsset = loadedAsset; |
| 81 | 317 | | OnSuccess?.Invoke(loadedAsset); |
| 81 | 318 | | ClearEvents(); |
| 81 | 319 | | } |
| | 320 | |
|
| | 321 | | public void ClearEvents() |
| | 322 | | { |
| 192 | 323 | | OnSuccessEvent = null; |
| 192 | 324 | | OnFailEvent = null; |
| 192 | 325 | | } |
| | 326 | | } |
| | 327 | | } |