| | 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; |
| 0 | 18 | | private BaseHashSet<string> visibleNames => DataStore.i.avatarsLOD.visibleNames; |
| | 19 | | private Vector3 cameraPosition; |
| | 20 | | private Vector3 cameraForward; |
| | 21 | | private GPUSkinningThrottlingCurveSO gpuSkinningThrottlingCurve; |
| | 22 | |
|
| 685 | 23 | | internal readonly Dictionary<string, IAvatarLODController> lodControllers = new Dictionary<string, IAvatarLODCon |
| | 24 | | internal bool enabled; |
| | 25 | |
|
| 685 | 26 | | public AvatarsLODController() |
| | 27 | | { |
| 685 | 28 | | gpuSkinningThrottlingCurve = Resources.Load<GPUSkinningThrottlingCurveSO>("GPUSkinningThrottlingCurve"); |
| 685 | 29 | | KernelConfig.i.EnsureConfigInitialized() |
| | 30 | | .Then(config => |
| | 31 | | { |
| 685 | 32 | | KernelConfig.i.OnChange += OnKernelConfigChanged; |
| 685 | 33 | | OnKernelConfigChanged(config, null); |
| 685 | 34 | | }); |
| 685 | 35 | | } |
| | 36 | |
|
| | 37 | | private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) |
| | 38 | | { |
| 2650 | 39 | | if (enabled == current.features.enableAvatarLODs) |
| 2634 | 40 | | return; |
| 16 | 41 | | Initialize(current); |
| 16 | 42 | | } |
| | 43 | |
|
| | 44 | | internal void Initialize(KernelConfigModel config) |
| | 45 | | { |
| 20 | 46 | | enabled = config.features.enableAvatarLODs; |
| 20 | 47 | | if (!enabled) |
| 16 | 48 | | return; |
| | 49 | |
|
| 8 | 50 | | foreach (IAvatarLODController lodController in lodControllers.Values) |
| | 51 | | { |
| 0 | 52 | | lodController.Dispose(); |
| | 53 | | } |
| 4 | 54 | | lodControllers.Clear(); |
| | 55 | |
|
| 12 | 56 | | foreach (var keyValuePair in otherPlayers.Get()) |
| | 57 | | { |
| 2 | 58 | | RegisterAvatar(keyValuePair.Key, keyValuePair.Value); |
| | 59 | | } |
| 4 | 60 | | otherPlayers.OnAdded += RegisterAvatar; |
| 4 | 61 | | otherPlayers.OnRemoved += UnregisterAvatar; |
| 4 | 62 | | } |
| | 63 | |
|
| | 64 | | public void RegisterAvatar(string id, Player player) |
| | 65 | | { |
| 5 | 66 | | if (!enabled || lodControllers.ContainsKey(id)) |
| 1 | 67 | | return; |
| | 68 | |
|
| 4 | 69 | | lodControllers.Add(id, CreateLodController(player)); |
| 4 | 70 | | } |
| | 71 | |
|
| 0 | 72 | | protected internal virtual IAvatarLODController CreateLodController(Player player) => new AvatarLODController(pl |
| | 73 | |
|
| | 74 | | public void UnregisterAvatar(string id, Player player) |
| | 75 | | { |
| 3 | 76 | | if (!lodControllers.ContainsKey(id)) |
| 1 | 77 | | return; |
| | 78 | |
|
| 2 | 79 | | lodControllers[id].Dispose(); |
| 2 | 80 | | lodControllers.Remove(id); |
| 2 | 81 | | } |
| | 82 | |
|
| | 83 | | public void Update() |
| | 84 | | { |
| 16993 | 85 | | if (!enabled) |
| 16985 | 86 | | return; |
| | 87 | |
|
| 8 | 88 | | cameraPosition = CommonScriptableObjects.cameraPosition.Get(); |
| 8 | 89 | | cameraForward = CommonScriptableObjects.cameraForward.Get(); |
| | 90 | |
|
| 8 | 91 | | UpdateAllLODs(maxAvatars.Get(), maxImpostors.Get()); |
| 8 | 92 | | UpdateLODsBillboard(); |
| 8 | 93 | | } |
| | 94 | |
|
| | 95 | | internal void UpdateLODsBillboard() |
| | 96 | | { |
| 70 | 97 | | foreach (var kvp in lodControllers) |
| | 98 | | { |
| 27 | 99 | | Player player = kvp.Value.player; |
| | 100 | |
|
| 27 | 101 | | if (!IsInFrontOfCamera(player.worldPosition)) |
| | 102 | | continue; |
| | 103 | |
|
| 25 | 104 | | Vector3 previousForward = player.forwardDirection; |
| 25 | 105 | | Vector3 lookAtDir = (cameraPosition - player.worldPosition).normalized; |
| | 106 | |
|
| 25 | 107 | | lookAtDir.y = previousForward.y; |
| 25 | 108 | | player.renderer.SetImpostorForward(lookAtDir); |
| | 109 | | } |
| 8 | 110 | | } |
| | 111 | |
|
| | 112 | | internal void UpdateAllLODs(int maxAvatars = DataStore.DataStore_AvatarsLOD.DEFAULT_MAX_AVATAR, int maxImpostors |
| | 113 | | { |
| 10 | 114 | | int avatarsCount = 0; //Full Avatar + Simple Avatar |
| 10 | 115 | | int impostorCount = 0; //Impostor |
| | 116 | |
|
| 10 | 117 | | float lodDistance = LODDistance.Get(); |
| 10 | 118 | | float simpleAvatarDistance = this.simpleAvatarDistance.Get(); |
| 10 | 119 | | Vector3 ownPlayerPosition = CommonScriptableObjects.playerUnityPosition.Get(); |
| | 120 | |
|
| 10 | 121 | | (IAvatarLODController lodController, float distance)[] lodControllersByDistance = ComposeLODControllersSorte |
| 82 | 122 | | for (int index = 0; index < lodControllersByDistance.Length; index++) |
| | 123 | | { |
| 31 | 124 | | (IAvatarLODController lodController, float distance) = lodControllersByDistance[index]; |
| 31 | 125 | | if (distance < 0) //Behind camera |
| | 126 | | { |
| 6 | 127 | | visibleNames.Remove(lodController.player.id); |
| 6 | 128 | | lodController.SetInvisible(); |
| 6 | 129 | | continue; |
| | 130 | | } |
| | 131 | |
|
| 25 | 132 | | if (avatarsCount < maxAvatars) |
| | 133 | | { |
| 17 | 134 | | lodController.SetThrottling((int)gpuSkinningThrottlingCurve.curve.Evaluate(distance)); |
| 17 | 135 | | if (distance < simpleAvatarDistance) |
| 10 | 136 | | lodController.SetFullAvatar(); |
| | 137 | | else |
| 7 | 138 | | lodController.SetSimpleAvatar(); |
| 17 | 139 | | avatarsCount++; |
| 17 | 140 | | visibleNames.Add(lodController.player.id); |
| 17 | 141 | | continue; |
| | 142 | | } |
| | 143 | |
|
| 8 | 144 | | visibleNames.Remove(lodController.player.id); |
| 8 | 145 | | if (impostorCount < maxImpostors) |
| | 146 | | { |
| 7 | 147 | | lodController.SetImpostor(); |
| 7 | 148 | | lodController.UpdateImpostorTint(distance); |
| 7 | 149 | | impostorCount++; |
| 7 | 150 | | continue; |
| | 151 | | } |
| | 152 | |
|
| 1 | 153 | | lodController.SetInvisible(); |
| | 154 | | } |
| 10 | 155 | | } |
| | 156 | |
|
| | 157 | | private (IAvatarLODController lodController, float distance)[] ComposeLODControllersSortedByDistance(IEnumerable |
| | 158 | | { |
| 41 | 159 | | (IAvatarLODController lodController, float distance)[] lodControllersWithDistance = lodControllers.Select(x |
| 40 | 160 | | Array.Sort(lodControllersWithDistance, (x, y) => x.distance.CompareTo(y.distance)); |
| 10 | 161 | | return lodControllersWithDistance; |
| | 162 | | } |
| | 163 | |
|
| | 164 | | /// <summary> |
| | 165 | | /// Returns -1 if player is not in front of camera or not found |
| | 166 | | /// </summary> |
| | 167 | | /// <param name="player"></param> |
| | 168 | | /// <returns></returns> |
| | 169 | | private float DistanceToOwnPlayer(Player player, Vector3 ownPlayerPosition) |
| | 170 | | { |
| 31 | 171 | | if (player == null || !IsInFrontOfCamera(player.worldPosition)) |
| 6 | 172 | | return -1; |
| | 173 | |
|
| 25 | 174 | | return Vector3.Distance(ownPlayerPosition, player.worldPosition); |
| | 175 | | } |
| | 176 | |
|
| 58 | 177 | | private bool IsInFrontOfCamera(Vector3 position) { return Vector3.Dot(cameraForward, (position - cameraPosition) |
| | 178 | |
|
| | 179 | | public void Dispose() |
| | 180 | | { |
| 705 | 181 | | KernelConfig.i.OnChange -= OnKernelConfigChanged; |
| 1480 | 182 | | foreach (IAvatarLODController lodController in lodControllers.Values) |
| | 183 | | { |
| 35 | 184 | | lodController.Dispose(); |
| | 185 | | } |
| | 186 | |
|
| 705 | 187 | | otherPlayers.OnAdded -= RegisterAvatar; |
| 705 | 188 | | otherPlayers.OnRemoved -= UnregisterAvatar; |
| 705 | 189 | | } |
| | 190 | | } |
| | 191 | | } |