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