< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarUtils()0%2100%
MapSharedMaterialsRecursively(...)0%42600%
SetColorInHierarchy(...)0%2100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using DCL;
 4using DCL.Helpers;
 5using UnityEngine;
 6
 7public static class AvatarUtils
 8{
 09    public static int _BaseColor = Shader.PropertyToID("_BaseColor");
 010    public static int _EmissionColor = Shader.PropertyToID("_EmissionColor");
 011    public static int _BaseMap = Shader.PropertyToID("_BaseMap");
 012    public static int _EyesTexture = Shader.PropertyToID("_EyesTexture");
 013    public static int _EyeTint = Shader.PropertyToID("_EyeTint");
 014    public static int _IrisMask = Shader.PropertyToID("_IrisMask");
 015    public static int _TintMask = Shader.PropertyToID("_TintMask");
 16
 17    /// <summary>
 18    /// This will search all the transform hierachy for sharedMaterials filtered by name, and call a map function on the
 19    /// This means each material will be replaced with the function return value.
 20    /// </summary>
 21    public static void MapSharedMaterialsRecursively(Transform transformRoot,
 22        Func<Material, Material> mapFunction,
 23        string materialsContainingThisName = null)
 24    {
 025        Renderer[] renderers = transformRoot.GetComponentsInChildren<Renderer>(true);
 26
 027        for (int i = 0; i < renderers.Length; i++)
 28        {
 029            Renderer r = renderers[i];
 030            Material[] sharedMats = r.sharedMaterials;
 31
 032            for (int i1 = 0; i1 < sharedMats.Length; i1++)
 33            {
 034                Material m = sharedMats[i1];
 35
 036                if (m == null)
 37                    continue;
 38
 039                bool materialNameIsCorrect = true;
 40
 041                if ( !string.IsNullOrEmpty(materialsContainingThisName) )
 42                {
 043                    string materialName = m.name.ToLower();
 044                    materialNameIsCorrect = materialName.Contains(materialsContainingThisName.ToLower());
 45                }
 46
 047                if (!materialNameIsCorrect)
 48                    continue;
 49
 050                string newMatName = sharedMats[i1].name;
 051                Material newMat = mapFunction.Invoke(sharedMats[i1]);
 052                newMat.name = newMatName;
 053                sharedMats[i1] = newMat;
 54            }
 55
 056            r.sharedMaterials = sharedMats;
 57        }
 058    }
 59
 60    /// <summary>
 61    /// This will search all the transform hierachy, and change _Color on all materials containing the proper name.
 62    /// </summary>
 63    /// <param name="transformRoot">Transform where to start</param>
 64    /// <param name="materialsContainingThisName">name to filter in materials</param>
 65    /// <param name="colorToChange">color to change in the renderers</param>
 66    public static void SetColorInHierarchy(Transform transformRoot,
 67        string materialsContainingThisName,
 68        Color colorToChange,
 69        int propertyId)
 70    {
 071        MapSharedMaterialsRecursively(
 72            transformRoot,
 73            (mat) =>
 74            {
 075                mat.SetColor(propertyId, colorToChange);
 076                return mat;
 77            },
 78            materialsContainingThisName);
 079    }
 80}