| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Models; |
| | 6 | | using System; |
| | 7 | | using System.Collections; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Linq; |
| | 10 | | using Decentraland.Sdk.Ecs6; |
| | 11 | | using System.Threading; |
| | 12 | | using UnityEngine; |
| | 13 | | using Object = UnityEngine.Object; |
| | 14 | |
|
| | 15 | | namespace DCL |
| | 16 | | { |
| | 17 | | public class DCLTexture : BaseDisposable |
| | 18 | | { |
| | 19 | | [Serializable] |
| | 20 | | public class Model : BaseModel |
| | 21 | | { |
| | 22 | | public string src; |
| | 23 | | public BabylonWrapMode wrap = BabylonWrapMode.CLAMP; |
| 254 | 24 | | public FilterMode samplingMode = FilterMode.Bilinear; |
| | 25 | |
|
| | 26 | | public override BaseModel GetDataFromJSON(string json) => |
| 79 | 27 | | Utils.SafeFromJson<Model>(json); |
| | 28 | |
|
| | 29 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 30 | | { |
| 0 | 31 | | if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.Texture) |
| 0 | 32 | | return Utils.SafeUnimplemented<DCLTexture, Model>(expected: ComponentBodyPayload.PayloadOneofCase.Te |
| | 33 | |
|
| 0 | 34 | | var pb = new Model(); |
| 0 | 35 | | if (pbModel.Texture.HasSrc) pb.src = pbModel.Texture.Src; |
| 0 | 36 | | if (pbModel.Texture.HasWrap) pb.wrap = (BabylonWrapMode)pbModel.Texture.Wrap; |
| 0 | 37 | | if (pbModel.Texture.HasSamplingMode) pb.samplingMode = (FilterMode)pbModel.Texture.SamplingMode; |
| | 38 | |
|
| 0 | 39 | | return pb; |
| | 40 | | } |
| | 41 | | } |
| | 42 | |
|
| | 43 | | public enum BabylonWrapMode |
| | 44 | | { |
| | 45 | | CLAMP, |
| | 46 | | WRAP, |
| | 47 | | MIRROR |
| | 48 | | } |
| | 49 | |
|
| | 50 | | private AssetPromise_Texture texturePromise; |
| | 51 | | private bool wasLoadedFromPromise; |
| | 52 | |
|
| 96 | 53 | | protected readonly Dictionary<ISharedComponent, HashSet<long>> attachedEntitiesByComponent = |
| | 54 | | new Dictionary<ISharedComponent, HashSet<long>>(); |
| | 55 | |
|
| | 56 | | public TextureWrapMode unityWrap; |
| | 57 | | public FilterMode unitySamplingMode; |
| | 58 | | public Texture2D texture; |
| | 59 | |
|
| | 60 | | protected bool isDisposed; |
| | 61 | | protected bool textureDisposed; |
| | 62 | |
|
| 40 | 63 | | public float resizingFactor => texturePromise?.asset.resizingFactor ?? 1; |
| | 64 | |
|
| | 65 | | public override int GetClassId() => |
| 0 | 66 | | (int)CLASS_ID.TEXTURE; |
| | 67 | |
|
| 96 | 68 | | public DCLTexture() |
| | 69 | | { |
| 96 | 70 | | model = new Model(); |
| 96 | 71 | | } |
| | 72 | |
|
| | 73 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 74 | | { |
| 160 | 75 | | yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get()); |
| | 76 | |
|
| | 77 | | //If the scene creates and destroy the component before our renderer has been turned on bad things happen! |
| | 78 | | //TODO: Analyze if we can catch this upstream and stop the IEnumerator |
| 80 | 79 | | if (isDisposed) |
| 0 | 80 | | yield break; |
| | 81 | |
|
| 80 | 82 | | Model model = (Model)newModel; |
| | 83 | |
|
| 80 | 84 | | unitySamplingMode = model.samplingMode; |
| | 85 | |
|
| 80 | 86 | | switch (model.wrap) |
| | 87 | | { |
| | 88 | | case BabylonWrapMode.CLAMP: |
| 79 | 89 | | unityWrap = TextureWrapMode.Clamp; |
| 79 | 90 | | break; |
| | 91 | | case BabylonWrapMode.WRAP: |
| 0 | 92 | | unityWrap = TextureWrapMode.Repeat; |
| 0 | 93 | | break; |
| | 94 | | case BabylonWrapMode.MIRROR: |
| 1 | 95 | | unityWrap = TextureWrapMode.Mirror; |
| | 96 | | break; |
| | 97 | | } |
| | 98 | |
|
| 80 | 99 | | if (texture == null && !string.IsNullOrEmpty(model.src)) |
| | 100 | | { |
| 78 | 101 | | bool isBase64 = model.src.Contains("image/png;base64"); |
| | 102 | |
|
| 78 | 103 | | if (isBase64) |
| | 104 | | { |
| 21 | 105 | | string base64Data = model.src.Substring(model.src.IndexOf(',') + 1); |
| | 106 | |
|
| 21 | 107 | | wasLoadedFromPromise = false; |
| | 108 | |
|
| | 109 | | // The used texture variable can't be null for the ImageConversion.LoadImage to work |
| 42 | 110 | | if (texture == null) { texture = new Texture2D(1, 1); } |
| | 111 | |
|
| 21 | 112 | | if (!ImageConversion.LoadImage(texture, Convert.FromBase64String(base64Data))) { Debug.LogError($"DC |
| | 113 | |
|
| 21 | 114 | | if (texture != null) |
| | 115 | | { |
| 21 | 116 | | texture.wrapMode = unityWrap; |
| 21 | 117 | | texture.filterMode = unitySamplingMode; |
| | 118 | |
|
| 21 | 119 | | if (DataStore.i.textureConfig.runCompression.Get()) |
| 0 | 120 | | texture.Compress(false); |
| | 121 | |
|
| 21 | 122 | | texture.Apply(unitySamplingMode != FilterMode.Point, true); |
| 21 | 123 | | texture = TextureHelpers.ClampSize(texture, DataStore.i.textureConfig.generalMaxSize.Get()); |
| | 124 | | } |
| | 125 | | } |
| | 126 | | else |
| | 127 | | { |
| 57 | 128 | | string contentsUrl = string.Empty; |
| 57 | 129 | | bool isExternalURL = model.src.Contains("http://") || model.src.Contains("https://"); |
| | 130 | |
|
| 57 | 131 | | if (isExternalURL) |
| 0 | 132 | | contentsUrl = model.src; |
| | 133 | | else |
| 57 | 134 | | scene.contentProvider.TryGetContentsUrl(model.src, out contentsUrl); |
| | 135 | |
|
| 57 | 136 | | var prevPromise = texturePromise; |
| | 137 | |
|
| 57 | 138 | | if (!string.IsNullOrEmpty(contentsUrl)) |
| | 139 | | { |
| 57 | 140 | | if (texturePromise != null) |
| 0 | 141 | | AssetPromiseKeeper_Texture.i.Forget(texturePromise); |
| | 142 | |
|
| 57 | 143 | | wasLoadedFromPromise = true; |
| 57 | 144 | | texturePromise = new AssetPromise_Texture(contentsUrl, unityWrap, unitySamplingMode, storeDefaul |
| 113 | 145 | | texturePromise.OnSuccessEvent += (x) => texture = x.texture; |
| 57 | 146 | | texturePromise.OnFailEvent += (x, error) => { texture = null; }; |
| | 147 | |
|
| 57 | 148 | | AssetPromiseKeeper_Texture.i.Keep(texturePromise); |
| 57 | 149 | | yield return texturePromise; |
| | 150 | | } |
| | 151 | |
|
| 57 | 152 | | AssetPromiseKeeper_Texture.i.Forget(prevPromise); |
| 57 | 153 | | } |
| | 154 | | } |
| 80 | 155 | | } |
| | 156 | |
|
| | 157 | | public virtual void AttachTo(ISharedComponent component) |
| | 158 | | { |
| 86 | 159 | | AddReference(component); |
| 86 | 160 | | } |
| | 161 | |
|
| | 162 | | public virtual void DetachFrom(ISharedComponent component) |
| | 163 | | { |
| 62 | 164 | | if (RemoveReference(component)) |
| | 165 | | { |
| 37 | 166 | | if (attachedEntitiesByComponent.Count == 0) |
| | 167 | | { |
| 26 | 168 | | DisposeTexture(); |
| | 169 | | } |
| | 170 | | } |
| 62 | 171 | | } |
| | 172 | |
|
| | 173 | | public void AddReference(ISharedComponent component) |
| | 174 | | { |
| 99 | 175 | | if (attachedEntitiesByComponent.ContainsKey(component)) |
| 1 | 176 | | return; |
| | 177 | |
|
| 98 | 178 | | attachedEntitiesByComponent.Add(component, new HashSet<long>()); |
| | 179 | |
|
| 362 | 180 | | foreach (var entity in component.GetAttachedEntities()) |
| | 181 | | { |
| 83 | 182 | | attachedEntitiesByComponent[component].Add(entity.entityId); |
| 83 | 183 | | DataStore.i.sceneWorldObjects.AddTexture(scene.sceneData.sceneNumber, entity.entityId, texture); |
| | 184 | | } |
| 98 | 185 | | } |
| | 186 | |
|
| | 187 | | public bool RemoveReference(ISharedComponent component) |
| | 188 | | { |
| 78 | 189 | | if (!attachedEntitiesByComponent.ContainsKey(component)) |
| 25 | 190 | | return false; |
| | 191 | |
|
| 259 | 192 | | foreach (var entityId in attachedEntitiesByComponent[component]) { DataStore.i.sceneWorldObjects.RemoveTextu |
| | 193 | |
|
| 53 | 194 | | return attachedEntitiesByComponent.Remove(component); |
| | 195 | | } |
| | 196 | |
|
| | 197 | | public override void Dispose() |
| | 198 | | { |
| 52 | 199 | | if (isDisposed) |
| 24 | 200 | | return; |
| | 201 | |
|
| 28 | 202 | | isDisposed = true; |
| | 203 | |
|
| 54 | 204 | | while (attachedEntitiesByComponent.Count > 0) { RemoveReference(attachedEntitiesByComponent.First().Key); } |
| | 205 | |
|
| 28 | 206 | | DisposeTexture(); |
| | 207 | |
|
| 28 | 208 | | base.Dispose(); |
| 28 | 209 | | } |
| | 210 | |
|
| | 211 | | protected virtual void DisposeTexture() |
| | 212 | | { |
| 53 | 213 | | textureDisposed = true; |
| | 214 | |
|
| 53 | 215 | | if (texturePromise != null) |
| | 216 | | { |
| 28 | 217 | | AssetPromiseKeeper_Texture.i.Forget(texturePromise); |
| 28 | 218 | | texturePromise = null; |
| | 219 | | } |
| | 220 | | // only destroy textures created by this script |
| | 221 | | // textures created by the asset promise should be cleaned up from the asset library |
| 25 | 222 | | else if (!wasLoadedFromPromise) |
| | 223 | | { |
| 14 | 224 | | if (texture) |
| 14 | 225 | | Object.Destroy(texture); |
| | 226 | | } |
| 25 | 227 | | } |
| | 228 | |
|
| | 229 | | public class Fetcher : IDisposable |
| | 230 | | { |
| | 231 | | private CancellationTokenSource cancellationTokenSource; |
| | 232 | |
|
| | 233 | | public async UniTask Fetch(IParcelScene scene, string componentId, |
| | 234 | | Func<DCLTexture, bool> attachCallback) |
| | 235 | | { |
| 102 | 236 | | Cancel(); |
| | 237 | |
|
| 102 | 238 | | cancellationTokenSource = new CancellationTokenSource(); |
| 102 | 239 | | CancellationToken ct = cancellationTokenSource.Token; |
| | 240 | |
|
| 102 | 241 | | if (!scene.componentsManagerLegacy.HasSceneSharedComponent(componentId)) |
| | 242 | | { |
| 1 | 243 | | Debug.Log($"couldn't fetch texture, the DCLTexture component with id {componentId} doesn't exist"); |
| 1 | 244 | | return; |
| | 245 | | } |
| | 246 | |
|
| 101 | 247 | | DCLTexture textureComponent = scene.componentsManagerLegacy.GetSceneSharedComponent(componentId) as DCLT |
| | 248 | |
|
| 101 | 249 | | if (textureComponent == null) |
| | 250 | | { |
| 0 | 251 | | Debug.Log($"couldn't fetch texture, the shared component with id {componentId} is NOT a DCLTexture") |
| 0 | 252 | | return; |
| | 253 | | } |
| | 254 | |
|
| 101 | 255 | | bool textureWasReLoaded = false; |
| | 256 | |
|
| | 257 | | // If texture was previously disposed we load it again |
| 101 | 258 | | if (textureComponent.textureDisposed) |
| | 259 | | { |
| 2 | 260 | | textureComponent.textureDisposed = false; |
| | 261 | |
|
| | 262 | | try |
| | 263 | | { |
| 4 | 264 | | await textureComponent.ApplyChanges(textureComponent.model).WithCancellation(ct); |
| 2 | 265 | | ct.ThrowIfCancellationRequested(); |
| 2 | 266 | | textureWasReLoaded = true; |
| 2 | 267 | | } |
| 0 | 268 | | catch (OperationCanceledException _) |
| | 269 | | { |
| 0 | 270 | | textureComponent.DisposeTexture(); |
| 0 | 271 | | } |
| | 272 | | } |
| | 273 | |
|
| 494 | 274 | | await UniTask.WaitUntil(() => textureComponent.texture, PlayerLoopTiming.Update, ct); |
| | 275 | |
|
| | 276 | | // We dispose texture was re-loaded but attachment |
| | 277 | | // was unsuccessful |
| 99 | 278 | | if (!attachCallback(textureComponent) && textureWasReLoaded) |
| | 279 | | { |
| 0 | 280 | | textureComponent.DisposeTexture(); |
| | 281 | | } |
| | 282 | |
|
| 99 | 283 | | cancellationTokenSource.Dispose(); |
| 99 | 284 | | cancellationTokenSource = null; |
| 100 | 285 | | } |
| | 286 | |
|
| | 287 | | public void Cancel() |
| | 288 | | { |
| 189 | 289 | | if (cancellationTokenSource != null) |
| | 290 | | { |
| 3 | 291 | | cancellationTokenSource.Cancel(); |
| 3 | 292 | | cancellationTokenSource.Dispose(); |
| 3 | 293 | | cancellationTokenSource = null; |
| | 294 | | } |
| 189 | 295 | | } |
| | 296 | |
|
| | 297 | | public void Dispose() |
| | 298 | | { |
| 87 | 299 | | Cancel(); |
| 87 | 300 | | } |
| | 301 | | } |
| | 302 | | } |
| | 303 | | } |