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