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