| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Models; |
| | 3 | | using UnityEngine; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Collections; |
| | 6 | | using System; |
| | 7 | | using DCL.Helpers; |
| | 8 | |
|
| | 9 | | namespace DCL.Controllers |
| | 10 | | { |
| | 11 | | public class SceneBoundsChecker : ISceneBoundsChecker |
| | 12 | | { |
| | 13 | | public const int TRIGGER_HIGHPRIO_VALUE = 1000; |
| | 14 | | public event Action<IDCLEntity, bool> OnEntityBoundsCheckerStatusChanged; |
| | 15 | |
|
| 1078 | 16 | | public bool enabled => entitiesCheckRoutine != null; |
| | 17 | |
|
| 707 | 18 | | public float timeBetweenChecks { get; set; } = 0.5f; |
| | 19 | |
|
| | 20 | | // We use Hashset instead of Queue to be able to have a unique representation of each entity when added. |
| 676 | 21 | | HashSet<IDCLEntity> highPrioEntitiesToCheck = new HashSet<IDCLEntity>(); |
| 676 | 22 | | HashSet<IDCLEntity> entitiesToCheck = new HashSet<IDCLEntity>(); |
| 676 | 23 | | HashSet<IDCLEntity> checkedEntities = new HashSet<IDCLEntity>(); |
| | 24 | | Coroutine entitiesCheckRoutine = null; |
| | 25 | | float lastCheckTime; |
| 676 | 26 | | private HashSet<IDCLEntity> persistentEntities = new HashSet<IDCLEntity>(); |
| | 27 | |
|
| 2 | 28 | | public int entitiesToCheckCount => entitiesToCheck.Count; |
| 4 | 29 | | public int highPrioEntitiesToCheckCount => highPrioEntitiesToCheck.Count; |
| | 30 | |
|
| | 31 | | private ISceneBoundsFeedbackStyle feedbackStyle; |
| | 32 | |
|
| 2028 | 33 | | public SceneBoundsChecker(ISceneBoundsFeedbackStyle feedbackStyle = null) { this.feedbackStyle = feedbackStyle ? |
| | 34 | |
|
| | 35 | | public void SetFeedbackStyle(ISceneBoundsFeedbackStyle feedbackStyle) |
| | 36 | | { |
| 37 | 37 | | this.feedbackStyle.CleanFeedback(); |
| 37 | 38 | | this.feedbackStyle = feedbackStyle; |
| 37 | 39 | | Restart(); |
| 37 | 40 | | } |
| | 41 | |
|
| 565 | 42 | | public ISceneBoundsFeedbackStyle GetFeedbackStyle() { return feedbackStyle; } |
| | 43 | |
|
| 530 | 44 | | public List<Material> GetOriginalMaterials(MeshesInfo meshesInfo) { return feedbackStyle.GetOriginalMaterials(me |
| | 45 | |
|
| | 46 | |
|
| | 47 | | // TODO: Improve MessagingControllersManager.i.timeBudgetCounter usage once we have the centralized budget contr |
| | 48 | | IEnumerator CheckEntities() |
| | 49 | | { |
| 10698 | 50 | | while (true) |
| | 51 | | { |
| 11411 | 52 | | float elapsedTime = Time.realtimeSinceStartup - lastCheckTime; |
| 11411 | 53 | | if ((entitiesToCheck.Count > 0 || highPrioEntitiesToCheck.Count > 0) && (timeBetweenChecks <= 0f || elap |
| | 54 | | { |
| | 55 | | //TODO(Brian): Remove later when we implement a centralized way of handling time budgets |
| 101 | 56 | | var messagingManager = Environment.i.messaging.manager as MessagingControllersManager; |
| | 57 | |
|
| | 58 | | void processEntitiesList(HashSet<IDCLEntity> entities) |
| | 59 | | { |
| 202 | 60 | | if (messagingManager != null && messagingManager.timeBudgetCounter <= 0f) |
| 0 | 61 | | return; |
| | 62 | |
|
| 202 | 63 | | using HashSet<IDCLEntity>.Enumerator iterator = entities.GetEnumerator(); |
| 315 | 64 | | while (iterator.MoveNext()) |
| | 65 | | { |
| 113 | 66 | | if (messagingManager != null && messagingManager.timeBudgetCounter <= 0f) |
| 0 | 67 | | break; |
| | 68 | |
|
| 113 | 69 | | float startTime = Time.realtimeSinceStartup; |
| | 70 | |
|
| 113 | 71 | | EvaluateEntityPosition(iterator.Current); |
| 113 | 72 | | checkedEntities.Add(iterator.Current); |
| | 73 | |
|
| 113 | 74 | | float finishTime = Time.realtimeSinceStartup; |
| | 75 | |
|
| 113 | 76 | | if ( messagingManager != null ) |
| 111 | 77 | | messagingManager.timeBudgetCounter -= (finishTime - startTime); |
| | 78 | | } |
| 404 | 79 | | } |
| | 80 | |
|
| 101 | 81 | | processEntitiesList(highPrioEntitiesToCheck); |
| 101 | 82 | | processEntitiesList(entitiesToCheck); |
| | 83 | |
|
| | 84 | | // As we can't modify the hashset while traversing it, we keep track of the entities that should be |
| 101 | 85 | | using (var iterator = checkedEntities.GetEnumerator()) |
| | 86 | | { |
| 208 | 87 | | while (iterator.MoveNext()) |
| | 88 | | { |
| 107 | 89 | | if (!persistentEntities.Contains(iterator.Current)) |
| | 90 | | { |
| 104 | 91 | | entitiesToCheck.Remove(iterator.Current); |
| 104 | 92 | | highPrioEntitiesToCheck.Remove(iterator.Current); |
| | 93 | | } |
| | 94 | | } |
| 101 | 95 | | } |
| | 96 | |
|
| 101 | 97 | | checkedEntities.Clear(); |
| | 98 | |
|
| 101 | 99 | | lastCheckTime = Time.realtimeSinceStartup; |
| | 100 | | } |
| | 101 | |
|
| 11411 | 102 | | yield return null; |
| | 103 | | } |
| | 104 | | } |
| | 105 | |
|
| | 106 | | public void Restart() |
| | 107 | | { |
| 37 | 108 | | Stop(); |
| 37 | 109 | | Start(); |
| 37 | 110 | | } |
| | 111 | |
|
| | 112 | | public void Start() |
| | 113 | | { |
| 713 | 114 | | if (entitiesCheckRoutine != null) |
| 0 | 115 | | return; |
| | 116 | |
|
| 713 | 117 | | lastCheckTime = Time.realtimeSinceStartup; |
| 713 | 118 | | entitiesCheckRoutine = CoroutineStarter.Start(CheckEntities()); |
| 713 | 119 | | } |
| | 120 | |
|
| | 121 | | public void Stop() |
| | 122 | | { |
| 791 | 123 | | if (entitiesCheckRoutine == null) |
| 78 | 124 | | return; |
| | 125 | |
|
| 713 | 126 | | CoroutineStarter.Stop(entitiesCheckRoutine); |
| 713 | 127 | | entitiesCheckRoutine = null; |
| 713 | 128 | | } |
| | 129 | |
|
| | 130 | | public void AddEntityToBeChecked(IDCLEntity entity) |
| | 131 | | { |
| 565 | 132 | | if (!enabled) |
| 167 | 133 | | return; |
| | 134 | |
|
| 398 | 135 | | OnAddEntity(entity); |
| 398 | 136 | | } |
| | 137 | |
|
| | 138 | | /// <summary> |
| | 139 | | /// Add an entity that will be consistently checked, until manually removed from the list. |
| | 140 | | /// </summary> |
| | 141 | | public void AddPersistent(IDCLEntity entity) |
| | 142 | | { |
| 49 | 143 | | if (!enabled) |
| 22 | 144 | | return; |
| | 145 | |
|
| 27 | 146 | | AddEntityBasedOnPriority(entity); |
| | 147 | |
|
| 27 | 148 | | persistentEntities.Add(entity); |
| 27 | 149 | | } |
| | 150 | |
|
| | 151 | | /// <summary> |
| | 152 | | /// Returns whether an entity was added to be consistently checked |
| | 153 | | /// </summary> |
| | 154 | | /// |
| 4 | 155 | | public bool WasAddedAsPersistent(IDCLEntity entity) { return persistentEntities.Contains(entity); } |
| | 156 | |
|
| | 157 | | public void RemoveEntityToBeChecked(IDCLEntity entity) |
| | 158 | | { |
| 56 | 159 | | if (!enabled) |
| 0 | 160 | | return; |
| | 161 | |
|
| 56 | 162 | | OnRemoveEntity(entity); |
| 56 | 163 | | } |
| | 164 | |
|
| | 165 | | public void EvaluateEntityPosition(IDCLEntity entity) |
| | 166 | | { |
| 136 | 167 | | if (entity == null || entity.scene == null || entity.gameObject == null) |
| 0 | 168 | | return; |
| | 169 | |
|
| | 170 | | // Recursively evaluate entity children as well, we need to check this up front because this entity may not |
| 136 | 171 | | if (entity.children.Count > 0) |
| | 172 | | { |
| 5 | 173 | | using (var iterator = entity.children.GetEnumerator()) |
| | 174 | | { |
| 10 | 175 | | while (iterator.MoveNext()) |
| | 176 | | { |
| 5 | 177 | | EvaluateEntityPosition(iterator.Current.Value); |
| | 178 | | } |
| 5 | 179 | | } |
| | 180 | | } |
| | 181 | |
|
| 136 | 182 | | if (entity.meshRootGameObject == null || entity.meshesInfo.renderers == null || entity.meshesInfo.renderers. |
| | 183 | | { |
| 55 | 184 | | UpdateComponents(entity, entity.scene.IsInsideSceneBoundaries(entity.gameObject.transform.position + Com |
| 55 | 185 | | return; |
| | 186 | | } |
| | 187 | |
|
| | 188 | | // If the mesh is being loaded we should skip the evaluation (it will be triggered again later when the load |
| 81 | 189 | | if (entity.meshRootGameObject.GetComponent<MaterialTransitionController>()) // the object's MaterialTransiti |
| | 190 | | { |
| 1 | 191 | | return; |
| | 192 | | } |
| | 193 | |
|
| 80 | 194 | | var loadWrapper = LoadableShape.GetLoaderForEntity(entity); |
| 80 | 195 | | if (loadWrapper != null && !loadWrapper.alreadyLoaded) |
| 7 | 196 | | return; |
| | 197 | |
|
| 73 | 198 | | EvaluateMeshBounds(entity); |
| 73 | 199 | | } |
| | 200 | |
|
| | 201 | | public bool IsEntityInsideSceneBoundaries(IDCLEntity entity) |
| | 202 | | { |
| 125 | 203 | | if (entity.meshesInfo == null || entity.meshesInfo.meshRootGameObject == null || entity.meshesInfo.mergedBou |
| 25 | 204 | | return false; |
| | 205 | |
|
| | 206 | | // 1st check (full mesh AABB) |
| 100 | 207 | | bool isInsideBoundaries = entity.scene.IsInsideSceneBoundaries(entity.meshesInfo.mergedBounds); |
| | 208 | |
|
| | 209 | | // 2nd check (submeshes AABB) |
| 100 | 210 | | if (!isInsideBoundaries) |
| | 211 | | { |
| 71 | 212 | | isInsideBoundaries = AreSubmeshesInsideBoundaries(entity); |
| | 213 | | } |
| | 214 | |
|
| 100 | 215 | | return isInsideBoundaries; |
| | 216 | | } |
| | 217 | |
|
| | 218 | | void EvaluateMeshBounds(IDCLEntity entity) |
| | 219 | | { |
| 73 | 220 | | bool isInsideBoundaries = IsEntityInsideSceneBoundaries(entity); |
| 73 | 221 | | if (entity.isInsideBoundaries != isInsideBoundaries) |
| | 222 | | { |
| 35 | 223 | | entity.isInsideBoundaries = isInsideBoundaries; |
| 35 | 224 | | OnEntityBoundsCheckerStatusChanged?.Invoke(entity, isInsideBoundaries); |
| | 225 | | } |
| | 226 | |
|
| 73 | 227 | | UpdateEntityMeshesValidState(entity.meshesInfo, isInsideBoundaries); |
| 73 | 228 | | UpdateEntityCollidersValidState(entity.meshesInfo, isInsideBoundaries); |
| 73 | 229 | | UpdateComponents(entity, isInsideBoundaries); |
| 73 | 230 | | } |
| | 231 | |
|
| | 232 | | protected bool AreSubmeshesInsideBoundaries(IDCLEntity entity) |
| | 233 | | { |
| 142 | 234 | | for (int i = 0; i < entity.meshesInfo.renderers.Length; i++) |
| | 235 | | { |
| 71 | 236 | | if (entity.meshesInfo.renderers[i] == null) |
| | 237 | | continue; |
| | 238 | |
|
| 71 | 239 | | if (!entity.scene.IsInsideSceneBoundaries(entity.meshesInfo.renderers[i].GetSafeBounds())) |
| 71 | 240 | | return false; |
| | 241 | | } |
| | 242 | |
|
| 0 | 243 | | return true; |
| | 244 | | } |
| | 245 | |
|
| 146 | 246 | | protected void UpdateEntityMeshesValidState(MeshesInfo meshesInfo, bool isInsideBoundaries) { feedbackStyle.Appl |
| | 247 | |
|
| | 248 | | protected void UpdateEntityCollidersValidState(MeshesInfo meshesInfo, bool isInsideBoundaries) |
| | 249 | | { |
| 73 | 250 | | if (meshesInfo == null || meshesInfo.colliders == null) |
| 0 | 251 | | return; |
| | 252 | |
|
| 73 | 253 | | int collidersCount = meshesInfo.colliders.Count; |
| | 254 | |
|
| 73 | 255 | | if (collidersCount == 0) |
| 8 | 256 | | return; |
| | 257 | |
|
| 65 | 258 | | if (meshesInfo.colliders[0] == null) |
| 0 | 259 | | return; |
| | 260 | |
|
| 65 | 261 | | if (collidersCount > 0 && isInsideBoundaries != meshesInfo.colliders[0].enabled && meshesInfo.currentShape.H |
| | 262 | | { |
| 234 | 263 | | for (int i = 0; i < collidersCount; i++) |
| | 264 | | { |
| 72 | 265 | | if (meshesInfo.colliders[i] != null) |
| 72 | 266 | | meshesInfo.colliders[i].enabled = isInsideBoundaries; |
| | 267 | | } |
| | 268 | | } |
| 65 | 269 | | } |
| | 270 | |
|
| | 271 | | protected void UpdateComponents(IDCLEntity entity, bool isInsideBoundaries) |
| | 272 | | { |
| 128 | 273 | | IOutOfSceneBoundariesHandler[] components = entity.gameObject.GetComponentsInChildren<IOutOfSceneBoundariesH |
| | 274 | |
|
| 264 | 275 | | for (int i = 0; i < components.Length; i++) |
| | 276 | | { |
| 4 | 277 | | components[i].UpdateOutOfBoundariesState(isInsideBoundaries); |
| | 278 | | } |
| 128 | 279 | | } |
| | 280 | |
|
| | 281 | | protected void OnAddEntity(IDCLEntity entity) |
| | 282 | | { |
| 398 | 283 | | AddEntityBasedOnPriority(entity); |
| 398 | 284 | | } |
| | 285 | |
|
| | 286 | | protected void OnRemoveEntity(IDCLEntity entity) |
| | 287 | | { |
| 56 | 288 | | highPrioEntitiesToCheck.Remove(entity); |
| 56 | 289 | | entitiesToCheck.Remove(entity); |
| 56 | 290 | | persistentEntities.Remove(entity); |
| 56 | 291 | | feedbackStyle.ApplyFeedback(entity.meshesInfo, true); |
| 56 | 292 | | } |
| | 293 | |
|
| | 294 | | protected void AddEntityBasedOnPriority(IDCLEntity entity) |
| | 295 | | { |
| 425 | 296 | | if (IsHighPrioEntity(entity) && !highPrioEntitiesToCheck.Contains(entity)) |
| 8 | 297 | | highPrioEntitiesToCheck.Add(entity); |
| 417 | 298 | | else if (!entitiesToCheck.Contains(entity)) |
| 230 | 299 | | entitiesToCheck.Add(entity); |
| 417 | 300 | | } |
| | 301 | |
|
| | 302 | | protected bool IsHighPrioEntity(IDCLEntity entity) |
| | 303 | | { |
| 425 | 304 | | if (entity.gameObject == null) |
| 0 | 305 | | return false; |
| | 306 | |
|
| 425 | 307 | | Vector3 scale = entity.gameObject.transform.lossyScale; |
| 425 | 308 | | Vector3 position = entity.gameObject.transform.localPosition; |
| 425 | 309 | | return scale.x > TRIGGER_HIGHPRIO_VALUE || scale.y > TRIGGER_HIGHPRIO_VALUE || scale.z > TRIGGER_HIGHPRIO_VA |
| | 310 | | } |
| | 311 | | } |
| | 312 | | } |