This commit is contained in:
2021-06-13 10:28:03 +02:00
parent eb70603c85
commit df2d24cbd3
7487 changed files with 943244 additions and 0 deletions

View File

@@ -0,0 +1,155 @@
using System.Collections;
using NUnit.Framework;
using UnityEngine.Experimental.Rendering.Universal;
using UnityEngine.TestTools;
namespace UnityEngine.Rendering.Universal.Tests
{
[TestFixture]
class MultipleObjectLight2DTests
{
GameObject m_TestObject1;
GameObject m_TestObject2;
GameObject m_TestObject3;
GameObject m_TestObject4;
GameObject m_TestObjectCached;
[SetUp]
public void Setup()
{
m_TestObject1 = new GameObject("Test Object 1");
m_TestObject2 = new GameObject("Test Object 2");
m_TestObject3 = new GameObject("Test Object 3");
m_TestObject4 = new GameObject("Test Object 4");
m_TestObjectCached = new GameObject("Test Object Cached");
}
[TearDown]
public void Cleanup()
{
Object.DestroyImmediate(m_TestObjectCached);
Object.DestroyImmediate(m_TestObject4);
Object.DestroyImmediate(m_TestObject3);
Object.DestroyImmediate(m_TestObject2);
Object.DestroyImmediate(m_TestObject1);
}
[Test]
public void LightsAreSortedByLightOrder()
{
var light1 = m_TestObject1.AddComponent<Light2D>();
var light2 = m_TestObject2.AddComponent<Light2D>();
var light3 = m_TestObject3.AddComponent<Light2D>();
light1.lightOrder = 1;
light2.lightOrder = 2;
light3.lightOrder = 0;
var camera = m_TestObject4.AddComponent<Camera>();
var cameraPos = camera.transform.position;
light1.transform.position = cameraPos;
light2.transform.position = cameraPos;
light3.transform.position = cameraPos;
light1.UpdateMesh(true);
light1.UpdateBoundingSphere();
light2.UpdateMesh(true);
light2.UpdateBoundingSphere();
light3.UpdateMesh(true);
light3.UpdateBoundingSphere();
var cullResult = new Light2DCullResult();
var cullingParams = new ScriptableCullingParameters();
camera.TryGetCullingParameters(out cullingParams);
cullResult.SetupCulling(ref cullingParams, camera);
Assert.AreSame(light3, cullResult.visibleLights[0]);
Assert.AreSame(light1, cullResult.visibleLights[1]);
Assert.AreSame(light2, cullResult.visibleLights[2]);
}
[Test]
public void LightIsInVisibleListIfInCameraView()
{
var camera = m_TestObject1.AddComponent<Camera>();
var light = m_TestObject2.AddComponent<Light2D>();
light.transform.position = camera.transform.position;
light.UpdateMesh(true);
light.UpdateBoundingSphere();
var cullResult = new Light2DCullResult();
var cullingParams = new ScriptableCullingParameters();
camera.TryGetCullingParameters(out cullingParams);
cullResult.SetupCulling(ref cullingParams, camera);
Assert.Contains(light, cullResult.visibleLights);
}
[Test]
public void LightIsNotInVisibleListIfNotInCameraView()
{
var camera = m_TestObject1.AddComponent<Camera>();
var light = m_TestObject2.AddComponent<Light2D>();
light.transform.position = camera.transform.position + new Vector3(9999.0f, 0.0f, 0.0f);
light.UpdateMesh(true);
light.UpdateBoundingSphere();
var cullResult = new Light2DCullResult();
var cullingParams = new ScriptableCullingParameters();
camera.TryGetCullingParameters(out cullingParams);
cullResult.SetupCulling(ref cullingParams, camera);
Assert.IsFalse(cullResult.visibleLights.Contains(light));
}
[Test]
public void CachedMeshDataIsUpdatedOnChange()
{
var shapePath = new Vector3[4] { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 0), new Vector3(0, 1, 0) };
var light = m_TestObjectCached.AddComponent<Light2D>();
light.lightType = Light2D.LightType.Freeform;
light.SetShapePath(shapePath);
light.UpdateMesh(true);
Assert.AreEqual(true, light.hasCachedMesh);
}
[Test]
public void CachedMeshDataIsOverriddenByRuntimeChanges()
{
var shapePath = new Vector3[4] { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 0), new Vector3(0, 1, 0) };
var light = m_TestObjectCached.AddComponent<Light2D>();
light.lightType = Light2D.LightType.Freeform;
light.SetShapePath(shapePath);
light.UpdateMesh(true);
int vertexCount = 0, triangleCount = 0;
// Check if Cached Data and the actual data are the same.
Assert.AreEqual(true, light.hasCachedMesh);
vertexCount = light.lightMesh.triangles.Length;
triangleCount = light.lightMesh.vertices.Length;
// Simulate Runtime Behavior.
var shapePathChanged = new Vector3[5] { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 0), new Vector3(0.5f, 1.5f, 0), new Vector3(0, 1, 0) };
light.SetShapePath(shapePathChanged);
light.UpdateMesh(true);
// Check if Cached Data and the actual data are no longer the same. (We don't save cache on Runtime)
Assert.AreNotEqual(vertexCount, light.lightMesh.triangles.Length);
Assert.AreNotEqual(triangleCount, light.lightMesh.vertices.Length);
}
[Test]
public void EnsureShapeMeshGenerationDoesNotOverflowAllocation()
{
var shapePath = new Vector3[4] { new Vector3(-76.04548f, 7.522535f, 0f), new Vector3(-66.52518f, 18.88778f, 0f), new Vector3(-66.35441f, 24.34475f, 0), new Vector3(-75.15407f, 33.0358f, 0) };
var light = m_TestObjectCached.AddComponent<Light2D>();
light.lightType = Light2D.LightType.Freeform;
LightUtility.GenerateShapeMesh(light, shapePath, 180.0f);
Assert.AreEqual(true, light.hasCachedMesh);
}
}
}

View File

@@ -0,0 +1,326 @@
using NUnit.Framework;
using UnityEngine.Experimental.Rendering.Universal;
namespace UnityEngine.Rendering.Universal.Tests
{
internal class PixelPerfectCameraTests
{
internal class PixelPerfectCameraTestComponent : IPixelPerfectCamera
{
public int assetsPPU { get; set; }
public int refResolutionX { get; set; }
public int refResolutionY { get; set; }
public bool upscaleRT { get; set; }
public bool pixelSnapping { get; set; }
public bool cropFrameX { get; set; }
public bool cropFrameY { get; set; }
public bool stretchFill { get; set; }
}
internal class CalculateCameraPropertiesResult
{
public int zoom;
public bool useOffscreenRT;
public int offscreenRTWidth;
public int offscreenRTHeight;
public Rect pixelRect;
public float orthoSize;
public float unitsPerPixel;
}
[UnityEngine.Scripting.Preserve]
private static object[] GetCalculateCameraPropertiesTestCases()
{
object[] testCaseArray = new object[9];
for (int i = 0; i < testCaseArray.Length; ++i)
{
PixelPerfectCameraTestComponent testComponent = new PixelPerfectCameraTestComponent();
int screenWidth = 0;
int screenHeight = 0;
CalculateCameraPropertiesResult expected = new CalculateCameraPropertiesResult();
switch (i)
{
case 0:
testComponent.assetsPPU = 100;
testComponent.refResolutionX = 400;
testComponent.refResolutionY = 300;
testComponent.upscaleRT = false;
testComponent.pixelSnapping = true;
testComponent.cropFrameX = true;
testComponent.cropFrameY = true;
testComponent.stretchFill = true;
screenWidth = 800;
screenHeight = 500;
expected.zoom = 1;
expected.useOffscreenRT = true;
expected.offscreenRTWidth = 400;
expected.offscreenRTHeight = 300;
expected.pixelRect = new Rect(0.0f, 0.0f, 400, 300);
expected.orthoSize = 1.5f;
expected.unitsPerPixel = 0.01f;
break;
case 1:
testComponent.assetsPPU = 100;
testComponent.refResolutionX = 400;
testComponent.refResolutionY = 300;
testComponent.upscaleRT = true;
testComponent.pixelSnapping = true;
testComponent.cropFrameX = true;
testComponent.cropFrameY = true;
testComponent.stretchFill = true;
screenWidth = 1100;
screenHeight = 900;
expected.zoom = 2;
expected.useOffscreenRT = true;
expected.offscreenRTWidth = 400;
expected.offscreenRTHeight = 300;
expected.pixelRect = new Rect(0.0f, 0.0f, 400, 300);
expected.orthoSize = 1.5f;
expected.unitsPerPixel = 0.01f;
break;
case 2:
testComponent.assetsPPU = 100;
testComponent.refResolutionX = 400;
testComponent.refResolutionY = 300;
testComponent.upscaleRT = true;
testComponent.pixelSnapping = true;
testComponent.cropFrameX = false;
testComponent.cropFrameY = true;
testComponent.stretchFill = false;
screenWidth = 400;
screenHeight = 250;
expected.zoom = 1;
expected.useOffscreenRT = true;
expected.offscreenRTWidth = 400;
expected.offscreenRTHeight = 300;
expected.pixelRect = new Rect(0.0f, 0.0f, 400, 300);
expected.orthoSize = 1.5f;
expected.unitsPerPixel = 0.01f;
break;
case 3:
testComponent.assetsPPU = 100;
testComponent.refResolutionX = 400;
testComponent.refResolutionY = 300;
testComponent.upscaleRT = true;
testComponent.pixelSnapping = true;
testComponent.cropFrameX = true;
testComponent.cropFrameY = false;
testComponent.stretchFill = false;
screenWidth = 1600;
screenHeight = 1200;
expected.zoom = 4;
expected.useOffscreenRT = true;
expected.offscreenRTWidth = 400;
expected.offscreenRTHeight = 300;
expected.pixelRect = new Rect(0.0f, 0.0f, 400, 300);
expected.orthoSize = 1.5f;
expected.unitsPerPixel = 0.01f;
break;
case 4:
testComponent.assetsPPU = 100;
testComponent.refResolutionX = 400;
testComponent.refResolutionY = 300;
testComponent.upscaleRT = true;
testComponent.pixelSnapping = true;
testComponent.cropFrameX = false;
testComponent.cropFrameY = false;
testComponent.stretchFill = false;
screenWidth = 1600;
screenHeight = 1100;
expected.zoom = 3;
expected.useOffscreenRT = true;
expected.offscreenRTWidth = 532;
expected.offscreenRTHeight = 366;
expected.pixelRect = new Rect(0.0f, 0.0f, 532, 366);
expected.orthoSize = 1.83f;
expected.unitsPerPixel = 0.01f;
break;
case 5:
testComponent.assetsPPU = 100;
testComponent.refResolutionX = 400;
testComponent.refResolutionY = 300;
testComponent.upscaleRT = false;
testComponent.pixelSnapping = false;
testComponent.cropFrameX = false;
testComponent.cropFrameY = false;
testComponent.stretchFill = true;
screenWidth = 800;
screenHeight = 600;
expected.zoom = 2;
expected.useOffscreenRT = false;
expected.offscreenRTWidth = 0;
expected.offscreenRTHeight = 0;
expected.pixelRect = Rect.zero;
expected.orthoSize = 1.5f;
expected.unitsPerPixel = 0.005f;
break;
case 6:
testComponent.assetsPPU = 100;
testComponent.refResolutionX = 400;
testComponent.refResolutionY = 300;
testComponent.upscaleRT = false;
testComponent.pixelSnapping = false;
testComponent.cropFrameX = true;
testComponent.cropFrameY = true;
testComponent.stretchFill = false;
screenWidth = 800;
screenHeight = 700;
expected.zoom = 2;
expected.useOffscreenRT = true;
expected.offscreenRTWidth = 800;
expected.offscreenRTHeight = 600;
expected.pixelRect = new Rect(0.0f, 0.0f, 800, 600);
expected.orthoSize = 1.5f;
expected.unitsPerPixel = 0.005f;
break;
case 7:
testComponent.assetsPPU = 100;
testComponent.refResolutionX = 400;
testComponent.refResolutionY = 300;
testComponent.upscaleRT = false;
testComponent.pixelSnapping = true;
testComponent.cropFrameX = false;
testComponent.cropFrameY = true;
testComponent.stretchFill = false;
screenWidth = 900;
screenHeight = 600;
expected.zoom = 2;
expected.useOffscreenRT = true;
expected.offscreenRTWidth = 900;
expected.offscreenRTHeight = 600;
expected.pixelRect = new Rect(0.0f, 0.0f, 900, 600);
expected.orthoSize = 1.5f;
expected.unitsPerPixel = 0.01f;
break;
case 8:
testComponent.assetsPPU = 100;
testComponent.refResolutionX = 400;
testComponent.refResolutionY = 300;
testComponent.upscaleRT = false;
testComponent.pixelSnapping = true;
testComponent.cropFrameX = true;
testComponent.cropFrameY = false;
testComponent.stretchFill = false;
screenWidth = 900;
screenHeight = 600;
expected.zoom = 2;
expected.useOffscreenRT = true;
expected.offscreenRTWidth = 800;
expected.offscreenRTHeight = 600;
expected.pixelRect = new Rect(0.0f, 0.0f, 800, 600);
expected.orthoSize = 1.5f;
expected.unitsPerPixel = 0.01f;
break;
}
testCaseArray[i] = new object[] { testComponent, screenWidth, screenHeight, expected };
}
return testCaseArray;
}
[Test, TestCaseSource("GetCalculateCameraPropertiesTestCases")]
public void CalculateCameraPropertiesProvidesCorrectResultsWithVariousInputs(PixelPerfectCameraTestComponent testComponent, int screenWidth, int screenHeight, CalculateCameraPropertiesResult expected)
{
PixelPerfectCameraInternal internals = new PixelPerfectCameraInternal(testComponent);
internals.CalculateCameraProperties(screenWidth, screenHeight);
Assert.AreEqual(expected.zoom, internals.zoom);
Assert.AreEqual(expected.useOffscreenRT, internals.useOffscreenRT);
Assert.AreEqual(expected.offscreenRTWidth, internals.offscreenRTWidth);
Assert.AreEqual(expected.offscreenRTHeight, internals.offscreenRTHeight);
Assert.AreEqual(expected.pixelRect, internals.pixelRect);
Assert.AreEqual(expected.orthoSize, internals.orthoSize);
Assert.AreEqual(expected.unitsPerPixel, internals.unitsPerPixel);
}
[Test]
public void CalculateFinalBlitPixelRectStretchToFitHeightWorks()
{
PixelPerfectCameraTestComponent testComponent = new PixelPerfectCameraTestComponent();
testComponent.refResolutionX = 200;
testComponent.refResolutionY = 100;
PixelPerfectCameraInternal internals = new PixelPerfectCameraInternal(testComponent);
internals.useStretchFill = true;
Rect pixelRect = internals.CalculateFinalBlitPixelRect(400, 100);
Rect expected = new Rect(100.0f, 0.0f, 200.0f, 100.0f);
Assert.AreEqual(expected, pixelRect);
}
[Test]
public void CalculateFinalBlitPixelRectStretchToFitWidthWorks()
{
PixelPerfectCameraTestComponent testComponent = new PixelPerfectCameraTestComponent();
testComponent.refResolutionX = 200;
testComponent.refResolutionY = 100;
PixelPerfectCameraInternal internals = new PixelPerfectCameraInternal(testComponent);
internals.useStretchFill = true;
Rect pixelRect = internals.CalculateFinalBlitPixelRect(200, 200);
Rect expected = new Rect(0.0f, 50.0f, 200.0f, 100.0f);
Assert.AreEqual(expected, pixelRect);
}
[Test]
public void CalculateFinalBlitPixelRectCenteredWorksWithUpscaleRT()
{
PixelPerfectCameraTestComponent testComponent = new PixelPerfectCameraTestComponent();
testComponent.upscaleRT = true;
testComponent.refResolutionX = 400;
testComponent.refResolutionY = 300;
PixelPerfectCameraInternal internals = new PixelPerfectCameraInternal(testComponent);
internals.useStretchFill = false;
internals.zoom = 2;
internals.offscreenRTWidth = 400;
internals.offscreenRTHeight = 300;
Rect pixelRect = internals.CalculateFinalBlitPixelRect(1600, 1200);
Rect expected = new Rect(400.0f, 300.0f, 800.0f, 600.0f);
Assert.AreEqual(expected, pixelRect);
}
[Test]
public void CalculateFinalBlitPixelRectCenteredWorksWithoutUpscaleRT()
{
PixelPerfectCameraTestComponent testComponent = new PixelPerfectCameraTestComponent();
testComponent.upscaleRT = false;
testComponent.refResolutionX = 400;
testComponent.refResolutionY = 300;
PixelPerfectCameraInternal internals = new PixelPerfectCameraInternal(testComponent);
internals.useStretchFill = false;
internals.zoom = 2;
internals.offscreenRTWidth = 400;
internals.offscreenRTHeight = 300;
Rect pixelRect = internals.CalculateFinalBlitPixelRect(1600, 1200);
Rect expected = new Rect(600.0f, 450.0f, 400.0f, 300.0f);
Assert.AreEqual(expected, pixelRect);
}
}
}

View File

@@ -0,0 +1,75 @@
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering;
[TestFixture]
class RuntimeTests
{
GameObject go;
Camera camera;
RenderPipelineAsset currentAsset;
[SetUp]
public void Setup()
{
go = new GameObject();
camera = go.AddComponent<Camera>();
currentAsset = GraphicsSettings.renderPipelineAsset;
}
[TearDown]
public void Cleanup()
{
GraphicsSettings.renderPipelineAsset = currentAsset;
Object.DestroyImmediate(go);
}
// When LWRP pipeline is active, lightsUseLinearIntensity must match active color space.
[UnityTest]
public IEnumerator PipelineHasCorrectColorSpace()
{
AssetCheck();
camera.Render();
yield return null;
Assert.AreEqual(QualitySettings.activeColorSpace == ColorSpace.Linear, GraphicsSettings.lightsUseLinearIntensity,
"GraphicsSettings.lightsUseLinearIntensity must match active color space.");
}
// When switching to LWRP it sets "UniversalPipeline" as global shader tag.
// When switching to Built-in it sets "" as global shader tag.
#if UNITY_EDITOR // TODO This API call does not reset in player
[UnityTest]
public IEnumerator PipelineSetsAndRestoreGlobalShaderTagCorrectly()
{
AssetCheck();
camera.Render();
yield return null;
Assert.AreEqual("UniversalPipeline,LightweightPipeline", Shader.globalRenderPipeline, "Wrong render pipeline shader tag.");
GraphicsSettings.renderPipelineAsset = null;
camera.Render();
yield return null;
Assert.AreEqual("", Shader.globalRenderPipeline, "Render Pipeline shader tag is not restored.");
}
#endif
void AssetCheck()
{
//Assert.IsNotNull(currentAsset, "Render Pipeline Asset is Null");
// Temp fix, test passes if project isnt setup for Universal RP
if (currentAsset == null)
Assert.Pass("Render Pipeline Asset is Null, test pass by default");
Assert.AreEqual(currentAsset.GetType(), typeof(UniversalRenderPipelineAsset),
"Pipeline Asset is not Universal RP");
}
}

View File

@@ -0,0 +1,20 @@
{
"name": "Unity.RenderPipelines.Universal.Runtime.Tests",
"references": [
"GUID:15fc0a57446b3144c949da3e2b9737a9",
"GUID:27619889b8ba8c24980f49ee34dbb44a",
"GUID:0acc523941302664db1f4e527237feb3"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": true,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": []
}