|
| 1 | +using NUnit.Framework; |
| 2 | +using System; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using UnityEditor; |
| 5 | +using UnityEditor.SceneManagement; |
| 6 | +using UnityEditor.TestTools.Graphics; |
| 7 | +using UnityEngine; |
| 8 | +using UnityEngine.Rendering; |
| 9 | +using UnityEngine.SceneManagement; |
| 10 | +using UnityEngine.TestTools.Graphics; |
| 11 | + |
| 12 | +public class SceneViewVisibilityTests |
| 13 | +{ |
| 14 | + static int s_ForwardPlusQualityLevel = 7; |
| 15 | + int m_PreviousQualityLevelIndex = 0; |
| 16 | + |
| 17 | + [SetUp] |
| 18 | + public void SetUp() |
| 19 | + { |
| 20 | + const string scenePath = "Assets/CodeBasedTests/SceneViewVisibility.unity"; |
| 21 | + EditorSceneManager.OpenScene(scenePath); |
| 22 | + |
| 23 | + m_PreviousQualityLevelIndex = QualitySettings.GetQualityLevel(); |
| 24 | + QualitySettings.SetQualityLevel(s_ForwardPlusQualityLevel, true); |
| 25 | + } |
| 26 | + |
| 27 | + [TearDown] |
| 28 | + public void TearDown() |
| 29 | + { |
| 30 | + QualitySettings.SetQualityLevel(m_PreviousQualityLevelIndex, true); |
| 31 | + } |
| 32 | + |
| 33 | + async Task RunTest(GraphicsTestCase testCase, Action<SceneView> setup) |
| 34 | + { |
| 35 | + const int numFramesToWarmup = 4; |
| 36 | + |
| 37 | + var sceneView = EditorWindow.CreateWindow<SceneView>(); |
| 38 | + sceneView.overlayCanvas.overlaysEnabled = false; |
| 39 | + sceneView.showGrid = false; |
| 40 | + sceneView.name = "TestSceneView"; |
| 41 | + |
| 42 | + for (int i = 0; i < numFramesToWarmup; i++) |
| 43 | + { |
| 44 | + await Task.Yield(); |
| 45 | + } |
| 46 | + |
| 47 | + setup(sceneView); |
| 48 | + |
| 49 | + var halfASecond = new TimeSpan(0, 0, 0, 0, 500); |
| 50 | + var captureSettings = new SceneViewCaptureSettings(128, 128, halfASecond, halfASecond, Camera.main.transform); |
| 51 | + |
| 52 | + var renderPipelineAsset = GraphicsSettings.currentRenderPipeline as IGPUResidentRenderPipeline; |
| 53 | + Assert.IsNotNull(renderPipelineAsset); |
| 54 | + |
| 55 | + Texture2D capturedTexture = null; |
| 56 | + Texture2D grdCapturedTexture = null; |
| 57 | + |
| 58 | + // GRD not supported on OpenGLCore |
| 59 | + if (SystemInfo.graphicsDeviceType != GraphicsDeviceType.OpenGLCore) |
| 60 | + { |
| 61 | + // Capture with GRD |
| 62 | + renderPipelineAsset.gpuResidentDrawerMode = GPUResidentDrawerMode.InstancedDrawing; |
| 63 | + sceneView.Repaint(); |
| 64 | + grdCapturedTexture = await EditorWindowCapture.CaptureAsync(sceneView, captureSettings); |
| 65 | + } |
| 66 | + |
| 67 | + // Capture without GRD |
| 68 | + renderPipelineAsset.gpuResidentDrawerMode = GPUResidentDrawerMode.Disabled; |
| 69 | + sceneView.Repaint(); |
| 70 | + capturedTexture = await EditorWindowCapture.CaptureAsync(sceneView, captureSettings); |
| 71 | + |
| 72 | + sceneView.Close(); |
| 73 | + |
| 74 | + ImageComparisonSettings imageComparisonSettings = new() |
| 75 | + { |
| 76 | + TargetWidth = capturedTexture.width, |
| 77 | + TargetHeight = capturedTexture.height, |
| 78 | + AverageCorrectnessThreshold = 0.01f |
| 79 | + }; |
| 80 | + |
| 81 | + try |
| 82 | + { |
| 83 | + ImageAssert.AreEqual(testCase.ReferenceImage.Image, capturedTexture, imageComparisonSettings); |
| 84 | + } |
| 85 | + catch (Exception e) |
| 86 | + { |
| 87 | + Assert.Fail("Without GPU Resident Drawer : " + e.Message); |
| 88 | + } |
| 89 | + |
| 90 | + try |
| 91 | + { |
| 92 | + if (grdCapturedTexture != null) |
| 93 | + ImageAssert.AreEqual(testCase.ReferenceImage.Image, grdCapturedTexture, imageComparisonSettings); |
| 94 | + } |
| 95 | + catch (Exception e) |
| 96 | + { |
| 97 | + Assert.Fail("With GPU Resident Drawer : " + e.Message); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + void SetupGameObjectVisibility(SceneView _) |
| 102 | + { |
| 103 | + var cube = GameObject.Find("Cube"); |
| 104 | + var sphere = GameObject.Find("Sphere"); |
| 105 | + |
| 106 | + if (cube) |
| 107 | + { |
| 108 | + SceneVisibilityManager.instance.Hide(cube, false); |
| 109 | + } |
| 110 | + |
| 111 | + if (sphere) |
| 112 | + { |
| 113 | + SceneVisibilityManager.instance.Hide(sphere, false); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + void SetupSceneVisibility(SceneView sceneView) |
| 118 | + { |
| 119 | + SetupGameObjectVisibility(sceneView); |
| 120 | + SceneVisibilityManager.instance.Hide(SceneManager.GetActiveScene()); |
| 121 | + } |
| 122 | + |
| 123 | + void SetupSceneVisibilityReset(SceneView sceneView) |
| 124 | + { |
| 125 | + SetupSceneVisibility(sceneView); |
| 126 | + SceneVisibilityManager.instance.Show(SceneManager.GetActiveScene()); |
| 127 | + } |
| 128 | + |
| 129 | + static void SetSceneVisibilityEnabled(SceneView sceneView, bool isEnabled) |
| 130 | + { |
| 131 | + sceneView.sceneVisActive = isEnabled; |
| 132 | + } |
| 133 | + |
| 134 | + void SetupSceneVisibilityEnabled(SceneView sceneView) |
| 135 | + { |
| 136 | + SetupSceneVisibility(sceneView); |
| 137 | + SetSceneVisibilityEnabled(sceneView, false); |
| 138 | + } |
| 139 | + |
| 140 | + void SetupSceneVisibilityEnabledReset(SceneView sceneView) |
| 141 | + { |
| 142 | + SetupSceneVisibilityEnabled(sceneView); |
| 143 | + SetSceneVisibilityEnabled(sceneView, true); |
| 144 | + } |
| 145 | + |
| 146 | + [Test, Category("Graphics"), GraphicsTest] |
| 147 | + [Ignore("Test disabled due to needing BRG stripping level set to Keep All with increased build times, UUM-120684")] |
| 148 | + public async Task GameObjectVisibilityWorks(GraphicsTestCase testCase) |
| 149 | + { |
| 150 | + await RunTest(testCase, SetupGameObjectVisibility); |
| 151 | + } |
| 152 | + |
| 153 | + [Test, Category("Graphics"), GraphicsTest] |
| 154 | + [Ignore("Test disabled due to needing BRG stripping level set to Keep All with increased build times, UUM-120684")] |
| 155 | + public async Task SceneVisibilityWorks(GraphicsTestCase testCase) |
| 156 | + { |
| 157 | + await RunTest(testCase, SetupSceneVisibility); |
| 158 | + } |
| 159 | + |
| 160 | + [Test, Category("Graphics"), GraphicsTest] |
| 161 | + [Ignore("Test disabled due to needing BRG stripping level set to Keep All with increased build times, UUM-120684")] |
| 162 | + public async Task SceneVisibilityResetWorks(GraphicsTestCase testCase) |
| 163 | + { |
| 164 | + await RunTest(testCase, SetupSceneVisibilityReset); |
| 165 | + } |
| 166 | + |
| 167 | + [Test, Category("Graphics"), GraphicsTest] |
| 168 | + [Ignore("Test disabled due to needing BRG stripping level set to Keep All with increased build times, UUM-120684")] |
| 169 | + public async Task SceneVisibilityEnabledWorks(GraphicsTestCase testCase) |
| 170 | + { |
| 171 | + await RunTest(testCase, SetupSceneVisibilityEnabled); |
| 172 | + } |
| 173 | + |
| 174 | + [Test, Category("Graphics"), GraphicsTest] |
| 175 | + [Ignore("Test disabled due to needing BRG stripping level set to Keep All with increased build times, UUM-120684")] |
| 176 | + public async Task SceneVisibilityEnabledResetWorks(GraphicsTestCase testCase) |
| 177 | + { |
| 178 | + await RunTest(testCase, SetupSceneVisibilityEnabledReset); |
| 179 | + } |
| 180 | +} |
| 181 | + |
0 commit comments