| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using UnityEngine; |
| | 7 | | using Object = UnityEngine.Object; |
| | 8 | |
|
| | 9 | | namespace AvatarSystem |
| | 10 | | { |
| | 11 | | public static class AvatarSystemUtils |
| | 12 | | { |
| | 13 | | public const float AVATAR_Y_OFFSET = 0.75f; |
| | 14 | | private const string AB_FEATURE_FLAG_NAME = "wearable_asset_bundles"; |
| | 15 | |
|
| 12 | 16 | | public static bool IsCategoryRequired(string category) { return WearableLiterals.Categories.REQUIRED_CATEGORIES. |
| | 17 | |
|
| | 18 | | public static bool UseAssetBundles() |
| | 19 | | { |
| 84 | 20 | | var featureFlags = DataStore.i.featureFlags.flags.Get(); |
| 84 | 21 | | return featureFlags != null && featureFlags.IsFeatureEnabled(AB_FEATURE_FLAG_NAME); |
| | 22 | | } |
| | 23 | |
|
| | 24 | | public static (string mainTextureUrl, string maskTextureUrl) GetFacialFeatureTexturesUrls(string bodyshapeId, We |
| | 25 | | { |
| 0 | 26 | | if (facialFeature.data.category != WearableLiterals.Categories.EYES && facialFeature.data.category == Wearab |
| 0 | 27 | | return (null, null); |
| | 28 | |
|
| 0 | 29 | | var representation = facialFeature.GetRepresentation(bodyshapeId); |
| 0 | 30 | | string mainTextureHash = representation?.contents?.FirstOrDefault(x => x.key == representation?.mainFile)?.h |
| 0 | 31 | | if (string.IsNullOrEmpty(mainTextureHash)) |
| 0 | 32 | | mainTextureHash = representation?.contents?.FirstOrDefault(x => !x.key.ToLower().Contains("_mask.png"))? |
| 0 | 33 | | if (string.IsNullOrEmpty(mainTextureHash)) |
| 0 | 34 | | return (null, null); |
| | 35 | |
|
| 0 | 36 | | string maskTextureHash = representation?.contents?.FirstOrDefault(x => x.key.ToLower().Contains("_mask.png") |
| | 37 | |
|
| 0 | 38 | | string mainTextureUrl = facialFeature.baseUrl + mainTextureHash; |
| 0 | 39 | | string maskTextureUrl = maskTextureHash == null ? null : facialFeature.baseUrl + maskTextureHash; |
| | 40 | |
|
| 0 | 41 | | return (mainTextureUrl, maskTextureUrl); |
| | 42 | | } |
| | 43 | |
|
| | 44 | | public static void CopyBones(SkinnedMeshRenderer source, IEnumerable<SkinnedMeshRenderer> targets) |
| | 45 | | { |
| 2 | 46 | | if (source == null) |
| 0 | 47 | | return; |
| | 48 | |
|
| 24 | 49 | | foreach (SkinnedMeshRenderer skinnedMeshRenderer in targets) |
| | 50 | | { |
| 10 | 51 | | skinnedMeshRenderer.rootBone = source.rootBone; |
| 10 | 52 | | skinnedMeshRenderer.bones = source.bones; |
| | 53 | | } |
| 2 | 54 | | } |
| | 55 | |
|
| | 56 | | public static void PrepareMaterialColors(Rendereable rendereable, Color skinColor, Color hairColor) |
| | 57 | | { |
| 88 | 58 | | foreach (Renderer renderer in rendereable.renderers) |
| | 59 | | { |
| 128 | 60 | | foreach (Material material in renderer.materials) |
| | 61 | | { |
| 32 | 62 | | if (material.name.ToLower().Contains("skin")) |
| 7 | 63 | | material.SetColor(ShaderUtils.BaseColor, skinColor); |
| 25 | 64 | | else if (material.name.ToLower().Contains("hair")) |
| 5 | 65 | | material.SetColor(ShaderUtils.BaseColor, hairColor); |
| | 66 | | } |
| | 67 | | } |
| 12 | 68 | | } |
| | 69 | |
|
| | 70 | | public static (WearableItem bodyshape, WearableItem eyes, WearableItem eyebrows, WearableItem mouth, List<Wearab |
| | 71 | | { |
| 0 | 72 | | WearableItem bodyshape = null; |
| 0 | 73 | | WearableItem eyes = null; |
| 0 | 74 | | WearableItem eyebrows = null; |
| 0 | 75 | | WearableItem mouth = null; |
| 0 | 76 | | List<WearableItem> resultWearables = new List<WearableItem>(); |
| 0 | 77 | | foreach (WearableItem wearable in wearables) |
| | 78 | | { |
| 0 | 79 | | switch (wearable.data.category) |
| | 80 | | { |
| | 81 | | case WearableLiterals.Categories.BODY_SHAPE: |
| 0 | 82 | | bodyshape = wearable; |
| 0 | 83 | | break; |
| | 84 | | case WearableLiterals.Categories.EYES: |
| 0 | 85 | | eyes = wearable; |
| 0 | 86 | | break; |
| | 87 | | case WearableLiterals.Categories.EYEBROWS: |
| 0 | 88 | | eyebrows = wearable; |
| 0 | 89 | | break; |
| | 90 | | case WearableLiterals.Categories.MOUTH: |
| 0 | 91 | | mouth = wearable; |
| 0 | 92 | | break; |
| | 93 | | default: |
| 0 | 94 | | resultWearables.Add(wearable); |
| | 95 | | break; |
| | 96 | | } |
| | 97 | | } |
| | 98 | |
|
| 0 | 99 | | return (bodyshape, eyes, eyebrows, mouth, resultWearables); |
| | 100 | | } |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// Extract bodyparts of a Rendereable. |
| | 104 | | /// |
| | 105 | | /// Using this on a Rendereable that doesn't comes from a bodyshape might result in unexpected result |
| | 106 | | /// </summary> |
| | 107 | | /// <param name="rendereable"></param> |
| | 108 | | /// <returns></returns> |
| | 109 | | public static ( |
| | 110 | | SkinnedMeshRenderer head, |
| | 111 | | SkinnedMeshRenderer upperBody, |
| | 112 | | SkinnedMeshRenderer lowerBody, |
| | 113 | | SkinnedMeshRenderer feet, |
| | 114 | | SkinnedMeshRenderer eyes, |
| | 115 | | SkinnedMeshRenderer eyebrows, |
| | 116 | | SkinnedMeshRenderer mouth |
| | 117 | | ) ExtractBodyshapeParts(Rendereable rendereable) |
| | 118 | | { |
| 1 | 119 | | SkinnedMeshRenderer head = null; |
| 1 | 120 | | SkinnedMeshRenderer upperBody = null; |
| 1 | 121 | | SkinnedMeshRenderer lowerBody = null; |
| 1 | 122 | | SkinnedMeshRenderer feet = null; |
| 1 | 123 | | SkinnedMeshRenderer eyes = null; |
| 1 | 124 | | SkinnedMeshRenderer eyebrows = null; |
| 1 | 125 | | SkinnedMeshRenderer mouth = null; |
| | 126 | |
|
| 16 | 127 | | foreach (Renderer r in rendereable.renderers) |
| | 128 | | { |
| 7 | 129 | | if (!(r is SkinnedMeshRenderer renderer)) |
| | 130 | | continue; |
| | 131 | |
|
| 7 | 132 | | string parentName = renderer.transform.parent.name.ToLower(); |
| | 133 | |
|
| 7 | 134 | | if (parentName.Contains("head")) |
| 1 | 135 | | head = renderer; |
| 6 | 136 | | else if (parentName.Contains("ubody")) |
| 1 | 137 | | upperBody = renderer; |
| 5 | 138 | | else if (parentName.Contains("lbody")) |
| 1 | 139 | | lowerBody = renderer; |
| 4 | 140 | | else if (parentName.Contains("feet")) |
| 1 | 141 | | feet = renderer; |
| 3 | 142 | | else if (parentName.Contains("eyes")) |
| 1 | 143 | | eyes = renderer; |
| 2 | 144 | | else if (parentName.Contains("eyebrows")) |
| 1 | 145 | | eyebrows = renderer; |
| 1 | 146 | | else if (parentName.Contains("mouth")) |
| 1 | 147 | | mouth = renderer; |
| | 148 | | } |
| | 149 | |
|
| 1 | 150 | | return (head, upperBody, lowerBody, feet, eyes, eyebrows, mouth); |
| | 151 | | } |
| | 152 | |
|
| | 153 | | /// <summary> |
| | 154 | | /// Filters hidden wearables. |
| | 155 | | /// Conflicts will be resolved by order in the array |
| | 156 | | /// </summary> |
| | 157 | | /// <param name="bodyshapeId"></param> |
| | 158 | | /// <param name="wearables"></param> |
| | 159 | | /// <returns></returns> |
| | 160 | | public static List<WearableItem> FilterHiddenWearables(string bodyshapeId, IEnumerable<WearableItem> wearables) |
| | 161 | | { |
| 0 | 162 | | HashSet<string> hiddenCategories = new HashSet<string>(); |
| 0 | 163 | | List<WearableItem> filteredWearables = new List<WearableItem>(); |
| 0 | 164 | | foreach (WearableItem wearable in wearables) |
| | 165 | | { |
| 0 | 166 | | if (hiddenCategories.Contains(wearable.data.category)) |
| | 167 | | { |
| | 168 | | continue; |
| | 169 | | } |
| | 170 | |
|
| 0 | 171 | | filteredWearables.Add(wearable); |
| 0 | 172 | | hiddenCategories.UnionWith(wearable.GetHidesList(bodyshapeId)); |
| | 173 | | } |
| | 174 | |
|
| 0 | 175 | | return filteredWearables; |
| | 176 | | } |
| | 177 | |
|
| | 178 | | public static (bool headVisible, bool upperBodyVisible, bool lowerBodyVisible, bool feetVisible) GetActiveBodyPa |
| | 179 | | { |
| 2 | 180 | | bool headVisible = true; |
| 2 | 181 | | bool upperBodyVisible = true; |
| 2 | 182 | | bool lowerBodyVisible = true; |
| 2 | 183 | | bool feetVisible = true; |
| | 184 | |
|
| 2 | 185 | | HashSet<string> hiddenList = new HashSet<string>(); |
| 2 | 186 | | HashSet<string> usedCategories = new HashSet<string>(); |
| | 187 | |
|
| 24 | 188 | | foreach (WearableItem wearable in wearables) |
| | 189 | | { |
| 10 | 190 | | usedCategories.Add(wearable.data.category); |
| 10 | 191 | | string[] hiddenByThisWearable = wearable.GetHidesList(bodyshapeId); |
| 10 | 192 | | if (hiddenByThisWearable != null) |
| 10 | 193 | | hiddenList.UnionWith(hiddenByThisWearable); |
| | 194 | | } |
| | 195 | |
|
| 2 | 196 | | headVisible = !hiddenList.Contains(WearableLiterals.Misc.HEAD) && !usedCategories.Contains(WearableLiterals. |
| 2 | 197 | | upperBodyVisible = !hiddenList.Contains(WearableLiterals.Categories.UPPER_BODY) && !usedCategories.Contains( |
| 2 | 198 | | lowerBodyVisible = !hiddenList.Contains(WearableLiterals.Categories.LOWER_BODY) && !usedCategories.Contains( |
| 2 | 199 | | feetVisible = !hiddenList.Contains(WearableLiterals.Categories.FEET) && !usedCategories.Contains(WearableLit |
| 2 | 200 | | return (headVisible, upperBodyVisible, lowerBodyVisible, feetVisible); |
| | 201 | | } |
| | 202 | |
|
| | 203 | | public static List<SkinnedMeshRenderer> GetActiveBodyPartsRenderers(IBodyshapeLoader bodyshapeLoader, bool headV |
| | 204 | | { |
| 2 | 205 | | List<SkinnedMeshRenderer> result = new List<SkinnedMeshRenderer>(); |
| 2 | 206 | | if (headVisible) |
| 2 | 207 | | result.Add(bodyshapeLoader.headRenderer); |
| 2 | 208 | | if (upperBodyVisible) |
| 0 | 209 | | result.Add(bodyshapeLoader.upperBodyRenderer); |
| 2 | 210 | | if (lowerBodyVisible) |
| 0 | 211 | | result.Add(bodyshapeLoader.lowerBodyRenderer); |
| 2 | 212 | | if (feetVisible) |
| 0 | 213 | | result.Add(bodyshapeLoader.feetRenderer); |
| 2 | 214 | | return result; |
| | 215 | | } |
| | 216 | |
|
| | 217 | | public static void SpawnAvatarLoadedParticles(Transform avatarContainer, GameObject particlePrefab) |
| | 218 | | { |
| 0 | 219 | | if (!particlePrefab.TryGetComponent(out DestroyParticlesOnFinish selfDestroyScript)) |
| 0 | 220 | | throw new Exception("A self destructive particles prefab is expected"); |
| | 221 | |
|
| 0 | 222 | | GameObject particles = Object.Instantiate(particlePrefab); |
| 0 | 223 | | particles.transform.position = avatarContainer.position + particlePrefab.transform.position; |
| 0 | 224 | | } |
| | 225 | | } |
| | 226 | | } |