| | 1 | | using System; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using NFTShape_Internal; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.ECSComponents |
| | 8 | | { |
| | 9 | | public interface INFTShapeFrame |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Get the shape that represent the frame |
| | 13 | | /// </summary> |
| | 14 | | IShape shape { get; } |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// The gameObject that represent the frame |
| | 18 | | /// </summary> |
| | 19 | | GameObject gameObject { get; } |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Set the visibility of the frame |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="isVisible"></param> |
| | 25 | | void SetVisibility(bool isVisible); |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Enable or disable the collider of the frame |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="withCollision"></param> |
| | 31 | | void SetHasCollisions(bool withCollision); |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Enable or disable the pointer events on the frame |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="isPointerBlocker"></param> |
| | 37 | | void SetPointerBlocker(bool isPointerBlocker); |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// This set the image and creates the HQ Texture handler |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="name"></param> |
| | 43 | | /// <param name="url"></param> |
| | 44 | | /// <param name="nftAsset"></param> |
| | 45 | | void SetImage(string name, string url, INFTAsset nftAsset); |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Dispose the frame |
| | 49 | | /// </summary> |
| | 50 | | void Dispose(); |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Show that it has been an error loading the frame |
| | 54 | | /// </summary> |
| | 55 | | void FailLoading(); |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Update the background of the texture so you can set the same background color as the image |
| | 59 | | /// </summary> |
| | 60 | | /// <param name="newColor"></param> |
| | 61 | | void UpdateBackgroundColor(UnityEngine.Color newColor); |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public class NFTShapeFrame : MonoBehaviour, IShape, INFTShapeLoaderController, INFTShapeFrame |
| | 65 | | { |
| | 66 | | [SerializeField] private BoxCollider boxCollider; |
| | 67 | | [SerializeField] private MeshRenderer meshRenderer; |
| | 68 | | [SerializeField] private GameObject loadingSpinnerGameObject; |
| | 69 | | [SerializeField] private NFTShapeMaterial[] materials; |
| | 70 | |
|
| | 71 | | [Header("Noise Shader")] |
| 49 | 72 | | [SerializeField] NoiseType noiseType = NoiseType.Simplex; |
| | 73 | | [SerializeField] bool noiseIs3D = false; |
| | 74 | | [SerializeField] bool noiseIsFractal = false; |
| | 75 | |
|
| | 76 | | private NFTShapeHQImageHandler hqTextureHandler; |
| | 77 | | private Material frameMaterial; |
| | 78 | | private Material imageMaterial; |
| | 79 | | private Material backgroundMaterial; |
| | 80 | |
|
| 1 | 81 | | static readonly int BASEMAP_SHADER_PROPERTY = Shader.PropertyToID("_BaseMap"); |
| 1 | 82 | | static readonly int COLOR_SHADER_PROPERTY = Shader.PropertyToID("_BaseColor"); |
| | 83 | |
|
| | 84 | | public enum NoiseType |
| | 85 | | { |
| | 86 | | ClassicPerlin, |
| | 87 | | PeriodicPerlin, |
| | 88 | | Simplex, |
| | 89 | | SimplexNumericalGrad, |
| | 90 | | SimplexAnalyticalGrad, |
| | 91 | | None |
| | 92 | | } |
| | 93 | |
|
| | 94 | | private void Awake() |
| | 95 | | { |
| | 96 | | // NOTE: we use half scale to keep backward compatibility cause we are using 512px to normalize the scale wi |
| 3 | 97 | | meshRenderer.transform.localScale = new UnityEngine.Vector3(0.5f, 0.5f, 1); |
| 3 | 98 | | InitializeMaterials(); |
| 3 | 99 | | } |
| | 100 | |
|
| 0 | 101 | | public bool IsVisible() { return gameObject.activeInHierarchy; } |
| | 102 | |
|
| 0 | 103 | | public bool HasCollisions() { return boxCollider.enabled; } |
| | 104 | |
|
| 0 | 105 | | public BoxCollider nftCollider => boxCollider; |
| | 106 | |
|
| 3 | 107 | | public IShape shape => this; |
| | 108 | |
|
| | 109 | | private void Start() |
| | 110 | | { |
| 0 | 111 | | loadingSpinnerGameObject.layer = LayerMask.NameToLayer("ViewportCullingIgnored"); |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | public void SetVisibility(bool isVisible) |
| | 115 | | { |
| 3 | 116 | | gameObject.SetActive(isVisible); |
| 3 | 117 | | } |
| | 118 | |
|
| | 119 | | public void SetHasCollisions(bool withCollision) |
| | 120 | | { |
| 3 | 121 | | boxCollider.enabled = withCollision; |
| 3 | 122 | | } |
| | 123 | |
|
| | 124 | | public void SetPointerBlocker(bool isPointerBlocker) |
| | 125 | | { |
| 3 | 126 | | int colliderLayer = isPointerBlocker ? PhysicsLayers.onPointerEventLayer : DCL.Configuration.PhysicsLayers.d |
| | 127 | |
|
| 3 | 128 | | boxCollider.gameObject.layer = colliderLayer; |
| 3 | 129 | | } |
| | 130 | |
|
| | 131 | | public void SetImage(string name, string url, INFTAsset nftAsset) |
| | 132 | | { |
| 0 | 133 | | if (nftAsset.previewAsset != null) |
| 0 | 134 | | SetFrameImage(nftAsset.previewAsset.texture, resizeFrameMesh: true); |
| | 135 | |
|
| 0 | 136 | | loadingSpinnerGameObject.SetActive(false); |
| 0 | 137 | | var hqImageHandlerConfig = new NFTShapeHQImageConfig() |
| | 138 | | { |
| | 139 | | controller = this, |
| | 140 | | name = name, |
| | 141 | | imageUrl = url, |
| | 142 | | asset = nftAsset |
| | 143 | | }; |
| | 144 | |
|
| 0 | 145 | | hqTextureHandler = NFTShapeHQImageHandler.Create(hqImageHandlerConfig); |
| 0 | 146 | | nftAsset.OnTextureUpdate += UpdateTexture; |
| 0 | 147 | | } |
| | 148 | |
|
| 0 | 149 | | public void Update() { hqTextureHandler?.Update(); } |
| | 150 | |
|
| | 151 | | public void Dispose() |
| | 152 | | { |
| 3 | 153 | | hqTextureHandler?.Dispose(); |
| 0 | 154 | | } |
| | 155 | |
|
| | 156 | | public void FailLoading() |
| | 157 | | { |
| 3 | 158 | | loadingSpinnerGameObject.SetActive(false); |
| | 159 | | #if UNITY_EDITOR |
| 3 | 160 | | gameObject.name += " - Failed loading"; |
| | 161 | | #endif |
| 3 | 162 | | } |
| | 163 | |
|
| | 164 | | public void UpdateBackgroundColor(UnityEngine.Color newColor) |
| | 165 | | { |
| 3 | 166 | | if (backgroundMaterial == null) |
| 0 | 167 | | return; |
| | 168 | |
|
| 3 | 169 | | backgroundMaterial.SetColor(COLOR_SHADER_PROPERTY, newColor); |
| 3 | 170 | | } |
| | 171 | |
|
| | 172 | | private void SetFrameImage(Texture2D texture, bool resizeFrameMesh = false) |
| | 173 | | { |
| 0 | 174 | | if (texture == null) |
| 0 | 175 | | return; |
| | 176 | |
|
| 0 | 177 | | UpdateTexture(texture); |
| | 178 | |
|
| 0 | 179 | | if (resizeFrameMesh && meshRenderer != null) |
| | 180 | | { |
| | 181 | | float w, h; |
| 0 | 182 | | w = h = 0.5f; |
| 0 | 183 | | if (texture.width > texture.height) |
| 0 | 184 | | h *= texture.height / (float) texture.width; |
| 0 | 185 | | else if (texture.width < texture.height) |
| 0 | 186 | | w *= texture.width / (float) texture.height; |
| 0 | 187 | | UnityEngine.Vector3 newScale = new UnityEngine.Vector3(w, h, 1f); |
| | 188 | |
|
| 0 | 189 | | meshRenderer.transform.localScale = newScale; |
| | 190 | | } |
| 0 | 191 | | } |
| | 192 | |
|
| | 193 | | private void UpdateTexture(Texture2D texture) |
| | 194 | | { |
| 0 | 195 | | if (imageMaterial == null) |
| 0 | 196 | | return; |
| | 197 | |
|
| 0 | 198 | | imageMaterial.SetTexture(BASEMAP_SHADER_PROPERTY, texture); |
| 0 | 199 | | imageMaterial.SetColor(COLOR_SHADER_PROPERTY, UnityEngine.Color.white); |
| 0 | 200 | | } |
| | 201 | |
|
| | 202 | | private void InitializeMaterials() |
| | 203 | | { |
| 3 | 204 | | Material[] meshMaterials = new Material[materials.Length]; |
| 24 | 205 | | for (int i = 0; i < materials.Length; i++) |
| | 206 | | { |
| 9 | 207 | | switch (materials[i].type) |
| | 208 | | { |
| | 209 | | case NFTShapeMaterial.MaterialType.BACKGROUND: |
| 3 | 210 | | backgroundMaterial = new Material(materials[i].material); |
| 3 | 211 | | meshMaterials[i] = backgroundMaterial; |
| 3 | 212 | | break; |
| | 213 | | case NFTShapeMaterial.MaterialType.FRAME: |
| 3 | 214 | | frameMaterial = materials[i].material; |
| 3 | 215 | | meshMaterials[i] = frameMaterial; |
| 3 | 216 | | break; |
| | 217 | | case NFTShapeMaterial.MaterialType.IMAGE: |
| 3 | 218 | | imageMaterial = new Material(materials[i].material); |
| 3 | 219 | | meshMaterials[i] = imageMaterial; |
| | 220 | | break; |
| | 221 | | } |
| | 222 | | } |
| | 223 | |
|
| 3 | 224 | | meshRenderer.materials = meshMaterials; |
| | 225 | |
|
| 3 | 226 | | if (frameMaterial == null) |
| 0 | 227 | | return; |
| | 228 | |
|
| 3 | 229 | | frameMaterial.shaderKeywords = null; |
| | 230 | |
|
| 3 | 231 | | if (noiseType == NoiseType.None) |
| 0 | 232 | | return; |
| | 233 | |
|
| 3 | 234 | | switch (noiseType) |
| | 235 | | { |
| | 236 | | case NoiseType.ClassicPerlin: |
| 0 | 237 | | frameMaterial.EnableKeyword("CNOISE"); |
| 0 | 238 | | break; |
| | 239 | | case NoiseType.PeriodicPerlin: |
| 0 | 240 | | frameMaterial.EnableKeyword("PNOISE"); |
| 0 | 241 | | break; |
| | 242 | | case NoiseType.Simplex: |
| 3 | 243 | | frameMaterial.EnableKeyword("SNOISE"); |
| 3 | 244 | | break; |
| | 245 | | case NoiseType.SimplexNumericalGrad: |
| 0 | 246 | | frameMaterial.EnableKeyword("SNOISE_NGRAD"); |
| 0 | 247 | | break; |
| | 248 | | default: // SimplexAnalyticalGrad |
| 0 | 249 | | frameMaterial.EnableKeyword("SNOISE_AGRAD"); |
| | 250 | | break; |
| | 251 | | } |
| | 252 | |
|
| 3 | 253 | | if (noiseIs3D) |
| 0 | 254 | | frameMaterial.EnableKeyword("THREED"); |
| | 255 | |
|
| 3 | 256 | | if (noiseIsFractal) |
| 0 | 257 | | frameMaterial.EnableKeyword("FRACTAL"); |
| 3 | 258 | | } |
| | 259 | |
|
| | 260 | | } |
| | 261 | | } |