| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Models; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Rendering; |
| | 6 | | using UniversalRenderPipelineAsset = UnityEngine.Rendering.Universal.UniversalRenderPipelineAsset; |
| | 7 | | using static DCL.Rendering.CullingControllerUtils; |
| | 8 | |
|
| | 9 | | namespace DCL.Rendering |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// CullingController has the following responsibilities: |
| | 13 | | /// - Hides small renderers (detail objects). |
| | 14 | | /// - Disable unneeded shadows. |
| | 15 | | /// - Enable/disable animation culling for skinned renderers and animation components. |
| | 16 | | /// </summary> |
| | 17 | | public class CullingController : ICullingController |
| | 18 | | { |
| | 19 | | private const string ANIMATION_CULLING_STATUS_FEATURE_FLAG = "animation_culling_status"; |
| | 20 | | private const bool DRAW_GIZMOS = false; |
| | 21 | | internal List<CullingControllerProfile> profiles = null; |
| | 22 | |
|
| | 23 | | private CullingControllerSettings settings; |
| | 24 | |
|
| 114 | 25 | | private HashSet<Renderer> hiddenRenderers = new HashSet<Renderer>(); |
| 114 | 26 | | private HashSet<Renderer> shadowlessRenderers = new HashSet<Renderer>(); |
| | 27 | |
|
| | 28 | | public UniversalRenderPipelineAsset urpAsset; |
| | 29 | |
|
| 1087 | 30 | | public ICullingObjectsTracker objectsTracker { get; private set; } |
| | 31 | | private Coroutine updateCoroutine; |
| | 32 | | private float timeBudgetCount = 0; |
| | 33 | | private bool resetObjectsNextFrame = false; |
| | 34 | | private bool playerPositionDirty; |
| | 35 | | private bool objectPositionsDirty; |
| | 36 | | private bool running = false; |
| | 37 | |
|
| | 38 | | // Cache to avoid allocations when getting names |
| 114 | 39 | | private readonly HashSet<Shader> avatarShaders = new HashSet<Shader>(); |
| 114 | 40 | | private readonly HashSet<Shader> nonAvatarShaders = new HashSet<Shader>(); |
| | 41 | |
|
| 341 | 42 | | private BaseVariable<FeatureFlag> featureFlags => DataStore.i.featureFlags.flags; |
| | 43 | |
|
| | 44 | | public event ICullingController.DataReport OnDataReport; |
| | 45 | |
|
| | 46 | | public static CullingController Create() |
| | 47 | | { |
| 104 | 48 | | return new CullingController( |
| | 49 | | GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset, |
| | 50 | | new CullingControllerSettings() |
| | 51 | | ); |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | private CullingController() { } |
| | 55 | |
|
| 114 | 56 | | public CullingController(UniversalRenderPipelineAsset urpAsset, CullingControllerSettings settings, ICullingObje |
| | 57 | | { |
| 114 | 58 | | if (cullingObjectsTracker == null) |
| 104 | 59 | | objectsTracker = new CullingObjectsTracker(); |
| | 60 | | else |
| 10 | 61 | | objectsTracker = cullingObjectsTracker; |
| | 62 | |
|
| 114 | 63 | | objectsTracker.SetIgnoredLayersMask(settings.ignoredLayersMask); |
| | 64 | |
|
| 114 | 65 | | this.urpAsset = urpAsset; |
| 114 | 66 | | this.settings = settings; |
| | 67 | |
|
| 114 | 68 | | featureFlags.OnChange += OnFeatureFlagChange; |
| 114 | 69 | | OnFeatureFlagChange(featureFlags.Get(), null); |
| 114 | 70 | | } |
| | 71 | |
|
| | 72 | | private void OnFeatureFlagChange(FeatureFlag current, FeatureFlag previous) |
| | 73 | | { |
| 114 | 74 | | SetAnimationCulling(current.IsFeatureEnabled(ANIMATION_CULLING_STATUS_FEATURE_FLAG)); |
| 114 | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Starts culling update coroutine. |
| | 79 | | /// The coroutine will keep running until Stop() is called or this class is disposed. |
| | 80 | | /// </summary> |
| | 81 | | public void Start() |
| | 82 | | { |
| 111 | 83 | | if (running) |
| 0 | 84 | | return; |
| | 85 | |
|
| 111 | 86 | | running = true; |
| 111 | 87 | | CommonScriptableObjects.rendererState.OnChange += OnRendererStateChange; |
| 111 | 88 | | CommonScriptableObjects.playerUnityPosition.OnChange += OnPlayerUnityPositionChange; |
| 111 | 89 | | MeshesInfo.OnAnyUpdated += MarkDirty; |
| 111 | 90 | | objectsTracker?.MarkDirty(); |
| 111 | 91 | | StartInternal(); |
| 111 | 92 | | } |
| | 93 | |
|
| | 94 | | public void Restart() |
| | 95 | | { |
| 4 | 96 | | Stop(); |
| 4 | 97 | | Start(); |
| 4 | 98 | | } |
| | 99 | |
|
| | 100 | | private void StartInternal() |
| | 101 | | { |
| 114 | 102 | | if (updateCoroutine != null) |
| 2 | 103 | | return; |
| | 104 | |
|
| 112 | 105 | | RaiseDataReport(); |
| 112 | 106 | | profiles = new List<CullingControllerProfile> { settings.rendererProfile, settings.skinnedRendererProfile }; |
| 112 | 107 | | updateCoroutine = CoroutineStarter.Start(UpdateCoroutine()); |
| 112 | 108 | | } |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// Stops culling update coroutine. |
| | 112 | | /// </summary> |
| | 113 | | public void Stop() |
| | 114 | | { |
| 120 | 115 | | if (!running) |
| 9 | 116 | | return; |
| | 117 | |
|
| 111 | 118 | | running = false; |
| 111 | 119 | | CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChange; |
| 111 | 120 | | CommonScriptableObjects.playerUnityPosition.OnChange -= OnPlayerUnityPositionChange; |
| 111 | 121 | | MeshesInfo.OnAnyUpdated -= MarkDirty; |
| 111 | 122 | | StopInternal(); |
| 111 | 123 | | objectsTracker?.ForcePopulateRenderersList(); |
| 111 | 124 | | ResetObjects(); |
| 111 | 125 | | } |
| | 126 | |
|
| | 127 | | private void StopInternal() |
| | 128 | | { |
| 115 | 129 | | if (updateCoroutine == null) |
| 3 | 130 | | return; |
| | 131 | |
|
| 112 | 132 | | CoroutineStarter.Stop(updateCoroutine); |
| 112 | 133 | | updateCoroutine = null; |
| 112 | 134 | | } |
| | 135 | |
|
| | 136 | | /// <summary> |
| | 137 | | /// Process all sceneObject renderers with the parameters set by the given profile. |
| | 138 | | /// </summary> |
| | 139 | | /// <param name="profile">any CullingControllerProfile</param> |
| | 140 | | /// <returns>IEnumerator to be yielded.</returns> |
| | 141 | | internal IEnumerator ProcessProfile(CullingControllerProfile profile) |
| | 142 | | { |
| | 143 | | // If profile matches the skinned renderer profile in settings the skinned renderers are going to be used. |
| 14 | 144 | | IReadOnlyList<Renderer> renderers = profile == |
| | 145 | | settings.rendererProfile ? |
| | 146 | | objectsTracker.GetRenderers() : |
| | 147 | | objectsTracker.GetSkinnedRenderers(); |
| | 148 | |
|
| 14 | 149 | | yield return settings.enableShadowCulling |
| | 150 | | ? ProcessProfileWithEnabledCulling(profile, renderers) |
| | 151 | | : (object)ProcessProfileWithDisabledCulling(profile, renderers); |
| 12 | 152 | | } |
| | 153 | |
|
| | 154 | | internal IEnumerator ProcessProfileWithEnabledCulling(CullingControllerProfile profile, IReadOnlyList<Renderer> |
| | 155 | | { |
| 14 | 156 | | Vector3 playerPosition = CommonScriptableObjects.playerUnityPosition; |
| 14 | 157 | | float currentStartTime = Time.realtimeSinceStartup; |
| | 158 | |
|
| 50 | 159 | | foreach (Renderer r in renderers) |
| | 160 | | { |
| 11 | 161 | | if (r == null) |
| | 162 | | continue; |
| | 163 | |
|
| 11 | 164 | | if (Time.realtimeSinceStartup - currentStartTime >= CullingControllerSettings.MAX_TIME_BUDGET) |
| | 165 | | { |
| 1 | 166 | | yield return null; |
| 1 | 167 | | playerPosition = CommonScriptableObjects.playerUnityPosition; |
| 1 | 168 | | currentStartTime = Time.realtimeSinceStartup; |
| | 169 | | } |
| | 170 | |
|
| 11 | 171 | | Bounds bounds = MeshesInfoUtils.GetSafeBounds(r.bounds, r.transform.position); |
| 11 | 172 | | Vector3 boundingPoint = bounds.ClosestPoint(playerPosition); |
| | 173 | |
|
| 11 | 174 | | float distance = Vector3.Distance(playerPosition, boundingPoint); |
| 11 | 175 | | float boundsSize = bounds.size.magnitude; |
| 11 | 176 | | float viewportSize = (boundsSize / distance) * Mathf.Rad2Deg; |
| | 177 | |
|
| 11 | 178 | | float shadowTexelSize = ComputeShadowMapTexelSize(boundsSize, urpAsset.shadowDistance, urpAsset.mainLigh |
| | 179 | |
|
| 11 | 180 | | bool shouldBeVisible = |
| | 181 | | distance < profile.visibleDistanceThreshold || |
| | 182 | | bounds.Contains(playerPosition) || |
| | 183 | | // At the end we perform queries for emissive and opaque conditions |
| | 184 | | // these are the last conditions because IsEmissive and IsOpaque are a bit more costly |
| | 185 | | viewportSize > profile.emissiveSizeThreshold && IsEmissive(r) || |
| | 186 | | viewportSize > profile.opaqueSizeThreshold && IsOpaque(r) |
| | 187 | | ; |
| | 188 | |
|
| 11 | 189 | | bool shouldHaveShadow = !settings.enableShadowCulling || TestRendererShadowRule(profile, viewportSize, d |
| | 190 | |
|
| 11 | 191 | | if (r is SkinnedMeshRenderer skr) |
| | 192 | | { |
| 5 | 193 | | Material mat = skr.sharedMaterial; |
| | 194 | |
|
| 5 | 195 | | if (IsAvatarRenderer(mat)) |
| 0 | 196 | | shouldHaveShadow &= TestAvatarShadowRule(profile, distance); |
| | 197 | |
|
| 5 | 198 | | skr.updateWhenOffscreen = false; |
| | 199 | | } |
| | 200 | |
|
| 11 | 201 | | if (OnDataReport != null) |
| | 202 | | { |
| 0 | 203 | | if (!shouldBeVisible && !hiddenRenderers.Contains(r)) |
| 0 | 204 | | hiddenRenderers.Add(r); |
| | 205 | |
|
| 0 | 206 | | if (shouldBeVisible && !shouldHaveShadow && !shadowlessRenderers.Contains(r)) |
| 0 | 207 | | shadowlessRenderers.Add(r); |
| | 208 | | } |
| | 209 | |
|
| 11 | 210 | | SetCullingForRenderer(r, shouldBeVisible, shouldHaveShadow); |
| | 211 | | #if UNITY_EDITOR |
| | 212 | | if (DRAW_GIZMOS) |
| | 213 | | DrawDebugGizmos(shouldBeVisible, bounds, boundingPoint); |
| | 214 | | #endif |
| 11 | 215 | | } |
| 14 | 216 | | } |
| | 217 | |
|
| | 218 | | internal IEnumerator ProcessProfileWithDisabledCulling(CullingControllerProfile profile, IEnumerable<Renderer> r |
| | 219 | | { |
| 0 | 220 | | Vector3 playerPosition = CommonScriptableObjects.playerUnityPosition; |
| 0 | 221 | | float currentStartTime = Time.realtimeSinceStartup; |
| 0 | 222 | | foreach (Renderer r in renderers) |
| | 223 | | { |
| 0 | 224 | | if (r == null) |
| | 225 | | continue; |
| | 226 | |
|
| 0 | 227 | | if (Time.realtimeSinceStartup - currentStartTime >= CullingControllerSettings.MAX_TIME_BUDGET) |
| | 228 | | { |
| 0 | 229 | | yield return null; |
| 0 | 230 | | playerPosition = CommonScriptableObjects.playerUnityPosition; |
| 0 | 231 | | currentStartTime = Time.realtimeSinceStartup; |
| | 232 | | } |
| | 233 | |
|
| 0 | 234 | | Bounds bounds = MeshesInfoUtils.GetSafeBounds(r.bounds, r.transform.position); |
| 0 | 235 | | Vector3 boundingPoint = bounds.ClosestPoint(playerPosition); |
| | 236 | |
|
| 0 | 237 | | float distance = Vector3.Distance(playerPosition, boundingPoint); |
| 0 | 238 | | float boundsSize = bounds.size.magnitude; |
| 0 | 239 | | float viewportSize = (boundsSize / distance) * Mathf.Rad2Deg; |
| | 240 | |
|
| 0 | 241 | | float shadowTexelSize = ComputeShadowMapTexelSize(boundsSize, urpAsset.shadowDistance, urpAsset.mainLigh |
| 0 | 242 | | bool shouldHaveShadow = TestRendererShadowRule(profile, viewportSize, distance, shadowTexelSize); |
| | 243 | |
|
| 0 | 244 | | if (r is SkinnedMeshRenderer skr) |
| 0 | 245 | | skr.updateWhenOffscreen = false; |
| | 246 | |
|
| 0 | 247 | | if (OnDataReport != null) |
| | 248 | | { |
| 0 | 249 | | if (!shouldHaveShadow && !shadowlessRenderers.Contains(r)) |
| 0 | 250 | | shadowlessRenderers.Add(r); |
| | 251 | | } |
| | 252 | |
|
| 0 | 253 | | SetCullingForRenderer(r, true, shouldHaveShadow); |
| | 254 | |
|
| | 255 | | #if UNITY_EDITOR |
| | 256 | | if (DRAW_GIZMOS) |
| | 257 | | DrawDebugGizmos(true, bounds, boundingPoint); |
| | 258 | | #endif |
| 0 | 259 | | } |
| 0 | 260 | | } |
| | 261 | |
|
| | 262 | | /// <summary> |
| | 263 | | /// Checks if the material is from an Avatar by checking if the shader is DCL/Toon Shader |
| | 264 | | /// This Method avoids the allocation of the name getter by storing the result on a HashSet |
| | 265 | | /// </summary> |
| | 266 | | /// <param name="mat"></param> |
| | 267 | | /// <returns></returns> |
| | 268 | | private bool IsAvatarRenderer(Material mat) |
| | 269 | | { |
| 5 | 270 | | if (mat != null && mat.shader != null) |
| | 271 | | { |
| 0 | 272 | | Shader matShader = mat.shader; |
| | 273 | |
|
| 0 | 274 | | if (!avatarShaders.Contains(matShader) && !nonAvatarShaders.Contains(matShader)) |
| | 275 | | { |
| | 276 | | // This allocates memory on the GC |
| 0 | 277 | | bool isAvatar = matShader.name == "DCL/Toon Shader"; |
| | 278 | |
|
| 0 | 279 | | if (isAvatar) |
| 0 | 280 | | avatarShaders.Add(matShader); |
| | 281 | | else |
| 0 | 282 | | nonAvatarShaders.Add(matShader); |
| | 283 | | } |
| | 284 | |
|
| 0 | 285 | | return avatarShaders.Contains(matShader); |
| | 286 | |
|
| | 287 | | } |
| | 288 | |
|
| 5 | 289 | | return false; |
| | 290 | | } |
| | 291 | |
|
| | 292 | | /// <summary> |
| | 293 | | /// Main culling loop. Controlled by Start() and Stop() methods. |
| | 294 | | /// </summary> |
| | 295 | | IEnumerator UpdateCoroutine() |
| | 296 | | { |
| 5 | 297 | | while (true) |
| | 298 | | { |
| 11717 | 299 | | bool shouldCheck = objectPositionsDirty || playerPositionDirty; |
| | 300 | |
|
| 11717 | 301 | | playerPositionDirty = false; |
| 11717 | 302 | | objectPositionsDirty = false; |
| | 303 | |
|
| 11717 | 304 | | if (!shouldCheck) |
| | 305 | | { |
| 11703 | 306 | | timeBudgetCount = 0; |
| 11703 | 307 | | yield return null; |
| 11600 | 308 | | continue; |
| | 309 | | } |
| | 310 | |
|
| 14 | 311 | | yield return objectsTracker.PopulateRenderersList(); |
| | 312 | |
|
| 7 | 313 | | if (resetObjectsNextFrame) |
| | 314 | | { |
| 3 | 315 | | ResetObjects(); |
| 3 | 316 | | resetObjectsNextFrame = false; |
| | 317 | | } |
| | 318 | |
|
| 7 | 319 | | yield return ProcessAnimations(); |
| | 320 | |
|
| 7 | 321 | | if (OnDataReport != null) |
| | 322 | | { |
| 0 | 323 | | hiddenRenderers.Clear(); |
| 0 | 324 | | shadowlessRenderers.Clear(); |
| | 325 | | } |
| | 326 | |
|
| 7 | 327 | | int profilesCount = profiles.Count; |
| 34 | 328 | | for (int profileIndex = 0; profileIndex < profilesCount; profileIndex++) |
| 12 | 329 | | yield return ProcessProfile(profiles[profileIndex]); |
| | 330 | |
|
| 5 | 331 | | RaiseDataReport(); |
| 5 | 332 | | timeBudgetCount = 0; |
| 5 | 333 | | yield return null; |
| | 334 | | } |
| | 335 | | } |
| | 336 | |
|
| | 337 | | /// <summary> |
| | 338 | | /// Sets shadows and visibility for a given renderer. |
| | 339 | | /// </summary> |
| | 340 | | /// <param name="r">Renderer to be culled</param> |
| | 341 | | /// <param name="shouldBeVisible">If false, the renderer visibility will be set to false.</param> |
| | 342 | | /// <param name="shouldHaveShadow">If false, the renderer shadow will be toggled off.</param> |
| | 343 | | internal void SetCullingForRenderer(Renderer r, bool shouldBeVisible, bool shouldHaveShadow) |
| | 344 | | { |
| 21 | 345 | | var targetMode = shouldHaveShadow ? ShadowCastingMode.On : ShadowCastingMode.Off; |
| | 346 | |
|
| 21 | 347 | | if (r.forceRenderingOff != !shouldBeVisible) |
| 12 | 348 | | r.forceRenderingOff = !shouldBeVisible; |
| | 349 | |
|
| 21 | 350 | | if (r.shadowCastingMode != targetMode) |
| 13 | 351 | | r.shadowCastingMode = targetMode; |
| 21 | 352 | | } |
| | 353 | |
|
| | 354 | | /// <summary> |
| | 355 | | /// Sets cullingType to all tracked animation components according to our culling rules. |
| | 356 | | /// </summary> |
| | 357 | | /// <returns>IEnumerator to be yielded.</returns> |
| | 358 | | internal IEnumerator ProcessAnimations() |
| | 359 | | { |
| 11 | 360 | | if (!settings.enableAnimationCulling) |
| 8 | 361 | | yield break; |
| | 362 | |
|
| 3 | 363 | | Animation[] animations = objectsTracker.GetAnimations(); |
| 3 | 364 | | int animsLength = animations.Length; |
| | 365 | |
|
| 24 | 366 | | for (var i = 0; i < animsLength; i++) |
| | 367 | | { |
| 9 | 368 | | if (timeBudgetCount > CullingControllerSettings.MAX_TIME_BUDGET) |
| | 369 | | { |
| 0 | 370 | | timeBudgetCount = 0; |
| 0 | 371 | | yield return null; |
| | 372 | | } |
| | 373 | |
|
| 9 | 374 | | Animation anim = animations[i]; |
| | 375 | |
|
| 9 | 376 | | if (anim == null) |
| | 377 | | continue; |
| | 378 | |
|
| 9 | 379 | | float startTime = Time.realtimeSinceStartup; |
| 9 | 380 | | Transform t = anim.transform; |
| | 381 | |
|
| 9 | 382 | | Vector3 playerPosition = CommonScriptableObjects.playerUnityPosition; |
| 9 | 383 | | float distance = Vector3.Distance(playerPosition, t.position); |
| | 384 | |
|
| 9 | 385 | | if (distance > settings.enableAnimationCullingDistance) |
| 4 | 386 | | anim.cullingType = AnimationCullingType.BasedOnRenderers; |
| | 387 | | else |
| 5 | 388 | | anim.cullingType = AnimationCullingType.AlwaysAnimate; |
| | 389 | |
|
| 9 | 390 | | timeBudgetCount += Time.realtimeSinceStartup - startTime; |
| | 391 | | } |
| 3 | 392 | | } |
| | 393 | |
|
| | 394 | | /// <summary> |
| | 395 | | /// Reset all tracked renderers properties. Needed when toggling or changing settings. |
| | 396 | | /// </summary> |
| | 397 | | internal void ResetObjects() |
| | 398 | | { |
| 115 | 399 | | IEnumerable<Renderer> renderers = objectsTracker.GetRenderers(); |
| 115 | 400 | | IEnumerable<SkinnedMeshRenderer> skinnedRenderers = objectsTracker.GetSkinnedRenderers(); |
| 115 | 401 | | Animation[] animations = objectsTracker.GetAnimations(); |
| | 402 | |
|
| 246 | 403 | | foreach (Renderer renderer in renderers) |
| | 404 | | { |
| 8 | 405 | | if (renderer != null) |
| 8 | 406 | | renderer.forceRenderingOff = false; |
| | 407 | | } |
| | 408 | |
|
| 232 | 409 | | foreach (SkinnedMeshRenderer skinnedRenderer in skinnedRenderers) |
| | 410 | | { |
| 1 | 411 | | if (skinnedRenderer != null) |
| 1 | 412 | | skinnedRenderer.updateWhenOffscreen = true; |
| | 413 | | } |
| | 414 | |
|
| 232 | 415 | | for (int i = 0; i < animations?.Length; i++) |
| | 416 | | { |
| 1 | 417 | | if (animations[i] != null) |
| 1 | 418 | | animations[i].cullingType = AnimationCullingType.AlwaysAnimate; |
| | 419 | | } |
| 115 | 420 | | } |
| | 421 | |
|
| | 422 | | public void Dispose() |
| | 423 | | { |
| 113 | 424 | | objectsTracker.Dispose(); |
| 113 | 425 | | Stop(); |
| 113 | 426 | | featureFlags.OnChange -= OnFeatureFlagChange; |
| 113 | 427 | | } |
| | 428 | |
|
| | 429 | | public void Initialize() |
| | 430 | | { |
| 104 | 431 | | Start(); |
| 104 | 432 | | } |
| | 433 | |
|
| | 434 | | /// <summary> |
| | 435 | | /// Method suscribed to renderer state change |
| | 436 | | /// </summary> |
| | 437 | | private void OnRendererStateChange(bool rendererState, bool oldRendererState) |
| | 438 | | { |
| 7 | 439 | | if (!running) |
| 0 | 440 | | return; |
| | 441 | |
|
| 7 | 442 | | MarkDirty(); |
| | 443 | |
|
| 7 | 444 | | if (rendererState) |
| 3 | 445 | | StartInternal(); |
| | 446 | | else |
| 4 | 447 | | StopInternal(); |
| 4 | 448 | | } |
| | 449 | |
|
| | 450 | | /// <summary> |
| | 451 | | /// Method suscribed to playerUnityPosition change |
| | 452 | | /// </summary> |
| 78 | 453 | | private void OnPlayerUnityPositionChange(Vector3 previous, Vector3 current) { playerPositionDirty = true; } |
| | 454 | |
|
| | 455 | | /// <summary> |
| | 456 | | /// Sets the scene objects dirtiness. |
| | 457 | | /// In the next update iteration, all the scene objects are going to be gathered. |
| | 458 | | /// This method has performance impact. |
| | 459 | | /// </summary> |
| 64 | 460 | | public void MarkDirty() { objectPositionsDirty = true; } |
| | 461 | |
|
| | 462 | | /// <summary> |
| | 463 | | /// Gets the scene objects dirtiness. |
| | 464 | | /// </summary> |
| 2 | 465 | | public bool IsDirty() { return objectPositionsDirty; } |
| | 466 | |
|
| | 467 | | /// <summary> |
| | 468 | | /// Set settings. This will dirty the scene objects and has performance impact. |
| | 469 | | /// </summary> |
| | 470 | | /// <param name="settings">Settings to be set</param> |
| | 471 | | public void SetSettings(CullingControllerSettings settings) |
| | 472 | | { |
| 17 | 473 | | this.settings = settings; |
| 17 | 474 | | profiles = new List<CullingControllerProfile> { settings.rendererProfile, settings.skinnedRendererProfile }; |
| | 475 | |
|
| 17 | 476 | | objectsTracker?.SetIgnoredLayersMask(settings.ignoredLayersMask); |
| 17 | 477 | | objectsTracker?.MarkDirty(); |
| 17 | 478 | | MarkDirty(); |
| 17 | 479 | | resetObjectsNextFrame = true; |
| 17 | 480 | | } |
| | 481 | |
|
| | 482 | | /// <summary> |
| | 483 | | /// Get current settings copy. If you need to modify it, you must set them via SetSettings afterwards. |
| | 484 | | /// </summary> |
| | 485 | | /// <returns>Current settings object copy.</returns> |
| 7 | 486 | | public CullingControllerSettings GetSettingsCopy() { return settings.Clone(); } |
| | 487 | |
|
| | 488 | | /// <summary> |
| | 489 | | /// Enable or disable object visibility culling. |
| | 490 | | /// </summary> |
| | 491 | | /// <param name="enabled">If disabled, object visibility culling will be toggled. |
| | 492 | | /// </param> |
| | 493 | | public void SetObjectCulling(bool enabled) |
| | 494 | | { |
| 0 | 495 | | if (settings.enableObjectCulling == enabled) |
| 0 | 496 | | return; |
| | 497 | |
|
| 0 | 498 | | settings.enableObjectCulling = enabled; |
| 0 | 499 | | resetObjectsNextFrame = true; |
| 0 | 500 | | MarkDirty(); |
| 0 | 501 | | objectsTracker?.MarkDirty(); |
| 0 | 502 | | } |
| | 503 | |
|
| | 504 | | /// <summary> |
| | 505 | | /// Enable or disable animation culling. |
| | 506 | | /// </summary> |
| | 507 | | /// <param name="enabled">If disabled, animation culling will be toggled.</param> |
| | 508 | | public void SetAnimationCulling(bool enabled) |
| | 509 | | { |
| 114 | 510 | | if (settings.enableAnimationCulling == enabled) |
| 114 | 511 | | return; |
| | 512 | |
|
| 0 | 513 | | settings.enableAnimationCulling = enabled; |
| 0 | 514 | | resetObjectsNextFrame = true; |
| 0 | 515 | | MarkDirty(); |
| 0 | 516 | | objectsTracker?.MarkDirty(); |
| 0 | 517 | | } |
| | 518 | |
|
| | 519 | | /// <summary> |
| | 520 | | /// Enable or disable shadow culling |
| | 521 | | /// </summary> |
| | 522 | | /// <param name="enabled">If disabled, no shadows will be toggled.</param> |
| | 523 | | public void SetShadowCulling(bool enabled) |
| | 524 | | { |
| 0 | 525 | | if (settings.enableShadowCulling == enabled) |
| 0 | 526 | | return; |
| | 527 | |
|
| 0 | 528 | | settings.enableShadowCulling = enabled; |
| 0 | 529 | | resetObjectsNextFrame = true; |
| 0 | 530 | | MarkDirty(); |
| 0 | 531 | | objectsTracker?.MarkDirty(); |
| 0 | 532 | | } |
| | 533 | |
|
| | 534 | | /// <summary> |
| | 535 | | /// Fire the DataReport event. This will be useful for showing stats in a debug panel. |
| | 536 | | /// </summary> |
| | 537 | | private void RaiseDataReport() |
| | 538 | | { |
| 117 | 539 | | if (OnDataReport == null) |
| 117 | 540 | | return; |
| | 541 | |
|
| 0 | 542 | | int rendererCount = (objectsTracker.GetRenderers()?.Count ?? 0) + (objectsTracker.GetSkinnedRenderers()?.Cou |
| | 543 | |
|
| 0 | 544 | | OnDataReport.Invoke(rendererCount, hiddenRenderers.Count, shadowlessRenderers.Count); |
| 0 | 545 | | } |
| | 546 | |
|
| | 547 | | /// <summary> |
| | 548 | | /// Returns true if the culling loop is running |
| | 549 | | /// </summary> |
| | 550 | | public bool IsRunning() |
| | 551 | | { |
| 8 | 552 | | return updateCoroutine != null; |
| | 553 | | } |
| | 554 | |
|
| | 555 | | /// <summary> |
| | 556 | | /// Draw debug gizmos on the scene view. |
| | 557 | | /// </summary> |
| | 558 | | /// <param name="shouldBeVisible"></param> |
| | 559 | | /// <param name="bounds"></param> |
| | 560 | | /// <param name="boundingPoint"></param> |
| | 561 | | private static void DrawDebugGizmos(bool shouldBeVisible, Bounds bounds, Vector3 boundingPoint) |
| | 562 | | { |
| 0 | 563 | | if (!shouldBeVisible) |
| | 564 | | { |
| 0 | 565 | | DrawBounds(bounds, Color.blue, 1); |
| 0 | 566 | | DrawBounds(new Bounds() { center = boundingPoint, size = Vector3.one }, Color.red, 1); |
| | 567 | | } |
| 0 | 568 | | } |
| | 569 | | } |
| | 570 | | } |