< Summary

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

Metrics

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

File(s)

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

#LineLine coverage
 1using UnityEngine;
 2
 3namespace DCL
 4{
 5    public static class AvatarRendererHelpers
 6    {
 07        private static readonly int IMPOSTOR_TEXTURE_PROPERTY = Shader.PropertyToID("_BaseMap");
 08        private static readonly int IMPOSTOR_TEXTURE_COLOR_PROPERTY = Shader.PropertyToID("_BaseColor");
 9
 10        // Manually tweaked values
 011        public static readonly float IMPOSTOR_MOVEMENT_INTERPOLATION = 1.79f;
 12        private const float IMPOSTOR_TINT_MIN_DISTANCE = 30f;
 13        private const float IMPOSTOR_TINT_MAX_DISTANCE = 81.33f;
 14        private const float IMPOSTOR_TINT_NEAREST_BLACKNESS = 0f;
 15        private const float IMPOSTOR_TINT_FAREST_BLACKNESS = 0.74f;
 16        private const float IMPOSTOR_ALPHA_NEAREST_VALUE = 1f;
 17        private const float IMPOSTOR_ALPHA_FAREST_VALUE = 1f;
 18
 19        // 2048x2048 atlas with 8 512x1024 snapshot-sprites
 20        private const int GENERIC_IMPOSTORS_ATLAS_COLUMNS = 4;
 21        private const int GENERIC_IMPOSTORS_ATLAS_ROWS = 2;
 22
 23        public static void RandomizeAndApplyGenericImpostor(Mesh impostorMesh)
 24        {
 025            Vector2 spriteSize = new Vector2(1f / GENERIC_IMPOSTORS_ATLAS_COLUMNS, 1f / GENERIC_IMPOSTORS_ATLAS_ROWS);
 026            float randomUVXPos = Random.Range(0, GENERIC_IMPOSTORS_ATLAS_COLUMNS) * spriteSize.x;
 027            float randomUVYPos = Random.Range(0, GENERIC_IMPOSTORS_ATLAS_ROWS) * spriteSize.y;
 28
 029            Vector2[] uvs = new Vector2[4]; // Quads have only 4 vertices
 030            uvs[0].Set(randomUVXPos, randomUVYPos);
 031            uvs[1].Set(randomUVXPos + spriteSize.x, randomUVYPos);
 032            uvs[2].Set(randomUVXPos, randomUVYPos + spriteSize.y);
 033            uvs[3].Set(randomUVXPos + spriteSize.x, randomUVYPos + spriteSize.y);
 034            impostorMesh.uv = uvs;
 035        }
 36
 37        private static void ResetImpostorMeshUVs(Mesh impostorMesh)
 38        {
 039            Vector2[] uvs = new Vector2[4]; // Quads have only 4 vertices
 040            uvs[0].Set(0, 0);
 041            uvs[1].Set(1, 0);
 042            uvs[2].Set(0, 1);
 043            uvs[3].Set(1, 1);
 044            impostorMesh.uv = uvs;
 045        }
 46
 47        public static void SetImpostorTexture(Texture2D impostorTexture, Mesh impostorMesh, Material impostorMaterial)
 48        {
 049            if (impostorTexture == null)
 050                return;
 51
 052            ResetImpostorMeshUVs(impostorMesh);
 53
 054            impostorMaterial.SetTexture(IMPOSTOR_TEXTURE_PROPERTY, impostorTexture);
 055        }
 56
 57        public static void SetImpostorTintColor(Material impostorMaterial, Color newColor)
 58        {
 059            if (impostorMaterial == null)
 060                return;
 61
 062            impostorMaterial.SetColor(IMPOSTOR_TEXTURE_COLOR_PROPERTY, newColor);
 063        }
 64
 65        public static Color CalculateImpostorTint(float distanceToMainPlayer)
 66        {
 067            float initialStep = Mathf.Max(IMPOSTOR_TINT_MIN_DISTANCE, distanceToMainPlayer);
 068            float tintStep = Mathf.InverseLerp(IMPOSTOR_TINT_MIN_DISTANCE, IMPOSTOR_TINT_MAX_DISTANCE, initialStep);
 069            float tintValue = Mathf.Lerp(IMPOSTOR_TINT_NEAREST_BLACKNESS, IMPOSTOR_TINT_FAREST_BLACKNESS, tintStep);
 070            Color newColor = Color.Lerp(Color.white, Color.black, tintValue);
 071            newColor.a = Mathf.Lerp(IMPOSTOR_ALPHA_NEAREST_VALUE, IMPOSTOR_ALPHA_FAREST_VALUE, tintStep);
 72
 073            return newColor;
 74        }
 75    }
 76}