| | 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 | | internal const string COULD_NOT_FETCH_DAR_URL = "Couldn't fetch DAR url '{0}' for NFTShape."; |
| | 12 | | internal const string ACCEPTED_URL_FORMAT = "The accepted format is 'ethereum://ContractAddress/TokenID'."; |
| | 13 | | internal const string SUPPORTED_PROTOCOL = "The only protocol currently supported is 'ethereum'."; |
| | 14 | | internal const string DOES_NOT_SUPPORT_POLYGON = "Warning: OpenSea API does not support fetching Polygon assets."; |
| | 15 | | internal const string COULD_NOT_FETCH_NFT_FROM_API = "Couldn't fetch NFT: '{0}/{1}'."; |
| | 16 | | internal const string COULD_NOT_FETCH_NFT_IMAGE = "Couldn't fetch NFT image for: '{0}/{1}': {2}."; |
| | 17 | |
|
| | 18 | | public enum NoiseType |
| | 19 | | { |
| | 20 | | ClassicPerlin, |
| | 21 | | PeriodicPerlin, |
| | 22 | | Simplex, |
| | 23 | | SimplexNumericalGrad, |
| | 24 | | SimplexAnalyticalGrad, |
| | 25 | | None |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public NFTShapeConfig config; |
| | 29 | | public MeshRenderer meshRenderer; |
| | 30 | | public new BoxCollider collider; |
| | 31 | | public Color backgroundColor; |
| | 32 | | public GameObject spinner; |
| | 33 | | public GameObject errorFeedback; |
| | 34 | |
|
| | 35 | | [HideInInspector] public bool alreadyLoadedAsset = false; |
| | 36 | | private INFTInfoLoadHelper nftInfoLoadHelper; |
| | 37 | | private INFTAssetLoadHelper nftAssetLoadHelper; |
| | 38 | | private NFTShapeHQImageHandler hqTextureHandler = null; |
| | 39 | | private Coroutine loadNftAssetCoroutine; |
| | 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] |
| 30 | 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 | |
|
| 0 | 60 | | public Material frameMaterial { private set; get; } = null; |
| 0 | 61 | | public Material imageMaterial { private set; get; } = null; |
| 0 | 62 | | public Material backgroundMaterial { private set; get; } = null; |
| | 63 | |
|
| 1 | 64 | | static readonly int BASEMAP_SHADER_PROPERTY = Shader.PropertyToID("_BaseMap"); |
| 1 | 65 | | static readonly int COLOR_SHADER_PROPERTY = Shader.PropertyToID("_BaseColor"); |
| | 66 | | void Awake() |
| | 67 | | { |
| | 68 | | // NOTE: we use half scale to keep backward compatibility cause we are using 512px to normalize the scale with a |
| 7 | 69 | | meshRenderer.transform.localScale = new Vector3(0.5f, 0.5f, 1); |
| | 70 | |
|
| 7 | 71 | | InitializeMaterials(); |
| | 72 | |
|
| 7 | 73 | | nftInfoLoadHelper = new NFTInfoLoadHelper(); |
| 7 | 74 | | nftAssetLoadHelper = new NFTAssetLoadHelper(); |
| 7 | 75 | | } |
| | 76 | |
|
| | 77 | | private void OnEnable() |
| | 78 | | { |
| 7 | 79 | | nftInfoLoadHelper.OnFetchInfoSuccess += FetchNFTInfoSuccess; |
| 7 | 80 | | nftInfoLoadHelper.OnFetchInfoFail += FetchNFTInfoFail; |
| 7 | 81 | | } |
| | 82 | |
|
| | 83 | | private void OnDisable() |
| | 84 | | { |
| 7 | 85 | | nftInfoLoadHelper.OnFetchInfoSuccess -= FetchNFTInfoSuccess; |
| 7 | 86 | | nftInfoLoadHelper.OnFetchInfoFail -= FetchNFTInfoFail; |
| 7 | 87 | | } |
| | 88 | |
|
| 4 | 89 | | private void Start() { spinner.layer = LayerMask.NameToLayer("ViewportCullingIgnored"); } |
| | 90 | |
|
| 3 | 91 | | void Update() { hqTextureHandler?.Update(); } |
| | 92 | |
|
| | 93 | | public void LoadAsset(string url, bool loadEvenIfAlreadyLoaded = false) |
| | 94 | | { |
| 3 | 95 | | if (string.IsNullOrEmpty(url) || (!loadEvenIfAlreadyLoaded && alreadyLoadedAsset)) |
| 0 | 96 | | return; |
| | 97 | |
|
| 3 | 98 | | ShowErrorFeedback(false); |
| 3 | 99 | | UpdateBackgroundColor(backgroundColor); |
| | 100 | |
|
| | 101 | | // Check the src follows the needed format e.g.: 'ethereum://0x06012c8cf97BEaD5deAe237070F9587f8E7A266d/558536' |
| 3 | 102 | | var regexMatches = Regex.Matches(url, "(?<protocol>[^:]+)://(?<registry>0x([A-Fa-f0-9])+)(?:/(?<asset>.+))?"); |
| 3 | 103 | | if (regexMatches.Count == 0) |
| | 104 | | { |
| 0 | 105 | | Debug.LogError(string.Format(COULD_NOT_FETCH_DAR_URL + " " + ACCEPTED_URL_FORMAT, url)); |
| 0 | 106 | | ShowErrorFeedback(true); |
| 0 | 107 | | OnLoadingAssetFail?.Invoke(); |
| | 108 | |
|
| 0 | 109 | | return; |
| | 110 | | } |
| | 111 | |
|
| 3 | 112 | | Match match = regexMatches[0]; |
| 3 | 113 | | if (match.Groups["protocol"] == null || match.Groups["registry"] == null || match.Groups["asset"] == null) |
| | 114 | | { |
| 0 | 115 | | Debug.LogError(string.Format(COULD_NOT_FETCH_DAR_URL + " " + ACCEPTED_URL_FORMAT, url)); |
| 0 | 116 | | ShowErrorFeedback(true); |
| 0 | 117 | | OnLoadingAssetFail?.Invoke(); |
| | 118 | |
|
| 0 | 119 | | return; |
| | 120 | | } |
| | 121 | |
|
| 3 | 122 | | darURLProtocol = match.Groups["protocol"].ToString(); |
| 3 | 123 | | if (darURLProtocol != "ethereum") |
| | 124 | | { |
| 0 | 125 | | Debug.LogError(string.Format(COULD_NOT_FETCH_DAR_URL + " " + SUPPORTED_PROTOCOL + " " + ACCEPTED_URL_FORMAT, |
| 0 | 126 | | ShowErrorFeedback(true); |
| 0 | 127 | | OnLoadingAssetFail?.Invoke(); |
| | 128 | |
|
| 0 | 129 | | return; |
| | 130 | | } |
| | 131 | |
|
| 3 | 132 | | darURLRegistry = match.Groups["registry"].ToString(); |
| 3 | 133 | | darURLAsset = match.Groups["asset"].ToString(); |
| | 134 | |
|
| 3 | 135 | | alreadyLoadedAsset = false; |
| | 136 | |
|
| 3 | 137 | | FetchNFTContents(); |
| 3 | 138 | | } |
| | 139 | |
|
| | 140 | | public void UpdateBackgroundColor(Color newColor) |
| | 141 | | { |
| 6 | 142 | | if (backgroundMaterial == null) |
| 0 | 143 | | return; |
| | 144 | |
|
| 6 | 145 | | backgroundMaterial.SetColor(COLOR_SHADER_PROPERTY, newColor); |
| 6 | 146 | | } |
| | 147 | |
|
| | 148 | | private void FetchNFTContents() |
| | 149 | | { |
| 3 | 150 | | ShowLoading(true); |
| 3 | 151 | | nftInfoLoadHelper.FetchNFTInfo(darURLRegistry, darURLAsset); |
| 3 | 152 | | } |
| | 153 | |
|
| | 154 | | private void FetchNFTInfoSuccess(NFTInfo nftInfo) |
| | 155 | | { |
| 0 | 156 | | loadNftAssetCoroutine = StartCoroutine(LoadNFTAssetCoroutine(nftInfo)); |
| 0 | 157 | | } |
| | 158 | |
|
| | 159 | | private void FetchNFTInfoFail() |
| | 160 | | { |
| 0 | 161 | | ShowErrorFeedback(true); |
| 0 | 162 | | FinishLoading(false); |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | private void PrepareFrame(INFTAsset nftAsset, string nftName, string nftImageUrl) |
| | 166 | | { |
| 0 | 167 | | SetFrameImage(nftAsset.previewAsset.texture, resizeFrameMesh: true); |
| | 168 | |
|
| 0 | 169 | | var hqImageHandlerConfig = new NFTShapeHQImageConfig() |
| | 170 | | { |
| | 171 | | controller = this, |
| | 172 | | nftShapeConfig = config, |
| | 173 | | name = nftName, |
| | 174 | | imageUrl = nftImageUrl, |
| | 175 | | asset = nftAsset |
| | 176 | | }; |
| | 177 | |
|
| 0 | 178 | | hqTextureHandler = NFTShapeHQImageHandler.Create(hqImageHandlerConfig); |
| 0 | 179 | | nftAsset.OnTextureUpdate += UpdateTexture; |
| 0 | 180 | | } |
| | 181 | |
|
| | 182 | | internal IEnumerator LoadNFTAssetCoroutine(NFTInfo nftInfo) |
| | 183 | | { |
| 0 | 184 | | yield return new DCL.WaitUntil(() => (CommonScriptableObjects.playerUnityPosition - transform.position).sqrMagni |
| | 185 | |
|
| | 186 | | // We download the "preview" 256px image |
| 0 | 187 | | yield return nftAssetLoadHelper.LoadNFTAsset( |
| | 188 | | nftInfo.previewImageUrl, |
| | 189 | | (result) => |
| | 190 | | { |
| 0 | 191 | | PrepareFrame(result, nftInfo.name, nftInfo.imageUrl); |
| 0 | 192 | | FinishLoading(true); |
| 0 | 193 | | }, |
| | 194 | | (exc) => |
| | 195 | | { |
| 0 | 196 | | Debug.LogError(string.Format(COULD_NOT_FETCH_NFT_IMAGE, darURLRegistry, darURLAsset, |
| | 197 | | nftInfo.previewImageUrl)); |
| | 198 | |
|
| 0 | 199 | | ShowErrorFeedback(true); |
| 0 | 200 | | OnLoadingAssetFail?.Invoke(); |
| 0 | 201 | | FinishLoading(false); |
| 0 | 202 | | }); |
| 0 | 203 | | } |
| | 204 | |
|
| | 205 | | void FinishLoading(bool loadedSuccessfully) |
| | 206 | | { |
| 0 | 207 | | if (loadedSuccessfully) |
| | 208 | | { |
| 0 | 209 | | ShowLoading(false); |
| 0 | 210 | | OnLoadingAssetSuccess?.Invoke(); |
| 0 | 211 | | } |
| | 212 | | else |
| | 213 | | { |
| 0 | 214 | | OnLoadingAssetFail?.Invoke(); |
| | 215 | | } |
| 0 | 216 | | } |
| | 217 | |
|
| | 218 | | void SetFrameImage(Texture2D texture, bool resizeFrameMesh = false) |
| | 219 | | { |
| 0 | 220 | | if (texture == null) |
| 0 | 221 | | return; |
| | 222 | |
|
| 0 | 223 | | UpdateTexture(texture); |
| | 224 | |
|
| 0 | 225 | | if (resizeFrameMesh && meshRenderer != null) |
| | 226 | | { |
| | 227 | | float w, h; |
| 0 | 228 | | w = h = 0.5f; |
| 0 | 229 | | if (texture.width > texture.height) |
| 0 | 230 | | h *= texture.height / (float) texture.width; |
| 0 | 231 | | else if (texture.width < texture.height) |
| 0 | 232 | | w *= texture.width / (float) texture.height; |
| 0 | 233 | | Vector3 newScale = new Vector3(w, h, 1f); |
| | 234 | |
|
| 0 | 235 | | meshRenderer.transform.localScale = newScale; |
| | 236 | | } |
| 0 | 237 | | } |
| | 238 | |
|
| | 239 | | public void UpdateTexture(Texture2D texture) |
| | 240 | | { |
| 0 | 241 | | if (imageMaterial == null) |
| 0 | 242 | | return; |
| | 243 | |
|
| 0 | 244 | | imageMaterial.SetTexture(BASEMAP_SHADER_PROPERTY, texture); |
| 0 | 245 | | imageMaterial.SetColor(COLOR_SHADER_PROPERTY, Color.white); |
| 0 | 246 | | } |
| | 247 | |
|
| | 248 | | private void ShowLoading(bool isVisible) |
| | 249 | | { |
| 3 | 250 | | if (spinner == null) |
| 0 | 251 | | return; |
| | 252 | |
|
| 3 | 253 | | spinner.SetActive(isVisible); |
| 3 | 254 | | } |
| | 255 | |
|
| | 256 | | private void ShowErrorFeedback(bool isVisible) |
| | 257 | | { |
| 3 | 258 | | if (errorFeedback == null) |
| 0 | 259 | | return; |
| | 260 | |
|
| 3 | 261 | | if (isVisible) |
| 0 | 262 | | ShowLoading(false); |
| | 263 | |
|
| 3 | 264 | | errorFeedback.SetActive(isVisible); |
| 3 | 265 | | } |
| | 266 | |
|
| | 267 | | void InitializeMaterials() |
| | 268 | | { |
| 7 | 269 | | Material[] meshMaterials = new Material[materials.Length]; |
| 56 | 270 | | for (int i = 0; i < materials.Length; i++) |
| | 271 | | { |
| 21 | 272 | | switch (materials[i].type) |
| | 273 | | { |
| | 274 | | case NFTShapeMaterial.MaterialType.BACKGROUND: |
| 7 | 275 | | backgroundMaterial = new Material(materials[i].material); |
| 7 | 276 | | meshMaterials[i] = backgroundMaterial; |
| 7 | 277 | | break; |
| | 278 | | case NFTShapeMaterial.MaterialType.FRAME: |
| 7 | 279 | | frameMaterial = materials[i].material; |
| 7 | 280 | | meshMaterials[i] = frameMaterial; |
| 7 | 281 | | break; |
| | 282 | | case NFTShapeMaterial.MaterialType.IMAGE: |
| 7 | 283 | | imageMaterial = new Material(materials[i].material); |
| 7 | 284 | | meshMaterials[i] = imageMaterial; |
| | 285 | | break; |
| | 286 | | } |
| | 287 | | } |
| | 288 | |
|
| | 289 | |
|
| 7 | 290 | | meshRenderer.materials = meshMaterials; |
| | 291 | |
|
| 7 | 292 | | if (frameMaterial == null) |
| 0 | 293 | | return; |
| | 294 | |
|
| 7 | 295 | | frameMaterial.shaderKeywords = null; |
| | 296 | |
|
| 7 | 297 | | if (noiseType == NoiseType.None) |
| 0 | 298 | | return; |
| | 299 | |
|
| 7 | 300 | | switch (noiseType) |
| | 301 | | { |
| | 302 | | case NoiseType.ClassicPerlin: |
| 0 | 303 | | frameMaterial.EnableKeyword("CNOISE"); |
| 0 | 304 | | break; |
| | 305 | | case NoiseType.PeriodicPerlin: |
| 0 | 306 | | frameMaterial.EnableKeyword("PNOISE"); |
| 0 | 307 | | break; |
| | 308 | | case NoiseType.Simplex: |
| 7 | 309 | | frameMaterial.EnableKeyword("SNOISE"); |
| 7 | 310 | | break; |
| | 311 | | case NoiseType.SimplexNumericalGrad: |
| 0 | 312 | | frameMaterial.EnableKeyword("SNOISE_NGRAD"); |
| 0 | 313 | | break; |
| | 314 | | default: // SimplexAnalyticalGrad |
| 0 | 315 | | frameMaterial.EnableKeyword("SNOISE_AGRAD"); |
| | 316 | | break; |
| | 317 | | } |
| | 318 | |
|
| 7 | 319 | | if (noiseIs3D) |
| 0 | 320 | | frameMaterial.EnableKeyword("THREED"); |
| | 321 | |
|
| 7 | 322 | | if (noiseIsFractal) |
| 0 | 323 | | frameMaterial.EnableKeyword("FRACTAL"); |
| 7 | 324 | | } |
| | 325 | |
|
| | 326 | | } |