| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Rendering; |
| | 4 | | using NSubstitute; |
| | 5 | | using NSubstitute.Exceptions; |
| | 6 | | using NUnit.Framework; |
| | 7 | | using UnityEditor; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.Rendering; |
| | 10 | | using UnityEngine.Rendering.Universal; |
| | 11 | | using UnityEngine.TestTools; |
| | 12 | | using Assert = UnityEngine.Assertions.Assert; |
| | 13 | |
|
| | 14 | | namespace CullingControllerTests |
| | 15 | | { |
| | 16 | | public class CullingControllerShould |
| | 17 | | { |
| | 18 | | public CullingController cullingController; |
| | 19 | |
|
| | 20 | | [SetUp] |
| 18 | 21 | | public void SetUp() { cullingController = CreateMockedCulledController(null, null, null); } |
| | 22 | |
|
| | 23 | | private static CullingController CreateMockedCulledController(UniversalRenderPipelineAsset urpAsset, CullingCont |
| | 24 | | { |
| 10 | 25 | | var result = Substitute.ForPartsOf<CullingController>( |
| | 26 | | urpAsset == null ? GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset : urpAsset, |
| | 27 | | settings ?? new CullingControllerSettings(), |
| | 28 | | cullingObjectsTracker ?? new CullingObjectsTracker()); |
| | 29 | |
|
| 10 | 30 | | result.SetSettings(new CullingControllerSettings() { maxTimeBudget = float.MaxValue }); |
| | 31 | |
|
| 10 | 32 | | return result; |
| | 33 | | } |
| | 34 | |
|
| | 35 | | [TearDown] |
| 18 | 36 | | public void TearDown() { cullingController.Dispose(); } |
| | 37 | |
|
| | 38 | | [Test] |
| | 39 | | public void ReturnCopyWhenGetSettingsIsCalled() |
| | 40 | | { |
| | 41 | | // Ensure settings never return null |
| 1 | 42 | | var settings = cullingController.GetSettingsCopy(); |
| 1 | 43 | | Assert.IsNotNull(settings, "Settings should never be null!"); |
| | 44 | |
|
| 1 | 45 | | var prevAnimationCulling = settings.enableAnimationCulling; |
| | 46 | |
|
| | 47 | | // This is needed because SetSettings sets dirty flag and other stuff. We don't want direct access. |
| | 48 | | // Settings is not a property because to make the SetSetting performance hit more obvious. |
| 1 | 49 | | settings.enableAnimationCulling = !settings.enableAnimationCulling; |
| 1 | 50 | | settings = cullingController.GetSettingsCopy(); |
| 1 | 51 | | Assert.IsTrue(settings.enableAnimationCulling == prevAnimationCulling, "GetSettings should return a copy!"); |
| 1 | 52 | | } |
| | 53 | |
|
| | 54 | | [Test] |
| | 55 | | public void SetSettingsProperly() |
| | 56 | | { |
| | 57 | | // Ensure settings never return null |
| 1 | 58 | | var settings = cullingController.GetSettingsCopy(); |
| 1 | 59 | | Assert.IsNotNull(settings, "Settings should never be null!"); |
| | 60 | |
|
| 1 | 61 | | var prevAnimationCulling = settings.enableAnimationCulling; |
| | 62 | |
|
| | 63 | | // Ensure SetSettings works as intended. |
| 1 | 64 | | settings.enableAnimationCulling = !settings.enableAnimationCulling; |
| 1 | 65 | | cullingController.SetSettings(settings); |
| 1 | 66 | | settings = cullingController.GetSettingsCopy(); |
| 1 | 67 | | Assert.IsTrue(settings.enableAnimationCulling != prevAnimationCulling, "SetSettings should assign the settin |
| 1 | 68 | | } |
| | 69 | |
|
| | 70 | | [Test] |
| | 71 | | public void EvaluateRendererVisibility() |
| | 72 | | { |
| | 73 | | //Arrange |
| 1 | 74 | | var profile = new CullingControllerProfile(); |
| 1 | 75 | | profile.emissiveSizeThreshold = 10; |
| 1 | 76 | | profile.opaqueSizeThreshold = 20; |
| 1 | 77 | | profile.visibleDistanceThreshold = 5; |
| | 78 | |
|
| | 79 | | // Act |
| | 80 | | // Truth tests |
| 1 | 81 | | var farAndBigTest = CullingControllerUtils.TestRendererVisibleRule(profile, 30, 20, false, true, true); |
| 1 | 82 | | var smallAndNearTest = CullingControllerUtils.TestRendererVisibleRule(profile, 5, 2, false, true, true); |
| 1 | 83 | | var cameraInBoundsTest = CullingControllerUtils.TestRendererVisibleRule(profile, 1, 100, true, true, true); |
| 1 | 84 | | var emissiveTest = CullingControllerUtils.TestRendererVisibleRule(profile, 15, 20, false, false, true); |
| | 85 | |
|
| | 86 | | // Falsehood tests |
| 1 | 87 | | var farAndSmallTest = CullingControllerUtils.TestRendererVisibleRule(profile, 5, 20, false, true, true); |
| 1 | 88 | | var emissiveAndFarTest = CullingControllerUtils.TestRendererVisibleRule(profile, 5, 20, false, false, true); |
| 1 | 89 | | var farAndTransparentTest = CullingControllerUtils.TestRendererVisibleRule(profile, 1, 50, false, false, fal |
| | 90 | |
|
| | 91 | | // Assert |
| 1 | 92 | | Assert.IsTrue(farAndBigTest); |
| 1 | 93 | | Assert.IsTrue(smallAndNearTest); |
| 1 | 94 | | Assert.IsTrue(cameraInBoundsTest); |
| 1 | 95 | | Assert.IsTrue(emissiveTest); |
| | 96 | |
|
| 1 | 97 | | Assert.IsFalse(farAndSmallTest); |
| 1 | 98 | | Assert.IsFalse(emissiveAndFarTest); |
| 1 | 99 | | Assert.IsFalse(farAndTransparentTest); |
| 1 | 100 | | } |
| | 101 | |
|
| | 102 | | [Test] |
| | 103 | | public void EvaluateShadowVisibility() |
| | 104 | | { |
| | 105 | | // Arrange |
| 1 | 106 | | var profile = new CullingControllerProfile(); |
| 1 | 107 | | profile.shadowMapProjectionSizeThreshold = 6; |
| 1 | 108 | | profile.shadowRendererSizeThreshold = 20; |
| 1 | 109 | | profile.shadowDistanceThreshold = 15; |
| | 110 | |
|
| | 111 | | // Act |
| 1 | 112 | | var nearTest = CullingControllerUtils.TestRendererShadowRule(profile, 1, 5, 10); |
| 1 | 113 | | var nearButSmallTexel = CullingControllerUtils.TestRendererShadowRule(profile, 1, 5, 1); |
| 1 | 114 | | var farAndBigEnough = CullingControllerUtils.TestRendererShadowRule(profile, 30, 30, 30); |
| 1 | 115 | | var farAndSmall = CullingControllerUtils.TestRendererShadowRule(profile, 10, 30, 30); |
| 1 | 116 | | var farAndSmallTexel = CullingControllerUtils.TestRendererShadowRule(profile, 10, 30, 1); |
| | 117 | |
|
| | 118 | | // Assert |
| 1 | 119 | | Assert.IsTrue(nearTest); |
| 1 | 120 | | Assert.IsTrue(farAndBigEnough); |
| 1 | 121 | | Assert.IsFalse(nearButSmallTexel); |
| 1 | 122 | | Assert.IsFalse(farAndSmall); |
| 1 | 123 | | Assert.IsFalse(farAndSmallTexel); |
| 1 | 124 | | } |
| | 125 | |
|
| | 126 | | [Test] |
| | 127 | | public void EvaluateSkinnedMeshesOffscreenUpdate() |
| | 128 | | { |
| | 129 | | // Arrange |
| 1 | 130 | | var profile = new CullingControllerProfile(); |
| 1 | 131 | | profile.shadowMapProjectionSizeThreshold = 6; |
| 1 | 132 | | profile.shadowRendererSizeThreshold = 20; |
| 1 | 133 | | profile.shadowDistanceThreshold = 15; |
| | 134 | |
|
| 1 | 135 | | var settings = new CullingControllerSettings(); |
| 1 | 136 | | settings.enableAnimationCullingDistance = 20; |
| 1 | 137 | | settings.enableAnimationCulling = true; |
| | 138 | |
|
| | 139 | | // Act |
| 1 | 140 | | var farTest = CullingControllerUtils.TestSkinnedRendererOffscreenRule(settings, 30); |
| 1 | 141 | | var nearTest = CullingControllerUtils.TestSkinnedRendererOffscreenRule(settings, 10); |
| | 142 | |
|
| 1 | 143 | | settings.enableAnimationCulling = false; |
| 1 | 144 | | var farTestWithCullingDisabled = CullingControllerUtils.TestSkinnedRendererOffscreenRule(settings, 30); |
| | 145 | |
|
| | 146 | | // Assert |
| 1 | 147 | | Assert.IsTrue(nearTest); |
| 1 | 148 | | Assert.IsFalse(farTest); |
| 1 | 149 | | Assert.IsTrue(farTestWithCullingDisabled); |
| 1 | 150 | | } |
| | 151 | |
|
| | 152 | | [Test] |
| | 153 | | public void ResetObjects() |
| | 154 | | { |
| | 155 | | // Arrange |
| 1 | 156 | | GameObject go1 = new GameObject(); |
| 1 | 157 | | GameObject go2 = new GameObject(); |
| | 158 | |
|
| 1 | 159 | | var r = go1.AddComponent<MeshRenderer>(); |
| 1 | 160 | | var skr = go2.AddComponent<SkinnedMeshRenderer>(); |
| 1 | 161 | | var anim = go2.AddComponent<Animation>(); |
| | 162 | |
|
| 1 | 163 | | r.forceRenderingOff = true; |
| 1 | 164 | | skr.updateWhenOffscreen = false; |
| 1 | 165 | | anim.cullingType = AnimationCullingType.BasedOnRenderers; |
| | 166 | |
|
| 1 | 167 | | var mockTracker = Substitute.For<ICullingObjectsTracker>(); |
| 1 | 168 | | cullingController = CreateMockedCulledController(null, null, mockTracker); |
| | 169 | |
|
| 2 | 170 | | mockTracker.GetRenderers().Returns(info => go1.GetComponentsInChildren<Renderer>()); |
| 2 | 171 | | mockTracker.GetSkinnedRenderers().Returns(info => go2.GetComponentsInChildren<SkinnedMeshRenderer>()); |
| 2 | 172 | | mockTracker.GetAnimations().Returns(info => go2.GetComponentsInChildren<Animation>()); |
| | 173 | |
|
| | 174 | | // Act |
| 1 | 175 | | cullingController.ResetObjects(); |
| | 176 | |
|
| | 177 | | // Assert |
| 1 | 178 | | Assert.IsFalse(r.forceRenderingOff); |
| 1 | 179 | | Assert.IsTrue(skr.updateWhenOffscreen); |
| 1 | 180 | | Assert.IsTrue(anim.cullingType == AnimationCullingType.AlwaysAnimate); |
| | 181 | |
|
| | 182 | | // Annihilate |
| 1 | 183 | | Object.Destroy(go1); |
| 1 | 184 | | Object.Destroy(go2); |
| 1 | 185 | | } |
| | 186 | |
|
| | 187 | | [UnityTest] |
| | 188 | | public IEnumerator ProcessAnimationCulling() |
| | 189 | | { |
| | 190 | | // Pre-arrange |
| | 191 | | const int GO_COUNT = 3; |
| 1 | 192 | | GameObject[] gos = new GameObject[GO_COUNT]; |
| 1 | 193 | | Animation[] anims = new Animation[GO_COUNT]; |
| | 194 | |
|
| 8 | 195 | | for (int i = 0; i < GO_COUNT; i++) |
| | 196 | | { |
| 3 | 197 | | gos[i] = new GameObject("Test " + i); |
| 3 | 198 | | anims[i] = gos[i].AddComponent<Animation>(); |
| 3 | 199 | | anims[i].cullingType = AnimationCullingType.AlwaysAnimate; |
| | 200 | | } |
| | 201 | |
|
| 1 | 202 | | var settings = cullingController.GetSettingsCopy(); |
| | 203 | |
|
| 1 | 204 | | settings.enableAnimationCulling = true; |
| 1 | 205 | | settings.enableAnimationCullingDistance = 15; |
| | 206 | |
|
| 1 | 207 | | cullingController.SetSettings(settings); |
| | 208 | |
|
| 1 | 209 | | cullingController.objectsTracker.MarkDirty(); |
| 1 | 210 | | yield return cullingController.objectsTracker.PopulateRenderersList(); |
| | 211 | |
|
| | 212 | | // Test #1 |
| 1 | 213 | | gos[0].transform.position = Vector3.zero; |
| 1 | 214 | | gos[1].transform.position = new Vector3(0, 0, 30); |
| 1 | 215 | | gos[2].transform.position = new Vector3(0, 0, 10); |
| 1 | 216 | | CommonScriptableObjects.playerUnityPosition.Set(Vector3.zero); |
| | 217 | |
|
| 1 | 218 | | yield return cullingController.ProcessAnimations(); |
| | 219 | |
|
| 1 | 220 | | Assert.AreEqual(AnimationCullingType.AlwaysAnimate, anims[0].cullingType); |
| 1 | 221 | | Assert.AreEqual(AnimationCullingType.BasedOnRenderers, anims[1].cullingType); |
| 1 | 222 | | Assert.AreEqual(AnimationCullingType.AlwaysAnimate, anims[2].cullingType); |
| | 223 | |
|
| | 224 | | // Test #2 |
| 1 | 225 | | gos[2].transform.position = new Vector3(0, 0, 30); |
| 1 | 226 | | CommonScriptableObjects.playerUnityPosition.Set(Vector3.zero); |
| | 227 | |
|
| 1 | 228 | | yield return cullingController.ProcessAnimations(); |
| | 229 | |
|
| 1 | 230 | | Assert.AreEqual(AnimationCullingType.BasedOnRenderers, anims[2].cullingType); |
| | 231 | |
|
| | 232 | | // Test #3 |
| 1 | 233 | | gos[2].transform.position = new Vector3(0, 0, 10); |
| 1 | 234 | | CommonScriptableObjects.playerUnityPosition.Set(Vector3.zero); |
| | 235 | |
|
| 1 | 236 | | yield return cullingController.ProcessAnimations(); |
| | 237 | |
|
| | 238 | | // Test #4 |
| 1 | 239 | | settings.enableAnimationCulling = false; |
| 1 | 240 | | CommonScriptableObjects.playerUnityPosition.Set(Vector3.one * 10000); |
| | 241 | |
|
| 8 | 242 | | for (int i = 0; i < GO_COUNT; i++) |
| | 243 | | { |
| 3 | 244 | | anims[i].cullingType = AnimationCullingType.AlwaysAnimate; |
| | 245 | | } |
| | 246 | |
|
| 1 | 247 | | cullingController.SetSettings(settings); |
| | 248 | |
|
| 1 | 249 | | yield return cullingController.ProcessAnimations(); |
| | 250 | |
|
| 8 | 251 | | for (int i = 0; i < GO_COUNT; i++) |
| | 252 | | { |
| 3 | 253 | | Assert.AreEqual(AnimationCullingType.AlwaysAnimate, anims[i].cullingType); |
| | 254 | | } |
| | 255 | |
|
| | 256 | | // Annihilate |
| 8 | 257 | | for (int i = 0; i < GO_COUNT; i++) |
| | 258 | | { |
| 3 | 259 | | Object.Destroy(gos[i]); |
| | 260 | | } |
| 1 | 261 | | } |
| | 262 | |
|
| | 263 | | [UnityTest] |
| | 264 | | public IEnumerator ReactToRendererState() |
| | 265 | | { |
| 1 | 266 | | cullingController.Start(); |
| 1 | 267 | | yield return null; |
| 1 | 268 | | CommonScriptableObjects.rendererState.Set(true); |
| 1 | 269 | | CommonScriptableObjects.rendererState.Set(false); |
| 1 | 270 | | Assert.IsFalse(cullingController.IsRunning()); |
| 1 | 271 | | CommonScriptableObjects.rendererState.Set(true); |
| 1 | 272 | | Assert.IsTrue(cullingController.IsRunning()); |
| 1 | 273 | | CommonScriptableObjects.rendererState.Set(false); |
| 1 | 274 | | Assert.IsFalse(cullingController.IsRunning()); |
| 1 | 275 | | cullingController.Stop(); |
| 1 | 276 | | yield return null; |
| 1 | 277 | | CommonScriptableObjects.rendererState.Set(true); |
| 1 | 278 | | Assert.IsFalse(cullingController.IsRunning()); |
| 1 | 279 | | } |
| | 280 | |
|
| | 281 | | [UnityTest] |
| | 282 | | public IEnumerator ProcessProfile() |
| | 283 | | { |
| | 284 | | // Pre-arrange |
| | 285 | | const int GO_COUNT = 10; |
| 1 | 286 | | GameObject[] gos = new GameObject[GO_COUNT]; |
| 1 | 287 | | Renderer[] rends = new Renderer[GO_COUNT]; |
| | 288 | |
|
| 22 | 289 | | for (int i = 0; i < GO_COUNT; i++) |
| | 290 | | { |
| 10 | 291 | | gos[i] = GameObject.CreatePrimitive(PrimitiveType.Cube); |
| | 292 | |
|
| 10 | 293 | | if (i >= GO_COUNT >> 1) |
| | 294 | | { |
| 5 | 295 | | Object.DestroyImmediate(gos[i].GetComponent<Renderer>()); |
| 5 | 296 | | gos[i].AddComponent<SkinnedMeshRenderer>(); |
| | 297 | | } |
| | 298 | |
|
| 10 | 299 | | rends[i] = gos[i].GetComponent<Renderer>(); |
| | 300 | | } |
| | 301 | |
|
| 1 | 302 | | var settings = cullingController.GetSettingsCopy(); |
| | 303 | |
|
| 1 | 304 | | settings.skinnedRendererProfile.visibleDistanceThreshold = 0; |
| 1 | 305 | | settings.skinnedRendererProfile.emissiveSizeThreshold = 0; |
| 1 | 306 | | settings.skinnedRendererProfile.opaqueSizeThreshold = 0; |
| 1 | 307 | | settings.skinnedRendererProfile.shadowDistanceThreshold = 0; |
| 1 | 308 | | settings.skinnedRendererProfile.shadowRendererSizeThreshold = 0; |
| 1 | 309 | | settings.skinnedRendererProfile.shadowMapProjectionSizeThreshold = 0; |
| | 310 | |
|
| 1 | 311 | | settings.rendererProfile.visibleDistanceThreshold = 0; |
| 1 | 312 | | settings.rendererProfile.emissiveSizeThreshold = 0; |
| 1 | 313 | | settings.rendererProfile.opaqueSizeThreshold = 0; |
| 1 | 314 | | settings.rendererProfile.shadowDistanceThreshold = 0; |
| 1 | 315 | | settings.rendererProfile.shadowRendererSizeThreshold = 0; |
| 1 | 316 | | settings.rendererProfile.shadowMapProjectionSizeThreshold = 0; |
| | 317 | |
|
| 1 | 318 | | settings.enableObjectCulling = true; |
| | 319 | |
|
| 1 | 320 | | cullingController.SetSettings(settings); |
| 1 | 321 | | cullingController.objectsTracker.MarkDirty(); |
| | 322 | |
|
| 1 | 323 | | yield return cullingController.objectsTracker.PopulateRenderersList(); |
| | 324 | |
|
| 1 | 325 | | yield return cullingController.ProcessProfile(settings.rendererProfile); |
| | 326 | |
|
| 12 | 327 | | for (int i = 0; i < 5; i++) |
| | 328 | | { |
| 5 | 329 | | cullingController.Received().SetCullingForRenderer(rends[i], true, true); |
| | 330 | | } |
| | 331 | |
|
| 1 | 332 | | yield return cullingController.ProcessProfile(settings.skinnedRendererProfile); |
| | 333 | |
|
| 12 | 334 | | for (int i = 5; i < GO_COUNT; i++) |
| | 335 | | { |
| 5 | 336 | | cullingController.Received().SetCullingForRenderer(rends[i], true, true); |
| | 337 | | } |
| | 338 | |
|
| | 339 | | // Annihilate |
| 22 | 340 | | for (int i = 0; i < GO_COUNT; i++) |
| | 341 | | { |
| 10 | 342 | | Object.Destroy(gos[i]); |
| | 343 | | } |
| 1 | 344 | | } |
| | 345 | | } |
| | 346 | | } |