| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | public static class AvatarUtils |
| | 8 | | { |
| 0 | 9 | | public static int _BaseColor = Shader.PropertyToID("_BaseColor"); |
| 0 | 10 | | public static int _EmissionColor = Shader.PropertyToID("_EmissionColor"); |
| 0 | 11 | | public static int _BaseMap = Shader.PropertyToID("_BaseMap"); |
| 0 | 12 | | public static int _EyesTexture = Shader.PropertyToID("_EyesTexture"); |
| 0 | 13 | | public static int _EyeTint = Shader.PropertyToID("_EyeTint"); |
| 0 | 14 | | public static int _IrisMask = Shader.PropertyToID("_IrisMask"); |
| 0 | 15 | | 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 | | { |
| 0 | 25 | | Renderer[] renderers = transformRoot.GetComponentsInChildren<Renderer>(); |
| | 26 | |
|
| 0 | 27 | | for (int i = 0; i < renderers.Length; i++) |
| | 28 | | { |
| 0 | 29 | | Renderer r = renderers[i]; |
| 0 | 30 | | Material[] sharedMats = r.sharedMaterials; |
| | 31 | |
|
| 0 | 32 | | for (int i1 = 0; i1 < sharedMats.Length; i1++) |
| | 33 | | { |
| 0 | 34 | | Material m = sharedMats[i1]; |
| | 35 | |
|
| 0 | 36 | | if (m == null) |
| | 37 | | continue; |
| | 38 | |
|
| 0 | 39 | | string materialName = m.name.ToLower(); |
| | 40 | |
|
| 0 | 41 | | if (string.IsNullOrEmpty(materialsContainingThisName) || materialName.Contains(materialsContainingThisNa |
| | 42 | | { |
| 0 | 43 | | string newMatName = sharedMats[i1].name; |
| 0 | 44 | | Material newMat = mapFunction.Invoke(sharedMats[i1]); |
| 0 | 45 | | newMat.name = newMatName; |
| 0 | 46 | | sharedMats[i1] = newMat; |
| | 47 | | } |
| | 48 | | } |
| | 49 | |
|
| 0 | 50 | | r.sharedMaterials = sharedMats; |
| | 51 | | } |
| 0 | 52 | | } |
| | 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 | | { |
| 0 | 65 | | int _Color = Shader.PropertyToID(shaderId); |
| | 66 | |
|
| 0 | 67 | | MapSharedMaterialsRecursively( |
| | 68 | | transformRoot, |
| | 69 | | (mat) => |
| | 70 | | { |
| 0 | 71 | | mat.SetColor(_Color, colorToChange); |
| 0 | 72 | | return mat; |
| | 73 | | }, |
| | 74 | | materialsContainingThisName); |
| 0 | 75 | | } |
| | 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 | | { |
| 0 | 88 | | List<Material> result = new List<Material>(); |
| | 89 | |
|
| 0 | 90 | | MapSharedMaterialsRecursively( |
| | 91 | | transformRoot, |
| | 92 | | (mat) => |
| | 93 | | { |
| 0 | 94 | | Material copy = new Material(replaceThemWith); |
| | 95 | |
|
| 0 | 96 | | Texture _MatCap = null; |
| 0 | 97 | | Texture _GMatCap = null; |
| 0 | 98 | | Texture _FMatCap = null; |
| | 99 | |
|
| 0 | 100 | | if (replaceThemWith.HasProperty(ShaderUtils.MatCap)) |
| 0 | 101 | | _MatCap = replaceThemWith.GetTexture(ShaderUtils.MatCap); |
| | 102 | |
|
| 0 | 103 | | if (replaceThemWith.HasProperty(ShaderUtils.GlossMatCap)) |
| 0 | 104 | | _GMatCap = replaceThemWith.GetTexture(ShaderUtils.GlossMatCap); |
| | 105 | |
|
| 0 | 106 | | if (replaceThemWith.HasProperty(ShaderUtils.FresnelMatCap)) |
| 0 | 107 | | _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. |
| 0 | 112 | | copy.CopyPropertiesFromMaterial(mat); |
| | 113 | |
|
| 0 | 114 | | if (_GMatCap != null) |
| 0 | 115 | | copy.SetTexture(ShaderUtils.GlossMatCap, _GMatCap); |
| | 116 | |
|
| 0 | 117 | | if (_FMatCap != null) |
| 0 | 118 | | copy.SetTexture(ShaderUtils.FresnelMatCap, _FMatCap); |
| | 119 | |
|
| 0 | 120 | | if (_MatCap != null) |
| 0 | 121 | | copy.SetTexture(ShaderUtils.MatCap, _MatCap); |
| | 122 | |
|
| 0 | 123 | | SRPBatchingHelper.OptimizeMaterial(copy); |
| | 124 | |
|
| 0 | 125 | | result.Add(copy); |
| 0 | 126 | | return copy; |
| | 127 | | }, |
| | 128 | | materialsContainingThisName); |
| | 129 | |
|
| 0 | 130 | | return result; |
| | 131 | | } |
| | 132 | | } |