| | 1 | | using System; |
| | 2 | | using System.Threading; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DCL.GLTFast.Wrappers; |
| | 5 | | using GLTFast; |
| | 6 | | using GLTFast.Logging; |
| | 7 | | using GLTFast.Materials; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | // Disable async call not being awaited warning |
| | 11 | | #pragma warning disable CS4014 |
| | 12 | |
|
| | 13 | | namespace DCL |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Do not use this class directly to instantiate a new GLTF, use AssetPromise_GLTFast_Instance instead |
| | 17 | | /// </summary> |
| | 18 | | public class AssetPromise_GLTFast_Loader : AssetPromise_WithUrl<Asset_GLTFast_Loader> |
| | 19 | | { |
| | 20 | | private const string SHADER_DCL_LIT = "DCL/Universal Render Pipeline/Lit"; |
| | 21 | | private const string GLTFAST_THROTTLER_NAME = "GLTFastThrottler"; |
| | 22 | | private readonly ContentProvider contentProvider; |
| | 23 | | private readonly string assetDirectoryPath; |
| | 24 | | private readonly GltFastDownloadProvider gltFastDownloadProvider; |
| | 25 | | private readonly CancellationTokenSource cancellationSource; |
| | 26 | |
|
| | 27 | | private readonly IMaterialGenerator gltFastMaterialGenerator; |
| | 28 | | private readonly ConsoleLogger consoleLogger; |
| | 29 | |
|
| | 30 | | private static IDeferAgent staticDeferAgent; |
| | 31 | |
|
| | 32 | | public AssetPromise_GLTFast_Loader(string contentUrl, string hash, IWebRequestController requestController, Cont |
| 0 | 33 | | : base(contentUrl, hash) |
| | 34 | | { |
| 0 | 35 | | this.contentProvider = contentProvider; |
| 0 | 36 | | assetDirectoryPath = URIHelper.GetDirectoryName(contentUrl); |
| | 37 | |
|
| 0 | 38 | | if (staticDeferAgent == null) |
| | 39 | | { |
| 0 | 40 | | var agentObject = new GameObject(GLTFAST_THROTTLER_NAME); |
| 0 | 41 | | staticDeferAgent = agentObject.AddComponent<GltFastDeferAgent>(); |
| | 42 | | } |
| | 43 | |
|
| 0 | 44 | | string baseUrl = contentProvider is null ? string.Empty : contentProvider.baseUrl; |
| 0 | 45 | | gltFastDownloadProvider = new GltFastDownloadProvider(baseUrl, requestController, FileToUrl); |
| 0 | 46 | | cancellationSource = new CancellationTokenSource(); |
| 0 | 47 | | gltFastMaterialGenerator = new DecentralandMaterialGenerator(SHADER_DCL_LIT); |
| 0 | 48 | | consoleLogger = new ConsoleLogger(); |
| 0 | 49 | | } |
| | 50 | |
|
| 0 | 51 | | protected override void OnBeforeLoadOrReuse() { } |
| | 52 | |
|
| 0 | 53 | | protected override void OnAfterLoadOrReuse() { } |
| | 54 | |
|
| | 55 | | protected override bool AddToLibrary() |
| | 56 | | { |
| 0 | 57 | | if (!library.Add(asset)) |
| | 58 | | { |
| 0 | 59 | | Debug.Log("add to library fail?"); |
| 0 | 60 | | return false; |
| | 61 | | } |
| | 62 | |
|
| 0 | 63 | | if (asset == null) |
| | 64 | | { |
| 0 | 65 | | Debug.LogWarning($"Asset is null when trying to add it to the library: hash == {this.GetId()}"); |
| 0 | 66 | | return false; |
| | 67 | | } |
| | 68 | |
|
| 0 | 69 | | asset = library.Get(asset.id); |
| 0 | 70 | | return true; |
| | 71 | | } |
| | 72 | |
|
| | 73 | | protected override void OnCancelLoading() |
| | 74 | | { |
| 0 | 75 | | cancellationSource.Cancel(); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | protected override void OnLoad(Action onSuccess, Action<Exception> onFail) |
| | 79 | | { |
| 0 | 80 | | ImportGltfAsync(onSuccess, onFail, cancellationSource.Token); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | internal override void Unload() |
| | 84 | | { |
| 0 | 85 | | base.Unload(); |
| 0 | 86 | | gltFastDownloadProvider.Dispose(); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | private async UniTaskVoid ImportGltfAsync(Action onSuccess, Action<Exception> onFail, CancellationToken cancella |
| | 90 | | { |
| | 91 | | try |
| | 92 | | { |
| 0 | 93 | | string url = contentProvider.baseUrl + hash; |
| | 94 | |
|
| 0 | 95 | | var gltfImport = new GltfImport(gltFastDownloadProvider, staticDeferAgent, gltFastMaterialGenerator, con |
| | 96 | |
|
| 0 | 97 | | var gltFastSettings = new ImportSettings |
| | 98 | | { |
| | 99 | | GenerateMipMaps = false, |
| | 100 | | AnisotropicFilterLevel = 3, |
| | 101 | | NodeNameMethod = NameImportMethod.OriginalUnique |
| | 102 | | }; |
| | 103 | |
|
| 0 | 104 | | bool success = await gltfImport.Load(url, gltFastSettings, cancellationSourceToken).AsUniTask().AttachEx |
| | 105 | |
|
| 0 | 106 | | if (cancellationSourceToken.IsCancellationRequested) |
| | 107 | | { |
| 0 | 108 | | gltfImport.Dispose(); |
| 0 | 109 | | cancellationSourceToken.ThrowIfCancellationRequested(); |
| | 110 | | } |
| | 111 | |
|
| 0 | 112 | | if (!success) |
| 0 | 113 | | onFail?.Invoke(new Exception($"[GLTFast] Failed to load asset {url}")); |
| | 114 | | else |
| | 115 | | { |
| 0 | 116 | | asset.Setup(gltfImport); |
| 0 | 117 | | onSuccess.Invoke(); |
| | 118 | | } |
| 0 | 119 | | } |
| 0 | 120 | | catch (Exception e) |
| | 121 | | { |
| 0 | 122 | | if (e is OperationCanceledException) |
| 0 | 123 | | return; |
| | 124 | |
|
| 0 | 125 | | Debug.LogException(e); |
| 0 | 126 | | onFail?.Invoke(e); |
| 0 | 127 | | } |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | private bool FileToUrl(string fileName, out string fileHash) => |
| 0 | 131 | | contentProvider.TryGetContentsUrl(assetDirectoryPath + fileName, out fileHash); |
| | 132 | | } |
| | 133 | | } |