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