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