| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Helpers.NFT; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Text.RegularExpressions; |
| | 5 | | using UnityEngine; |
| | 6 | | using DCL; |
| | 7 | | using NFTShape_Internal; |
| | 8 | |
|
| | 9 | | public class NFTShapeLoaderController : MonoBehaviour |
| | 10 | | { |
| | 11 | | public enum NoiseType |
| | 12 | | { |
| | 13 | | ClassicPerlin, |
| | 14 | | PeriodicPerlin, |
| | 15 | | Simplex, |
| | 16 | | SimplexNumericalGrad, |
| | 17 | | SimplexAnalyticalGrad, |
| | 18 | | None |
| | 19 | | } |
| | 20 | |
|
| | 21 | | public NFTShapeConfig config; |
| | 22 | | public MeshRenderer meshRenderer; |
| | 23 | | public new BoxCollider collider; |
| | 24 | | public Color backgroundColor; |
| | 25 | | public GameObject spinner; |
| | 26 | |
|
| | 27 | | [HideInInspector] public bool alreadyLoadedAsset = false; |
| | 28 | |
|
| | 29 | | public event System.Action OnLoadingAssetSuccess; |
| | 30 | | public event System.Action OnLoadingAssetFail; |
| | 31 | |
|
| | 32 | | [SerializeField] |
| | 33 | | NFTShapeMaterial[] materials; |
| | 34 | |
|
| | 35 | | [Header("Noise Shader")] |
| | 36 | | [SerializeField] |
| 29 | 37 | | NoiseType noiseType = NoiseType.Simplex; |
| | 38 | |
|
| | 39 | | [SerializeField] bool noiseIs3D = false; |
| | 40 | | [SerializeField] bool noiseIsFractal = false; |
| | 41 | |
|
| | 42 | | System.Action<LoadWrapper> OnSuccess; |
| | 43 | | System.Action<LoadWrapper> OnFail; |
| | 44 | | string darURLProtocol; |
| | 45 | | string darURLRegistry; |
| | 46 | | string darURLAsset; |
| | 47 | |
|
| 0 | 48 | | public Material frameMaterial { private set; get; } = null; |
| 0 | 49 | | public Material imageMaterial { private set; get; } = null; |
| 0 | 50 | | public Material backgroundMaterial { private set; get; } = null; |
| | 51 | |
|
| 29 | 52 | | int BASEMAP_SHADER_PROPERTY = Shader.PropertyToID("_BaseMap"); |
| 29 | 53 | | int COLOR_SHADER_PROPERTY = Shader.PropertyToID("_BaseColor"); |
| | 54 | |
|
| | 55 | | IPromiseLike_TextureAsset assetPromise = null; |
| | 56 | | GifPlayer gifPlayer = null; |
| | 57 | | NFTShapeHQImageHandler hqTextureHandler = null; |
| | 58 | |
|
| | 59 | | bool isDestroyed = false; |
| | 60 | |
|
| | 61 | | void Awake() |
| | 62 | | { |
| 6 | 63 | | Material[] meshMaterials = new Material[materials.Length]; |
| 48 | 64 | | for (int i = 0; i < materials.Length; i++) |
| | 65 | | { |
| 18 | 66 | | switch (materials[i].type) |
| | 67 | | { |
| | 68 | | case NFTShapeMaterial.MaterialType.BACKGROUND: |
| 6 | 69 | | backgroundMaterial = new Material(materials[i].material); |
| 6 | 70 | | meshMaterials[i] = backgroundMaterial; |
| 6 | 71 | | break; |
| | 72 | | case NFTShapeMaterial.MaterialType.FRAME: |
| 6 | 73 | | frameMaterial = materials[i].material; |
| 6 | 74 | | meshMaterials[i] = frameMaterial; |
| 6 | 75 | | break; |
| | 76 | | case NFTShapeMaterial.MaterialType.IMAGE: |
| 6 | 77 | | imageMaterial = new Material(materials[i].material); |
| 6 | 78 | | meshMaterials[i] = imageMaterial; |
| | 79 | | break; |
| | 80 | | } |
| | 81 | | } |
| 6 | 82 | | meshRenderer.materials = meshMaterials; |
| | 83 | |
|
| | 84 | | // NOTE: we use half scale to keep backward compatibility cause we are using 512px to normalize the scale with a |
| 6 | 85 | | meshRenderer.transform.localScale = new Vector3(0.5f, 0.5f, 1); |
| | 86 | |
|
| 6 | 87 | | InitializePerlinNoise(); |
| 6 | 88 | | } |
| | 89 | |
|
| 2 | 90 | | void Update() { hqTextureHandler?.Update(); } |
| | 91 | |
|
| | 92 | | public void LoadAsset(string url, bool loadEvenIfAlreadyLoaded = false) |
| | 93 | | { |
| 2 | 94 | | if (string.IsNullOrEmpty(url) || (!loadEvenIfAlreadyLoaded && alreadyLoadedAsset)) |
| 0 | 95 | | return; |
| | 96 | |
|
| 2 | 97 | | UpdateBackgroundColor(backgroundColor); |
| | 98 | |
|
| | 99 | | // Check the src follows the needed format e.g.: 'ethereum://0x06012c8cf97BEaD5deAe237070F9587f8E7A266d/558536' |
| 2 | 100 | | var regexMatches = Regex.Matches(url, "(?<protocol>[^:]+)://(?<registry>0x([A-Fa-f0-9])+)(?:/(?<asset>.+))?"); |
| 2 | 101 | | if (regexMatches.Count == 0) |
| | 102 | | { |
| 0 | 103 | | Debug.LogError($"Couldn't fetch DAR url '{url}' for NFTShape. The accepted format is 'ethereum://ContractAdd |
| | 104 | |
|
| 0 | 105 | | OnLoadingAssetFail?.Invoke(); |
| | 106 | |
|
| 0 | 107 | | return; |
| | 108 | | } |
| | 109 | |
|
| 2 | 110 | | Match match = regexMatches[0]; |
| 2 | 111 | | if (match.Groups["protocol"] == null || match.Groups["registry"] == null || match.Groups["asset"] == null) |
| | 112 | | { |
| 0 | 113 | | Debug.LogError($"Couldn't fetch DAR url '{url}' for NFTShape."); |
| | 114 | |
|
| 0 | 115 | | OnLoadingAssetFail?.Invoke(); |
| | 116 | |
|
| 0 | 117 | | return; |
| | 118 | | } |
| | 119 | |
|
| 2 | 120 | | darURLProtocol = match.Groups["protocol"].ToString(); |
| 2 | 121 | | if (darURLProtocol != "ethereum") |
| | 122 | | { |
| 0 | 123 | | Debug.LogError($"Couldn't fetch DAR url '{url}' for NFTShape. The only protocol currently supported is 'ethe |
| | 124 | |
|
| 0 | 125 | | OnLoadingAssetFail?.Invoke(); |
| | 126 | |
|
| 0 | 127 | | return; |
| | 128 | | } |
| | 129 | |
|
| 2 | 130 | | darURLRegistry = match.Groups["registry"].ToString(); |
| 2 | 131 | | darURLAsset = match.Groups["asset"].ToString(); |
| | 132 | |
|
| 2 | 133 | | alreadyLoadedAsset = false; |
| | 134 | |
|
| 2 | 135 | | StartCoroutine(FetchNFTImage()); |
| 2 | 136 | | } |
| | 137 | |
|
| | 138 | | public void UpdateBackgroundColor(Color newColor) |
| | 139 | | { |
| 4 | 140 | | if (backgroundMaterial == null) |
| 0 | 141 | | return; |
| | 142 | |
|
| 4 | 143 | | backgroundMaterial.SetColor(COLOR_SHADER_PROPERTY, newColor); |
| 4 | 144 | | } |
| | 145 | |
|
| | 146 | | IEnumerator FetchNFTImage() |
| | 147 | | { |
| 2 | 148 | | if (spinner != null) |
| 2 | 149 | | spinner.SetActive(true); |
| | 150 | |
|
| 2 | 151 | | NFTInfo nftInfo = new NFTInfo(); |
| | 152 | |
|
| 2 | 153 | | bool isError = false; |
| | 154 | |
|
| 2 | 155 | | yield return NFTHelper.FetchNFTInfo(darURLRegistry, darURLAsset, |
| | 156 | | (info) => |
| | 157 | | { |
| 0 | 158 | | nftInfo = info; |
| 0 | 159 | | }, |
| | 160 | | (error) => |
| | 161 | | { |
| 0 | 162 | | Debug.LogError($"Couldn't fetch NFT: '{darURLRegistry}/{darURLAsset}' {error}"); |
| 0 | 163 | | OnLoadingAssetFail?.Invoke(); |
| 0 | 164 | | isError = true; |
| 0 | 165 | | }); |
| | 166 | |
|
| 0 | 167 | | if (isError) |
| 0 | 168 | | yield break; |
| | 169 | |
|
| 0 | 170 | | yield return new DCL.WaitUntil(() => (CommonScriptableObjects.playerUnityPosition - transform.position).sqrMagni |
| | 171 | |
|
| | 172 | | // We download the "preview" 256px image |
| 0 | 173 | | bool foundDCLImage = false; |
| 0 | 174 | | if (!string.IsNullOrEmpty(nftInfo.previewImageUrl)) |
| | 175 | | { |
| 0 | 176 | | yield return WrappedTextureUtils.Fetch(nftInfo.previewImageUrl, (promise) => |
| | 177 | | { |
| 0 | 178 | | foundDCLImage = true; |
| 0 | 179 | | this.assetPromise = promise; |
| 0 | 180 | | SetFrameImage(promise.asset, resizeFrameMesh: true); |
| 0 | 181 | | SetupGifPlayer(promise.asset); |
| | 182 | |
|
| 0 | 183 | | var hqImageHandlerConfig = new NFTShapeHQImageConfig() |
| | 184 | | { |
| | 185 | | controller = this, |
| | 186 | | nftConfig = config, |
| | 187 | | nftInfo = nftInfo, |
| | 188 | | asset = NFTAssetFactory.CreateAsset(promise.asset, config, UpdateTexture, gifPlayer) |
| | 189 | | }; |
| 0 | 190 | | hqTextureHandler = NFTShapeHQImageHandler.Create(hqImageHandlerConfig); |
| 0 | 191 | | }); |
| | 192 | | } |
| | 193 | |
|
| | 194 | | //We fall back to the nft original image which can have a really big size |
| 0 | 195 | | if (!foundDCLImage && !string.IsNullOrEmpty(nftInfo.originalImageUrl)) |
| | 196 | | { |
| 0 | 197 | | yield return WrappedTextureUtils.Fetch(nftInfo.originalImageUrl, |
| | 198 | | (promise) => |
| | 199 | | { |
| 0 | 200 | | foundDCLImage = true; |
| 0 | 201 | | this.assetPromise = promise; |
| 0 | 202 | | SetFrameImage(promise.asset, resizeFrameMesh: true); |
| 0 | 203 | | SetupGifPlayer(promise.asset); |
| 0 | 204 | | }, () => isError = true); |
| | 205 | | } |
| | 206 | |
|
| 0 | 207 | | if (isError) |
| | 208 | | { |
| 0 | 209 | | Debug.LogError($"Couldn't fetch NFT image for: '{darURLRegistry}/{darURLAsset}' {nftInfo.originalImageUrl}") |
| 0 | 210 | | OnLoadingAssetFail?.Invoke(); |
| 0 | 211 | | yield break; |
| | 212 | | } |
| | 213 | |
|
| 0 | 214 | | FinishLoading(foundDCLImage); |
| 0 | 215 | | } |
| | 216 | |
|
| | 217 | | void FinishLoading(bool loadedSuccessfully) |
| | 218 | | { |
| 0 | 219 | | if (loadedSuccessfully) |
| | 220 | | { |
| 0 | 221 | | if (spinner != null) |
| 0 | 222 | | spinner.SetActive(false); |
| | 223 | |
|
| 0 | 224 | | OnLoadingAssetSuccess?.Invoke(); |
| 0 | 225 | | } |
| | 226 | | else |
| | 227 | | { |
| 0 | 228 | | OnLoadingAssetFail?.Invoke(); |
| | 229 | | } |
| 0 | 230 | | } |
| | 231 | |
|
| | 232 | | void SetFrameImage(ITexture newAsset, bool resizeFrameMesh = false) |
| | 233 | | { |
| 0 | 234 | | if (newAsset == null) |
| 0 | 235 | | return; |
| | 236 | |
|
| 0 | 237 | | UpdateTexture(newAsset.texture); |
| | 238 | |
|
| 0 | 239 | | if (resizeFrameMesh && !isDestroyed && meshRenderer != null) |
| | 240 | | { |
| | 241 | | float w, h; |
| 0 | 242 | | w = h = 0.5f; |
| 0 | 243 | | if (newAsset.width > newAsset.height) |
| 0 | 244 | | h *= newAsset.height / (float)newAsset.width; |
| 0 | 245 | | else if (newAsset.width < newAsset.height) |
| 0 | 246 | | w *= newAsset.width / (float)newAsset.height; |
| 0 | 247 | | Vector3 newScale = new Vector3(w, h, 1f); |
| | 248 | |
|
| 0 | 249 | | meshRenderer.transform.localScale = newScale; |
| | 250 | | } |
| 0 | 251 | | } |
| | 252 | |
|
| | 253 | | public void UpdateTexture(Texture2D texture) |
| | 254 | | { |
| 0 | 255 | | if (imageMaterial == null) |
| 0 | 256 | | return; |
| | 257 | |
|
| 0 | 258 | | imageMaterial.SetTexture(BASEMAP_SHADER_PROPERTY, texture); |
| 0 | 259 | | imageMaterial.SetColor(COLOR_SHADER_PROPERTY, Color.white); |
| 0 | 260 | | } |
| | 261 | |
|
| | 262 | | void InitializePerlinNoise() |
| | 263 | | { |
| 6 | 264 | | if (frameMaterial == null) |
| 0 | 265 | | return; |
| | 266 | |
|
| 6 | 267 | | frameMaterial.shaderKeywords = null; |
| | 268 | |
|
| 6 | 269 | | if (noiseType == NoiseType.None) |
| 0 | 270 | | return; |
| | 271 | |
|
| 6 | 272 | | switch (noiseType) |
| | 273 | | { |
| | 274 | | case NoiseType.ClassicPerlin: |
| 0 | 275 | | frameMaterial.EnableKeyword("CNOISE"); |
| 0 | 276 | | break; |
| | 277 | | case NoiseType.PeriodicPerlin: |
| 0 | 278 | | frameMaterial.EnableKeyword("PNOISE"); |
| 0 | 279 | | break; |
| | 280 | | case NoiseType.Simplex: |
| 6 | 281 | | frameMaterial.EnableKeyword("SNOISE"); |
| 6 | 282 | | break; |
| | 283 | | case NoiseType.SimplexNumericalGrad: |
| 0 | 284 | | frameMaterial.EnableKeyword("SNOISE_NGRAD"); |
| 0 | 285 | | break; |
| | 286 | | default: // SimplexAnalyticalGrad |
| 0 | 287 | | frameMaterial.EnableKeyword("SNOISE_AGRAD"); |
| | 288 | | break; |
| | 289 | | } |
| | 290 | |
|
| 6 | 291 | | if (noiseIs3D) |
| 0 | 292 | | frameMaterial.EnableKeyword("THREED"); |
| | 293 | |
|
| 6 | 294 | | if (noiseIsFractal) |
| 0 | 295 | | frameMaterial.EnableKeyword("FRACTAL"); |
| 6 | 296 | | } |
| | 297 | |
|
| | 298 | | void OnDestroy() |
| | 299 | | { |
| 6 | 300 | | isDestroyed = true; |
| | 301 | |
|
| 6 | 302 | | if (hqTextureHandler != null) |
| | 303 | | { |
| 0 | 304 | | hqTextureHandler.Dispose(); |
| 0 | 305 | | hqTextureHandler = null; |
| | 306 | | } |
| | 307 | |
|
| 6 | 308 | | if (gifPlayer != null) |
| | 309 | | { |
| 0 | 310 | | gifPlayer.OnFrameTextureChanged -= UpdateTexture; |
| 0 | 311 | | gifPlayer.Dispose(); |
| | 312 | | } |
| | 313 | |
|
| 6 | 314 | | if (assetPromise != null) |
| | 315 | | { |
| 0 | 316 | | assetPromise.Forget(); |
| 0 | 317 | | assetPromise = null; |
| | 318 | | } |
| | 319 | |
|
| 6 | 320 | | if (backgroundMaterial != null) |
| | 321 | | { |
| 6 | 322 | | Object.Destroy(backgroundMaterial); |
| | 323 | | } |
| 6 | 324 | | if (imageMaterial != null) |
| | 325 | | { |
| 6 | 326 | | Object.Destroy(imageMaterial); |
| | 327 | | } |
| 6 | 328 | | } |
| | 329 | |
|
| | 330 | | private void SetupGifPlayer(ITexture asset) |
| | 331 | | { |
| 0 | 332 | | if (!(asset is Asset_Gif gifAsset)) |
| | 333 | | { |
| 0 | 334 | | return; |
| | 335 | | } |
| | 336 | |
|
| 0 | 337 | | gifPlayer = new GifPlayer(gifAsset); |
| 0 | 338 | | gifPlayer.Play(); |
| 0 | 339 | | gifPlayer.OnFrameTextureChanged += UpdateTexture; |
| 0 | 340 | | } |
| | 341 | | } |