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