| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL |
| | 5 | | { |
| | 6 | | public class AssetPromise_TextureResource : AssetPromise<Asset_TextureResource> |
| | 7 | | { |
| | 8 | | private TextureModel model; |
| | 9 | | private AssetPromise_Texture texturePromise; |
| | 10 | |
|
| 0 | 11 | | public AssetPromise_TextureResource(TextureModel model) |
| | 12 | | { |
| 0 | 13 | | this.model = model; |
| 0 | 14 | | } |
| | 15 | |
|
| 23 | 16 | | protected override void OnAfterLoadOrReuse() { } |
| | 17 | |
|
| 35 | 18 | | protected override void OnBeforeLoadOrReuse() { } |
| | 19 | |
|
| | 20 | | protected override void OnCancelLoading() |
| | 21 | | { |
| 12 | 22 | | if(texturePromise != null) |
| 12 | 23 | | AssetPromiseKeeper_Texture.i.Forget(texturePromise); |
| 12 | 24 | | } |
| | 25 | |
|
| | 26 | | internal override void OnForget() |
| | 27 | | { |
| 23 | 28 | | if(texturePromise != null) |
| 22 | 29 | | AssetPromiseKeeper_Texture.i.Forget(texturePromise); |
| 23 | 30 | | } |
| | 31 | |
|
| | 32 | | protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail) |
| | 33 | | { |
| 28 | 34 | | FilterMode unitySamplingMode = model.samplingMode; |
| 28 | 35 | | TextureWrapMode unityWrap = TextureWrapMode.Clamp; |
| 28 | 36 | | switch (model.wrap) |
| | 37 | | { |
| | 38 | | case TextureModel.BabylonWrapMode.CLAMP: |
| 4 | 39 | | unityWrap = TextureWrapMode.Clamp; |
| 4 | 40 | | break; |
| | 41 | | case TextureModel.BabylonWrapMode.WRAP: |
| 24 | 42 | | unityWrap = TextureWrapMode.Repeat; |
| 24 | 43 | | break; |
| | 44 | | case TextureModel.BabylonWrapMode.MIRROR: |
| 0 | 45 | | unityWrap = TextureWrapMode.Mirror; |
| | 46 | | break; |
| | 47 | | } |
| | 48 | |
|
| 28 | 49 | | if (!string.IsNullOrEmpty(model.src)) |
| | 50 | | { |
| 28 | 51 | | bool isBase64 = model.src.Contains("image/png;base64"); |
| | 52 | |
|
| 28 | 53 | | if (isBase64) |
| | 54 | | { |
| 0 | 55 | | string base64Data = model.src.Substring(model.src.IndexOf(',') + 1); |
| | 56 | |
|
| | 57 | | // The used texture variable can't be null for the ImageConversion.LoadImage to work |
| 0 | 58 | | Texture2D texture = new Texture2D(1, 1); |
| | 59 | |
|
| 0 | 60 | | if (!ImageConversion.LoadImage(texture, Convert.FromBase64String(base64Data))) |
| | 61 | | { |
| 0 | 62 | | Debug.LogError($"DCLTexture with model {model} couldn't parse its base64 image data."); |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | if (texture != null) |
| | 66 | | { |
| 0 | 67 | | texture.wrapMode = unityWrap; |
| 0 | 68 | | texture.filterMode = unitySamplingMode; |
| | 69 | |
|
| 0 | 70 | | if (DataStore.i.textureConfig.runCompression.Get()) |
| 0 | 71 | | texture.Compress(false); |
| | 72 | |
|
| 0 | 73 | | texture.Apply(unitySamplingMode != FilterMode.Point, true); |
| 0 | 74 | | asset.texture2D = texture; |
| | 75 | | } |
| 0 | 76 | | } |
| | 77 | | else |
| | 78 | | { |
| 28 | 79 | | string contentsUrl = model.src; |
| | 80 | |
|
| 28 | 81 | | if (!string.IsNullOrEmpty(contentsUrl)) |
| | 82 | | { |
| 28 | 83 | | Texture2D result = null; |
| 28 | 84 | | texturePromise = new AssetPromise_Texture(contentsUrl, unityWrap, unitySamplingMode, storeDefaul |
| 28 | 85 | | texturePromise.OnSuccessEvent += (texture) => |
| | 86 | | { |
| 16 | 87 | | asset.texture2D = texture.texture; |
| 16 | 88 | | OnSuccess?.Invoke(); |
| 16 | 89 | | }; |
| 28 | 90 | | texturePromise.OnFailEvent += (texture, error) => |
| | 91 | | { |
| 4 | 92 | | result = null; |
| 4 | 93 | | OnFail?.Invoke(error); |
| 4 | 94 | | }; |
| | 95 | |
|
| 28 | 96 | | AssetPromiseKeeper_Texture.i.Keep(texturePromise); |
| | 97 | | } |
| | 98 | | } |
| | 99 | | } |
| 28 | 100 | | } |
| | 101 | |
|
| 35 | 102 | | protected override object GetLibraryAssetCheckId() { return model; } |
| 140 | 103 | | public override object GetId() { return model; } |
| | 104 | | } |
| | 105 | | } |