| | 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>(true); |
| | 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 | | bool materialNameIsCorrect = true; |
| | 40 | |
|
| 0 | 41 | | if ( !string.IsNullOrEmpty(materialsContainingThisName) ) |
| | 42 | | { |
| 0 | 43 | | string materialName = m.name.ToLower(); |
| 0 | 44 | | materialNameIsCorrect = materialName.Contains(materialsContainingThisName.ToLower()); |
| | 45 | | } |
| | 46 | |
|
| 0 | 47 | | if (!materialNameIsCorrect) |
| | 48 | | continue; |
| | 49 | |
|
| 0 | 50 | | string newMatName = sharedMats[i1].name; |
| 0 | 51 | | Material newMat = mapFunction.Invoke(sharedMats[i1]); |
| 0 | 52 | | newMat.name = newMatName; |
| 0 | 53 | | sharedMats[i1] = newMat; |
| | 54 | | } |
| | 55 | |
|
| 0 | 56 | | r.sharedMaterials = sharedMats; |
| | 57 | | } |
| 0 | 58 | | } |
| | 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 | | { |
| 0 | 71 | | MapSharedMaterialsRecursively( |
| | 72 | | transformRoot, |
| | 73 | | (mat) => |
| | 74 | | { |
| 0 | 75 | | mat.SetColor(propertyId, colorToChange); |
| 0 | 76 | | return mat; |
| | 77 | | }, |
| | 78 | | materialsContainingThisName); |
| 0 | 79 | | } |
| | 80 | | } |