PixelPerfectCameraTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using NUnit.Framework;
  2. using UnityEngine.Experimental.Rendering.Universal;
  3. namespace UnityEngine.Rendering.Universal.Tests
  4. {
  5. internal class PixelPerfectCameraTests
  6. {
  7. internal class PixelPerfectCameraTestComponent : IPixelPerfectCamera
  8. {
  9. public int assetsPPU { get; set; }
  10. public int refResolutionX { get; set; }
  11. public int refResolutionY { get; set; }
  12. public bool upscaleRT { get; set; }
  13. public bool pixelSnapping { get; set; }
  14. public bool cropFrameX { get; set; }
  15. public bool cropFrameY { get; set; }
  16. public bool stretchFill { get; set; }
  17. }
  18. internal class CalculateCameraPropertiesResult
  19. {
  20. public int zoom;
  21. public bool useOffscreenRT;
  22. public int offscreenRTWidth;
  23. public int offscreenRTHeight;
  24. public Rect pixelRect;
  25. public float orthoSize;
  26. public float unitsPerPixel;
  27. }
  28. private static object[] GetCalculateCameraPropertiesTestCases()
  29. {
  30. object[] testCaseArray = new object[9];
  31. for (int i = 0; i < testCaseArray.Length; ++i)
  32. {
  33. PixelPerfectCameraTestComponent testComponent = new PixelPerfectCameraTestComponent();
  34. int screenWidth = 0;
  35. int screenHeight = 0;
  36. CalculateCameraPropertiesResult expected = new CalculateCameraPropertiesResult();
  37. switch (i)
  38. {
  39. case 0:
  40. testComponent.assetsPPU = 100;
  41. testComponent.refResolutionX = 400;
  42. testComponent.refResolutionY = 300;
  43. testComponent.upscaleRT = false;
  44. testComponent.pixelSnapping = true;
  45. testComponent.cropFrameX = true;
  46. testComponent.cropFrameY = true;
  47. testComponent.stretchFill = true;
  48. screenWidth = 800;
  49. screenHeight = 500;
  50. expected.zoom = 1;
  51. expected.useOffscreenRT = true;
  52. expected.offscreenRTWidth = 400;
  53. expected.offscreenRTHeight = 300;
  54. expected.pixelRect = new Rect(0.0f, 0.0f, 400, 300);
  55. expected.orthoSize = 1.5f;
  56. expected.unitsPerPixel = 0.01f;
  57. break;
  58. case 1:
  59. testComponent.assetsPPU = 100;
  60. testComponent.refResolutionX = 400;
  61. testComponent.refResolutionY = 300;
  62. testComponent.upscaleRT = true;
  63. testComponent.pixelSnapping = true;
  64. testComponent.cropFrameX = true;
  65. testComponent.cropFrameY = true;
  66. testComponent.stretchFill = true;
  67. screenWidth = 1100;
  68. screenHeight = 900;
  69. expected.zoom = 2;
  70. expected.useOffscreenRT = true;
  71. expected.offscreenRTWidth = 400;
  72. expected.offscreenRTHeight = 300;
  73. expected.pixelRect = new Rect(0.0f, 0.0f, 400, 300);
  74. expected.orthoSize = 1.5f;
  75. expected.unitsPerPixel = 0.01f;
  76. break;
  77. case 2:
  78. testComponent.assetsPPU = 100;
  79. testComponent.refResolutionX = 400;
  80. testComponent.refResolutionY = 300;
  81. testComponent.upscaleRT = true;
  82. testComponent.pixelSnapping = true;
  83. testComponent.cropFrameX = false;
  84. testComponent.cropFrameY = true;
  85. testComponent.stretchFill = false;
  86. screenWidth = 400;
  87. screenHeight = 250;
  88. expected.zoom = 1;
  89. expected.useOffscreenRT = true;
  90. expected.offscreenRTWidth = 400;
  91. expected.offscreenRTHeight = 300;
  92. expected.pixelRect = new Rect(0.0f, 0.0f, 400, 300);
  93. expected.orthoSize = 1.5f;
  94. expected.unitsPerPixel = 0.01f;
  95. break;
  96. case 3:
  97. testComponent.assetsPPU = 100;
  98. testComponent.refResolutionX = 400;
  99. testComponent.refResolutionY = 300;
  100. testComponent.upscaleRT = true;
  101. testComponent.pixelSnapping = true;
  102. testComponent.cropFrameX = true;
  103. testComponent.cropFrameY = false;
  104. testComponent.stretchFill = false;
  105. screenWidth = 1600;
  106. screenHeight = 1200;
  107. expected.zoom = 4;
  108. expected.useOffscreenRT = true;
  109. expected.offscreenRTWidth = 400;
  110. expected.offscreenRTHeight = 300;
  111. expected.pixelRect = new Rect(0.0f, 0.0f, 400, 300);
  112. expected.orthoSize = 1.5f;
  113. expected.unitsPerPixel = 0.01f;
  114. break;
  115. case 4:
  116. testComponent.assetsPPU = 100;
  117. testComponent.refResolutionX = 400;
  118. testComponent.refResolutionY = 300;
  119. testComponent.upscaleRT = true;
  120. testComponent.pixelSnapping = true;
  121. testComponent.cropFrameX = false;
  122. testComponent.cropFrameY = false;
  123. testComponent.stretchFill = false;
  124. screenWidth = 1600;
  125. screenHeight = 1100;
  126. expected.zoom = 3;
  127. expected.useOffscreenRT = true;
  128. expected.offscreenRTWidth = 532;
  129. expected.offscreenRTHeight = 366;
  130. expected.pixelRect = new Rect(0.0f, 0.0f, 532, 366);
  131. expected.orthoSize = 1.83f;
  132. expected.unitsPerPixel = 0.01f;
  133. break;
  134. case 5:
  135. testComponent.assetsPPU = 100;
  136. testComponent.refResolutionX = 400;
  137. testComponent.refResolutionY = 300;
  138. testComponent.upscaleRT = false;
  139. testComponent.pixelSnapping = false;
  140. testComponent.cropFrameX = false;
  141. testComponent.cropFrameY = false;
  142. testComponent.stretchFill = true;
  143. screenWidth = 800;
  144. screenHeight = 600;
  145. expected.zoom = 2;
  146. expected.useOffscreenRT = false;
  147. expected.offscreenRTWidth = 0;
  148. expected.offscreenRTHeight = 0;
  149. expected.pixelRect = Rect.zero;
  150. expected.orthoSize = 1.5f;
  151. expected.unitsPerPixel = 0.005f;
  152. break;
  153. case 6:
  154. testComponent.assetsPPU = 100;
  155. testComponent.refResolutionX = 400;
  156. testComponent.refResolutionY = 300;
  157. testComponent.upscaleRT = false;
  158. testComponent.pixelSnapping = false;
  159. testComponent.cropFrameX = true;
  160. testComponent.cropFrameY = true;
  161. testComponent.stretchFill = false;
  162. screenWidth = 800;
  163. screenHeight = 700;
  164. expected.zoom = 2;
  165. expected.useOffscreenRT = true;
  166. expected.offscreenRTWidth = 800;
  167. expected.offscreenRTHeight = 600;
  168. expected.pixelRect = new Rect(0.0f, 0.0f, 800, 600);
  169. expected.orthoSize = 1.5f;
  170. expected.unitsPerPixel = 0.005f;
  171. break;
  172. case 7:
  173. testComponent.assetsPPU = 100;
  174. testComponent.refResolutionX = 400;
  175. testComponent.refResolutionY = 300;
  176. testComponent.upscaleRT = false;
  177. testComponent.pixelSnapping = true;
  178. testComponent.cropFrameX = false;
  179. testComponent.cropFrameY = true;
  180. testComponent.stretchFill = false;
  181. screenWidth = 900;
  182. screenHeight = 600;
  183. expected.zoom = 2;
  184. expected.useOffscreenRT = true;
  185. expected.offscreenRTWidth = 900;
  186. expected.offscreenRTHeight = 600;
  187. expected.pixelRect = new Rect(0.0f, 0.0f, 900, 600);
  188. expected.orthoSize = 1.5f;
  189. expected.unitsPerPixel = 0.01f;
  190. break;
  191. case 8:
  192. testComponent.assetsPPU = 100;
  193. testComponent.refResolutionX = 400;
  194. testComponent.refResolutionY = 300;
  195. testComponent.upscaleRT = false;
  196. testComponent.pixelSnapping = true;
  197. testComponent.cropFrameX = true;
  198. testComponent.cropFrameY = false;
  199. testComponent.stretchFill = false;
  200. screenWidth = 900;
  201. screenHeight = 600;
  202. expected.zoom = 2;
  203. expected.useOffscreenRT = true;
  204. expected.offscreenRTWidth = 800;
  205. expected.offscreenRTHeight = 600;
  206. expected.pixelRect = new Rect(0.0f, 0.0f, 800, 600);
  207. expected.orthoSize = 1.5f;
  208. expected.unitsPerPixel = 0.01f;
  209. break;
  210. }
  211. testCaseArray[i] = new object[] { testComponent, screenWidth, screenHeight, expected };
  212. }
  213. return testCaseArray;
  214. }
  215. [Test, TestCaseSource("GetCalculateCameraPropertiesTestCases")]
  216. public void CalculateCameraPropertiesProvidesCorrectResultsWithVariousInputs(PixelPerfectCameraTestComponent testComponent, int screenWidth, int screenHeight, CalculateCameraPropertiesResult expected)
  217. {
  218. PixelPerfectCameraInternal internals = new PixelPerfectCameraInternal(testComponent);
  219. internals.CalculateCameraProperties(screenWidth, screenHeight);
  220. Assert.AreEqual(expected.zoom, internals.zoom);
  221. Assert.AreEqual(expected.useOffscreenRT, internals.useOffscreenRT);
  222. Assert.AreEqual(expected.offscreenRTWidth, internals.offscreenRTWidth);
  223. Assert.AreEqual(expected.offscreenRTHeight, internals.offscreenRTHeight);
  224. Assert.AreEqual(expected.pixelRect, internals.pixelRect);
  225. Assert.AreEqual(expected.orthoSize, internals.orthoSize);
  226. Assert.AreEqual(expected.unitsPerPixel, internals.unitsPerPixel);
  227. }
  228. [Test]
  229. public void CalculateFinalBlitPixelRectStretchToFitHeightWorks()
  230. {
  231. PixelPerfectCameraTestComponent testComponent = new PixelPerfectCameraTestComponent();
  232. testComponent.refResolutionX = 200;
  233. testComponent.refResolutionY = 100;
  234. PixelPerfectCameraInternal internals = new PixelPerfectCameraInternal(testComponent);
  235. internals.useStretchFill = true;
  236. Rect pixelRect = internals.CalculateFinalBlitPixelRect(400, 100);
  237. Rect expected = new Rect(100.0f, 0.0f, 200.0f, 100.0f);
  238. Assert.AreEqual(expected, pixelRect);
  239. }
  240. [Test]
  241. public void CalculateFinalBlitPixelRectStretchToFitWidthWorks()
  242. {
  243. PixelPerfectCameraTestComponent testComponent = new PixelPerfectCameraTestComponent();
  244. testComponent.refResolutionX = 200;
  245. testComponent.refResolutionY = 100;
  246. PixelPerfectCameraInternal internals = new PixelPerfectCameraInternal(testComponent);
  247. internals.useStretchFill = true;
  248. Rect pixelRect = internals.CalculateFinalBlitPixelRect(200, 200);
  249. Rect expected = new Rect(0.0f, 50.0f, 200.0f, 100.0f);
  250. Assert.AreEqual(expected, pixelRect);
  251. }
  252. [Test]
  253. public void CalculateFinalBlitPixelRectCenteredWorksWithUpscaleRT()
  254. {
  255. PixelPerfectCameraTestComponent testComponent = new PixelPerfectCameraTestComponent();
  256. testComponent.upscaleRT = true;
  257. testComponent.refResolutionX = 400;
  258. testComponent.refResolutionY = 300;
  259. PixelPerfectCameraInternal internals = new PixelPerfectCameraInternal(testComponent);
  260. internals.useStretchFill = false;
  261. internals.zoom = 2;
  262. internals.offscreenRTWidth = 400;
  263. internals.offscreenRTHeight = 300;
  264. Rect pixelRect = internals.CalculateFinalBlitPixelRect(1600, 1200);
  265. Rect expected = new Rect(400.0f, 300.0f, 800.0f, 600.0f);
  266. Assert.AreEqual(expected, pixelRect);
  267. }
  268. [Test]
  269. public void CalculateFinalBlitPixelRectCenteredWorksWithoutUpscaleRT()
  270. {
  271. PixelPerfectCameraTestComponent testComponent = new PixelPerfectCameraTestComponent();
  272. testComponent.upscaleRT = false;
  273. testComponent.refResolutionX = 400;
  274. testComponent.refResolutionY = 300;
  275. PixelPerfectCameraInternal internals = new PixelPerfectCameraInternal(testComponent);
  276. internals.useStretchFill = false;
  277. internals.zoom = 2;
  278. internals.offscreenRTWidth = 400;
  279. internals.offscreenRTHeight = 300;
  280. Rect pixelRect = internals.CalculateFinalBlitPixelRect(1600, 1200);
  281. Rect expected = new Rect(600.0f, 450.0f, 400.0f, 300.0f);
  282. Assert.AreEqual(expected, pixelRect);
  283. }
  284. }
  285. }