| | 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 System.Diagnostics; |
| | 9 | | using System.IO; |
| | 10 | | using UnityEngine; |
| | 11 | | using Debug = UnityEngine.Debug; |
| | 12 | |
|
| | 13 | | // Disable async call not being awaited warning |
| | 14 | | #pragma warning disable CS4014 |
| | 15 | |
|
| | 16 | | namespace DCL |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Do not use this class directly to instantiate a new GLTF, use AssetPromise_GLTFast_Instance instead |
| | 20 | | /// </summary> |
| | 21 | | public class AssetPromise_GLTFast_Loader : AssetPromise_WithUrl<Asset_GLTFast_Loader> |
| | 22 | | { |
| | 23 | | private const string SHADER_DCL_LIT = "DCL/Universal Render Pipeline/Lit"; |
| | 24 | | private const string GLTFAST_THROTTLER_NAME = "GLTFastThrottler"; |
| | 25 | | private readonly ContentProvider contentProvider; |
| | 26 | | private readonly string assetDirectoryPath; |
| | 27 | | private readonly GltFastDownloadProvider gltFastDownloadProvider; |
| | 28 | | private readonly CancellationTokenSource cancellationSource; |
| | 29 | |
|
| | 30 | | private readonly IMaterialGenerator gltFastMaterialGenerator; |
| | 31 | | private readonly GltfastEditorLogger consoleLogger; |
| | 32 | |
|
| | 33 | | private static IDeferAgent staticDeferAgent; |
| | 34 | | private bool isLoading = false; |
| | 35 | |
|
| | 36 | | public AssetPromise_GLTFast_Loader(string contentUrl, string hash, IWebRequestController requestController, Cont |
| 103 | 37 | | : base(contentUrl, hash) |
| | 38 | | { |
| 103 | 39 | | this.contentProvider = contentProvider; |
| 103 | 40 | | assetDirectoryPath = GetDirectoryName(contentUrl); |
| | 41 | |
|
| 103 | 42 | | if (staticDeferAgent == null) |
| | 43 | | { |
| 1 | 44 | | var agentObject = new GameObject(GLTFAST_THROTTLER_NAME); |
| 1 | 45 | | staticDeferAgent = agentObject.AddComponent<GltFastDeferAgent>(); |
| | 46 | | } |
| | 47 | |
|
| 103 | 48 | | string baseUrl = contentProvider is null ? string.Empty : contentProvider.baseUrl; |
| 103 | 49 | | gltFastDownloadProvider = new GltFastDownloadProvider(baseUrl, requestController, FileToUrl, AssetPromiseKee |
| 103 | 50 | | cancellationSource = new CancellationTokenSource(); |
| 103 | 51 | | gltFastMaterialGenerator = new DecentralandMaterialGenerator(SHADER_DCL_LIT); |
| 103 | 52 | | consoleLogger = new GltfastEditorLogger(); |
| 103 | 53 | | } |
| | 54 | |
|
| | 55 | | private static string GetDirectoryName(string fullPath) |
| | 56 | | { |
| 103 | 57 | | var fileName = Path.GetFileName(fullPath); |
| 103 | 58 | | return fullPath.Substring(0, fullPath.Length - fileName.Length); |
| | 59 | | } |
| | 60 | |
|
| 103 | 61 | | protected override void OnBeforeLoadOrReuse() { } |
| | 62 | |
|
| 100 | 63 | | protected override void OnAfterLoadOrReuse() { } |
| | 64 | |
|
| | 65 | | protected override bool AddToLibrary() |
| | 66 | | { |
| 97 | 67 | | if (!library.Add(asset)) |
| | 68 | | { |
| 0 | 69 | | Debug.Log("add to library fail?"); |
| 0 | 70 | | return false; |
| | 71 | | } |
| | 72 | |
|
| 97 | 73 | | if (asset == null) |
| | 74 | | { |
| 0 | 75 | | Debug.LogWarning($"Asset is null when trying to add it to the library: hash == {this.GetId()}"); |
| 0 | 76 | | return false; |
| | 77 | | } |
| | 78 | |
|
| 97 | 79 | | asset = library.Get(asset.id); |
| 97 | 80 | | return true; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | protected override void OnCancelLoading() |
| | 84 | | { |
| 3 | 85 | | cancellationSource.Cancel(); |
| 3 | 86 | | } |
| | 87 | |
|
| | 88 | | protected override void OnLoad(Action onSuccess, Action<Exception> onFail) |
| | 89 | | { |
| 100 | 90 | | isLoading = true; |
| 100 | 91 | | ImportGltfAsync(onSuccess, onFail, cancellationSource.Token); |
| 100 | 92 | | } |
| | 93 | |
|
| | 94 | | internal override void Unload() |
| | 95 | | { |
| 103 | 96 | | gltFastDownloadProvider.Dispose(); |
| 103 | 97 | | base.Unload(); |
| 103 | 98 | | } |
| | 99 | |
|
| 449 | 100 | | public override bool keepWaiting => isLoading; |
| | 101 | |
|
| | 102 | | private async UniTaskVoid ImportGltfAsync(Action onSuccess, Action<Exception> onFail, CancellationToken cancella |
| | 103 | | { |
| | 104 | | try |
| | 105 | | { |
| 100 | 106 | | string url = contentProvider.baseUrl + hash; |
| | 107 | |
|
| 100 | 108 | | var gltfImport = new GltfImport(gltFastDownloadProvider, staticDeferAgent, gltFastMaterialGenerator, con |
| | 109 | |
|
| 100 | 110 | | var gltFastSettings = new ImportSettings |
| | 111 | | { |
| | 112 | | GenerateMipMaps = false, |
| | 113 | | AnisotropicFilterLevel = 3, |
| | 114 | | NodeNameMethod = NameImportMethod.OriginalUnique |
| | 115 | | }; |
| | 116 | |
|
| 298 | 117 | | bool success = await gltfImport.Load(url, gltFastSettings, cancellationSourceToken); |
| | 118 | |
|
| 100 | 119 | | if (cancellationSourceToken.IsCancellationRequested) |
| | 120 | | { |
| 3 | 121 | | gltfImport.Dispose(); |
| 3 | 122 | | cancellationSourceToken.ThrowIfCancellationRequested(); |
| | 123 | | } |
| | 124 | |
|
| 97 | 125 | | if (!success) |
| 0 | 126 | | onFail?.Invoke(new GltFastLoadException($"[GLTFast] Load failed: {consoleLogger.LastErrorCode}")); |
| | 127 | | else |
| | 128 | | { |
| 97 | 129 | | asset.Setup(gltfImport); |
| 97 | 130 | | onSuccess.Invoke(); |
| | 131 | | } |
| 97 | 132 | | } |
| 3 | 133 | | catch (Exception e) |
| | 134 | | { |
| 3 | 135 | | if (e is OperationCanceledException) |
| 3 | 136 | | return; |
| | 137 | |
|
| 0 | 138 | | Debug.LogException(e); |
| 0 | 139 | | onFail?.Invoke(e); |
| 0 | 140 | | } |
| 100 | 141 | | finally { isLoading = false; } |
| 100 | 142 | | } |
| | 143 | |
|
| | 144 | | private bool FileToUrl(string fileName, out string fileHash) => |
| 39 | 145 | | contentProvider.TryGetContentsUrl(assetDirectoryPath + fileName, out fileHash); |
| | 146 | | } |
| | 147 | |
|
| | 148 | | public class GltfastEditorLogger : ICodeLogger |
| | 149 | | { |
| | 150 | | public LogCode LastErrorCode { get; private set; } |
| | 151 | |
|
| | 152 | | public void Error(LogCode code, params string[] messages) |
| | 153 | | { |
| | 154 | | LastErrorCode = code; |
| | 155 | | Debug.LogError(LogMessages.GetFullMessage(code, messages)); |
| | 156 | | } |
| | 157 | |
|
| | 158 | | public void Warning(LogCode code, params string[] messages) |
| | 159 | | { |
| | 160 | | LogWarning(LogMessages.GetFullMessage(code, messages)); |
| | 161 | | } |
| | 162 | |
|
| | 163 | | public void Info(LogCode code, params string[] messages) |
| | 164 | | { |
| | 165 | | LogVerbose(LogMessages.GetFullMessage(code, messages)); |
| | 166 | | } |
| | 167 | |
|
| | 168 | | public void Error(string message) |
| | 169 | | { |
| | 170 | | Debug.LogError(message); |
| | 171 | | } |
| | 172 | |
|
| | 173 | | public void Warning(string message) |
| | 174 | | { |
| | 175 | | LogWarning(message); |
| | 176 | | } |
| | 177 | |
|
| | 178 | | public void Info(string message) |
| | 179 | | { |
| | 180 | | LogVerbose(message); |
| | 181 | | } |
| | 182 | |
|
| | 183 | | [Conditional("UNITY_EDITOR")] |
| | 184 | | private void LogWarning(string message) |
| | 185 | | { |
| | 186 | | Debug.LogWarning(message); |
| | 187 | | } |
| | 188 | |
|
| | 189 | | [Conditional("UNITY_EDITOR")] |
| | 190 | | private void LogVerbose(string message) |
| | 191 | | { |
| | 192 | | Debug.Log(message); |
| | 193 | | } |
| | 194 | | } |
| | 195 | |
|
| | 196 | | public class GltFastLoadException : Exception |
| | 197 | | { |
| | 198 | | public GltFastLoadException(string message) : base(message) { } |
| | 199 | | } |
| | 200 | | } |