| | 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 | | } |
| | 13 | |
|
| | 14 | | public class BodyShapeController : WearableController, IBodyShapeController |
| | 15 | | { |
| 0 | 16 | | public string bodyShapeId => wearable.id; |
| | 17 | | private Transform animationTarget; |
| | 18 | |
|
| 0 | 19 | | public BodyShapeController(WearableItem wearableItem) : base(wearableItem) { } |
| | 20 | |
|
| 0 | 21 | | protected BodyShapeController(BodyShapeController original) : base(original) |
| | 22 | | { |
| 0 | 23 | | headRenderer = original.headRenderer; |
| 0 | 24 | | eyebrowsRenderer = original.eyebrowsRenderer; |
| 0 | 25 | | eyesRenderer = original.eyesRenderer; |
| 0 | 26 | | mouthRenderer = original.mouthRenderer; |
| 0 | 27 | | feetRenderer = original.feetRenderer; |
| 0 | 28 | | upperBodyRenderer = original.upperBodyRenderer; |
| 0 | 29 | | lowerBodyRenderer = original.lowerBodyRenderer; |
| 0 | 30 | | } |
| | 31 | |
|
| 0 | 32 | | public SkinnedMeshRenderer skinnedMeshRenderer { get; private set; } |
| | 33 | |
|
| | 34 | | public override void Load(string bodyShapeId, Transform parent, Action<WearableController> onSuccess, Action<Wearabl |
| | 35 | | { |
| 0 | 36 | | animationTarget = parent; |
| 0 | 37 | | base.Load(bodyShapeId, parent, onSuccess, onFail); |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | public void SetActiveParts(bool lowerBodyActive, bool upperBodyActive, bool feetActive) |
| | 41 | | { |
| 0 | 42 | | lowerBodyRenderer.gameObject.SetActive(lowerBodyActive); |
| 0 | 43 | | lowerBodyRenderer.enabled = lowerBodyActive; |
| | 44 | |
|
| 0 | 45 | | upperBodyRenderer.gameObject.SetActive(upperBodyActive); |
| 0 | 46 | | upperBodyRenderer.enabled = upperBodyActive; |
| | 47 | |
|
| 0 | 48 | | feetRenderer.gameObject.SetActive(feetActive); |
| 0 | 49 | | feetRenderer.enabled = feetActive; |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | public void SetupEyes(Material material, Texture texture, Texture mask, Color color) |
| | 53 | | { |
| 0 | 54 | | if (assetContainer?.transform == null) |
| | 55 | | { |
| 0 | 56 | | Debug.LogWarning("Tried to setup eyes when the asset not ready"); |
| 0 | 57 | | return; |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | AvatarUtils.MapSharedMaterialsRecursively(assetContainer.transform, |
| | 61 | | (mat) => |
| | 62 | | { |
| 0 | 63 | | material.SetTexture(AvatarUtils._EyesTexture, texture); |
| 0 | 64 | | material.SetTexture(AvatarUtils._IrisMask, mask); |
| 0 | 65 | | material.SetColor(AvatarUtils._EyeTint, color); |
| 0 | 66 | | return material; |
| | 67 | | }, |
| | 68 | | "eyes"); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public void SetupEyebrows(Material material, Texture texture, Color color) |
| | 72 | | { |
| 0 | 73 | | if (assetContainer?.transform == null) |
| | 74 | | { |
| 0 | 75 | | Debug.LogWarning("Tried to setup eyebrows when the asset not ready"); |
| 0 | 76 | | return; |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | AvatarUtils.MapSharedMaterialsRecursively(assetContainer.transform, |
| | 80 | | (mat) => |
| | 81 | | { |
| 0 | 82 | | material.SetTexture(AvatarUtils._BaseMap, texture); |
| | 83 | |
|
| | 84 | | //NOTE(Brian): This isn't an error, we must also apply hair color to this mat |
| 0 | 85 | | material.SetColor(AvatarUtils._BaseColor, color); |
| | 86 | |
|
| 0 | 87 | | return material; |
| | 88 | | }, |
| | 89 | | "eyebrows"); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | public override void SetAssetRenderersEnabled(bool active) |
| | 93 | | { |
| 0 | 94 | | base.SetAssetRenderersEnabled(active); |
| 0 | 95 | | if (skinnedMeshRenderer != null) |
| 0 | 96 | | skinnedMeshRenderer.enabled = true; |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | public void SetupMouth(Material material, Texture texture, Texture mask, Color color) |
| | 100 | | { |
| 0 | 101 | | if (assetContainer?.transform == null) |
| | 102 | | { |
| 0 | 103 | | Debug.LogWarning("Tried to setup mouth when the asset not ready"); |
| 0 | 104 | | return; |
| | 105 | | } |
| | 106 | |
|
| 0 | 107 | | AvatarUtils.MapSharedMaterialsRecursively(assetContainer.transform, |
| | 108 | | (mat) => |
| | 109 | | { |
| 0 | 110 | | material.SetTexture(AvatarUtils._BaseMap, texture); |
| 0 | 111 | | material.SetTexture(AvatarUtils._TintMask, mask); |
| | 112 | |
|
| | 113 | | //NOTE(Brian): This isn't an error, we must also apply skin color to this mat |
| 0 | 114 | | material.SetColor(AvatarUtils._BaseColor, color); |
| 0 | 115 | | return material; |
| | 116 | | }, |
| | 117 | | "mouth"); |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | protected override void PrepareWearable(GameObject assetContainer) |
| | 121 | | { |
| 0 | 122 | | Animation animation = null; |
| | 123 | |
|
| | 124 | | //NOTE(Brian): Fix to support hierarchy difference between AssetBundle and GLTF wearables. |
| 0 | 125 | | Utils.ForwardTransformChildTraversal<Transform>((x) => |
| | 126 | | { |
| 0 | 127 | | if (x.name.Contains("Armature")) |
| | 128 | | { |
| 0 | 129 | | var armatureGO = x.parent; |
| 0 | 130 | | animation = armatureGO.gameObject.GetOrCreateComponent<Animation>(); |
| 0 | 131 | | armatureGO.gameObject.GetOrCreateComponent<StickerAnimationListener>(); |
| 0 | 132 | | return false; //NOTE(Brian): If we return false the traversal is stopped. |
| | 133 | | } |
| | 134 | |
|
| 0 | 135 | | return true; |
| | 136 | | }, |
| | 137 | | assetContainer.transform); |
| | 138 | |
|
| 0 | 139 | | animation.cullingType = AnimationCullingType.BasedOnRenderers; |
| | 140 | |
|
| | 141 | | //We create a mock SkinnedMeshRenderer to hold the bones for the animations, |
| | 142 | | //since any of the others SkinnedMeshRenderers in the bodyshape can be disabled arbitrarily |
| 0 | 143 | | SkinnedMeshRenderer[] skinnedMeshRenderersInChild = assetContainer.GetComponentsInChildren<SkinnedMeshRenderer>( |
| 0 | 144 | | skinnedMeshRenderer = animation.gameObject.GetOrCreateComponent<SkinnedMeshRenderer>(); |
| 0 | 145 | | skinnedMeshRenderer.enabled = true; |
| 0 | 146 | | foreach (SkinnedMeshRenderer meshRenderer in skinnedMeshRenderersInChild) |
| | 147 | | { |
| 0 | 148 | | if (skinnedMeshRenderer != meshRenderer) |
| | 149 | | { |
| 0 | 150 | | skinnedMeshRenderer.rootBone = meshRenderer.rootBone; |
| 0 | 151 | | skinnedMeshRenderer.bones = meshRenderer.bones; |
| 0 | 152 | | break; |
| | 153 | | } |
| | 154 | | } |
| | 155 | |
|
| 0 | 156 | | var animator = animationTarget.GetComponent<AvatarAnimatorLegacy>(); |
| 0 | 157 | | animator.BindBodyShape(animation, bodyShapeId, animationTarget); |
| | 158 | |
|
| 0 | 159 | | var allRenderers = assetContainer.GetComponentsInChildren<SkinnedMeshRenderer>(true); |
| | 160 | |
|
| 0 | 161 | | foreach (var r in allRenderers) |
| | 162 | | { |
| 0 | 163 | | string parentName = r.transform.parent.name.ToLower(); |
| | 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 | | InitializeAvatarAudioHandlers(assetContainer, animation); |
| 0 | 182 | | } |
| | 183 | |
|
| 0 | 184 | | public SkinnedMeshRenderer headRenderer { get; private set; } |
| 0 | 185 | | public SkinnedMeshRenderer eyebrowsRenderer { get; private set; } |
| 0 | 186 | | public SkinnedMeshRenderer eyesRenderer { get; private set; } |
| 0 | 187 | | public SkinnedMeshRenderer mouthRenderer { get; private set; } |
| 0 | 188 | | public SkinnedMeshRenderer feetRenderer { get; private set; } |
| 0 | 189 | | public SkinnedMeshRenderer upperBodyRenderer { get; private set; } |
| 0 | 190 | | public SkinnedMeshRenderer lowerBodyRenderer { get; private set; } |
| | 191 | |
|
| | 192 | | public override void UpdateVisibility(HashSet<string> hiddenList) |
| | 193 | | { |
| 0 | 194 | | bool headIsVisible = !hiddenList.Contains(WearableLiterals.Misc.HEAD); |
| | 195 | |
|
| 0 | 196 | | headRenderer.enabled = headIsVisible; |
| 0 | 197 | | eyebrowsRenderer.enabled = headIsVisible; |
| 0 | 198 | | eyesRenderer.enabled = headIsVisible; |
| 0 | 199 | | mouthRenderer.enabled = headIsVisible; |
| | 200 | |
|
| 0 | 201 | | feetRenderer.enabled = !hiddenList.Contains(WearableLiterals.Categories.FEET); |
| 0 | 202 | | upperBodyRenderer.enabled = !hiddenList.Contains(WearableLiterals.Categories.UPPER_BODY); |
| 0 | 203 | | lowerBodyRenderer.enabled = !hiddenList.Contains(WearableLiterals.Categories.LOWER_BODY); |
| 0 | 204 | | } |
| | 205 | |
|
| | 206 | | private void InitializeAvatarAudioHandlers(GameObject container, Animation createdAnimation) |
| | 207 | | { |
| | 208 | | //NOTE(Mordi): Adds audio handler for animation events, and passes in the audioContainer for the avatar |
| 0 | 209 | | AvatarAnimationEventAudioHandler animationEventAudioHandler = createdAnimation.gameObject.AddComponent<AvatarAni |
| 0 | 210 | | AudioContainer audioContainer = container.transform.parent.parent.GetComponentInChildren<AudioContainer>(); |
| 0 | 211 | | if (audioContainer != null) |
| | 212 | | { |
| 0 | 213 | | animationEventAudioHandler.Init(audioContainer); |
| | 214 | |
|
| | 215 | | //NOTE(Mordi): If this is a remote avatar, pass the animation component so we can keep track of whether it i |
| 0 | 216 | | AvatarAudioHandlerRemote audioHandlerRemote = audioContainer.GetComponent<AvatarAudioHandlerRemote>(); |
| 0 | 217 | | if (audioHandlerRemote != null) |
| | 218 | | { |
| 0 | 219 | | audioHandlerRemote.Init(createdAnimation.gameObject); |
| | 220 | | } |
| | 221 | | } |
| 0 | 222 | | } |
| | 223 | | } |