| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public interface IBodyShapeController |
| | 7 | | { |
| | 8 | | string bodyShapeId { get; } |
| | 9 | | void SetupEyes(Material material, Texture texture, Texture mask, Color color); |
| | 10 | | void SetupEyebrows(Material material, Texture texture, Color color); |
| | 11 | | void SetupMouth(Material material, Texture texture, Texture mask, Color color); |
| | 12 | | void SetFacialFeaturesVisible(bool visible, bool updateVisibility = false); |
| | 13 | | } |
| | 14 | |
|
| | 15 | | public class BodyShapeController : WearableController, IBodyShapeController |
| | 16 | | { |
| 0 | 17 | | public string bodyShapeId => wearable.id; |
| | 18 | | private Transform animationTarget; |
| 0 | 19 | | private bool facialFeaturesVisible = true; |
| 0 | 20 | | private HashSet<string> currentHiddenList = new HashSet<string>(); |
| | 21 | | public Transform[] bones; |
| | 22 | | public Transform rootBone; |
| | 23 | |
|
| 0 | 24 | | private bool lowerBodyActive = true; |
| 0 | 25 | | private bool upperBodyActive = true; |
| 0 | 26 | | private bool feetActive = true; |
| | 27 | |
|
| 0 | 28 | | public BodyShapeController(WearableItem wearableItem) : base(wearableItem) { } |
| | 29 | |
|
| 0 | 30 | | protected BodyShapeController(BodyShapeController original) : base(original) |
| | 31 | | { |
| 0 | 32 | | headRenderer = original.headRenderer; |
| 0 | 33 | | eyebrowsRenderer = original.eyebrowsRenderer; |
| 0 | 34 | | eyesRenderer = original.eyesRenderer; |
| 0 | 35 | | mouthRenderer = original.mouthRenderer; |
| 0 | 36 | | feetRenderer = original.feetRenderer; |
| 0 | 37 | | upperBodyRenderer = original.upperBodyRenderer; |
| 0 | 38 | | lowerBodyRenderer = original.lowerBodyRenderer; |
| 0 | 39 | | bones = original.bones; |
| 0 | 40 | | rootBone = original.rootBone; |
| 0 | 41 | | upperBodyActive = original.upperBodyActive; |
| 0 | 42 | | lowerBodyActive = original.lowerBodyActive; |
| 0 | 43 | | feetActive = original.feetActive; |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | public override void Load(string bodyShapeId, Transform parent, Action<WearableController> onSuccess, Action<Wearabl |
| | 47 | | { |
| 0 | 48 | | animationTarget = parent; |
| 0 | 49 | | base.Load(bodyShapeId, parent, onSuccess, onFail); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | public void SetActiveParts(bool lowerBodyActive, bool upperBodyActive, bool feetActive) |
| | 53 | | { |
| 0 | 54 | | this.lowerBodyActive = lowerBodyActive; |
| 0 | 55 | | this.upperBodyActive = upperBodyActive; |
| 0 | 56 | | this.feetActive = feetActive; |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | public void SetupEyes(Material material, Texture texture, Texture mask, Color color) |
| | 60 | | { |
| 0 | 61 | | if (assetContainer != null && assetContainer.transform == null) |
| | 62 | | { |
| 0 | 63 | | Debug.LogWarning("Tried to setup eyes when the asset not ready"); |
| 0 | 64 | | return; |
| | 65 | | } |
| | 66 | |
|
| 0 | 67 | | material.SetTexture(AvatarUtils._EyesTexture, texture); |
| 0 | 68 | | material.SetTexture(AvatarUtils._IrisMask, mask); |
| 0 | 69 | | material.SetColor(AvatarUtils._EyeTint, color); |
| | 70 | |
|
| 0 | 71 | | AvatarUtils.MapSharedMaterialsRecursively(assetContainer.transform, |
| 0 | 72 | | (mat) => material, |
| | 73 | | "eyes"); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | public void SetupEyebrows(Material material, Texture texture, Color color) |
| | 77 | | { |
| 0 | 78 | | if (assetContainer != null && assetContainer.transform == null) |
| | 79 | | { |
| 0 | 80 | | Debug.LogWarning("Tried to setup eyebrows when the asset not ready"); |
| 0 | 81 | | return; |
| | 82 | | } |
| | 83 | |
|
| 0 | 84 | | material.SetTexture(AvatarUtils._BaseMap, texture); |
| | 85 | |
|
| | 86 | | //NOTE(Brian): This isn't an error, we must also apply hair color to this mat |
| 0 | 87 | | material.SetColor(AvatarUtils._BaseColor, color); |
| | 88 | |
|
| 0 | 89 | | AvatarUtils.MapSharedMaterialsRecursively(assetContainer.transform, |
| 0 | 90 | | (mat) => material, |
| | 91 | | "eyebrows"); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | public void SetupMouth(Material material, Texture texture, Texture mask, Color color) |
| | 95 | | { |
| 0 | 96 | | if (assetContainer != null && assetContainer.transform == null) |
| | 97 | | { |
| 0 | 98 | | Debug.LogWarning("Tried to setup mouth when the asset not ready"); |
| 0 | 99 | | return; |
| | 100 | | } |
| | 101 | |
|
| 0 | 102 | | material.SetTexture(AvatarUtils._BaseMap, texture); |
| 0 | 103 | | material.SetTexture(AvatarUtils._TintMask, mask); |
| | 104 | |
|
| | 105 | | //NOTE(Brian): This isn't an error, we must also apply skin color to this mat |
| 0 | 106 | | material.SetColor(AvatarUtils._BaseColor, color); |
| | 107 | |
|
| 0 | 108 | | AvatarUtils.MapSharedMaterialsRecursively(assetContainer.transform, |
| 0 | 109 | | (mat) => material, |
| | 110 | | "mouth"); |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | public void SetFacialFeaturesVisible(bool visible, bool updateVisibility = false) |
| | 114 | | { |
| 0 | 115 | | facialFeaturesVisible = visible; |
| | 116 | |
|
| 0 | 117 | | if ( updateVisibility ) |
| 0 | 118 | | RefreshFacialFeaturesVisibility(); |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | void RefreshFacialFeaturesVisibility() |
| | 122 | | { |
| 0 | 123 | | bool headIsVisible = !currentHiddenList.Contains(WearableLiterals.Misc.HEAD); |
| 0 | 124 | | eyebrowsRenderer.enabled = headIsVisible && facialFeaturesVisible; |
| 0 | 125 | | eyesRenderer.enabled = headIsVisible && facialFeaturesVisible; |
| 0 | 126 | | mouthRenderer.enabled = headIsVisible && facialFeaturesVisible; |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | protected override void PrepareWearable(GameObject assetContainer) |
| | 130 | | { |
| 0 | 131 | | Animation animation = null; |
| | 132 | |
|
| | 133 | | //NOTE(Brian): Fix to support hierarchy difference between AssetBundle and GLTF wearables. |
| 0 | 134 | | Utils.ForwardTransformChildTraversal<Transform>((x) => |
| | 135 | | { |
| 0 | 136 | | if (x.name.Contains("Armature")) |
| | 137 | | { |
| 0 | 138 | | var armatureGO = x.parent; |
| 0 | 139 | | animation = armatureGO.gameObject.GetOrCreateComponent<Animation>(); |
| 0 | 140 | | armatureGO.gameObject.GetOrCreateComponent<StickerAnimationListener>(); |
| 0 | 141 | | return false; //NOTE(Brian): If we return false the traversal is stopped. |
| | 142 | | } |
| | 143 | |
|
| 0 | 144 | | return true; |
| | 145 | | }, |
| | 146 | | assetContainer.transform); |
| | 147 | |
|
| | 148 | | // BasedOnRenderers only acts on child renderers. As all child renderers are deactivated, this feature doesn't w |
| | 149 | | // |
| | 150 | | // Adding the merged avatar as child require toggling the animation enabled state on/off and is delayed by a fra |
| | 151 | | // So the avatar can be seen as gigantic in this lapse. |
| | 152 | | // |
| | 153 | | // TODO(Brian): Find a better way to cull animations |
| 0 | 154 | | animation.cullingType = AnimationCullingType.AlwaysAnimate; |
| | 155 | |
|
| 0 | 156 | | SkinnedMeshRenderer firstRenderer = null; |
| 0 | 157 | | foreach (var r in GetRenderers()) |
| | 158 | | { |
| 0 | 159 | | string parentName = r.transform.parent.name.ToLower(); |
| | 160 | |
|
| 0 | 161 | | if ( firstRenderer == null ) |
| 0 | 162 | | firstRenderer = r; |
| | 163 | |
|
| 0 | 164 | | if (parentName.Contains("ubody")) |
| 0 | 165 | | upperBodyRenderer = r; |
| 0 | 166 | | else if (parentName.Contains("lbody")) |
| 0 | 167 | | lowerBodyRenderer = r; |
| 0 | 168 | | else if (parentName.Contains("feet")) |
| 0 | 169 | | feetRenderer = r; |
| 0 | 170 | | else if (parentName.Contains("head")) |
| 0 | 171 | | headRenderer = r; |
| 0 | 172 | | else if (parentName.Contains("eyebrows")) |
| 0 | 173 | | eyebrowsRenderer = r; |
| 0 | 174 | | else if (parentName.Contains("eyes")) |
| 0 | 175 | | eyesRenderer = r; |
| 0 | 176 | | else if (parentName.Contains("mouth")) |
| 0 | 177 | | mouthRenderer = r; |
| | 178 | | } |
| | 179 | |
|
| 0 | 180 | | if ( firstRenderer != null ) |
| | 181 | | { |
| 0 | 182 | | bones = firstRenderer.bones; |
| 0 | 183 | | rootBone = firstRenderer.rootBone; |
| | 184 | | } |
| | 185 | |
|
| 0 | 186 | | var animator = animationTarget.GetComponent<AvatarAnimatorLegacy>(); |
| 0 | 187 | | animator.BindBodyShape(animation, bodyShapeId, animationTarget); |
| | 188 | |
|
| 0 | 189 | | InitializeAvatarAudioHandlers(assetContainer, animation); |
| 0 | 190 | | } |
| | 191 | |
|
| 0 | 192 | | public SkinnedMeshRenderer headRenderer { get; private set; } |
| 0 | 193 | | public SkinnedMeshRenderer eyebrowsRenderer { get; private set; } |
| 0 | 194 | | public SkinnedMeshRenderer eyesRenderer { get; private set; } |
| 0 | 195 | | public SkinnedMeshRenderer mouthRenderer { get; private set; } |
| 0 | 196 | | public SkinnedMeshRenderer feetRenderer { get; private set; } |
| 0 | 197 | | public SkinnedMeshRenderer upperBodyRenderer { get; private set; } |
| 0 | 198 | | public SkinnedMeshRenderer lowerBodyRenderer { get; private set; } |
| | 199 | |
|
| | 200 | | public override void UpdateVisibility(HashSet<string> hiddenList) |
| | 201 | | { |
| 0 | 202 | | currentHiddenList = hiddenList; |
| | 203 | |
|
| 0 | 204 | | bool headIsVisible = !currentHiddenList.Contains(WearableLiterals.Misc.HEAD); |
| | 205 | |
|
| 0 | 206 | | headRenderer.enabled = headIsVisible; |
| | 207 | |
|
| 0 | 208 | | RefreshFacialFeaturesVisibility(); |
| | 209 | |
|
| 0 | 210 | | feetRenderer.enabled = feetActive && !currentHiddenList.Contains(WearableLiterals.Categories.FEET); |
| 0 | 211 | | upperBodyRenderer.enabled = upperBodyActive && !currentHiddenList.Contains(WearableLiterals.Categories.UPPER_BOD |
| 0 | 212 | | lowerBodyRenderer.enabled = lowerBodyActive && !currentHiddenList.Contains(WearableLiterals.Categories.LOWER_BOD |
| 0 | 213 | | } |
| | 214 | |
|
| | 215 | | private void InitializeAvatarAudioHandlers(GameObject container, Animation createdAnimation) |
| | 216 | | { |
| | 217 | | //NOTE(Mordi): Adds audio handler for animation events, and passes in the audioContainer for the avatar |
| 0 | 218 | | AvatarAnimationEventAudioHandler animationEventAudioHandler = createdAnimation.gameObject.AddComponent<AvatarAni |
| 0 | 219 | | AudioContainer audioContainer = container.transform.parent.parent.GetComponentInChildren<AudioContainer>(); |
| 0 | 220 | | if (audioContainer != null) |
| | 221 | | { |
| 0 | 222 | | animationEventAudioHandler.Init(audioContainer); |
| | 223 | |
|
| | 224 | | //NOTE(Mordi): If this is a remote avatar, pass the animation component so we can keep track of whether it i |
| 0 | 225 | | AvatarAudioHandlerRemote audioHandlerRemote = audioContainer.GetComponent<AvatarAudioHandlerRemote>(); |
| 0 | 226 | | if (audioHandlerRemote != null) |
| | 227 | | { |
| 0 | 228 | | audioHandlerRemote.Init(createdAnimation.gameObject); |
| | 229 | | } |
| | 230 | | } |
| 0 | 231 | | } |
| | 232 | |
|
| | 233 | | public override void CleanUp() |
| | 234 | | { |
| 0 | 235 | | facialFeaturesVisible = true; |
| 0 | 236 | | base.CleanUp(); |
| 0 | 237 | | } |
| | 238 | | } |