| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Shaders; |
| | 7 | | using UnityEngine; |
| | 8 | | using Object = UnityEngine.Object; |
| | 9 | |
|
| | 10 | | namespace AvatarSystem |
| | 11 | | { |
| | 12 | | public static class AvatarSystemUtils |
| | 13 | | { |
| | 14 | | public const float AVATAR_Y_OFFSET = 0.75f; |
| | 15 | | private const string AB_FEATURE_FLAG_NAME = "wearable_asset_bundles"; |
| | 16 | |
|
| | 17 | | public static bool IsCategoryRequired(string category) |
| | 18 | | { |
| 0 | 19 | | return WearableLiterals.Categories.REQUIRED_CATEGORIES.Contains(category); |
| | 20 | | } |
| | 21 | |
|
| | 22 | | public static bool UseAssetBundles() |
| | 23 | | { |
| 2 | 24 | | var featureFlags = DataStore.i.featureFlags.flags.Get(); |
| 2 | 25 | | return featureFlags != null && featureFlags.IsFeatureEnabled(AB_FEATURE_FLAG_NAME); |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public static (string mainTextureUrl, string maskTextureUrl) GetFacialFeatureTexturesUrls(string bodyshapeId, We |
| | 29 | | { |
| 0 | 30 | | if (facialFeature.data.category != WearableLiterals.Categories.EYES && facialFeature.data.category == Wearab |
| 0 | 31 | | return (null, null); |
| | 32 | |
|
| 0 | 33 | | var representation = facialFeature.GetRepresentation(bodyshapeId); |
| 0 | 34 | | string mainTextureHash = representation?.contents?.FirstOrDefault(x => x.key == representation?.mainFile)?.h |
| | 35 | |
|
| 0 | 36 | | if (string.IsNullOrEmpty(mainTextureHash)) |
| 0 | 37 | | mainTextureHash = representation?.contents?.FirstOrDefault(x => !x.key.ToLower().Contains("_mask.png"))? |
| | 38 | |
|
| 0 | 39 | | if (string.IsNullOrEmpty(mainTextureHash)) |
| 0 | 40 | | return (null, null); |
| | 41 | |
|
| 0 | 42 | | string maskTextureHash = representation?.contents?.FirstOrDefault(x => x.key.ToLower().Contains("_mask.png") |
| | 43 | |
|
| 0 | 44 | | string mainTextureUrl = facialFeature.baseUrl + mainTextureHash; |
| 0 | 45 | | string maskTextureUrl = maskTextureHash == null ? null : facialFeature.baseUrl + maskTextureHash; |
| | 46 | |
|
| 0 | 47 | | return (mainTextureUrl, maskTextureUrl); |
| | 48 | | } |
| | 49 | |
|
| | 50 | | public static void CopyBones(Transform rootBone, Transform[] bones, IEnumerable<SkinnedMeshRenderer> targets) |
| | 51 | | { |
| 0 | 52 | | if (rootBone == null || bones == null) |
| 0 | 53 | | return; |
| | 54 | |
|
| 0 | 55 | | foreach (SkinnedMeshRenderer skinnedMeshRenderer in targets) { CopyBones(rootBone, bones, skinnedMeshRendere |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public static void CopyBones(Transform rootBone, Transform[] bones, SkinnedMeshRenderer skinnedMeshRenderer) |
| | 59 | | { |
| 0 | 60 | | if (rootBone == null || bones == null || skinnedMeshRenderer == null) |
| 0 | 61 | | return; |
| | 62 | |
|
| 0 | 63 | | skinnedMeshRenderer.rootBone = rootBone; |
| 0 | 64 | | skinnedMeshRenderer.bones = bones; |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | public static void PrepareMaterialColors(Rendereable rendereable, Color skinColor, Color hairColor) |
| | 68 | | { |
| 12 | 69 | | foreach (Renderer renderer in rendereable.renderers) |
| | 70 | | { |
| 16 | 71 | | foreach (Material material in renderer.materials) |
| | 72 | | { |
| | 73 | | // If this is modified, check DecentralandMaterialGenerator.SetMaterialName, |
| | 74 | | // its important for the asset bundles materials to have normalized names but this functionality sho |
| 4 | 75 | | string name = material.name.ToLower(); |
| | 76 | |
|
| 4 | 77 | | if (name.Contains("skin")) |
| 2 | 78 | | material.SetColor(ShaderUtils.BaseColor, skinColor); |
| 2 | 79 | | else if (name.Contains("hair")) |
| 0 | 80 | | material.SetColor(ShaderUtils.BaseColor, hairColor); |
| | 81 | | } |
| | 82 | | } |
| 2 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Extract bodyparts of a Rendereable. |
| | 87 | | /// |
| | 88 | | /// Using this on a Rendereable that doesn't comes from a bodyshape might result in unexpected result |
| | 89 | | /// </summary> |
| | 90 | | /// <param name="rendereable"></param> |
| | 91 | | /// <returns></returns> |
| | 92 | |
|
| | 93 | | // TODO: The list of body shapes that need to be extracted should come from an external source, |
| | 94 | | // in order to make this scalable we should return a Dictionary<string, SkinnedMeshRenderer> with the results |
| | 95 | | public static ( |
| | 96 | | SkinnedMeshRenderer head, |
| | 97 | | SkinnedMeshRenderer upperBody, |
| | 98 | | SkinnedMeshRenderer lowerBody, |
| | 99 | | SkinnedMeshRenderer feet, |
| | 100 | | SkinnedMeshRenderer eyes, |
| | 101 | | SkinnedMeshRenderer eyebrows, |
| | 102 | | SkinnedMeshRenderer mouth, |
| | 103 | | SkinnedMeshRenderer hands, |
| | 104 | | List<SkinnedMeshRenderer> extraParts ) ExtractBodyShapeParts(Rendereable rendereable) |
| | 105 | | { |
| 0 | 106 | | SkinnedMeshRenderer head = null; |
| 0 | 107 | | SkinnedMeshRenderer upperBody = null; |
| 0 | 108 | | SkinnedMeshRenderer lowerBody = null; |
| 0 | 109 | | SkinnedMeshRenderer feet = null; |
| 0 | 110 | | SkinnedMeshRenderer eyes = null; |
| 0 | 111 | | SkinnedMeshRenderer eyebrows = null; |
| 0 | 112 | | SkinnedMeshRenderer mouth = null; |
| 0 | 113 | | SkinnedMeshRenderer hands = null; |
| 0 | 114 | | var extraParts = new List<SkinnedMeshRenderer>(); |
| | 115 | |
|
| 0 | 116 | | foreach (Renderer r in rendereable.renderers) |
| | 117 | | { |
| 0 | 118 | | if (r is not SkinnedMeshRenderer renderer) |
| | 119 | | continue; |
| | 120 | |
|
| 0 | 121 | | string name = renderer.name.ToLower(); |
| | 122 | |
|
| | 123 | | // we still support the old gltf hierarchy for ABs |
| 0 | 124 | | if (name.Contains("primitive")) |
| 0 | 125 | | name = renderer.transform.parent.name.ToLower(); |
| | 126 | |
|
| 0 | 127 | | if (name.Contains("head")) |
| 0 | 128 | | head = renderer; |
| 0 | 129 | | else if (name.Contains("ubody")) |
| 0 | 130 | | upperBody = renderer; |
| 0 | 131 | | else if (name.Contains("lbody")) |
| 0 | 132 | | lowerBody = renderer; |
| 0 | 133 | | else if (name.Contains("hands")) |
| 0 | 134 | | hands = renderer; |
| 0 | 135 | | else if (name.Contains("feet")) |
| 0 | 136 | | feet = renderer; |
| 0 | 137 | | else if (name.Contains("eyes")) |
| 0 | 138 | | eyes = renderer; |
| 0 | 139 | | else if (name.Contains("eyebrows")) |
| 0 | 140 | | eyebrows = renderer; |
| 0 | 141 | | else if (name.Contains("mouth")) |
| 0 | 142 | | mouth = renderer; |
| | 143 | | else |
| | 144 | | { |
| 0 | 145 | | Debug.LogWarning($"{name} has not been set-up as a valid body part", r); |
| 0 | 146 | | extraParts.Add(renderer); |
| | 147 | | } |
| | 148 | |
|
| | 149 | | } |
| | 150 | |
|
| 0 | 151 | | return (head, upperBody, lowerBody, feet, eyes, eyebrows, mouth, hands, extraParts); |
| | 152 | | } |
| | 153 | |
|
| | 154 | | public static List<SkinnedMeshRenderer> GetActiveBodyPartsRenderers(IBodyshapeLoader bodyshapeLoader, string bod |
| | 155 | | { |
| 0 | 156 | | HashSet<string> hiddenList = new HashSet<string>(); |
| 0 | 157 | | HashSet<string> usedCategories = new HashSet<string>(); |
| | 158 | |
|
| 0 | 159 | | foreach (WearableItem wearable in wearables) |
| | 160 | | { |
| 0 | 161 | | usedCategories.Add(wearable.data.category); |
| 0 | 162 | | string[] hiddenByThisWearable = wearable.GetHidesList(bodyShapeId); |
| | 163 | |
|
| 0 | 164 | | if (hiddenByThisWearable != null) |
| 0 | 165 | | hiddenList.UnionWith(hiddenByThisWearable); |
| | 166 | | } |
| | 167 | |
|
| 0 | 168 | | List<SkinnedMeshRenderer> result = new List<SkinnedMeshRenderer>(); |
| | 169 | |
|
| 0 | 170 | | if (!hiddenList.Contains(WearableLiterals.Categories.HEAD) && !usedCategories.Contains(WearableLiterals.Cate |
| 0 | 171 | | result.Add(bodyshapeLoader.headRenderer); |
| | 172 | |
|
| 0 | 173 | | if (!hiddenList.Contains(WearableLiterals.Categories.UPPER_BODY) && !usedCategories.Contains(WearableLiteral |
| 0 | 174 | | result.Add(bodyshapeLoader.upperBodyRenderer); |
| | 175 | |
|
| 0 | 176 | | if (!hiddenList.Contains(WearableLiterals.Categories.LOWER_BODY) && !usedCategories.Contains(WearableLiteral |
| 0 | 177 | | result.Add(bodyshapeLoader.lowerBodyRenderer); |
| | 178 | |
|
| 0 | 179 | | if (!hiddenList.Contains(WearableLiterals.Categories.FEET) && !usedCategories.Contains(WearableLiterals.Cate |
| 0 | 180 | | result.Add(bodyshapeLoader.feetRenderer); |
| | 181 | |
|
| 0 | 182 | | if (!hiddenList.Contains(WearableLiterals.Categories.HANDS) && !usedCategories.Contains(WearableLiterals.Cat |
| 0 | 183 | | result.Add(bodyshapeLoader.handsRenderer); |
| | 184 | |
|
| | 185 | | // We dont want to hide new body parts that are not configured yet |
| 0 | 186 | | result.AddRange(bodyshapeLoader.extraRenderers); |
| | 187 | |
|
| 0 | 188 | | return result; |
| | 189 | | } |
| | 190 | |
|
| | 191 | | public static void SpawnAvatarLoadedParticles(Transform avatarContainer, GameObject particlePrefab) |
| | 192 | | { |
| 0 | 193 | | if (!particlePrefab.TryGetComponent(out DestroyParticlesOnFinish _)) |
| 0 | 194 | | throw new Exception("A self destructive particles prefab is expected"); |
| | 195 | |
|
| 0 | 196 | | GameObject particles = Object.Instantiate(particlePrefab); |
| 0 | 197 | | particles.transform.position = avatarContainer.position + particlePrefab.transform.position; |
| 0 | 198 | | } |
| | 199 | | } |
| | 200 | | } |