< 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:50
Coverable lines:50
Total lines:132
Line coverage:0% (0 of 50)
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%
ReplaceMaterialsWithCopiesOf(...)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>();
 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                string materialName = m.name.ToLower();
 40
 041                if (string.IsNullOrEmpty(materialsContainingThisName) || materialName.Contains(materialsContainingThisNa
 42                {
 043                    string newMatName = sharedMats[i1].name;
 044                    Material newMat = mapFunction.Invoke(sharedMats[i1]);
 045                    newMat.name = newMatName;
 046                    sharedMats[i1] = newMat;
 47                }
 48            }
 49
 050            r.sharedMaterials = sharedMats;
 51        }
 052    }
 53
 54    /// <summary>
 55    /// This will search all the transform hierachy, and change _Color on all materials containing the proper name.
 56    /// </summary>
 57    /// <param name="transformRoot">Transform where to start</param>
 58    /// <param name="materialsContainingThisName">name to filter in materials</param>
 59    /// <param name="colorToChange">color to change in the renderers</param>
 60    public static void SetColorInHierarchy(Transform transformRoot,
 61        string materialsContainingThisName,
 62        Color colorToChange,
 63        string shaderId = "_BaseColor")
 64    {
 065        int _Color = Shader.PropertyToID(shaderId);
 66
 067        MapSharedMaterialsRecursively(
 68            transformRoot,
 69            (mat) =>
 70            {
 071                mat.SetColor(_Color, colorToChange);
 072                return mat;
 73            },
 74            materialsContainingThisName);
 075    }
 76
 77    /// <summary>
 78    /// This will search all the transform hierachy for all renderers,
 79    /// and replace all of its materials containing the specified name by the new one.
 80    /// </summary>
 81    /// <param name="transformRoot">Transform where to start the traversal</param>
 82    /// <param name="replaceThemWith">material to replace them</param>
 83    /// <param name="materialsContainingThisName">name to filter in materials</param>
 84    public static List<Material> ReplaceMaterialsWithCopiesOf(Transform transformRoot,
 85        Material replaceThemWith,
 86        string materialsContainingThisName = null)
 87    {
 088        List<Material> result = new List<Material>();
 89
 090        MapSharedMaterialsRecursively(
 91            transformRoot,
 92            (mat) =>
 93            {
 094                Material copy = new Material(replaceThemWith);
 95
 096                Texture _MatCap = null;
 097                Texture _GMatCap = null;
 098                Texture _FMatCap = null;
 99
 0100                if (replaceThemWith.HasProperty(ShaderUtils.MatCap))
 0101                    _MatCap = replaceThemWith.GetTexture(ShaderUtils.MatCap);
 102
 0103                if (replaceThemWith.HasProperty(ShaderUtils.GlossMatCap))
 0104                    _GMatCap = replaceThemWith.GetTexture(ShaderUtils.GlossMatCap);
 105
 0106                if (replaceThemWith.HasProperty(ShaderUtils.FresnelMatCap))
 0107                    _FMatCap = replaceThemWith.GetTexture(ShaderUtils.FresnelMatCap);
 108
 109                //NOTE(Brian): This method has a bug, if the material being copied lacks a property of the source materi
 110                //             the source material property will get erased. It can't be added back and even the materia
 111                //             Check the comment in Lit.shader.
 0112                copy.CopyPropertiesFromMaterial(mat);
 113
 0114                if (_GMatCap != null)
 0115                    copy.SetTexture(ShaderUtils.GlossMatCap, _GMatCap);
 116
 0117                if (_FMatCap != null)
 0118                    copy.SetTexture(ShaderUtils.FresnelMatCap, _FMatCap);
 119
 0120                if (_MatCap != null)
 0121                    copy.SetTexture(ShaderUtils.MatCap, _MatCap);
 122
 0123                SRPBatchingHelper.OptimizeMaterial(copy);
 124
 0125                result.Add(copy);
 0126                return copy;
 127            },
 128            materialsContainingThisName);
 129
 0130        return result;
 131    }
 132}