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