< 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:42
Uncovered lines:43
Coverable lines:85
Total lines:261
Line coverage:49.4% (42 of 85)
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%
IsVisible()0%2100%
HasCollisions()0%2100%
Start()0%2100%
SetVisibility(...)0%110100%
SetHasCollisions(...)0%110100%
SetPointerBlocker(...)0%330100%
SetImage(...)0%6200%
Update()0%6200%
Dispose()0%2.52050%
FailLoading()0%110100%
UpdateBackgroundColor(...)0%2.062075%
SetFrameImage(...)0%42600%
UpdateTexture(...)0%6200%
InitializeMaterials()0%20.6414067.65%

File(s)

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

#LineLine coverage
 1using System;
 2using DCL.Components;
 3using DCL.Configuration;
 4using NFTShape_Internal;
 5using UnityEngine;
 6
 7namespace 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")]
 4972        [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
 181        static readonly int BASEMAP_SHADER_PROPERTY = Shader.PropertyToID("_BaseMap");
 182        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
 397            meshRenderer.transform.localScale = new UnityEngine.Vector3(0.5f, 0.5f, 1);
 398            InitializeMaterials();
 399        }
 100
 0101        public bool IsVisible() { return gameObject.activeInHierarchy; }
 102
 0103        public bool HasCollisions() { return boxCollider.enabled; }
 104
 0105        public BoxCollider nftCollider  => boxCollider;
 106
 3107        public IShape shape => this;
 108
 109        private void Start()
 110        {
 0111            loadingSpinnerGameObject.layer = LayerMask.NameToLayer("ViewportCullingIgnored");
 0112        }
 113
 114        public void SetVisibility(bool isVisible)
 115        {
 3116            gameObject.SetActive(isVisible);
 3117        }
 118
 119        public void SetHasCollisions(bool withCollision)
 120        {
 3121            boxCollider.enabled = withCollision;
 3122        }
 123
 124        public void SetPointerBlocker(bool isPointerBlocker)
 125        {
 3126            int colliderLayer = isPointerBlocker ? PhysicsLayers.onPointerEventLayer : DCL.Configuration.PhysicsLayers.d
 127
 3128            boxCollider.gameObject.layer = colliderLayer;
 3129        }
 130
 131        public void SetImage(string name, string url, INFTAsset nftAsset)
 132        {
 0133            if (nftAsset.previewAsset != null)
 0134                SetFrameImage(nftAsset.previewAsset.texture, resizeFrameMesh: true);
 135
 0136            loadingSpinnerGameObject.SetActive(false);
 0137            var hqImageHandlerConfig = new NFTShapeHQImageConfig()
 138            {
 139                controller = this,
 140                name = name,
 141                imageUrl = url,
 142                asset = nftAsset
 143            };
 144
 0145            hqTextureHandler = NFTShapeHQImageHandler.Create(hqImageHandlerConfig);
 0146            nftAsset.OnTextureUpdate += UpdateTexture;
 0147        }
 148
 0149        public void Update() { hqTextureHandler?.Update(); }
 150
 151        public void Dispose()
 152        {
 3153            hqTextureHandler?.Dispose();
 0154        }
 155
 156        public void FailLoading()
 157        {
 3158            loadingSpinnerGameObject.SetActive(false);
 159#if UNITY_EDITOR
 3160            gameObject.name += " - Failed loading";
 161#endif
 3162        }
 163
 164        public void UpdateBackgroundColor(UnityEngine.Color newColor)
 165        {
 3166            if (backgroundMaterial == null)
 0167                return;
 168
 3169            backgroundMaterial.SetColor(COLOR_SHADER_PROPERTY, newColor);
 3170        }
 171
 172        private void SetFrameImage(Texture2D texture, bool resizeFrameMesh = false)
 173        {
 0174            if (texture == null)
 0175                return;
 176
 0177            UpdateTexture(texture);
 178
 0179            if (resizeFrameMesh && meshRenderer != null)
 180            {
 181                float w, h;
 0182                w = h = 0.5f;
 0183                if (texture.width > texture.height)
 0184                    h *= texture.height / (float) texture.width;
 0185                else if (texture.width < texture.height)
 0186                    w *= texture.width / (float) texture.height;
 0187                UnityEngine.Vector3 newScale = new UnityEngine.Vector3(w, h, 1f);
 188
 0189                meshRenderer.transform.localScale = newScale;
 190            }
 0191        }
 192
 193        private void UpdateTexture(Texture2D texture)
 194        {
 0195            if (imageMaterial == null)
 0196                return;
 197
 0198            imageMaterial.SetTexture(BASEMAP_SHADER_PROPERTY, texture);
 0199            imageMaterial.SetColor(COLOR_SHADER_PROPERTY, UnityEngine.Color.white);
 0200        }
 201
 202        private void InitializeMaterials()
 203        {
 3204            Material[] meshMaterials = new Material[materials.Length];
 24205            for (int i = 0; i < materials.Length; i++)
 206            {
 9207                switch (materials[i].type)
 208                {
 209                    case NFTShapeMaterial.MaterialType.BACKGROUND:
 3210                        backgroundMaterial = new Material(materials[i].material);
 3211                        meshMaterials[i] = backgroundMaterial;
 3212                        break;
 213                    case NFTShapeMaterial.MaterialType.FRAME:
 3214                        frameMaterial = materials[i].material;
 3215                        meshMaterials[i] = frameMaterial;
 3216                        break;
 217                    case NFTShapeMaterial.MaterialType.IMAGE:
 3218                        imageMaterial = new Material(materials[i].material);
 3219                        meshMaterials[i] = imageMaterial;
 220                        break;
 221                }
 222            }
 223
 3224            meshRenderer.materials = meshMaterials;
 225
 3226            if (frameMaterial == null)
 0227                return;
 228
 3229            frameMaterial.shaderKeywords = null;
 230
 3231            if (noiseType == NoiseType.None)
 0232                return;
 233
 3234            switch (noiseType)
 235            {
 236                case NoiseType.ClassicPerlin:
 0237                    frameMaterial.EnableKeyword("CNOISE");
 0238                    break;
 239                case NoiseType.PeriodicPerlin:
 0240                    frameMaterial.EnableKeyword("PNOISE");
 0241                    break;
 242                case NoiseType.Simplex:
 3243                    frameMaterial.EnableKeyword("SNOISE");
 3244                    break;
 245                case NoiseType.SimplexNumericalGrad:
 0246                    frameMaterial.EnableKeyword("SNOISE_NGRAD");
 0247                    break;
 248                default: // SimplexAnalyticalGrad
 0249                    frameMaterial.EnableKeyword("SNOISE_AGRAD");
 250                    break;
 251            }
 252
 3253            if (noiseIs3D)
 0254                frameMaterial.EnableKeyword("THREED");
 255
 3256            if (noiseIsFractal)
 0257                frameMaterial.EnableKeyword("FRACTAL");
 3258        }
 259
 260    }
 261}