| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | | using DCL.Interface; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class AvatarsLODController : IAvatarsLODController |
| | 10 | | { |
| | 11 | | internal const float RENDERED_DOT_PRODUCT_ANGLE = 0.25f; |
| | 12 | |
|
| 0 | 13 | | private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers; |
| 0 | 14 | | private BaseVariable<float> simpleAvatarDistance => DataStore.i.avatarsLOD.simpleAvatarDistance; |
| 0 | 15 | | private BaseVariable<float> LODDistance => DataStore.i.avatarsLOD.LODDistance; |
| 0 | 16 | | private BaseVariable<int> maxAvatars => DataStore.i.avatarsLOD.maxAvatars; |
| 0 | 17 | | private BaseVariable<int> maxImpostors => DataStore.i.avatarsLOD.maxImpostors; |
| | 18 | | private Vector3 cameraPosition; |
| | 19 | | private Vector3 cameraForward; |
| | 20 | |
|
| 684 | 21 | | internal readonly Dictionary<string, IAvatarLODController> lodControllers = new Dictionary<string, IAvatarLODCon |
| | 22 | | internal bool enabled; |
| | 23 | |
|
| 684 | 24 | | public AvatarsLODController() |
| | 25 | | { |
| 684 | 26 | | KernelConfig.i.EnsureConfigInitialized() |
| | 27 | | .Then(config => |
| | 28 | | { |
| 684 | 29 | | KernelConfig.i.OnChange += OnKernelConfigChanged; |
| 684 | 30 | | OnKernelConfigChanged(config, null); |
| 684 | 31 | | }); |
| 684 | 32 | | } |
| | 33 | |
|
| | 34 | | private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) |
| | 35 | | { |
| 2644 | 36 | | if (enabled == current.features.enableAvatarLODs) |
| 2629 | 37 | | return; |
| 15 | 38 | | Initialize(current); |
| 15 | 39 | | } |
| | 40 | |
|
| | 41 | | internal void Initialize(KernelConfigModel config) |
| | 42 | | { |
| 19 | 43 | | enabled = config.features.enableAvatarLODs; |
| 19 | 44 | | if (!enabled) |
| 15 | 45 | | return; |
| | 46 | |
|
| 8 | 47 | | foreach (IAvatarLODController lodController in lodControllers.Values) |
| | 48 | | { |
| 0 | 49 | | lodController.Dispose(); |
| | 50 | | } |
| 4 | 51 | | lodControllers.Clear(); |
| | 52 | |
|
| 12 | 53 | | foreach (var keyValuePair in otherPlayers.Get()) |
| | 54 | | { |
| 2 | 55 | | RegisterAvatar(keyValuePair.Key, keyValuePair.Value); |
| | 56 | | } |
| 4 | 57 | | otherPlayers.OnAdded += RegisterAvatar; |
| 4 | 58 | | otherPlayers.OnRemoved += UnregisterAvatar; |
| 4 | 59 | | } |
| | 60 | |
|
| | 61 | | public void RegisterAvatar(string id, Player player) |
| | 62 | | { |
| 5 | 63 | | if (!enabled || lodControllers.ContainsKey(id)) |
| 1 | 64 | | return; |
| | 65 | |
|
| 4 | 66 | | lodControllers.Add(id, CreateLodController(player)); |
| 4 | 67 | | } |
| | 68 | |
|
| 0 | 69 | | protected internal virtual IAvatarLODController CreateLodController(Player player) => new AvatarLODController(pl |
| | 70 | |
|
| | 71 | | public void UnregisterAvatar(string id, Player player) |
| | 72 | | { |
| 3 | 73 | | if (!lodControllers.ContainsKey(id)) |
| 1 | 74 | | return; |
| | 75 | |
|
| 2 | 76 | | lodControllers[id].Dispose(); |
| 2 | 77 | | lodControllers.Remove(id); |
| 2 | 78 | | } |
| | 79 | |
|
| | 80 | | public void Update() |
| | 81 | | { |
| 16972 | 82 | | if (!enabled) |
| 16965 | 83 | | return; |
| | 84 | |
|
| 7 | 85 | | cameraPosition = CommonScriptableObjects.cameraPosition.Get(); |
| 7 | 86 | | cameraForward = CommonScriptableObjects.cameraForward.Get(); |
| | 87 | |
|
| 7 | 88 | | UpdateAllLODs(maxAvatars.Get(), maxImpostors.Get()); |
| 7 | 89 | | UpdateLODsBillboard(); |
| 7 | 90 | | } |
| | 91 | |
|
| | 92 | | internal void UpdateLODsBillboard() |
| | 93 | | { |
| 58 | 94 | | foreach (var kvp in lodControllers) |
| | 95 | | { |
| 22 | 96 | | Player player = kvp.Value.player; |
| | 97 | |
|
| 22 | 98 | | if (!IsInFrontOfCamera(player.worldPosition)) |
| | 99 | | continue; |
| | 100 | |
|
| 21 | 101 | | Vector3 previousForward = player.forwardDirection; |
| 21 | 102 | | Vector3 lookAtDir = (cameraPosition - player.worldPosition).normalized; |
| | 103 | |
|
| 21 | 104 | | lookAtDir.y = previousForward.y; |
| 21 | 105 | | player.renderer.SetImpostorForward(lookAtDir); |
| | 106 | | } |
| 7 | 107 | | } |
| | 108 | |
|
| | 109 | | internal void UpdateAllLODs(int maxAvatars = DataStore.DataStore_AvatarsLOD.DEFAULT_MAX_AVATAR, int maxImpostors |
| | 110 | | { |
| 9 | 111 | | int avatarsCount = 0; //Full Avatar + Simple Avatar |
| 9 | 112 | | int impostorCount = 0; //Impostor |
| | 113 | |
|
| | 114 | | //Cache .Get() to boost performance. Also use squared values to boost distance comparison |
| 9 | 115 | | float lodDistance = LODDistance.Get() * LODDistance.Get(); |
| 9 | 116 | | float squaredSimpleAvatarDistance = simpleAvatarDistance.Get() * simpleAvatarDistance.Get(); |
| 9 | 117 | | Vector3 ownPlayerPosition = CommonScriptableObjects.playerUnityPosition.Get(); |
| | 118 | |
|
| 9 | 119 | | (IAvatarLODController lodController, float sqrDistance)[] lodControllersByDistance = ComposeLODControllersSo |
| 70 | 120 | | for (int index = 0; index < lodControllersByDistance.Length; index++) |
| | 121 | | { |
| 26 | 122 | | (IAvatarLODController lodController, float sqrtDistance) = lodControllersByDistance[index]; |
| 26 | 123 | | if (sqrtDistance < 0) //Behind camera |
| | 124 | | { |
| 5 | 125 | | lodController.SetInvisible(); |
| 5 | 126 | | continue; |
| | 127 | | } |
| | 128 | |
|
| | 129 | | //Nearby player |
| 21 | 130 | | if (sqrtDistance < lodDistance) |
| | 131 | | { |
| 15 | 132 | | if (avatarsCount < maxAvatars) |
| | 133 | | { |
| 12 | 134 | | if (sqrtDistance < squaredSimpleAvatarDistance) |
| 9 | 135 | | lodController.SetFullAvatar(); |
| | 136 | | else |
| 3 | 137 | | lodController.SetSimpleAvatar(); |
| 12 | 138 | | avatarsCount++; |
| 12 | 139 | | continue; |
| | 140 | | } |
| | 141 | |
|
| 3 | 142 | | lodController.SetInvisible(); |
| 3 | 143 | | continue; |
| | 144 | | } |
| | 145 | |
|
| 6 | 146 | | if (avatarsCount < maxAvatars) |
| | 147 | | { |
| 2 | 148 | | lodController.SetSimpleAvatar(); |
| 2 | 149 | | avatarsCount++; |
| 2 | 150 | | continue; |
| | 151 | | } |
| 4 | 152 | | if (impostorCount < maxImpostors) |
| | 153 | | { |
| 3 | 154 | | lodController.SetImpostor(); |
| | 155 | |
|
| 3 | 156 | | lodController.UpdateImpostorTint(Mathf.Sqrt(sqrtDistance)); |
| | 157 | |
|
| 3 | 158 | | impostorCount++; |
| 3 | 159 | | continue; |
| | 160 | | } |
| | 161 | |
|
| 1 | 162 | | lodController.SetInvisible(); |
| | 163 | | } |
| 9 | 164 | | } |
| | 165 | |
|
| | 166 | | private (IAvatarLODController lodController, float sqrDistance)[] ComposeLODControllersSortedByDistance(IEnumera |
| | 167 | | { |
| 35 | 168 | | (IAvatarLODController lodController, float sqrDistance)[] lodControllersWithDistance = lodControllers.Select |
| 32 | 169 | | Array.Sort(lodControllersWithDistance, (x, y) => x.sqrDistance.CompareTo(y.sqrDistance)); |
| 9 | 170 | | return lodControllersWithDistance; |
| | 171 | | } |
| | 172 | |
|
| | 173 | | /// <summary> |
| | 174 | | /// Returns -1 if player is not in front of camera or not found |
| | 175 | | /// </summary> |
| | 176 | | /// <param name="player"></param> |
| | 177 | | /// <returns></returns> |
| | 178 | | private float SqrDistanceToOwnPlayer(Player player, Vector3 ownPlayerPosition) |
| | 179 | | { |
| 26 | 180 | | if (player == null || !IsInFrontOfCamera(player.worldPosition)) |
| 5 | 181 | | return -1; |
| | 182 | |
|
| 21 | 183 | | return Vector3.SqrMagnitude(ownPlayerPosition - player.worldPosition); |
| | 184 | | } |
| | 185 | |
|
| 48 | 186 | | private bool IsInFrontOfCamera(Vector3 position) { return Vector3.Dot(cameraForward, (position - cameraPosition) |
| | 187 | |
|
| | 188 | | public void Dispose() |
| | 189 | | { |
| 704 | 190 | | KernelConfig.i.OnChange -= OnKernelConfigChanged; |
| 1468 | 191 | | foreach (IAvatarLODController lodController in lodControllers.Values) |
| | 192 | | { |
| 30 | 193 | | lodController.Dispose(); |
| | 194 | | } |
| | 195 | |
|
| 704 | 196 | | otherPlayers.OnAdded -= RegisterAvatar; |
| 704 | 197 | | otherPlayers.OnRemoved -= UnregisterAvatar; |
| 704 | 198 | | } |
| | 199 | | } |
| | 200 | | } |