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