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