| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Models; |
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using UnityEngine; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Linq; |
| | 10 | |
|
| | 11 | | namespace DCL |
| | 12 | | { |
| | 13 | | public class DCLTexture : BaseDisposable |
| | 14 | | { |
| | 15 | | [System.Serializable] |
| | 16 | | public class Model : BaseModel |
| | 17 | | { |
| | 18 | | public string src; |
| | 19 | | public BabylonWrapMode wrap = BabylonWrapMode.CLAMP; |
| 61 | 20 | | public FilterMode samplingMode = FilterMode.Bilinear; |
| | 21 | | public bool hasAlpha = false; |
| | 22 | |
|
| 59 | 23 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public enum BabylonWrapMode |
| | 27 | | { |
| | 28 | | CLAMP, |
| | 29 | | WRAP, |
| | 30 | | MIRROR |
| | 31 | | } |
| | 32 | |
|
| | 33 | | AssetPromise_Texture texturePromise = null; |
| | 34 | |
|
| 74 | 35 | | private Dictionary<ISharedComponent, HashSet<long>> attachedEntitiesByComponent = |
| | 36 | | new Dictionary<ISharedComponent, HashSet<long>>(); |
| | 37 | |
|
| | 38 | | public TextureWrapMode unityWrap; |
| | 39 | | public FilterMode unitySamplingMode; |
| | 40 | | public Texture2D texture; |
| | 41 | |
|
| | 42 | | protected bool isDisposed; |
| 17 | 43 | | public float resizingFactor => texturePromise?.asset.resizingFactor ?? 1; |
| | 44 | |
|
| 0 | 45 | | public override int GetClassId() { return (int) CLASS_ID.TEXTURE; } |
| | 46 | |
|
| 222 | 47 | | public DCLTexture() { model = new Model(); } |
| | 48 | |
|
| | 49 | | public static IEnumerator FetchFromComponent(IParcelScene scene, string componentId, |
| | 50 | | System.Action<Texture2D> OnFinish) |
| | 51 | | { |
| 0 | 52 | | yield return FetchTextureComponent(scene, componentId, (dclTexture) => { OnFinish?.Invoke(dclTexture.texture |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | public static IEnumerator FetchTextureComponent(IParcelScene scene, string componentId, |
| | 56 | | System.Action<DCLTexture> OnFinish) |
| | 57 | | { |
| 78 | 58 | | if (!scene.componentsManagerLegacy.HasSceneSharedComponent(componentId)) |
| | 59 | | { |
| 1 | 60 | | Debug.Log($"couldn't fetch texture, the DCLTexture component with id {componentId} doesn't exist"); |
| 1 | 61 | | yield break; |
| | 62 | | } |
| | 63 | |
|
| 77 | 64 | | DCLTexture textureComponent = scene.componentsManagerLegacy.GetSceneSharedComponent(componentId) as DCLTextu |
| | 65 | |
|
| 77 | 66 | | if (textureComponent == null) |
| | 67 | | { |
| 0 | 68 | | Debug.Log($"couldn't fetch texture, the shared component with id {componentId} is NOT a DCLTexture"); |
| 0 | 69 | | yield break; |
| | 70 | | } |
| | 71 | |
|
| 197 | 72 | | yield return new WaitUntil(() => textureComponent.texture != null); |
| | 73 | |
|
| 74 | 74 | | OnFinish.Invoke(textureComponent); |
| 74 | 75 | | } |
| | 76 | |
|
| | 77 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 78 | | { |
| 118 | 79 | | yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get()); |
| | 80 | |
|
| | 81 | | //If the scene creates and destroy the component before our renderer has been turned on bad things happen! |
| | 82 | | //TODO: Analyze if we can catch this upstream and stop the IEnumerator |
| 59 | 83 | | if (isDisposed) |
| 0 | 84 | | yield break; |
| | 85 | |
|
| 59 | 86 | | Model model = (Model) newModel; |
| | 87 | |
|
| 59 | 88 | | unitySamplingMode = model.samplingMode; |
| | 89 | |
|
| 59 | 90 | | switch (model.wrap) |
| | 91 | | { |
| | 92 | | case BabylonWrapMode.CLAMP: |
| 58 | 93 | | unityWrap = TextureWrapMode.Clamp; |
| 58 | 94 | | break; |
| | 95 | | case BabylonWrapMode.WRAP: |
| 0 | 96 | | unityWrap = TextureWrapMode.Repeat; |
| 0 | 97 | | break; |
| | 98 | | case BabylonWrapMode.MIRROR: |
| 1 | 99 | | unityWrap = TextureWrapMode.Mirror; |
| | 100 | | break; |
| | 101 | | } |
| | 102 | |
|
| 59 | 103 | | if (texture == null && !string.IsNullOrEmpty(model.src)) |
| | 104 | | { |
| 57 | 105 | | bool isBase64 = model.src.Contains("image/png;base64"); |
| | 106 | |
|
| 57 | 107 | | if (isBase64) |
| | 108 | | { |
| 0 | 109 | | string base64Data = model.src.Substring(model.src.IndexOf(',') + 1); |
| | 110 | |
|
| | 111 | | // The used texture variable can't be null for the ImageConversion.LoadImage to work |
| 0 | 112 | | if (texture == null) |
| | 113 | | { |
| 0 | 114 | | texture = new Texture2D(1, 1); |
| | 115 | | } |
| | 116 | |
|
| 0 | 117 | | if (!ImageConversion.LoadImage(texture, Convert.FromBase64String(base64Data))) |
| | 118 | | { |
| 0 | 119 | | Debug.LogError($"DCLTexture with id {id} couldn't parse its base64 image data."); |
| | 120 | | } |
| | 121 | |
|
| 0 | 122 | | if (texture != null) |
| | 123 | | { |
| 0 | 124 | | texture.wrapMode = unityWrap; |
| 0 | 125 | | texture.filterMode = unitySamplingMode; |
| | 126 | |
|
| 0 | 127 | | if (DataStore.i.textureConfig.runCompression.Get()) |
| 0 | 128 | | texture.Compress(false); |
| | 129 | |
|
| 0 | 130 | | texture.Apply(unitySamplingMode != FilterMode.Point, true); |
| 0 | 131 | | texture = TextureHelpers.ClampSize(texture, DataStore.i.textureConfig.generalMaxSize.Get()); |
| | 132 | | } |
| 0 | 133 | | } |
| | 134 | | else |
| | 135 | | { |
| 57 | 136 | | string contentsUrl = string.Empty; |
| 57 | 137 | | bool isExternalURL = model.src.Contains("http://") || model.src.Contains("https://"); |
| | 138 | |
|
| 57 | 139 | | if (isExternalURL) |
| 0 | 140 | | contentsUrl = model.src; |
| | 141 | | else |
| 57 | 142 | | scene.contentProvider.TryGetContentsUrl(model.src, out contentsUrl); |
| | 143 | |
|
| 57 | 144 | | if (!string.IsNullOrEmpty(contentsUrl)) |
| | 145 | | { |
| 57 | 146 | | if (texturePromise != null) |
| 0 | 147 | | AssetPromiseKeeper_Texture.i.Forget(texturePromise); |
| | 148 | |
|
| 57 | 149 | | texturePromise = new AssetPromise_Texture(contentsUrl, unityWrap, unitySamplingMode, storeDefaul |
| 110 | 150 | | texturePromise.OnSuccessEvent += (x) => texture = x.texture; |
| 57 | 151 | | texturePromise.OnFailEvent += (x, error) => { texture = null; }; |
| | 152 | |
|
| 57 | 153 | | AssetPromiseKeeper_Texture.i.Keep(texturePromise); |
| 57 | 154 | | yield return texturePromise; |
| | 155 | | } |
| | 156 | | } |
| | 157 | | } |
| 56 | 158 | | } |
| | 159 | |
|
| | 160 | | public virtual void AttachTo(ISharedComponent component) |
| | 161 | | { |
| 64 | 162 | | AddReference(component); |
| 64 | 163 | | } |
| | 164 | |
|
| | 165 | | public virtual void DetachFrom(ISharedComponent component) |
| | 166 | | { |
| 41 | 167 | | RemoveReference(component); |
| 41 | 168 | | } |
| | 169 | |
|
| | 170 | | public void AddReference(ISharedComponent component) |
| | 171 | | { |
| 74 | 172 | | if (attachedEntitiesByComponent.ContainsKey(component)) |
| 1 | 173 | | return; |
| | 174 | |
|
| 73 | 175 | | attachedEntitiesByComponent.Add(component, new HashSet<long>()); |
| | 176 | |
|
| 264 | 177 | | foreach (var entity in component.GetAttachedEntities()) |
| | 178 | | { |
| 59 | 179 | | attachedEntitiesByComponent[component].Add(entity.entityId); |
| 59 | 180 | | DataStore.i.sceneWorldObjects.AddTexture(scene.sceneData.id, entity.entityId, texture); |
| | 181 | | } |
| 73 | 182 | | } |
| | 183 | |
|
| | 184 | | public void RemoveReference(ISharedComponent component) |
| | 185 | | { |
| 52 | 186 | | if (!attachedEntitiesByComponent.ContainsKey(component)) |
| 17 | 187 | | return; |
| | 188 | |
|
| 136 | 189 | | foreach (var entityId in attachedEntitiesByComponent[component]) |
| | 190 | | { |
| 33 | 191 | | DataStore.i.sceneWorldObjects.RemoveTexture(scene.sceneData.id, entityId, texture); |
| | 192 | | } |
| | 193 | |
|
| 35 | 194 | | attachedEntitiesByComponent.Remove(component); |
| 35 | 195 | | } |
| | 196 | |
|
| | 197 | | public override void Dispose() |
| | 198 | | { |
| 52 | 199 | | if (isDisposed) |
| 24 | 200 | | return; |
| | 201 | |
|
| 28 | 202 | | isDisposed = true; |
| | 203 | |
|
| 38 | 204 | | while (attachedEntitiesByComponent.Count > 0) |
| | 205 | | { |
| 10 | 206 | | RemoveReference(attachedEntitiesByComponent.First().Key); |
| | 207 | | } |
| | 208 | |
|
| 28 | 209 | | if (texturePromise != null) |
| | 210 | | { |
| 27 | 211 | | AssetPromiseKeeper_Texture.i.Forget(texturePromise); |
| 27 | 212 | | texturePromise = null; |
| | 213 | | } |
| | 214 | |
|
| 28 | 215 | | base.Dispose(); |
| 28 | 216 | | } |
| | 217 | | } |
| | 218 | | } |