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