| | 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 | |
|
| | 10 | | namespace DCL |
| | 11 | | { |
| | 12 | | public class DCLTexture : BaseDisposable |
| | 13 | | { |
| | 14 | | [System.Serializable] |
| | 15 | | public class Model : BaseModel |
| | 16 | | { |
| | 17 | | public string src; |
| | 18 | | public BabylonWrapMode wrap = BabylonWrapMode.CLAMP; |
| 36 | 19 | | public FilterMode samplingMode = FilterMode.Bilinear; |
| | 20 | | public bool hasAlpha = false; |
| | 21 | |
|
| 34 | 22 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 23 | | } |
| | 24 | |
|
| | 25 | | public enum BabylonWrapMode |
| | 26 | | { |
| | 27 | | CLAMP, |
| | 28 | | WRAP, |
| | 29 | | MIRROR |
| | 30 | | } |
| | 31 | |
|
| | 32 | | AssetPromise_Texture texturePromise = null; |
| | 33 | |
|
| | 34 | | public TextureWrapMode unityWrap; |
| | 35 | | public FilterMode unitySamplingMode; |
| | 36 | | public Texture2D texture; |
| | 37 | | protected bool isDisposed; |
| | 38 | |
|
| 0 | 39 | | public override int GetClassId() { return (int) CLASS_ID.TEXTURE; } |
| | 40 | |
|
| 147 | 41 | | public DCLTexture() { model = new Model(); } |
| | 42 | |
|
| | 43 | | public static IEnumerator FetchFromComponent(IParcelScene scene, string componentId, |
| | 44 | | System.Action<Texture2D> OnFinish) |
| | 45 | | { |
| 0 | 46 | | yield return FetchTextureComponent(scene, componentId, (dclTexture) => { OnFinish?.Invoke(dclTexture.texture |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public static IEnumerator FetchTextureComponent(IParcelScene scene, string componentId, |
| | 50 | | System.Action<DCLTexture> OnFinish) |
| | 51 | | { |
| 44 | 52 | | if (!scene.disposableComponents.ContainsKey(componentId)) |
| | 53 | | { |
| 1 | 54 | | Debug.Log($"couldn't fetch texture, the DCLTexture component with id {componentId} doesn't exist"); |
| 1 | 55 | | yield break; |
| | 56 | | } |
| | 57 | |
|
| 43 | 58 | | DCLTexture textureComponent = scene.disposableComponents[componentId] as DCLTexture; |
| | 59 | |
|
| 43 | 60 | | if (textureComponent == null) |
| | 61 | | { |
| 0 | 62 | | Debug.Log($"couldn't fetch texture, the shared component with id {componentId} is NOT a DCLTexture"); |
| 0 | 63 | | yield break; |
| | 64 | | } |
| | 65 | |
|
| 89 | 66 | | yield return new WaitUntil(() => textureComponent.texture != null); |
| | 67 | |
|
| 43 | 68 | | OnFinish.Invoke(textureComponent); |
| 43 | 69 | | } |
| | 70 | |
|
| | 71 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 72 | | { |
| 68 | 73 | | yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get()); |
| | 74 | |
|
| | 75 | | //If the scene creates and destroy the component before our renderer has been turned on bad things happen! |
| | 76 | | //TODO: Analyze if we can catch this upstream and stop the IEnumerator |
| 34 | 77 | | if (isDisposed) |
| 0 | 78 | | yield break; |
| | 79 | |
|
| 34 | 80 | | Model model = (Model) newModel; |
| | 81 | |
|
| 34 | 82 | | unitySamplingMode = model.samplingMode; |
| | 83 | |
|
| 34 | 84 | | switch (model.wrap) |
| | 85 | | { |
| | 86 | | case BabylonWrapMode.CLAMP: |
| 33 | 87 | | unityWrap = TextureWrapMode.Clamp; |
| 33 | 88 | | break; |
| | 89 | | case BabylonWrapMode.WRAP: |
| 0 | 90 | | unityWrap = TextureWrapMode.Repeat; |
| 0 | 91 | | break; |
| | 92 | | case BabylonWrapMode.MIRROR: |
| 1 | 93 | | unityWrap = TextureWrapMode.Mirror; |
| | 94 | | break; |
| | 95 | | } |
| | 96 | |
|
| 34 | 97 | | if (texture == null && !string.IsNullOrEmpty(model.src)) |
| | 98 | | { |
| 32 | 99 | | bool isBase64 = model.src.Contains("image/png;base64"); |
| | 100 | |
|
| 32 | 101 | | if (isBase64) |
| | 102 | | { |
| 0 | 103 | | string base64Data = model.src.Substring(model.src.IndexOf(',') + 1); |
| | 104 | |
|
| | 105 | | // The used texture variable can't be null for the ImageConversion.LoadImage to work |
| 0 | 106 | | if (texture == null) |
| | 107 | | { |
| 0 | 108 | | texture = new Texture2D(1, 1); |
| | 109 | | } |
| | 110 | |
|
| 0 | 111 | | if (!ImageConversion.LoadImage(texture, Convert.FromBase64String(base64Data))) |
| | 112 | | { |
| 0 | 113 | | Debug.LogError($"DCLTexture with id {id} couldn't parse its base64 image data."); |
| | 114 | | } |
| | 115 | |
|
| 0 | 116 | | if (texture != null) |
| | 117 | | { |
| 0 | 118 | | texture.wrapMode = unityWrap; |
| 0 | 119 | | texture.filterMode = unitySamplingMode; |
| 0 | 120 | | texture.Compress(false); |
| 0 | 121 | | texture.Apply(unitySamplingMode != FilterMode.Point, true); |
| | 122 | | } |
| 0 | 123 | | } |
| | 124 | | else |
| | 125 | | { |
| 32 | 126 | | string contentsUrl = string.Empty; |
| 32 | 127 | | bool isExternalURL = model.src.Contains("http://") || model.src.Contains("https://"); |
| | 128 | |
|
| 32 | 129 | | if (isExternalURL) |
| 0 | 130 | | contentsUrl = model.src; |
| | 131 | | else |
| 32 | 132 | | scene.contentProvider.TryGetContentsUrl(model.src, out contentsUrl); |
| | 133 | |
|
| 32 | 134 | | if (!string.IsNullOrEmpty(contentsUrl)) |
| | 135 | | { |
| 32 | 136 | | if (texturePromise != null) |
| 0 | 137 | | AssetPromiseKeeper_Texture.i.Forget(texturePromise); |
| | 138 | |
|
| 32 | 139 | | texturePromise = new AssetPromise_Texture(contentsUrl, unityWrap, unitySamplingMode, storeDefaul |
| 63 | 140 | | texturePromise.OnSuccessEvent += (x) => texture = x.texture; |
| 32 | 141 | | texturePromise.OnFailEvent += (x) => { texture = null; }; |
| | 142 | |
|
| 32 | 143 | | AssetPromiseKeeper_Texture.i.Keep(texturePromise); |
| 32 | 144 | | yield return texturePromise; |
| | 145 | | } |
| | 146 | | } |
| | 147 | | } |
| 34 | 148 | | } |
| | 149 | |
|
| | 150 | | private int refCount; |
| | 151 | |
|
| 38 | 152 | | public virtual void AttachTo(PBRMaterial material) { AddRefCount(); } |
| | 153 | |
|
| 10 | 154 | | public virtual void AttachTo(BasicMaterial material) { AddRefCount(); } |
| | 155 | |
|
| 18 | 156 | | public virtual void AttachTo(UIImage image) { AddRefCount(); } |
| | 157 | |
|
| 0 | 158 | | public virtual void DetachFrom(PBRMaterial material) { RemoveRefCount(); } |
| | 159 | |
|
| 32 | 160 | | public virtual void DetachFrom(BasicMaterial material) { RemoveRefCount(); } |
| | 161 | |
|
| 18 | 162 | | public virtual void DetachFrom(UIImage image) { RemoveRefCount(); } |
| | 163 | |
|
| 0 | 164 | | public void AddRefCount() { refCount++; } |
| | 165 | |
|
| | 166 | | public void RemoveRefCount() |
| | 167 | | { |
| 25 | 168 | | if (refCount == 0) |
| 0 | 169 | | Dispose(); |
| 25 | 170 | | } |
| | 171 | |
|
| | 172 | | public override void Dispose() |
| | 173 | | { |
| 51 | 174 | | isDisposed = true; |
| 51 | 175 | | if (texturePromise != null) |
| | 176 | | { |
| 32 | 177 | | AssetPromiseKeeper_Texture.i.Forget(texturePromise); |
| 32 | 178 | | texturePromise = null; |
| | 179 | | } |
| | 180 | |
|
| 51 | 181 | | base.Dispose(); |
| 51 | 182 | | } |
| | 183 | | } |
| | 184 | | } |