| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public class AvatarsLODController : IAvatarsLODController |
| | 9 | | { |
| | 10 | | internal const string AVATAR_LODS_FLAG_NAME = "avatar_lods"; |
| | 11 | | internal const float RENDERED_DOT_PRODUCT_ANGLE = 0.25f; |
| | 12 | | internal const float AVATARS_INVISIBILITY_DISTANCE = 1.75f; |
| | 13 | | private const float MIN_DISTANCE_BETWEEN_NAMES_PIXELS = 70f; |
| | 14 | |
|
| 1304 | 15 | | private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers; |
| 9 | 16 | | private BaseVariable<float> simpleAvatarDistance => DataStore.i.avatarsLOD.simpleAvatarDistance; |
| 0 | 17 | | private BaseVariable<float> LODDistance => DataStore.i.avatarsLOD.LODDistance; |
| 8 | 18 | | private BaseVariable<int> maxAvatars => DataStore.i.avatarsLOD.maxAvatars; |
| 8 | 19 | | private BaseVariable<int> maxImpostors => DataStore.i.avatarsLOD.maxImpostors; |
| | 20 | | private Vector3 cameraPosition; |
| | 21 | | private Vector3 cameraForward; |
| | 22 | | private GPUSkinningThrottlingCurveSO gpuSkinningThrottlingCurve; |
| 646 | 23 | | private SimpleOverlappingTracker overlappingTracker = new SimpleOverlappingTracker(MIN_DISTANCE_BETWEEN_NAMES_PI |
| | 24 | |
|
| 646 | 25 | | internal readonly Dictionary<string, IAvatarLODController> lodControllers = new Dictionary<string, IAvatarLODCon |
| | 26 | | internal bool enabled; |
| | 27 | | private UnityEngine.Camera mainCamera; |
| | 28 | |
|
| 646 | 29 | | public AvatarsLODController () |
| | 30 | | { |
| 646 | 31 | | gpuSkinningThrottlingCurve = Resources.Load<GPUSkinningThrottlingCurveSO>("GPUSkinningThrottlingCurve"); |
| 646 | 32 | | DataStore.i.featureFlags.flags.OnChange += OnFeatureFlagChanged; |
| 646 | 33 | | } |
| | 34 | |
|
| | 35 | | public void Initialize() |
| | 36 | | { |
| 646 | 37 | | Environment.i.platform.updateEventHandler.AddListener(IUpdateEventHandler.EventType.Update, Update); |
| 646 | 38 | | } |
| | 39 | |
|
| | 40 | | private void OnFeatureFlagChanged(FeatureFlag current, FeatureFlag previous) |
| | 41 | | { |
| 0 | 42 | | if (enabled == current.IsFeatureEnabled(AVATAR_LODS_FLAG_NAME)) |
| 0 | 43 | | return; |
| | 44 | |
|
| 0 | 45 | | Initialize(current); |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | internal void Initialize(FeatureFlag current) |
| | 49 | | { |
| 4 | 50 | | enabled = current.IsFeatureEnabled(AVATAR_LODS_FLAG_NAME); |
| | 51 | |
|
| 4 | 52 | | if (!enabled) |
| 0 | 53 | | return; |
| | 54 | |
|
| 8 | 55 | | foreach (IAvatarLODController lodController in lodControllers.Values) |
| | 56 | | { |
| 0 | 57 | | lodController.Dispose(); |
| | 58 | | } |
| | 59 | |
|
| 4 | 60 | | lodControllers.Clear(); |
| | 61 | |
|
| 12 | 62 | | foreach (var keyValuePair in otherPlayers.Get()) |
| | 63 | | { |
| 2 | 64 | | RegisterAvatar(keyValuePair.Key, keyValuePair.Value); |
| | 65 | | } |
| | 66 | |
|
| 4 | 67 | | otherPlayers.OnAdded += RegisterAvatar; |
| 4 | 68 | | otherPlayers.OnRemoved += UnregisterAvatar; |
| 4 | 69 | | } |
| | 70 | |
|
| | 71 | | public void RegisterAvatar(string id, Player player) |
| | 72 | | { |
| 5 | 73 | | if (!enabled || lodControllers.ContainsKey(id)) |
| 1 | 74 | | return; |
| | 75 | |
|
| 4 | 76 | | lodControllers.Add(id, CreateLodController(player)); |
| 4 | 77 | | } |
| | 78 | |
|
| 0 | 79 | | protected internal virtual IAvatarLODController CreateLodController(Player player) => new AvatarLODController(pl |
| | 80 | |
|
| | 81 | | public void UnregisterAvatar(string id, Player player) |
| | 82 | | { |
| 3 | 83 | | if (!lodControllers.ContainsKey(id)) |
| 1 | 84 | | return; |
| | 85 | |
|
| 2 | 86 | | lodControllers[id].Dispose(); |
| 2 | 87 | | lodControllers.Remove(id); |
| 2 | 88 | | } |
| | 89 | |
|
| | 90 | | public void Update() |
| | 91 | | { |
| 6949 | 92 | | if (!enabled) |
| 6941 | 93 | | return; |
| | 94 | |
|
| 8 | 95 | | cameraPosition = CommonScriptableObjects.cameraPosition.Get(); |
| 8 | 96 | | cameraForward = CommonScriptableObjects.cameraForward.Get(); |
| | 97 | |
|
| 8 | 98 | | UpdateAllLODs(maxAvatars.Get(), maxImpostors.Get()); |
| 8 | 99 | | } |
| | 100 | |
|
| | 101 | | internal void UpdateAllLODs(int maxAvatars = DataStore_AvatarsLOD.DEFAULT_MAX_AVATAR, int maxImpostors = DataSto |
| | 102 | | { |
| 9 | 103 | | if (mainCamera == null) |
| 9 | 104 | | mainCamera = UnityEngine.Camera.main; |
| 9 | 105 | | int avatarsCount = 0; //Full Avatar + Simple Avatar |
| 9 | 106 | | int impostorCount = 0; //Impostor |
| | 107 | |
|
| 9 | 108 | | float simpleAvatarDistance = this.simpleAvatarDistance.Get(); |
| 9 | 109 | | Vector3 ownPlayerPosition = CommonScriptableObjects.playerUnityPosition.Get(); |
| | 110 | |
|
| 9 | 111 | | overlappingTracker.Reset(); |
| | 112 | |
|
| 9 | 113 | | (IAvatarLODController lodController, float distance)[] lodControllersByDistance = ComposeLODControllersSorte |
| | 114 | |
|
| 66 | 115 | | for (int index = 0; index < lodControllersByDistance.Length; index++) |
| | 116 | | { |
| 24 | 117 | | (IAvatarLODController lodController, float distance) = lodControllersByDistance[index]; |
| | 118 | |
|
| 24 | 119 | | if (IsInInvisibleDistance(distance)) |
| | 120 | | { |
| 4 | 121 | | lodController.SetNameVisible(false); |
| 4 | 122 | | lodController.SetInvisible(); |
| 4 | 123 | | continue; |
| | 124 | | } |
| | 125 | |
|
| 20 | 126 | | if (avatarsCount < maxAvatars) |
| | 127 | | { |
| 13 | 128 | | lodController.SetAnimationThrottling((int)gpuSkinningThrottlingCurve.curve.Evaluate(distance)); |
| 13 | 129 | | if (distance < simpleAvatarDistance) |
| 9 | 130 | | lodController.SetLOD0(); |
| | 131 | | else |
| 4 | 132 | | lodController.SetLOD1(); |
| 13 | 133 | | avatarsCount++; |
| | 134 | |
|
| 13 | 135 | | if (mainCamera == null) |
| 13 | 136 | | lodController.SetNameVisible(true); |
| | 137 | | else |
| 0 | 138 | | lodController.SetNameVisible(overlappingTracker.RegisterPosition(lodController.player.playerName |
| | 139 | |
|
| 0 | 140 | | continue; |
| | 141 | | } |
| | 142 | |
|
| 7 | 143 | | lodController.SetNameVisible(false); |
| 7 | 144 | | if (impostorCount < maxImpostors) |
| | 145 | | { |
| 6 | 146 | | lodController.SetLOD2(); |
| 6 | 147 | | lodController.UpdateImpostorTint(distance); |
| 6 | 148 | | impostorCount++; |
| 6 | 149 | | continue; |
| | 150 | | } |
| | 151 | |
|
| 1 | 152 | | lodController.SetInvisible(); |
| | 153 | | } |
| 9 | 154 | | } |
| | 155 | |
|
| | 156 | | private bool IsInInvisibleDistance(float distance) |
| | 157 | | { |
| 24 | 158 | | bool firstPersonCamera = CommonScriptableObjects.cameraMode.Get() == CameraMode.ModeId.FirstPerson; |
| | 159 | |
|
| 24 | 160 | | return firstPersonCamera ? distance < AVATARS_INVISIBILITY_DISTANCE : distance < 0f; // < 0 is behind camera |
| | 161 | | } |
| | 162 | |
|
| | 163 | | private (IAvatarLODController lodController, float distance)[] ComposeLODControllersSortedByDistance(IEnumerable |
| | 164 | | { |
| 33 | 165 | | (IAvatarLODController lodController, float distance)[] lodControllersWithDistance = lodControllers.Select(x |
| 30 | 166 | | Array.Sort(lodControllersWithDistance, (x, y) => x.distance.CompareTo(y.distance)); |
| 9 | 167 | | return lodControllersWithDistance; |
| | 168 | | } |
| | 169 | |
|
| | 170 | | /// <summary> |
| | 171 | | /// Returns -1 if player is not in front of camera or not found |
| | 172 | | /// </summary> |
| | 173 | | /// <param name="player"></param> |
| | 174 | | /// <returns></returns> |
| | 175 | | private float DistanceToOwnPlayer(Player player, Vector3 ownPlayerPosition) |
| | 176 | | { |
| 24 | 177 | | if (player == null || !IsInFrontOfCamera(player.worldPosition)) |
| 3 | 178 | | return -1; |
| | 179 | |
|
| 21 | 180 | | return Vector3.Distance(ownPlayerPosition, player.worldPosition); |
| | 181 | | } |
| | 182 | |
|
| 24 | 183 | | private bool IsInFrontOfCamera(Vector3 position) { return Vector3.Dot(cameraForward, (position - cameraPosition) |
| | 184 | |
|
| | 185 | | public void Dispose() |
| | 186 | | { |
| 646 | 187 | | DataStore.i.featureFlags.flags.OnChange -= OnFeatureFlagChanged; |
| 1346 | 188 | | foreach (IAvatarLODController lodController in lodControllers.Values) |
| | 189 | | { |
| 27 | 190 | | lodController.Dispose(); |
| | 191 | | } |
| | 192 | |
|
| 646 | 193 | | otherPlayers.OnAdded -= RegisterAvatar; |
| 646 | 194 | | otherPlayers.OnRemoved -= UnregisterAvatar; |
| 646 | 195 | | DCL.Environment.i.platform.updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.Update, Update); |
| 646 | 196 | | } |
| | 197 | | } |
| | 198 | | } |