| | 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; |
| 1004 | 12 | | public bool enabled => entitiesCheckRoutine != null; |
| 709 | 13 | | public float timeBetweenChecks { get; set; } = 0.5f; |
| 2 | 14 | | public int entitiesToCheckCount => entitiesToCheck.Count; |
| | 15 | |
|
| | 16 | | private const bool VERBOSE = false; |
| 670 | 17 | | private Logger logger = new Logger("SceneBoundsChecker") {verboseEnabled = VERBOSE}; |
| 670 | 18 | | private HashSet<IDCLEntity> entitiesToCheck = new HashSet<IDCLEntity>(); |
| 670 | 19 | | private HashSet<IDCLEntity> checkedEntities = new HashSet<IDCLEntity>(); |
| 670 | 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 | | { |
| 670 | 27 | | Start(); |
| 670 | 28 | | } |
| | 29 | |
|
| 670 | 30 | | public SceneBoundsChecker(ISceneBoundsFeedbackStyle feedbackStyle = null) |
| | 31 | | { |
| 670 | 32 | | this.feedbackStyle = feedbackStyle ?? new SceneBoundsFeedbackStyle_Simple(); |
| 670 | 33 | | } |
| | 34 | |
|
| | 35 | | public void SetFeedbackStyle(ISceneBoundsFeedbackStyle feedbackStyle) |
| | 36 | | { |
| 54 | 37 | | this.feedbackStyle.CleanFeedback(); |
| 54 | 38 | | this.feedbackStyle = feedbackStyle; |
| 54 | 39 | | Restart(); |
| 54 | 40 | | } |
| | 41 | |
|
| 78 | 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 | | { |
| 6859 | 50 | | while (true) |
| | 51 | | { |
| 7583 | 52 | | float elapsedTime = Time.realtimeSinceStartup - lastCheckTime; |
| 7583 | 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 |
| 74 | 56 | | var messagingManager = Environment.i.messaging.manager as MessagingControllersManager; |
| | 57 | |
|
| | 58 | | void processEntitiesList(HashSet<IDCLEntity> entities) |
| | 59 | | { |
| 74 | 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 | |
|
| 74 | 67 | | using HashSet<IDCLEntity>.Enumerator iterator = entities.GetEnumerator(); |
| 152 | 68 | | while (iterator.MoveNext()) |
| | 69 | | { |
| 78 | 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 | |
|
| 78 | 77 | | float startTime = Time.realtimeSinceStartup; |
| | 78 | |
|
| 78 | 79 | | RunEntityEvaluation(iterator.Current); |
| 78 | 80 | | checkedEntities.Add(iterator.Current); |
| | 81 | |
|
| 78 | 82 | | float finishTime = Time.realtimeSinceStartup; |
| | 83 | |
|
| 78 | 84 | | if ( messagingManager != null ) |
| 78 | 85 | | messagingManager.timeBudgetCounter -= (finishTime - startTime); |
| | 86 | | } |
| 148 | 87 | | } |
| | 88 | |
|
| 74 | 89 | | processEntitiesList(entitiesToCheck); |
| | 90 | |
|
| | 91 | | // As we can't modify the hashset while traversing it, we keep track of the entities that should be |
| 74 | 92 | | using (var iterator = checkedEntities.GetEnumerator()) |
| | 93 | | { |
| 152 | 94 | | while (iterator.MoveNext()) |
| | 95 | | { |
| 78 | 96 | | RemoveEntity(iterator.Current, removeIfPersistent: false, resetState: false); |
| | 97 | | } |
| 74 | 98 | | } |
| | 99 | |
|
| | 100 | | if(VERBOSE) |
| | 101 | | logger.Verbose($"Finished checking entities: checked entities {checkedEntities.Count}; entitiesT |
| | 102 | |
|
| 74 | 103 | | checkedEntities.Clear(); |
| | 104 | |
|
| 74 | 105 | | lastCheckTime = Time.realtimeSinceStartup; |
| | 106 | | } |
| | 107 | |
|
| 7583 | 108 | | yield return null; |
| | 109 | | } |
| | 110 | | } |
| | 111 | |
|
| | 112 | | public void Restart() |
| | 113 | | { |
| 54 | 114 | | Stop(); |
| 54 | 115 | | Start(); |
| 54 | 116 | | } |
| | 117 | |
|
| | 118 | | public void Start() |
| | 119 | | { |
| 724 | 120 | | if (entitiesCheckRoutine != null) |
| 0 | 121 | | return; |
| | 122 | |
|
| 724 | 123 | | lastCheckTime = Time.realtimeSinceStartup; |
| 724 | 124 | | entitiesCheckRoutine = CoroutineStarter.Start(CheckEntities()); |
| 724 | 125 | | } |
| | 126 | |
|
| | 127 | | public void Stop() |
| | 128 | | { |
| 745 | 129 | | if (entitiesCheckRoutine == null) |
| 21 | 130 | | return; |
| | 131 | |
|
| 724 | 132 | | CoroutineStarter.Stop(entitiesCheckRoutine); |
| 724 | 133 | | entitiesCheckRoutine = null; |
| 724 | 134 | | } |
| | 135 | |
|
| | 136 | | public void Dispose() |
| | 137 | | { |
| 670 | 138 | | Stop(); |
| 670 | 139 | | } |
| | 140 | |
|
| | 141 | | public void AddEntityToBeChecked(IDCLEntity entity, bool isPersistent = false, bool runPreliminaryEvaluation = f |
| | 142 | | { |
| 1279 | 143 | | if (!enabled || (entity.scene != null && entity.scene.isPersistent)) |
| 969 | 144 | | return; |
| | 145 | |
|
| 310 | 146 | | if (runPreliminaryEvaluation) |
| | 147 | | { |
| | 148 | | // The outer bounds check is cheaper than the regular check |
| 245 | 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. |
| 245 | 153 | | if (!isPersistent && !entity.isInsideSceneOuterBoundaries) |
| 89 | 154 | | return; |
| | 155 | | } |
| | 156 | |
|
| 221 | 157 | | entitiesToCheck.Add(entity); |
| | 158 | |
|
| 221 | 159 | | if (isPersistent) |
| 6 | 160 | | persistentEntities.Add(entity); |
| 221 | 161 | | } |
| | 162 | |
|
| | 163 | | public void RemoveEntity(IDCLEntity entity, bool removeIfPersistent = false, bool resetState = false) |
| | 164 | | { |
| 545 | 165 | | if (!enabled || (!removeIfPersistent && persistentEntities.Contains(entity))) |
| 0 | 166 | | return; |
| | 167 | |
|
| 545 | 168 | | entitiesToCheck.Remove(entity); |
| 545 | 169 | | persistentEntities.Remove(entity); |
| | 170 | |
|
| 545 | 171 | | if(resetState) |
| 465 | 172 | | SetMeshesAndComponentsInsideBoundariesState(entity, true); |
| 545 | 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 | | { |
| 92 | 180 | | RunEntityEvaluation(entity, false); |
| 92 | 181 | | } |
| | 182 | |
|
| | 183 | | public void RunEntityEvaluation(IDCLEntity entity, bool onlyOuterBoundsCheck) |
| | 184 | | { |
| 348 | 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 |
| 341 | 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 | |
|
| 341 | 200 | | if (HasMesh(entity)) // If it has a mesh we don't evaluate its position due to artists "pivot point sloppine |
| 163 | 201 | | EvaluateMeshBounds(entity, onlyOuterBoundsCheck); |
| 178 | 202 | | else if (entity.scene.componentsManagerLegacy.HasComponent(entity, CLASS_ID_COMPONENT.AVATAR_SHAPE)) // Avat |
| 0 | 203 | | EvaluateAvatarMeshBounds(entity, onlyOuterBoundsCheck); |
| | 204 | | else |
| 178 | 205 | | EvaluateEntityPosition(entity, onlyOuterBoundsCheck); |
| 178 | 206 | | } |
| | 207 | |
|
| | 208 | | private void EvaluateMeshBounds(IDCLEntity entity, bool onlyOuterBoundsCheck = false) |
| | 209 | | { |
| | 210 | | // TODO: Can we cache the MaterialTransitionController somewhere to avoid this GetComponent() call? |
| | 211 | | // If the mesh is being loaded we should skip the evaluation (it will be triggered again later when the load |
| 163 | 212 | | if (entity.meshRootGameObject.GetComponent<MaterialTransitionController>()) // the object's MaterialTransiti |
| 0 | 213 | | return; |
| | 214 | |
|
| 163 | 215 | | var loadWrapper = Environment.i.world.state.GetLoaderForEntity(entity); |
| 163 | 216 | | if (loadWrapper != null && !loadWrapper.alreadyLoaded) |
| 10 | 217 | | return; |
| | 218 | |
|
| 153 | 219 | | entity.isInsideSceneOuterBoundaries = entity.scene.IsInsideSceneOuterBoundaries(entity.meshesInfo.mergedBoun |
| | 220 | |
|
| 153 | 221 | | if (!entity.isInsideSceneOuterBoundaries) |
| 99 | 222 | | SetMeshesAndComponentsInsideBoundariesState(entity, false); |
| | 223 | |
|
| 153 | 224 | | if (onlyOuterBoundsCheck) |
| 104 | 225 | | return; |
| | 226 | |
|
| 49 | 227 | | SetMeshesAndComponentsInsideBoundariesState(entity, IsEntityMeshInsideSceneBoundaries(entity)); |
| 49 | 228 | | } |
| | 229 | |
|
| | 230 | | private void EvaluateEntityPosition(IDCLEntity entity, bool onlyOuterBoundsCheck = false) |
| | 231 | | { |
| 178 | 232 | | Vector3 entityGOPosition = entity.gameObject.transform.position; |
| 178 | 233 | | entity.isInsideSceneOuterBoundaries = entity.scene.IsInsideSceneOuterBoundaries(entityGOPosition); |
| | 234 | |
|
| 178 | 235 | | if (!entity.isInsideSceneOuterBoundaries) |
| | 236 | | { |
| 35 | 237 | | SetComponentsInsideBoundariesValidState(entity, false); |
| 35 | 238 | | SetEntityInsideBoundariesState(entity, false); |
| | 239 | | } |
| | 240 | |
|
| 178 | 241 | | if (onlyOuterBoundsCheck) |
| 139 | 242 | | return; |
| | 243 | |
|
| 39 | 244 | | bool isInsideBoundaries = entity.scene.IsInsideSceneBoundaries(entityGOPosition + CommonScriptableObjects.wo |
| 39 | 245 | | SetComponentsInsideBoundariesValidState(entity, isInsideBoundaries); |
| 39 | 246 | | SetEntityInsideBoundariesState(entity, isInsideBoundaries); |
| 39 | 247 | | } |
| | 248 | |
|
| | 249 | | private void EvaluateAvatarMeshBounds(IDCLEntity entity, bool onlyOuterBoundsCheck = false) |
| | 250 | | { |
| 0 | 251 | | Vector3 entityGOPosition = entity.gameObject.transform.position; |
| | 252 | |
|
| | 253 | | // Heuristic using the entity scale for the size of the avatar bounds, otherwise we should configure the |
| | 254 | | // entity's meshRootGameObject, etc. after its GPU skinning runs and use the regular entity mesh evaluation |
| 0 | 255 | | Bounds avatarBounds = new Bounds(); |
| 0 | 256 | | avatarBounds.center = entityGOPosition; |
| 0 | 257 | | avatarBounds.size = entity.gameObject.transform.lossyScale; |
| | 258 | |
|
| 0 | 259 | | entity.isInsideSceneOuterBoundaries = entity.scene.IsInsideSceneOuterBoundaries(avatarBounds); |
| | 260 | |
|
| 0 | 261 | | if (!entity.isInsideSceneOuterBoundaries) |
| | 262 | | { |
| 0 | 263 | | SetComponentsInsideBoundariesValidState(entity, false); |
| 0 | 264 | | SetEntityInsideBoundariesState(entity, false); |
| | 265 | | } |
| | 266 | |
|
| 0 | 267 | | if (onlyOuterBoundsCheck) |
| 0 | 268 | | return; |
| | 269 | |
|
| 0 | 270 | | bool isInsideBoundaries = entity.scene.IsInsideSceneBoundaries(avatarBounds); |
| 0 | 271 | | SetComponentsInsideBoundariesValidState(entity, isInsideBoundaries); |
| 0 | 272 | | SetEntityInsideBoundariesState(entity, isInsideBoundaries); |
| 0 | 273 | | } |
| | 274 | |
|
| | 275 | | private void SetEntityInsideBoundariesState(IDCLEntity entity, bool isInsideBoundaries) |
| | 276 | | { |
| 687 | 277 | | if (entity.isInsideSceneBoundaries == isInsideBoundaries) |
| 561 | 278 | | return; |
| | 279 | |
|
| 126 | 280 | | entity.isInsideSceneBoundaries = isInsideBoundaries; |
| 126 | 281 | | OnEntityBoundsCheckerStatusChanged?.Invoke(entity, isInsideBoundaries); |
| 3 | 282 | | } |
| | 283 | |
|
| | 284 | | private bool HasMesh(IDCLEntity entity) |
| | 285 | | { |
| 341 | 286 | | return entity.meshRootGameObject != null |
| | 287 | | && (entity.meshesInfo.colliders.Count > 0 |
| | 288 | | || (entity.meshesInfo.renderers != null |
| | 289 | | && entity.meshesInfo.renderers.Length > 0)); |
| | 290 | | } |
| | 291 | |
|
| | 292 | | public bool IsEntityMeshInsideSceneBoundaries(IDCLEntity entity) |
| | 293 | | { |
| 90 | 294 | | if (entity.meshesInfo == null |
| | 295 | | || entity.meshesInfo.meshRootGameObject == null |
| | 296 | | || entity.meshesInfo.mergedBounds == null) |
| 25 | 297 | | return false; |
| | 298 | |
|
| | 299 | | // 1st check (full mesh AABB) |
| 65 | 300 | | bool isInsideBoundaries = entity.scene.IsInsideSceneBoundaries(entity.meshesInfo.mergedBounds); |
| | 301 | |
|
| | 302 | | // 2nd check (submeshes & colliders AABB) |
| 65 | 303 | | if (!isInsideBoundaries) |
| | 304 | | { |
| 30 | 305 | | isInsideBoundaries = AreSubmeshesInsideBoundaries(entity) && AreCollidersInsideBoundaries(entity); |
| | 306 | | } |
| | 307 | |
|
| 65 | 308 | | return isInsideBoundaries; |
| | 309 | | } |
| | 310 | |
|
| | 311 | | private bool AreSubmeshesInsideBoundaries(IDCLEntity entity) |
| | 312 | | { |
| 70 | 313 | | for (int i = 0; i < entity.meshesInfo.renderers.Length; i++) |
| | 314 | | { |
| 31 | 315 | | Renderer renderer = entity.meshesInfo.renderers[i]; |
| 31 | 316 | | if (renderer == null) |
| | 317 | | continue; |
| | 318 | |
|
| 31 | 319 | | if (!entity.scene.IsInsideSceneBoundaries(MeshesInfoUtils.GetSafeBounds(renderer.bounds, renderer.transf |
| 26 | 320 | | return false; |
| | 321 | | } |
| | 322 | |
|
| 4 | 323 | | return true; |
| | 324 | | } |
| | 325 | |
|
| | 326 | | private bool AreCollidersInsideBoundaries(IDCLEntity entity) |
| | 327 | | { |
| 13 | 328 | | foreach (Collider collider in entity.meshesInfo.colliders) |
| | 329 | | { |
| 4 | 330 | | if (collider == null) |
| | 331 | | continue; |
| | 332 | |
|
| 4 | 333 | | if (!entity.scene.IsInsideSceneBoundaries(MeshesInfoUtils.GetSafeBounds(collider.bounds, collider.transf |
| 3 | 334 | | return false; |
| | 335 | | } |
| | 336 | |
|
| 1 | 337 | | return true; |
| 3 | 338 | | } |
| | 339 | |
|
| | 340 | | private void SetMeshesAndComponentsInsideBoundariesState(IDCLEntity entity, bool isInsideBoundaries) |
| | 341 | | { |
| 613 | 342 | | SetEntityMeshesInsideBoundariesState(entity.meshesInfo, isInsideBoundaries); |
| 613 | 343 | | SetEntityCollidersInsideBoundariesState(entity.meshesInfo, isInsideBoundaries); |
| 613 | 344 | | SetComponentsInsideBoundariesValidState(entity, isInsideBoundaries); |
| | 345 | |
|
| | 346 | | // Should always be set last as entity.isInsideSceneBoundaries is checked to avoid re-running code unnecessa |
| 613 | 347 | | SetEntityInsideBoundariesState(entity, isInsideBoundaries); |
| 613 | 348 | | } |
| | 349 | |
|
| | 350 | | private void SetEntityMeshesInsideBoundariesState(MeshesInfo meshesInfo, bool isInsideBoundaries) |
| | 351 | | { |
| 613 | 352 | | feedbackStyle.ApplyFeedback(meshesInfo, isInsideBoundaries); |
| 613 | 353 | | } |
| | 354 | |
|
| | 355 | | private void SetEntityCollidersInsideBoundariesState(MeshesInfo meshesInfo, bool isInsideBoundaries) |
| | 356 | | { |
| 613 | 357 | | if (meshesInfo == null || meshesInfo.colliders.Count == 0 || !meshesInfo.currentShape.HasCollisions()) |
| 394 | 358 | | return; |
| | 359 | |
|
| 978 | 360 | | foreach (Collider collider in meshesInfo.colliders) |
| | 361 | | { |
| 270 | 362 | | if (collider == null) continue; |
| | 363 | |
|
| 270 | 364 | | if (collider.enabled != isInsideBoundaries) |
| 140 | 365 | | collider.enabled = isInsideBoundaries; |
| | 366 | | } |
| 219 | 367 | | } |
| | 368 | |
|
| | 369 | | private void SetComponentsInsideBoundariesValidState(IDCLEntity entity, bool isInsideBoundaries) |
| | 370 | | { |
| 687 | 371 | | if(entity.isInsideSceneBoundaries == isInsideBoundaries || !DataStore.i.sceneBoundariesChecker.componentsChe |
| 684 | 372 | | return; |
| | 373 | |
|
| 12 | 374 | | foreach (IOutOfSceneBoundariesHandler component in DataStore.i.sceneBoundariesChecker.componentsCheckSceneBo |
| | 375 | | { |
| 3 | 376 | | component.UpdateOutOfBoundariesState(isInsideBoundaries); |
| | 377 | | } |
| 3 | 378 | | } |
| | 379 | | } |
| | 380 | | } |