123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- 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;
- }
- 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);
- }
- }
- }
|