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