< Summary

Class:DCL.ECSComponents.NFTShapeFrame
Assembly:DCL.ECSComponents.NFTShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/NFTShape/NFTShapeFrame.cs
Covered lines:44
Uncovered lines:32
Coverable lines:76
Total lines:217
Line coverage:57.8% (44 of 76)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
NFTShapeFrame()0%110100%
NFTShapeFrame()0%110100%
Awake()0%110100%
Start()0%2100%
SetImage(...)0%220100%
Update()0%6200%
Dispose()0%220100%
FailLoading()0%2100%
UpdateBackgroundColor(...)0%2.062075%
SetFrameImage(...)0%26.836016.67%
UpdateTexture(...)0%6200%
InitializeMaterials()0%17.6414073.53%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/NFTShape/NFTShapeFrame.cs

#LineLine coverage
 1using NFTShape_Internal;
 2using UnityEngine;
 3
 4namespace 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")]
 10047        [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
 156        static readonly int BASEMAP_SHADER_PROPERTY = Shader.PropertyToID("_BaseMap");
 157        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
 1669        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
 3074            meshRenderer.transform.localScale = new UnityEngine.Vector3(0.5f, 0.5f, 1);
 3075            InitializeMaterials();
 3076        }
 77
 78        private void Start()
 79        {
 080            loadingSpinnerGameObject.layer = LayerMask.NameToLayer("ViewportCullingIgnored");
 081        }
 82
 83        public void SetImage(string name, string url, INFTAsset nftAsset)
 84        {
 285            if (nftAsset.previewAsset != null)
 286                SetFrameImage(nftAsset.previewAsset.texture, resizeFrameMesh: true);
 87
 288            loadingSpinnerGameObject.SetActive(false);
 289            var hqImageHandlerConfig = new NFTShapeHQImageConfig()
 90            {
 91                transform = transform,
 92                collider = boxCollider,
 93                name = name,
 94                imageUrl = url,
 95                asset = nftAsset
 96            };
 97
 298            hqTextureHandler = NFTShapeHQImageHandler.Create(hqImageHandlerConfig);
 299            nftAsset.OnTextureUpdate += UpdateTexture;
 2100        }
 101
 102        public void Update()
 103        {
 0104            hqTextureHandler?.Update();
 0105        }
 106
 107        public void Dispose()
 108        {
 7109            hqTextureHandler?.Dispose();
 1110        }
 111
 112        public void FailLoading()
 113        {
 0114            loadingSpinnerGameObject.SetActive(false);
 115#if UNITY_EDITOR
 0116            gameObject.name += " - Failed loading";
 117#endif
 0118        }
 119
 120        public void UpdateBackgroundColor(Color newColor)
 121        {
 7122            if (backgroundMaterial == null)
 0123                return;
 124
 7125            backgroundMaterial.SetColor(COLOR_SHADER_PROPERTY, newColor);
 7126        }
 127
 128        private void SetFrameImage(Texture2D texture, bool resizeFrameMesh = false)
 129        {
 2130            if (texture == null)
 2131                return;
 132
 0133            UpdateTexture(texture);
 134
 0135            if (resizeFrameMesh && meshRenderer != null)
 136            {
 137                float w, h;
 0138                w = h = 0.5f;
 0139                if (texture.width > texture.height)
 0140                    h *= texture.height / (float)texture.width;
 0141                else if (texture.width < texture.height)
 0142                    w *= texture.width / (float)texture.height;
 0143                UnityEngine.Vector3 newScale = new UnityEngine.Vector3(w, h, 1f);
 144
 0145                meshRenderer.transform.localScale = newScale;
 146            }
 0147        }
 148
 149        private void UpdateTexture(Texture2D texture)
 150        {
 0151            if (imageMaterial == null)
 0152                return;
 153
 0154            imageMaterial.SetTexture(BASEMAP_SHADER_PROPERTY, texture);
 0155            imageMaterial.SetColor(COLOR_SHADER_PROPERTY, Color.white);
 0156        }
 157
 158        private void InitializeMaterials()
 159        {
 30160            Material[] meshMaterials = new Material[materials.Length];
 230161            for (int i = 0; i < materials.Length; i++)
 162            {
 85163                switch (materials[i].type)
 164                {
 165                    case NFTShapeMaterial.MaterialType.BACKGROUND:
 30166                        backgroundMaterial = new Material(materials[i].material);
 30167                        meshMaterials[i] = backgroundMaterial;
 30168                        break;
 169                    case NFTShapeMaterial.MaterialType.FRAME:
 26170                        frameMaterial = materials[i].material;
 26171                        meshMaterials[i] = frameMaterial;
 26172                        break;
 173                    case NFTShapeMaterial.MaterialType.IMAGE:
 29174                        imageMaterial = new Material(materials[i].material);
 29175                        meshMaterials[i] = imageMaterial;
 176                        break;
 177                }
 178            }
 179
 30180            meshRenderer.materials = meshMaterials;
 181
 30182            if (frameMaterial == null)
 4183                return;
 184
 26185            frameMaterial.shaderKeywords = null;
 186
 26187            if (noiseType == NoiseType.None)
 19188                return;
 189
 7190            switch (noiseType)
 191            {
 192                case NoiseType.ClassicPerlin:
 0193                    frameMaterial.EnableKeyword("CNOISE");
 0194                    break;
 195                case NoiseType.PeriodicPerlin:
 0196                    frameMaterial.EnableKeyword("PNOISE");
 0197                    break;
 198                case NoiseType.Simplex:
 7199                    frameMaterial.EnableKeyword("SNOISE");
 7200                    break;
 201                case NoiseType.SimplexNumericalGrad:
 0202                    frameMaterial.EnableKeyword("SNOISE_NGRAD");
 0203                    break;
 204                default: // SimplexAnalyticalGrad
 0205                    frameMaterial.EnableKeyword("SNOISE_AGRAD");
 206                    break;
 207            }
 208
 7209            if (noiseIs3D)
 0210                frameMaterial.EnableKeyword("THREED");
 211
 7212            if (noiseIsFractal)
 0213                frameMaterial.EnableKeyword("FRACTAL");
 7214        }
 215
 216    }
 217}