PixelPerfectCameraTests.cs 13 KB

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