| | 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 event Action<IDCLEntity, bool> OnEntityBoundsCheckerStatusChanged; |
| 995 | 12 | | public bool enabled => entitiesCheckRoutine != null; |
| 702 | 13 | | public float timeBetweenChecks { get; set; } = 0.5f; |
| 2 | 14 | | public int entitiesToCheckCount => entitiesToCheck.Count; |
| | 15 | |
|
| | 16 | | private const bool VERBOSE = false; |
| 666 | 17 | | private Logger logger = new Logger("SceneBoundsChecker") {verboseEnabled = VERBOSE}; |
| 666 | 18 | | private HashSet<IDCLEntity> entitiesToCheck = new HashSet<IDCLEntity>(); |
| 666 | 19 | | private HashSet<IDCLEntity> checkedEntities = new HashSet<IDCLEntity>(); |
| 666 | 20 | | private HashSet<IDCLEntity> persistentEntities = new HashSet<IDCLEntity>(); |
| | 21 | | private ISceneBoundsFeedbackStyle feedbackStyle; |
| | 22 | | private Coroutine entitiesCheckRoutine = null; |
| | 23 | | private float lastCheckTime; |
| | 24 | |
|
| | 25 | | public void Initialize() |
| | 26 | | { |
| 666 | 27 | | Start(); |
| 666 | 28 | | } |
| | 29 | |
|
| 666 | 30 | | public SceneBoundsChecker(ISceneBoundsFeedbackStyle feedbackStyle = null) |
| | 31 | | { |
| 666 | 32 | | this.feedbackStyle = feedbackStyle ?? new SceneBoundsFeedbackStyle_Simple(); |
| 666 | 33 | | } |
| | 34 | |
|
| | 35 | | public void SetFeedbackStyle(ISceneBoundsFeedbackStyle feedbackStyle) |
| | 36 | | { |
| 35 | 37 | | this.feedbackStyle.CleanFeedback(); |
| 35 | 38 | | this.feedbackStyle = feedbackStyle; |
| 35 | 39 | | Restart(); |
| 35 | 40 | | } |
| | 41 | |
|
| 71 | 42 | | public ISceneBoundsFeedbackStyle GetFeedbackStyle() { return feedbackStyle; } |
| | 43 | |
|
| 0 | 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 | | private IEnumerator CheckEntities() |
| | 49 | | { |
| 6813 | 50 | | while (true) |
| | 51 | | { |
| 7514 | 52 | | float elapsedTime = Time.realtimeSinceStartup - lastCheckTime; |
| 7514 | 53 | | if ((entitiesToCheck.Count > 0) && (timeBetweenChecks <= 0f || elapsedTime >= timeBetweenChecks)) |
| | 54 | | { |
| | 55 | | //TODO(Brian): Remove later when we implement a centralized way of handling time budgets |
| 69 | 56 | | var messagingManager = Environment.i.messaging.manager as MessagingControllersManager; |
| | 57 | |
|
| | 58 | | void processEntitiesList(HashSet<IDCLEntity> entities) |
| | 59 | | { |
| 69 | 60 | | if (messagingManager != null && messagingManager.timeBudgetCounter <= 0f) |
| | 61 | | { |
| | 62 | | if(VERBOSE) |
| | 63 | | logger.Verbose("Time budget reached, escaping entities processing until next iteration.. |
| 0 | 64 | | return; |
| | 65 | | } |
| | 66 | |
|
| 69 | 67 | | using HashSet<IDCLEntity>.Enumerator iterator = entities.GetEnumerator(); |
| 142 | 68 | | while (iterator.MoveNext()) |
| | 69 | | { |
| 73 | 70 | | if (messagingManager != null && messagingManager.timeBudgetCounter <= 0f) |
| | 71 | | { |
| 0 | 72 | | if(VERBOSE) |
| | 73 | | logger.Verbose("Time budget reached, escaping entities processing until next iterati |
| | 74 | | return; |
| | 75 | | } |
| | 76 | |
|
| 73 | 77 | | float startTime = Time.realtimeSinceStartup; |
| | 78 | |
|
| 73 | 79 | | RunEntityEvaluation(iterator.Current); |
| 73 | 80 | | checkedEntities.Add(iterator.Current); |
| | 81 | |
|
| 73 | 82 | | float finishTime = Time.realtimeSinceStartup; |
| | 83 | |
|
| 73 | 84 | | if ( messagingManager != null ) |
| 73 | 85 | | messagingManager.timeBudgetCounter -= (finishTime - startTime); |
| | 86 | | } |
| 138 | 87 | | } |
| | 88 | |
|
| 69 | 89 | | processEntitiesList(entitiesToCheck); |
| | 90 | |
|
| | 91 | | // As we can't modify the hashset while traversing it, we keep track of the entities that should be |
| 69 | 92 | | using (var iterator = checkedEntities.GetEnumerator()) |
| | 93 | | { |
| 142 | 94 | | while (iterator.MoveNext()) |
| | 95 | | { |
| 73 | 96 | | RemoveEntity(iterator.Current, removeIfPersistent: false, resetState: false); |
| | 97 | | } |
| 69 | 98 | | } |
| | 99 | |
|
| | 100 | | if(VERBOSE) |
| | 101 | | logger.Verbose($"Finished checking entities: checked entities {checkedEntities.Count}; entitiesT |
| | 102 | |
|
| 69 | 103 | | checkedEntities.Clear(); |
| | 104 | |
|
| 69 | 105 | | lastCheckTime = Time.realtimeSinceStartup; |
| | 106 | | } |
| | 107 | |
|
| 7514 | 108 | | yield return null; |
| | 109 | | } |
| | 110 | | } |
| | 111 | |
|
| | 112 | | public void Restart() |
| | 113 | | { |
| 35 | 114 | | Stop(); |
| 35 | 115 | | Start(); |
| 35 | 116 | | } |
| | 117 | |
|
| | 118 | | public void Start() |
| | 119 | | { |
| 701 | 120 | | if (entitiesCheckRoutine != null) |
| 0 | 121 | | return; |
| | 122 | |
|
| 701 | 123 | | lastCheckTime = Time.realtimeSinceStartup; |
| 701 | 124 | | entitiesCheckRoutine = CoroutineStarter.Start(CheckEntities()); |
| 701 | 125 | | } |
| | 126 | |
|
| | 127 | | public void Stop() |
| | 128 | | { |
| 722 | 129 | | if (entitiesCheckRoutine == null) |
| 21 | 130 | | return; |
| | 131 | |
|
| 701 | 132 | | CoroutineStarter.Stop(entitiesCheckRoutine); |
| 701 | 133 | | entitiesCheckRoutine = null; |
| 701 | 134 | | } |
| | 135 | |
|
| | 136 | | public void Dispose() |
| | 137 | | { |
| 666 | 138 | | Stop(); |
| 666 | 139 | | } |
| | 140 | |
|
| | 141 | | public void AddEntityToBeChecked(IDCLEntity entity, bool isPersistent = false, bool runPreliminaryEvaluation = f |
| | 142 | | { |
| 1261 | 143 | | if (!enabled || (entity.scene != null && entity.scene.isPersistent)) |
| 965 | 144 | | return; |
| | 145 | |
|
| 296 | 146 | | if (runPreliminaryEvaluation) |
| | 147 | | { |
| | 148 | | // The outer bounds check is cheaper than the regular check |
| 234 | 149 | | RunEntityEvaluation(entity, onlyOuterBoundsCheck: true); |
| | 150 | |
|
| | 151 | | // No need to add the entity to be checked later if we already found it outside scene outer boundaries. |
| | 152 | | // When the correct events are triggered again, the entity will be checked again. |
| 234 | 153 | | if (!isPersistent && !entity.isInsideSceneOuterBoundaries) |
| 84 | 154 | | return; |
| | 155 | | } |
| | 156 | |
|
| 212 | 157 | | entitiesToCheck.Add(entity); |
| | 158 | |
|
| 212 | 159 | | if (isPersistent) |
| 6 | 160 | | persistentEntities.Add(entity); |
| 212 | 161 | | } |
| | 162 | |
|
| | 163 | | public void RemoveEntity(IDCLEntity entity, bool removeIfPersistent = false, bool resetState = false) |
| | 164 | | { |
| 537 | 165 | | if (!enabled || (!removeIfPersistent && persistentEntities.Contains(entity))) |
| 0 | 166 | | return; |
| | 167 | |
|
| 537 | 168 | | entitiesToCheck.Remove(entity); |
| 537 | 169 | | persistentEntities.Remove(entity); |
| | 170 | |
|
| 537 | 171 | | if(resetState) |
| 462 | 172 | | SetMeshesAndComponentsInsideBoundariesState(entity, true); |
| 537 | 173 | | } |
| | 174 | |
|
| 7 | 175 | | public bool WasAddedAsPersistent(IDCLEntity entity) { return persistentEntities.Contains(entity); } |
| | 176 | |
|
| | 177 | | // TODO: When we remove the DCLBuilderEntity class we'll be able to remove this overload |
| | 178 | | public void RunEntityEvaluation(IDCLEntity entity) |
| | 179 | | { |
| 87 | 180 | | RunEntityEvaluation(entity, false); |
| 87 | 181 | | } |
| | 182 | |
|
| | 183 | | public void RunEntityEvaluation(IDCLEntity entity, bool onlyOuterBoundsCheck) |
| | 184 | | { |
| 332 | 185 | | if (entity == null || entity.gameObject == null || entity.scene == null || entity.scene.isPersistent) |
| 7 | 186 | | return; |
| | 187 | |
|
| | 188 | | // Recursively evaluate entity children as well, we need to check this up front because this entity may not |
| 325 | 189 | | if (entity.children.Count > 0) |
| | 190 | | { |
| 9 | 191 | | using (var iterator = entity.children.GetEnumerator()) |
| | 192 | | { |
| 18 | 193 | | while (iterator.MoveNext()) |
| | 194 | | { |
| 9 | 195 | | RunEntityEvaluation(iterator.Current.Value, onlyOuterBoundsCheck); |
| | 196 | | } |
| 9 | 197 | | } |
| | 198 | | } |
| | 199 | |
|
| | 200 | | // If it has a mesh we don't evaluate its position due to artists "pivot point sloppiness", we evaluate its |
| 325 | 201 | | if (HasMesh(entity)) |
| 154 | 202 | | EvaluateMeshBounds(entity, onlyOuterBoundsCheck); |
| | 203 | | else |
| 171 | 204 | | EvaluateEntityPosition(entity, onlyOuterBoundsCheck); |
| 171 | 205 | | } |
| | 206 | |
|
| | 207 | | private void EvaluateMeshBounds(IDCLEntity entity, bool onlyOuterBoundsCheck = false) |
| | 208 | | { |
| | 209 | | // TODO: Can we cache the MaterialTransitionController somewhere to avoid this GetComponent() call? |
| | 210 | | // If the mesh is being loaded we should skip the evaluation (it will be triggered again later when the load |
| 154 | 211 | | if (entity.meshRootGameObject.GetComponent<MaterialTransitionController>()) // the object's MaterialTransiti |
| 0 | 212 | | return; |
| | 213 | |
|
| 154 | 214 | | var loadWrapper = Environment.i.world.state.GetLoaderForEntity(entity); |
| 154 | 215 | | if (loadWrapper != null && !loadWrapper.alreadyLoaded) |
| 8 | 216 | | return; |
| | 217 | |
|
| 146 | 218 | | entity.isInsideSceneOuterBoundaries = entity.scene.IsInsideSceneOuterBoundaries(entity.meshesInfo.mergedBoun |
| | 219 | |
|
| 146 | 220 | | if (!entity.isInsideSceneOuterBoundaries) |
| 93 | 221 | | SetMeshesAndComponentsInsideBoundariesState(entity, false); |
| | 222 | |
|
| 146 | 223 | | if (!onlyOuterBoundsCheck) |
| 47 | 224 | | SetMeshesAndComponentsInsideBoundariesState(entity, IsEntityMeshInsideSceneBoundaries(entity)); |
| 146 | 225 | | } |
| | 226 | |
|
| | 227 | | private void EvaluateEntityPosition(IDCLEntity entity, bool onlyOuterBoundsCheck = false) |
| | 228 | | { |
| 171 | 229 | | Vector3 entityGOPosition = entity.gameObject.transform.position; |
| 171 | 230 | | entity.isInsideSceneOuterBoundaries = entity.scene.IsInsideSceneOuterBoundaries(entityGOPosition); |
| | 231 | |
|
| 171 | 232 | | if (!entity.isInsideSceneOuterBoundaries) |
| | 233 | | { |
| 33 | 234 | | SetComponentsInsideBoundariesValidState(entity, false); |
| 33 | 235 | | SetEntityInsideBoundariesState(entity, false); |
| | 236 | | } |
| | 237 | |
|
| 171 | 238 | | if (!onlyOuterBoundsCheck) |
| | 239 | | { |
| 37 | 240 | | bool isInsideBoundaries = entity.scene.IsInsideSceneBoundaries(entityGOPosition + CommonScriptableObject |
| 37 | 241 | | SetComponentsInsideBoundariesValidState(entity, isInsideBoundaries); |
| 37 | 242 | | SetEntityInsideBoundariesState(entity, isInsideBoundaries); |
| | 243 | | } |
| 171 | 244 | | } |
| | 245 | |
|
| | 246 | | private void SetEntityInsideBoundariesState(IDCLEntity entity, bool isInsideBoundaries) |
| | 247 | | { |
| 672 | 248 | | if (entity.isInsideSceneBoundaries == isInsideBoundaries) |
| 552 | 249 | | return; |
| | 250 | |
|
| 120 | 251 | | entity.isInsideSceneBoundaries = isInsideBoundaries; |
| 120 | 252 | | OnEntityBoundsCheckerStatusChanged?.Invoke(entity, isInsideBoundaries); |
| 3 | 253 | | } |
| | 254 | |
|
| | 255 | | private bool HasMesh(IDCLEntity entity) |
| | 256 | | { |
| 325 | 257 | | return entity.meshRootGameObject != null |
| | 258 | | && (entity.meshesInfo.colliders.Count > 0 |
| | 259 | | || (entity.meshesInfo.renderers != null |
| | 260 | | && entity.meshesInfo.renderers.Length > 0)); |
| | 261 | | } |
| | 262 | |
|
| | 263 | | public bool IsEntityMeshInsideSceneBoundaries(IDCLEntity entity) |
| | 264 | | { |
| 88 | 265 | | if (entity.meshesInfo == null |
| | 266 | | || entity.meshesInfo.meshRootGameObject == null |
| | 267 | | || entity.meshesInfo.mergedBounds == null) |
| 25 | 268 | | return false; |
| | 269 | |
|
| | 270 | | // 1st check (full mesh AABB) |
| 63 | 271 | | bool isInsideBoundaries = entity.scene.IsInsideSceneBoundaries(entity.meshesInfo.mergedBounds); |
| | 272 | |
|
| | 273 | | // 2nd check (submeshes & colliders AABB) |
| 63 | 274 | | if (!isInsideBoundaries) |
| | 275 | | { |
| 28 | 276 | | isInsideBoundaries = AreSubmeshesInsideBoundaries(entity) && AreCollidersInsideBoundaries(entity); |
| | 277 | | } |
| | 278 | |
|
| 63 | 279 | | return isInsideBoundaries; |
| | 280 | | } |
| | 281 | |
|
| | 282 | | private bool AreSubmeshesInsideBoundaries(IDCLEntity entity) |
| | 283 | | { |
| 66 | 284 | | for (int i = 0; i < entity.meshesInfo.renderers.Length; i++) |
| | 285 | | { |
| 29 | 286 | | Renderer renderer = entity.meshesInfo.renderers[i]; |
| 29 | 287 | | if (renderer == null) |
| | 288 | | continue; |
| | 289 | |
|
| 29 | 290 | | if (!entity.scene.IsInsideSceneBoundaries(MeshesInfoUtils.GetSafeBounds(renderer.bounds, renderer.transf |
| 24 | 291 | | return false; |
| | 292 | | } |
| | 293 | |
|
| 4 | 294 | | return true; |
| | 295 | | } |
| | 296 | |
|
| | 297 | | private bool AreCollidersInsideBoundaries(IDCLEntity entity) |
| | 298 | | { |
| 13 | 299 | | foreach (Collider collider in entity.meshesInfo.colliders) |
| | 300 | | { |
| 4 | 301 | | if (collider == null) |
| | 302 | | continue; |
| | 303 | |
|
| 4 | 304 | | if (!entity.scene.IsInsideSceneBoundaries(MeshesInfoUtils.GetSafeBounds(collider.bounds, collider.transf |
| 3 | 305 | | return false; |
| | 306 | | } |
| | 307 | |
|
| 1 | 308 | | return true; |
| 3 | 309 | | } |
| | 310 | |
|
| | 311 | | private void SetMeshesAndComponentsInsideBoundariesState(IDCLEntity entity, bool isInsideBoundaries) |
| | 312 | | { |
| 602 | 313 | | SetEntityMeshesInsideBoundariesState(entity.meshesInfo, isInsideBoundaries); |
| 602 | 314 | | SetEntityCollidersInsideBoundariesState(entity.meshesInfo, isInsideBoundaries); |
| 602 | 315 | | SetComponentsInsideBoundariesValidState(entity, isInsideBoundaries); |
| | 316 | |
|
| | 317 | | // Should always be set last as entity.isInsideSceneBoundaries is checked to avoid re-running code unnecessa |
| 602 | 318 | | SetEntityInsideBoundariesState(entity, isInsideBoundaries); |
| 602 | 319 | | } |
| | 320 | |
|
| | 321 | | private void SetEntityMeshesInsideBoundariesState(MeshesInfo meshesInfo, bool isInsideBoundaries) |
| | 322 | | { |
| 602 | 323 | | feedbackStyle.ApplyFeedback(meshesInfo, isInsideBoundaries); |
| 602 | 324 | | } |
| | 325 | |
|
| | 326 | | private void SetEntityCollidersInsideBoundariesState(MeshesInfo meshesInfo, bool isInsideBoundaries) |
| | 327 | | { |
| 602 | 328 | | if (meshesInfo == null || meshesInfo.colliders.Count == 0 || !meshesInfo.currentShape.HasCollisions()) |
| 394 | 329 | | return; |
| | 330 | |
|
| 910 | 331 | | foreach (Collider collider in meshesInfo.colliders) |
| | 332 | | { |
| 247 | 333 | | if (collider == null) continue; |
| | 334 | |
|
| 247 | 335 | | if (collider.enabled != isInsideBoundaries) |
| 128 | 336 | | collider.enabled = isInsideBoundaries; |
| | 337 | | } |
| 208 | 338 | | } |
| | 339 | |
|
| | 340 | | private void SetComponentsInsideBoundariesValidState(IDCLEntity entity, bool isInsideBoundaries) |
| | 341 | | { |
| 672 | 342 | | if(entity.isInsideSceneBoundaries == isInsideBoundaries || !DataStore.i.sceneBoundariesChecker.componentsChe |
| 670 | 343 | | return; |
| | 344 | |
|
| 8 | 345 | | foreach (IOutOfSceneBoundariesHandler component in DataStore.i.sceneBoundariesChecker.componentsCheckSceneBo |
| | 346 | | { |
| 2 | 347 | | component.UpdateOutOfBoundariesState(isInsideBoundaries); |
| | 348 | | } |
| 2 | 349 | | } |
| | 350 | | } |
| | 351 | | } |