| | 1 | | using System; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using UnityGLTF; |
| | 4 | |
|
| | 5 | | namespace DCL |
| | 6 | | { |
| | 7 | | public class AssetPromise_GLTF : AssetPromise<Asset_GLTF> |
| | 8 | | { |
| 148 | 9 | | public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering(); |
| | 10 | | protected string assetDirectoryPath; |
| | 11 | |
|
| | 12 | | protected ContentProvider provider = null; |
| 0 | 13 | | public string fileName { get; private set; } |
| | 14 | |
|
| | 15 | | GLTFComponent gltfComponent = null; |
| | 16 | | IWebRequestController webRequestController = null; |
| | 17 | |
|
| | 18 | | object id = null; |
| | 19 | |
|
| | 20 | | private Action OnSuccess; |
| | 21 | | private Action OnFail; |
| | 22 | | private bool waitingAssetLoad = false; |
| | 23 | |
|
| | 24 | | public AssetPromise_GLTF(string url, IWebRequestController webRequestController) |
| 18 | 25 | | : this(new ContentProvider_Dummy(), url, null, webRequestController) { } |
| | 26 | |
|
| | 27 | | public AssetPromise_GLTF(ContentProvider provider, string url, string hash = null) |
| 274 | 28 | | : this(provider, url, hash, Environment.i.platform.webRequest) { } |
| | 29 | |
|
| | 30 | | public AssetPromise_GLTF(ContentProvider provider, string url, IWebRequestController webRequestController) |
| 4 | 31 | | : this(provider, url, null, webRequestController) { } |
| | 32 | |
|
| 148 | 33 | | public AssetPromise_GLTF(ContentProvider provider, string url, string hash, IWebRequestController webRequestCont |
| | 34 | | { |
| 148 | 35 | | this.provider = provider; |
| 148 | 36 | | this.fileName = url.Substring(url.LastIndexOf('/') + 1); |
| 148 | 37 | | this.id = hash ?? url; |
| 148 | 38 | | this.webRequestController = webRequestController; |
| | 39 | | // We separate the directory path of the GLB and its file name, to be able to use the directory path when |
| | 40 | | // fetching relative assets like textures in the ParseGLTFWebRequestedFile() event call |
| 148 | 41 | | assetDirectoryPath = URIHelper.GetDirectoryName(url); |
| 148 | 42 | | } |
| | 43 | |
|
| | 44 | | protected override void OnBeforeLoadOrReuse() |
| | 45 | | { |
| | 46 | | #if UNITY_EDITOR |
| 223 | 47 | | asset.container.name = "GLTF: " + this.id; |
| | 48 | | #endif |
| 223 | 49 | | settings.ApplyBeforeLoad(asset.container.transform); |
| 223 | 50 | | } |
| | 51 | |
|
| | 52 | | protected override void OnAfterLoadOrReuse() |
| | 53 | | { |
| 110 | 54 | | if (asset?.container != null) |
| | 55 | | { |
| 109 | 56 | | settings.ApplyAfterLoad(asset.container.transform); |
| | 57 | | } |
| 110 | 58 | | } |
| | 59 | |
|
| 909 | 60 | | public override object GetId() { return id; } |
| | 61 | |
|
| | 62 | | internal override void Load() |
| | 63 | | { |
| 137 | 64 | | if (waitingAssetLoad) |
| 1 | 65 | | return; |
| | 66 | |
|
| 136 | 67 | | base.Load(); |
| 136 | 68 | | } |
| | 69 | |
|
| | 70 | | protected override void OnLoad(Action OnSuccess, Action OnFail) |
| | 71 | | { |
| 110 | 72 | | gltfComponent = asset.container.AddComponent<GLTFComponent>(); |
| 110 | 73 | | gltfComponent.Initialize(webRequestController); |
| | 74 | |
|
| 110 | 75 | | GLTFComponent.Settings tmpSettings = new GLTFComponent.Settings() |
| | 76 | | { |
| | 77 | | useVisualFeedback = settings.visibleFlags == AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TR |
| | 78 | | initialVisibility = settings.visibleFlags != AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE, |
| | 79 | | shaderOverride = settings.shaderOverride, |
| | 80 | | addMaterialsToPersistentCaching = (settings.cachingFlags & MaterialCachingHelper.Mode.CACHE_MATERIALS) ! |
| | 81 | | }; |
| | 82 | |
|
| 110 | 83 | | gltfComponent.LoadAsset(provider.baseUrl ?? assetDirectoryPath, fileName, GetId() as string, |
| | 84 | | false, tmpSettings, FileToHash); |
| | 85 | |
|
| 110 | 86 | | this.OnSuccess = OnSuccess; |
| 110 | 87 | | this.OnFail = OnFail; |
| | 88 | |
|
| 110 | 89 | | gltfComponent.OnSuccess += this.OnSuccess; |
| 110 | 90 | | gltfComponent.OnFail += this.OnFail; |
| | 91 | |
|
| 110 | 92 | | asset.name = fileName; |
| 110 | 93 | | } |
| | 94 | |
|
| | 95 | | bool FileToHash(string fileName, out string hash) |
| | 96 | | { |
| 266 | 97 | | return provider.TryGetContentHash(assetDirectoryPath + fileName, out hash); |
| | 98 | | } |
| | 99 | |
|
| | 100 | | protected override void OnReuse(Action OnSuccess) |
| | 101 | | { |
| | 102 | | //NOTE(Brian): Show the asset using the simple gradient feedback. |
| 26 | 103 | | asset.Show(settings.visibleFlags == AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TRANSITION, OnS |
| 26 | 104 | | } |
| | 105 | |
|
| | 106 | | protected override bool AddToLibrary() |
| | 107 | | { |
| 87 | 108 | | if (!library.Add(asset)) |
| 0 | 109 | | return false; |
| | 110 | |
|
| | 111 | | //NOTE(Brian): If the asset did load "in world" add it to library and then Get it immediately |
| | 112 | | // So it keeps being there. As master gltfs can't be in the world. |
| | 113 | | // |
| | 114 | | // ApplySettings will reposition the newly Get asset to the proper coordinates. |
| 87 | 115 | | if (settings.forceNewInstance) |
| | 116 | | { |
| 2 | 117 | | asset = (library as AssetLibrary_GLTF).GetCopyFromOriginal(asset.id); |
| 2 | 118 | | } |
| | 119 | | else |
| | 120 | | { |
| 85 | 121 | | asset = library.Get(asset.id); |
| | 122 | | } |
| | 123 | |
|
| | 124 | | //NOTE(Brian): Call again this method because we are replacing the asset. |
| 87 | 125 | | OnBeforeLoadOrReuse(); |
| 87 | 126 | | return true; |
| | 127 | | } |
| | 128 | |
|
| | 129 | | protected override void OnCancelLoading() |
| | 130 | | { |
| 7 | 131 | | if (asset != null) |
| | 132 | | { |
| 7 | 133 | | asset.CancelShow(); |
| | 134 | | } |
| 7 | 135 | | } |
| | 136 | |
|
| | 137 | | protected override Asset_GLTF GetAsset(object id) |
| | 138 | | { |
| 26 | 139 | | if (settings.forceNewInstance) |
| | 140 | | { |
| 9 | 141 | | return ((AssetLibrary_GLTF)library).GetCopyFromOriginal(id); |
| | 142 | | } |
| | 143 | | else |
| | 144 | | { |
| 17 | 145 | | return base.GetAsset(id); |
| | 146 | | } |
| | 147 | | } |
| | 148 | |
|
| | 149 | | internal override void Unload() |
| | 150 | | { |
| 116 | 151 | | if (waitingAssetLoad) |
| 0 | 152 | | return; |
| | 153 | |
|
| 116 | 154 | | settings.parent = null; |
| | 155 | |
|
| | 156 | | // NOTE: if Unload is called before finish loading we wait until gltf is loaded or failed before unloading i |
| 116 | 157 | | if (state == AssetPromiseState.LOADING && gltfComponent != null) |
| | 158 | | { |
| 20 | 159 | | waitingAssetLoad = true; |
| | 160 | |
|
| 20 | 161 | | state = AssetPromiseState.IDLE_AND_EMPTY; |
| | 162 | |
|
| 20 | 163 | | var pendingAsset = asset; |
| 20 | 164 | | asset.Hide(); |
| 20 | 165 | | asset = null; |
| | 166 | |
|
| 20 | 167 | | gltfComponent.OnSuccess -= OnSuccess; |
| 20 | 168 | | gltfComponent.OnFail -= OnFail; |
| | 169 | |
|
| 22 | 170 | | gltfComponent.OnSuccess += () => OnLoadedAfterForget(true, pendingAsset); |
| 37 | 171 | | gltfComponent.OnFail += () => OnLoadedAfterForget(false, pendingAsset); |
| | 172 | |
|
| 20 | 173 | | gltfComponent.CancelIfQueued(); |
| | 174 | |
|
| 20 | 175 | | return; |
| | 176 | | } |
| | 177 | |
|
| 96 | 178 | | base.Unload(); |
| 96 | 179 | | } |
| | 180 | |
|
| | 181 | | // NOTE: master promise are silently forgotten. We should make sure that they are loaded anyway since |
| | 182 | | // other promises are waiting for them |
| | 183 | | internal void OnSilentForget() |
| | 184 | | { |
| 3 | 185 | | asset.Hide(); |
| | 186 | |
|
| 3 | 187 | | if (gltfComponent != null) |
| | 188 | | { |
| 3 | 189 | | gltfComponent.SetPrioritized(); |
| | 190 | | } |
| 3 | 191 | | } |
| | 192 | |
|
| | 193 | | private void OnLoadedAfterForget(bool success, Asset_GLTF loadedAsset) |
| | 194 | | { |
| 19 | 195 | | asset = loadedAsset; |
| | 196 | |
|
| 19 | 197 | | if (success && asset != null) |
| | 198 | | { |
| 2 | 199 | | AddToLibrary(); |
| | 200 | | } |
| | 201 | |
|
| 19 | 202 | | ClearEvents(); |
| 19 | 203 | | CallAndClearEvents(success); |
| 19 | 204 | | Cleanup(); |
| 19 | 205 | | } |
| | 206 | | } |
| | 207 | | } |