Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PixelPerfectCameraTests.cs 14KB

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