< Summary

Class:DCL.AvatarRendererHelpers
Assembly:AvatarRendererHelpers
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarRendererHelpers/AvatarRendererHelpers.cs
Covered lines:29
Uncovered lines:7
Coverable lines:36
Total lines:80
Line coverage:80.5% (29 of 36)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarRendererHelpers()0%110100%
RandomizeAndApplyGenericImpostor(...)0%110100%
ResetImpostorMeshUVs(...)0%110100%
SetImpostorTexture(...)0%2.032080%
SetImpostorTintColor(...)0%110100%
CalculateImpostorTint(...)0%2100%
ResetImpostor(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarRendererHelpers/AvatarRendererHelpers.cs

#LineLine coverage
 1using UnityEngine;
 2
 3namespace DCL
 4{
 5    public static class AvatarRendererHelpers
 6    {
 17        private static readonly int IMPOSTOR_TEXTURE_PROPERTY = Shader.PropertyToID("_BaseMap");
 18        private static readonly int IMPOSTOR_TEXTURE_COLOR_PROPERTY = Shader.PropertyToID("_BaseColor");
 9
 10        // Manually tweaked values
 11        private const float IMPOSTOR_TINT_MIN_DISTANCE = 30f;
 12        private const float IMPOSTOR_TINT_MAX_DISTANCE = 81.33f;
 13        private const float IMPOSTOR_TINT_NEAREST_BLACKNESS = 0f;
 14        private const float IMPOSTOR_TINT_FAREST_BLACKNESS = 0.74f;
 15
 16        // 2048x2048 atlas with 8 512x1024 snapshot-sprites
 17        private const int GENERIC_IMPOSTORS_ATLAS_COLUMNS = 4;
 18        private const int GENERIC_IMPOSTORS_ATLAS_ROWS = 2;
 19
 120        private static Texture2D genericImpostorsTexture = Resources.Load<Texture2D>("Textures/avatar-impostors-atlas");
 121        private static Texture2D transparentPixelTexture = Resources.Load<Texture2D>("Textures/transparent-pixel");
 22
 23        public static void RandomizeAndApplyGenericImpostor(Mesh impostorMesh, Material impostorMaterial)
 24        {
 1025            SetImpostorTexture(genericImpostorsTexture, impostorMesh, impostorMaterial);
 26
 1027            Vector2 spriteSize = new Vector2(1f / GENERIC_IMPOSTORS_ATLAS_COLUMNS, 1f / GENERIC_IMPOSTORS_ATLAS_ROWS);
 1028            float randomUVXPos = Random.Range(0, GENERIC_IMPOSTORS_ATLAS_COLUMNS) * spriteSize.x;
 1029            float randomUVYPos = Random.Range(0, GENERIC_IMPOSTORS_ATLAS_ROWS) * spriteSize.y;
 30
 1031            Vector2[] uvs = new Vector2[4]; // Quads have only 4 vertices
 1032            uvs[0].Set(randomUVXPos, randomUVYPos);
 1033            uvs[1].Set(randomUVXPos + spriteSize.x, randomUVYPos);
 1034            uvs[2].Set(randomUVXPos, randomUVYPos + spriteSize.y);
 1035            uvs[3].Set(randomUVXPos + spriteSize.x, randomUVYPos + spriteSize.y);
 1036            impostorMesh.uv = uvs;
 1037        }
 38
 39        private static void ResetImpostorMeshUVs(Mesh impostorMesh)
 40        {
 1041            Vector2[] uvs = new Vector2[4]; // Quads have only 4 vertices
 1042            uvs[0].Set(0, 0);
 1043            uvs[1].Set(1, 0);
 1044            uvs[2].Set(0, 1);
 1045            uvs[3].Set(1, 1);
 1046            impostorMesh.uv = uvs;
 1047        }
 48
 49        public static void SetImpostorTexture(Texture2D impostorTexture, Mesh impostorMesh, Material impostorMaterial)
 50        {
 1051            if (TextureUtils.IsQuestionMarkPNG(impostorTexture))
 052                return;
 53
 1054            ResetImpostorMeshUVs(impostorMesh);
 55
 1056            impostorMaterial.SetTexture(IMPOSTOR_TEXTURE_PROPERTY, impostorTexture);
 1057        }
 58
 59        /// <summary>
 60        /// Sets impostor texture tint color ignoring the alpha channel
 61        /// </summary>
 62        public static void SetImpostorTintColor(Material impostorMaterial, Color newColor)
 63        {
 264            newColor.a = impostorMaterial.GetColor(IMPOSTOR_TEXTURE_COLOR_PROPERTY).a;
 265            impostorMaterial.SetColor(IMPOSTOR_TEXTURE_COLOR_PROPERTY, newColor);
 266        }
 67
 68        public static Color CalculateImpostorTint(float distanceToMainPlayer)
 69        {
 070            float initialStep = Mathf.Max(IMPOSTOR_TINT_MIN_DISTANCE, distanceToMainPlayer);
 071            float tintStep = Mathf.InverseLerp(IMPOSTOR_TINT_MIN_DISTANCE, IMPOSTOR_TINT_MAX_DISTANCE, initialStep);
 072            float tintValue = Mathf.Lerp(IMPOSTOR_TINT_NEAREST_BLACKNESS, IMPOSTOR_TINT_FAREST_BLACKNESS, tintStep);
 073            Color newColor = Color.Lerp(Color.white, Color.black, tintValue);
 74
 075            return newColor;
 76        }
 77
 078        public static void ResetImpostor(Mesh impostorMesh, Material impostorMaterial) { SetImpostorTexture(transparentP
 79    }
 80}