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