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