/CommandLibrary/Test/Editor/UnitTests/TestCommands~Tween.cs

https://bitbucket.org/Darcy_Rayner/commandqueue · C# · 778 lines · 657 code · 95 blank · 26 comment · 3 complexity · 253c34c531d09f6179e434b59180239f MD5 · raw file

  1. using System;
  2. using UnityEngine;
  3. using CommandLibrary;
  4. using CommandLibrary.Test;
  5. namespace CommandLibrary.UnitTests
  6. {
  7. [TestGroup]
  8. public class TestCommands_Tween
  9. {
  10. [Test]
  11. public static void TestChangeBy()
  12. {
  13. const float floatOffset = 4.8f;
  14. const float floatStart = 1.2f;
  15. float floatVal = floatStart;
  16. Ref<float> floatRef = new Ref<float>(
  17. () => floatVal,
  18. t => floatVal = t
  19. );
  20. const double doubleOffset = 3.2;
  21. const double doubleStart = 9.2;
  22. double doubleVal = doubleStart;
  23. Ref<double> doubleRef = new Ref<double>(
  24. () => doubleVal,
  25. t => doubleVal = t
  26. );
  27. Vector2 vec2Offset = new Vector2(9.5f, 2.0f);
  28. Vector2 vec2Start = new Vector2(4.0f, 5.0f);
  29. Vector2 vec2Val = vec2Start;
  30. Ref<Vector2> vec2Ref = new Ref<Vector2>(
  31. () => vec2Val,
  32. t => vec2Val = t
  33. );
  34. Vector3 vec3Offset = new Vector3(4.0f, 19.0f, 2.0f);
  35. Vector3 vec3Start = new Vector3(92.0f, 0.5f, 34.0f);
  36. Vector3 vec3Val = vec3Start;
  37. Ref<Vector3> vec3Ref = new Ref<Vector3>(
  38. () => vec3Val,
  39. t => vec3Val = t
  40. );
  41. Vector4 vec4Offset = new Vector4(92.0f, 0.5f, 14.0f, 7.0f);
  42. Vector4 vec4Start = new Vector4(0.4f, 10.0f, 3.0f, 82.0f);
  43. Vector4 vec4Val = vec4Start;
  44. Ref<Vector4> vec4Ref = new Ref<Vector4>(
  45. () => vec4Val,
  46. t => vec4Val = t
  47. );
  48. CommandQueue queue = new CommandQueue();
  49. queue.Enqueue(
  50. Commands.Repeat(2,
  51. Commands.Parallel(
  52. Commands.ChangeBy(floatRef, floatOffset, 1.0),
  53. Commands.ChangeBy(doubleRef, doubleOffset, 1.0),
  54. Commands.ChangeBy(vec2Ref, vec2Offset, 1.0),
  55. Commands.ChangeBy(vec3Ref, vec3Offset, 1.0),
  56. Commands.ChangeBy(vec4Ref, vec4Offset, 1.0)
  57. )
  58. )
  59. );
  60. queue.Update(0.3);
  61. // Check basic lerping works.
  62. TestRunner.AssertApprox(floatVal, floatOffset * 0.3f + floatStart, 0.01);
  63. TestRunner.AssertApprox(doubleVal, doubleOffset * 0.3 + doubleStart, 0.01);
  64. TestRunner.AssertApprox(vec2Val, vec2Offset * 0.3f + vec2Start, 0.01f);
  65. TestRunner.AssertApprox(vec3Val, vec3Offset * 0.3f + vec3Start, 0.01f);
  66. TestRunner.AssertApprox(vec4Val, vec4Offset * 0.3f + vec4Start, 0.01f);
  67. queue.Update(0.7);
  68. // Completes the offset
  69. TestRunner.AssertApprox(floatVal, floatOffset + floatStart, 0.01f);
  70. TestRunner.AssertApprox(doubleVal, doubleOffset + doubleStart, 0.01);
  71. TestRunner.AssertApprox(vec2Val, vec2Offset + vec2Start, 0.01f);
  72. TestRunner.AssertApprox(vec3Val, vec3Offset + vec3Start, 0.01f);
  73. TestRunner.AssertApprox(vec4Val, vec4Offset + vec4Start, 0.01f);
  74. queue.Update(0.3);
  75. // Check that it compounds the result
  76. TestRunner.AssertApprox(floatVal, floatOffset * 1.3f + floatStart, 0.01f);
  77. TestRunner.AssertApprox(doubleVal, doubleOffset * 1.3 + doubleStart, 0.01);
  78. TestRunner.AssertApprox(vec2Val, vec2Offset * 1.3f + vec2Start, 0.01f);
  79. TestRunner.AssertApprox(vec3Val, vec3Offset * 1.3f + vec3Start, 0.01f);
  80. TestRunner.AssertApprox(vec4Val, vec4Offset * 1.3f + vec4Start, 0.01f);
  81. // Reset the vals to zero.
  82. floatVal = 0.0f;
  83. doubleVal = 0.0;
  84. vec2Val = Vector2.zero;
  85. vec3Val = Vector3.zero;
  86. vec4Val = Vector4.zero;
  87. queue.Update(0.7);
  88. // And check the offset continues.
  89. TestRunner.AssertApprox(floatVal, floatOffset * 0.7f, 0.01f);
  90. TestRunner.AssertApprox(doubleVal, doubleOffset * 0.7, 0.01);
  91. TestRunner.AssertApprox(vec2Val, vec2Offset * 0.7f, 0.01f);
  92. TestRunner.AssertApprox(vec3Val, vec3Offset * 0.7f, 0.01f);
  93. TestRunner.AssertApprox(vec4Val, vec4Offset * 0.7f, 0.01f);
  94. }
  95. [Test]
  96. public static void TestChangeTo()
  97. {
  98. const float floatEnd = 4.8f;
  99. const float floatStart = 1.2f;
  100. float floatVal = floatStart;
  101. Ref<float> floatRef = new Ref<float>(
  102. () => floatVal,
  103. t => floatVal = t
  104. );
  105. const double doubleEnd = 3.2;
  106. const double doubleStart = 9.2;
  107. double doubleVal = doubleStart;
  108. Ref<double> doubleRef = new Ref<double>(
  109. () => doubleVal,
  110. t => doubleVal = t
  111. );
  112. Vector2 vec2End = new Vector2(9.5f, 2.0f);
  113. Vector2 vec2Start = new Vector2(4.0f, 5.0f);
  114. Vector2 vec2Val = vec2Start;
  115. Ref<Vector2> vec2Ref = new Ref<Vector2>(
  116. () => vec2Val,
  117. t => vec2Val = t
  118. );
  119. Vector3 vec3End = new Vector3(4.0f, 19.0f, 2.0f);
  120. Vector3 vec3Start = new Vector3(92.0f, 0.5f, 34.0f);
  121. Vector3 vec3Val = vec3Start;
  122. Ref<Vector3> vec3Ref = new Ref<Vector3>(
  123. () => vec3Val,
  124. t => vec3Val = t
  125. );
  126. Vector4 vec4End = new Vector4(92.0f, 0.5f, 14.0f, 7.0f);
  127. Vector4 vec4Start = new Vector4(0.4f, 10.0f, 3.0f, 82.0f);
  128. Vector4 vec4Val = vec4Start;
  129. Ref<Vector4> vec4Ref = new Ref<Vector4>(
  130. () => vec4Val,
  131. t => vec4Val = t
  132. );
  133. CommandQueue queue = new CommandQueue();
  134. queue.Enqueue(
  135. Commands.Repeat(2,
  136. Commands.Parallel(
  137. Commands.ChangeTo(floatRef, floatEnd, 1.0),
  138. Commands.ChangeTo(doubleRef, doubleEnd, 1.0),
  139. Commands.ChangeTo(vec2Ref, vec2End, 1.0),
  140. Commands.ChangeTo(vec3Ref, vec3End, 1.0),
  141. Commands.ChangeTo(vec4Ref, vec4End, 1.0)
  142. )
  143. )
  144. );
  145. queue.Update(0.3);
  146. // Check basic lerping works.
  147. TestRunner.AssertApprox(floatVal, floatEnd * 0.3f + floatStart * 0.7f, 0.01);
  148. TestRunner.AssertApprox(doubleVal, doubleEnd * 0.3 + doubleStart * 0.7, 0.01);
  149. TestRunner.AssertApprox(vec2Val, vec2End * 0.3f + vec2Start * 0.7f, 0.01f);
  150. TestRunner.AssertApprox(vec3Val, vec3End * 0.3f + vec3Start * 0.7f, 0.01f);
  151. TestRunner.AssertApprox(vec4Val, vec4End * 0.3f + vec4Start * 0.7f, 0.01f);
  152. // Reset the vals to zero. Checks that 'ChangeTo' will force itself back on
  153. // track.
  154. floatVal = 0.0f;
  155. doubleVal = 0.0;
  156. vec2Val = Vector2.zero;
  157. vec3Val = Vector3.zero;
  158. vec4Val = Vector4.zero;
  159. queue.Update(0.2);
  160. // Completes the offset
  161. TestRunner.AssertApprox(floatVal, floatEnd * 0.5f + floatStart * 0.5f, 0.01);
  162. TestRunner.AssertApprox(doubleVal, doubleEnd * 0.5 + doubleStart * 0.5, 0.01);
  163. TestRunner.AssertApprox(vec2Val, vec2End * 0.5f + vec2Start * 0.5f, 0.01f);
  164. TestRunner.AssertApprox(vec3Val, vec3End * 0.5f + vec3Start * 0.5f, 0.01f);
  165. TestRunner.AssertApprox(vec4Val, vec4End * 0.5f + vec4Start * 0.5f, 0.01f);
  166. queue.Update(0.5);
  167. TestRunner.AssertApprox(floatVal, floatEnd, 0.01f);
  168. TestRunner.AssertApprox(doubleVal, doubleEnd, 0.01);
  169. TestRunner.AssertApprox(vec2Val, vec2End, 0.01f);
  170. TestRunner.AssertApprox(vec3Val, vec3End, 0.01f);
  171. TestRunner.AssertApprox(vec4Val, vec4End, 0.01f);
  172. queue.Update(0.5);
  173. // Check that it doesn't move once it has reached it's final position.
  174. TestRunner.AssertApprox(floatVal, floatEnd, 0.01f);
  175. TestRunner.AssertApprox(doubleVal, doubleEnd, 0.01);
  176. TestRunner.AssertApprox(vec2Val, vec2End, 0.01f);
  177. TestRunner.AssertApprox(vec3Val, vec3End, 0.01f);
  178. TestRunner.AssertApprox(vec4Val, vec4End, 0.01f);
  179. Rect rectEnd = new Rect(-1.0f, 1.0f, 5.0f, 5.0f);
  180. Rect rectStart = new Rect(0.0f,2.0f, 6.0f, 6.0f);
  181. Rect rectVal = new Rect();
  182. Ref<Rect> rectRef = new Ref<Rect>(
  183. () => { return rectVal; },
  184. t => { rectVal = t; }
  185. );
  186. Vector2 firstAnchor = new Vector2(0.0f, 0.0f);
  187. Vector2 secondAnchor = new Vector2(1.0f, 0.0f);
  188. Vector3 thirdAnchor = new Vector2(0.0f, 1.0f);
  189. Vector2 forthAnchor = new Vector2(1.0f, 1.0f);
  190. CommandDelegate reset = Commands.Do(() => { rectVal = rectStart; });
  191. queue = new CommandQueue();
  192. queue.Enqueue(
  193. reset,
  194. Commands.ChangeTo(rectRef, rectEnd, 1.0, firstAnchor),
  195. Commands.WaitForFrames(1),
  196. reset,
  197. Commands.ChangeTo(rectRef, rectEnd, 1.0, secondAnchor),
  198. Commands.WaitForFrames(1),
  199. reset,
  200. Commands.ChangeTo(rectRef, rectEnd, 1.0, thirdAnchor),
  201. Commands.WaitForFrames(1),
  202. reset,
  203. Commands.ChangeTo(rectRef, rectEnd, 1.0, forthAnchor)
  204. );
  205. // Test the top left corner.
  206. queue.Update(0.5);
  207. TestRunner.AssertApprox(rectVal, new Rect(
  208. -0.5f, 1.5f,
  209. (rectStart.width + rectEnd.width) * 0.5f,
  210. (rectStart.height + rectEnd.height) * 0.5f), 0.001f
  211. );
  212. queue.Update(0.5);
  213. TestRunner.AssertApprox(rectVal, rectEnd, 0.001f);
  214. queue.Update(0.0f);
  215. // Test the top right corner.
  216. queue.Update(0.3);
  217. TestRunner.AssertApprox(rectVal, new Rect(
  218. 5.4f - 5.7f, 1.7f,
  219. rectStart.width * 0.7f + rectEnd.width * 0.3f,
  220. rectStart.height * 0.7f + rectEnd.height * 0.3f), 0.001f
  221. );
  222. queue.Update(0.7);
  223. TestRunner.AssertApprox(rectVal, rectEnd, 0.001f);
  224. queue.Update(0.0f);
  225. // Test the bottom left corner.
  226. queue.Update(0.4);
  227. TestRunner.AssertApprox(rectVal, new Rect(
  228. -0.4f, 7.2f - 5.6f,
  229. rectStart.width * 0.6f + rectEnd.width * 0.4f,
  230. rectStart.height * 0.6f + rectEnd.height * 0.4f), 0.001f
  231. );
  232. queue.Update(0.6);
  233. TestRunner.AssertApprox(rectVal, rectEnd, 0.001f);
  234. queue.Update(0.0f);
  235. // Test the bottom right corner.
  236. queue.Update(0.4);
  237. TestRunner.AssertApprox(rectVal, new Rect(
  238. 5.2f - 5.6f, 7.2f - 5.6f,
  239. rectStart.width * 0.6f + rectEnd.width * 0.4f,
  240. rectStart.height * 0.6f + rectEnd.height * 0.4f), 0.001f
  241. );
  242. queue.Update(0.6);
  243. TestRunner.AssertApprox(rectVal, rectEnd, 0.001f);
  244. queue.Update(0.0f);
  245. }
  246. [Test]
  247. public static void TestChangeFrom()
  248. {
  249. const float floatEnd = 4.8f;
  250. const float floatStart = 1.2f;
  251. float floatVal = floatStart;
  252. Ref<float> floatRef = new Ref<float>(
  253. () => floatVal,
  254. t => floatVal = t
  255. );
  256. const double doubleEnd = 3.2;
  257. const double doubleStart = 9.2;
  258. double doubleVal = doubleStart;
  259. Ref<double> doubleRef = new Ref<double>(
  260. () => doubleVal,
  261. t => doubleVal = t
  262. );
  263. Vector2 vec2End = new Vector2(9.5f, 2.0f);
  264. Vector2 vec2Start = new Vector2(4.0f, 5.0f);
  265. Vector2 vec2Val = vec2Start;
  266. Ref<Vector2> vec2Ref = new Ref<Vector2>(
  267. () => vec2Val,
  268. t => vec2Val = t
  269. );
  270. Vector3 vec3End = new Vector3(4.0f, 19.0f, 2.0f);
  271. Vector3 vec3Start = new Vector3(92.0f, 0.5f, 34.0f);
  272. Vector3 vec3Val = vec3Start;
  273. Ref<Vector3> vec3Ref = new Ref<Vector3>(
  274. () => vec3Val,
  275. t => vec3Val = t
  276. );
  277. Vector4 vec4End = new Vector4(92.0f, 0.5f, 14.0f, 7.0f);
  278. Vector4 vec4Start = new Vector4(0.4f, 10.0f, 3.0f, 82.0f);
  279. Vector4 vec4Val = vec4Start;
  280. Ref<Vector4> vec4Ref = new Ref<Vector4>(
  281. () => vec4Val,
  282. t => vec4Val = t
  283. );
  284. CommandQueue queue = new CommandQueue();
  285. queue.Enqueue(
  286. Commands.Repeat(2,
  287. Commands.Sequence(
  288. Commands.Parallel(
  289. Commands.ChangeFrom(floatRef, floatEnd, 1.0),
  290. Commands.ChangeFrom(doubleRef, doubleEnd, 1.0),
  291. Commands.ChangeFrom(vec2Ref, vec2End, 1.0),
  292. Commands.ChangeFrom(vec3Ref, vec3End, 1.0),
  293. Commands.ChangeFrom(vec4Ref, vec4End, 1.0)
  294. ),
  295. Commands.WaitForFrames(1)
  296. )
  297. )
  298. );
  299. queue.Update(0.3);
  300. // Check basic lerping works.
  301. TestRunner.AssertApprox(floatVal, floatEnd * 0.7f + floatStart * 0.3f, 0.01);
  302. TestRunner.AssertApprox(doubleVal, doubleEnd * 0.7 + doubleStart * 0.3, 0.01);
  303. TestRunner.AssertApprox(vec2Val, vec2End * 0.7f + vec2Start * 0.3f, 0.01f);
  304. TestRunner.AssertApprox(vec3Val, vec3End * 0.7f + vec3Start * 0.3f, 0.01f);
  305. TestRunner.AssertApprox(vec4Val, vec4End * 0.7f + vec4Start * 0.3f, 0.01f);
  306. // Reset the vals to zero. Checks that 'ChangeTo' will force itself back on
  307. // track.
  308. floatVal = 0.0f;
  309. doubleVal = 0.0;
  310. vec2Val = Vector2.zero;
  311. vec3Val = Vector3.zero;
  312. vec4Val = Vector4.zero;
  313. queue.Update(0.2);
  314. // Completes the offset
  315. TestRunner.AssertApprox(floatVal, floatEnd * 0.5f + floatStart * 0.5f, 0.01);
  316. TestRunner.AssertApprox(doubleVal, doubleEnd * 0.5 + doubleStart * 0.5, 0.01);
  317. TestRunner.AssertApprox(vec2Val, vec2End * 0.5f + vec2Start * 0.5f, 0.01f);
  318. TestRunner.AssertApprox(vec3Val, vec3End * 0.5f + vec3Start * 0.5f, 0.01f);
  319. TestRunner.AssertApprox(vec4Val, vec4End * 0.5f + vec4Start * 0.5f, 0.01f);
  320. queue.Update(0.5);
  321. TestRunner.AssertApprox(floatVal, floatStart, 0.01f);
  322. TestRunner.AssertApprox(doubleVal, doubleStart, 0.01);
  323. TestRunner.AssertApprox(vec2Val, vec2Start, 0.01f);
  324. TestRunner.AssertApprox(vec3Val, vec3Start, 0.01f);
  325. TestRunner.AssertApprox(vec4Val, vec4Start, 0.01f);
  326. queue.Update(0.0);
  327. queue.Update(0.5);
  328. // Check that it does jump on repeat
  329. TestRunner.AssertApprox(floatVal, floatEnd * 0.5f + floatStart * 0.5f, 0.01);
  330. TestRunner.AssertApprox(doubleVal, doubleEnd * 0.5 + doubleStart * 0.5, 0.01);
  331. TestRunner.AssertApprox(vec2Val, vec2End * 0.5f + vec2Start * 0.5f, 0.01f);
  332. TestRunner.AssertApprox(vec3Val, vec3End * 0.5f + vec3Start * 0.5f, 0.01f);
  333. TestRunner.AssertApprox(vec4Val, vec4End * 0.5f + vec4Start * 0.5f, 0.01f);
  334. Rect rectEnd = new Rect(-1.0f, 1.0f, 5.0f, 5.0f);
  335. Rect rectStart = new Rect(0.0f,2.0f, 6.0f, 6.0f);
  336. Rect rectVal = rectStart;
  337. Ref<Rect> rectRef = new Ref<Rect>(
  338. () => rectVal,
  339. t => rectVal = t
  340. );
  341. Vector2 firstAnchor = new Vector2(0.0f, 0.0f);
  342. Vector2 secondAnchor = new Vector2(1.0f, 0.0f);
  343. Vector3 thirdAnchor = new Vector2(0.0f, 1.0f);
  344. Vector2 forthAnchor = new Vector2(1.0f, 1.0f);
  345. queue = new CommandQueue();
  346. queue.Enqueue(
  347. Commands.ChangeFrom(rectRef, rectEnd, 1.0, firstAnchor),
  348. Commands.WaitForFrames(1),
  349. Commands.ChangeFrom(rectRef, rectEnd, 1.0, secondAnchor),
  350. Commands.WaitForFrames(1),
  351. Commands.ChangeFrom(rectRef, rectEnd, 1.0, thirdAnchor),
  352. Commands.WaitForFrames(1),
  353. Commands.ChangeFrom(rectRef, rectEnd, 1.0, forthAnchor)
  354. );
  355. // Test the top left corner.
  356. queue.Update(0.5);
  357. TestRunner.AssertApprox(rectVal, new Rect(
  358. -0.5f, 1.5f,
  359. (rectStart.width + rectEnd.width) * 0.5f,
  360. (rectStart.height + rectEnd.height) * 0.5f), 0.001f
  361. );
  362. queue.Update(0.5);
  363. TestRunner.AssertApprox(rectVal, rectStart, 0.001f);
  364. queue.Update(0.0f);
  365. // Test the top right corner.
  366. queue.Update(0.7);
  367. TestRunner.AssertApprox(rectVal, new Rect(
  368. 5.4f - 5.7f, 1.7f,
  369. rectStart.width * 0.7f + rectEnd.width * 0.3f,
  370. rectStart.height * 0.7f + rectEnd.height * 0.3f), 0.001f
  371. );
  372. queue.Update(0.3);
  373. TestRunner.AssertApprox(rectVal, rectStart, 0.001f);
  374. queue.Update(0.0f);
  375. // Test the bottom left corner.
  376. queue.Update(0.6);
  377. TestRunner.AssertApprox(rectVal, new Rect(
  378. -0.4f, 7.2f - 5.6f,
  379. rectStart.width * 0.6f + rectEnd.width * 0.4f,
  380. rectStart.height * 0.6f + rectEnd.height * 0.4f), 0.001f
  381. );
  382. queue.Update(0.4);
  383. TestRunner.AssertApprox(rectVal, rectStart, 0.001f);
  384. queue.Update(0.0);
  385. // Test the bottom right corner.
  386. queue.Update(0.6);
  387. TestRunner.AssertApprox(rectVal, new Rect(
  388. 5.2f - 5.6f, 7.2f - 5.6f,
  389. rectStart.width * 0.6f + rectEnd.width * 0.4f,
  390. rectStart.height * 0.6f + rectEnd.height * 0.4f), 0.001f
  391. );
  392. queue.Update(0.4);
  393. TestRunner.AssertApprox(rectVal, rectStart, 0.001f);
  394. queue.Update(0.0f);
  395. }
  396. [Test]
  397. public static void TestRotateBy()
  398. {
  399. Quaternion quatStart = Quaternion.Euler(10.0f, 20.0f, 30.0f);
  400. Quaternion quatOffset = Quaternion.Euler(30.0f,20.0f,10.0f);
  401. Quaternion quatVal = quatStart;
  402. Ref<Quaternion> quatRef = new Ref<Quaternion>(
  403. () => quatVal,
  404. t => quatVal = t
  405. );
  406. CommandQueue queue = new CommandQueue();
  407. queue.Enqueue(
  408. Commands.Repeat(2,
  409. Commands.RotateBy(quatRef, quatOffset, 1.0)
  410. )
  411. );
  412. queue.Update(0.5f);
  413. TestRunner.AssertApprox(quatVal, Quaternion.Slerp(quatStart, quatStart * quatOffset, 0.5f), 0.000001f);
  414. quatVal = Quaternion.identity;
  415. queue.Update(0.5f);
  416. TestRunner.AssertApprox(quatVal, Quaternion.Slerp(Quaternion.identity, quatOffset, 0.5f), 0.000001f);
  417. queue.Update(0.5f);
  418. TestRunner.AssertApprox(quatVal, quatOffset, 0.001f);
  419. queue.Update(0.5f);
  420. TestRunner.AssertApprox(quatVal, quatOffset * Quaternion.Slerp(Quaternion.identity, quatOffset, 0.5f), 0.000001f);
  421. queue = new CommandQueue();
  422. // Make sure the rotation ends in the correct position when given a complex easing function.
  423. quatVal = Quaternion.identity;
  424. queue.Enqueue(
  425. Commands.RotateBy(quatRef, quatOffset, 1f, Ease.OutElastic())
  426. );
  427. while (!queue.Update(1 / 30f)) {}
  428. TestRunner.AssertApprox(quatVal, quatOffset, 0.001f);
  429. }
  430. [Test]
  431. public static void TestRotateTo()
  432. {
  433. Quaternion quatStart = Quaternion.Euler(10.0f, 20.0f, 30.0f);
  434. Quaternion quatEnd = Quaternion.Euler(30.0f,20.0f,10.0f);
  435. Quaternion quatVal = quatStart;
  436. Ref<Quaternion> quatRef = new Ref<Quaternion>(
  437. () => quatVal,
  438. t => quatVal = t
  439. );
  440. CommandQueue queue = new CommandQueue();
  441. queue.Enqueue(
  442. Commands.Repeat(2,
  443. Commands.RotateTo(quatRef, quatEnd, 1.0)
  444. )
  445. );
  446. queue.Update(0.5);
  447. TestRunner.AssertApprox(quatVal, Quaternion.Slerp(quatStart, quatEnd, 0.5f), 0.000001f);
  448. quatVal = Quaternion.identity;
  449. queue.Update(0.5);
  450. TestRunner.AssertApprox(quatVal, quatEnd, 0.000001f);
  451. queue.Update(0.5);
  452. TestRunner.AssertApprox(quatVal, quatEnd, 0.000001f);
  453. // Make sure the rotation ends in the correct position when given a complex easing function.
  454. queue = new CommandQueue();
  455. quatVal = quatStart;
  456. queue.Enqueue(
  457. Commands.RotateTo(quatRef, quatEnd, 1f, Ease.OutElastic())
  458. );
  459. while (!queue.Update(1 / 30f)) {}
  460. TestRunner.AssertApprox(quatVal, quatEnd, 0.001f);
  461. }
  462. [Test]
  463. public static void TestRotateFrom()
  464. {
  465. Quaternion quatStart = Quaternion.Euler(10.0f, 20.0f, 30.0f);
  466. Quaternion quatEnd = Quaternion.Euler(30.0f,20.0f,10.0f);
  467. Quaternion quatVal = quatStart;
  468. Ref<Quaternion> quatRef = new Ref<Quaternion>(
  469. () => quatVal,
  470. t => quatVal = t
  471. );
  472. CommandQueue queue = new CommandQueue();
  473. queue.Enqueue(
  474. Commands.Repeat(2,
  475. Commands.Sequence(
  476. Commands.RotateFrom(quatRef, quatEnd, 1.0),
  477. Commands.WaitForFrames(1)
  478. )
  479. )
  480. );
  481. queue.Update(0.5);
  482. TestRunner.AssertApprox(quatVal, Quaternion.Slerp(quatEnd, quatStart, 0.5f), 0.000001f);
  483. quatVal = Quaternion.identity;
  484. queue.Update(0.5);
  485. TestRunner.AssertApprox(quatVal, quatStart, 0.000001f);
  486. queue.Update(0.0);
  487. queue.Update(0.5);
  488. TestRunner.AssertApprox(quatVal, Quaternion.Slerp(quatEnd, quatStart, 0.5f), 0.000001f);
  489. // Make sure the rotation ends in the correct position when given a complex easing function.
  490. queue = new CommandQueue();
  491. quatVal = quatStart;
  492. queue.Enqueue(
  493. Commands.RotateFrom(quatRef, quatEnd, 1f, Ease.OutElastic())
  494. );
  495. while (!queue.Update(1 / 30f)) {}
  496. TestRunner.AssertApprox(quatVal, quatStart, 0.001f);
  497. }
  498. [Test]
  499. public static void TestScaleBy()
  500. {
  501. const float floatScale = 4.8f;
  502. const float floatStart = 1.2f;
  503. float floatVal = floatStart;
  504. Ref<float> floatRef = new Ref<float>(
  505. () => floatVal,
  506. t => floatVal = t
  507. );
  508. const double doubleScale = 3.2;
  509. const double doubleStart = 9.2;
  510. double doubleVal = doubleStart;
  511. Ref<double> doubleRef = new Ref<double>(
  512. () => doubleVal,
  513. t => doubleVal = t
  514. );
  515. Vector2 vec2Scale = new Vector2(9.5f, 2.0f);
  516. Vector2 vec2Start = new Vector2(4.0f, 5.0f);
  517. Vector2 vec2Val = vec2Start;
  518. Ref<Vector2> vec2Ref = new Ref<Vector2>(
  519. () => vec2Val,
  520. t => vec2Val = t
  521. );
  522. Vector3 vec3Scale = new Vector3(4.0f, 19.0f, 2.0f);
  523. Vector3 vec3Start = new Vector3(92.0f, 0.5f, 34.0f);
  524. Vector3 vec3Val = vec3Start;
  525. Ref<Vector3> vec3Ref = new Ref<Vector3>(
  526. () => vec3Val,
  527. t => vec3Val = t
  528. );
  529. Vector4 vec4Scale = new Vector4(92.0f, 0.5f, 14.0f, 7.0f);
  530. Vector4 vec4Start = new Vector4(0.4f, 10.0f, 3.0f, 82.0f);
  531. Vector4 vec4Val = vec4Start;
  532. Ref<Vector4> vec4Ref = new Ref<Vector4>(
  533. () => vec4Val,
  534. t => vec4Val = t
  535. );
  536. CommandQueue queue = new CommandQueue();
  537. queue.Enqueue(
  538. Commands.Repeat(2,
  539. Commands.Sequence(
  540. Commands.Parallel(
  541. Commands.ScaleBy(floatRef, floatScale, 1.0),
  542. Commands.ScaleBy(doubleRef, doubleScale, 1.0),
  543. Commands.ScaleBy(vec2Ref, vec2Scale, 1.0),
  544. Commands.ScaleBy(vec3Ref, vec3Scale, 1.0),
  545. Commands.ScaleBy(vec4Ref, vec4Scale, 1.0)
  546. ),
  547. Commands.WaitForFrames(1)
  548. )
  549. )
  550. );
  551. queue.Update(0.2f);
  552. Vector2 vec2ExpectedScale = vec2Scale;
  553. Vector3 vec3ExpectedScale = vec3Scale;
  554. Vector4 vec4ExpectedScale = vec4Scale;
  555. vec2ExpectedScale.Scale(new Vector2(0.2f, 0.2f));
  556. vec3ExpectedScale.Scale(new Vector3(0.2f, 0.2f, 0.2f));
  557. vec4ExpectedScale.Scale(new Vector4(0.2f, 0.2f, 0.2f, 0.2f));
  558. vec2ExpectedScale += new Vector2(0.8f, 0.8f);
  559. vec3ExpectedScale += new Vector3(0.8f, 0.8f, 0.8f);
  560. vec4ExpectedScale += new Vector4(0.8f, 0.8f, 0.8f, 0.8f);
  561. vec2ExpectedScale.Scale(vec2Start);
  562. vec3ExpectedScale.Scale(vec3Start);
  563. vec4ExpectedScale.Scale(vec4Start);
  564. TestRunner.AssertApprox(floatVal, floatStart * (0.8f + floatScale * 0.2f), 0.001f);
  565. TestRunner.AssertApprox(doubleVal, doubleStart * (0.8 + doubleScale * 0.2), 0.001f);
  566. TestRunner.AssertApprox(vec2Val, vec2ExpectedScale, 0.001f);
  567. TestRunner.AssertApprox(vec3Val, vec3ExpectedScale, 0.001f);
  568. TestRunner.AssertApprox(vec4Val, vec4ExpectedScale, 0.001f);
  569. queue.Update(0.8);
  570. vec2ExpectedScale = vec2Scale;
  571. vec3ExpectedScale = vec3Scale;
  572. vec4ExpectedScale = vec4Scale;
  573. vec2ExpectedScale.Scale(vec2Start);
  574. vec3ExpectedScale.Scale(vec3Start);
  575. vec4ExpectedScale.Scale(vec4Start);
  576. TestRunner.AssertApprox(floatVal, floatStart * floatScale, 0.001f);
  577. TestRunner.AssertApprox(doubleVal, doubleStart * doubleScale, 0.001f);
  578. TestRunner.AssertApprox(vec2Val, vec2ExpectedScale, 0.001f);
  579. TestRunner.AssertApprox(vec3Val, vec3ExpectedScale, 0.001f);
  580. TestRunner.AssertApprox(vec4Val, vec4ExpectedScale, 0.001f);
  581. floatVal = floatStart;
  582. doubleVal = doubleStart;
  583. vec2Val = vec2Start;
  584. vec3Val = vec3Start;
  585. vec4Val = vec4Start;
  586. queue.Update(0.0);
  587. queue.Update(0.5);
  588. vec2ExpectedScale = vec2Scale;
  589. vec3ExpectedScale = vec3Scale;
  590. vec4ExpectedScale = vec4Scale;
  591. vec2ExpectedScale.Scale(new Vector2(0.5f, 0.5f));
  592. vec3ExpectedScale.Scale(new Vector3(0.5f, 0.5f, 0.5f));
  593. vec4ExpectedScale.Scale(new Vector4(0.5f, 0.5f, 0.5f, 0.5f));
  594. vec2ExpectedScale += new Vector2(0.5f, 0.5f);
  595. vec3ExpectedScale += new Vector3(0.5f, 0.5f, 0.5f);
  596. vec4ExpectedScale += new Vector4(0.5f, 0.5f, 0.5f, 0.5f);
  597. vec2ExpectedScale.Scale(vec2Start);
  598. vec3ExpectedScale.Scale(vec3Start);
  599. vec4ExpectedScale.Scale(vec4Start);
  600. TestRunner.AssertApprox(floatVal, floatStart * (0.5f + floatScale * 0.5f), 0.001f);
  601. TestRunner.AssertApprox(doubleVal, doubleStart * (0.5 + doubleScale * 0.5), 0.001f);
  602. TestRunner.AssertApprox(vec2Val, vec2ExpectedScale, 0.001f);
  603. TestRunner.AssertApprox(vec3Val, vec3ExpectedScale, 0.001f);
  604. TestRunner.AssertApprox(vec4Val, vec4ExpectedScale, 0.001f);
  605. }
  606. [Test]
  607. public static void TestTintBy()
  608. {
  609. Color colourStart = new Color(0.4f, 0.2f, 0.7f, 0.5f);
  610. Color colourOffset = new Color(0.3f, 0.4f, 0.15f, 0.25f);
  611. Color colourVal = colourStart;
  612. Ref<Color> colourRef = new Ref<Color>(
  613. () => colourVal,
  614. t => colourVal = t
  615. );
  616. CommandQueue queue = new CommandQueue();
  617. queue.Enqueue(
  618. Commands.Repeat(2,
  619. Commands.TintBy(colourRef, colourOffset, 1.0)
  620. )
  621. );
  622. queue.Update(0.5);
  623. TestRunner.AssertApprox(colourVal, new Color(0.55f, 0.4f, 0.775f, 0.625f), 0.001f);
  624. colourVal = colourStart;
  625. queue.Update(0.5);
  626. TestRunner.AssertApprox(colourVal, new Color(0.55f, 0.4f, 0.775f, 0.625f), 0.001f);
  627. queue.Update(0.5);
  628. TestRunner.AssertApprox(colourVal, new Color(0.7f, 0.6f, 0.85f, 0.75f), 0.001f);
  629. }
  630. [Test]
  631. public static void TestTintTo()
  632. {
  633. Color colourStart = new Color(0.4f, 0.2f, 0.7f, 0.5f);
  634. Color colourEnd = new Color(0.3f, 0.4f, 0.15f, 0.25f);
  635. Color colourVal = colourStart;
  636. Ref<Color> colourRef = new Ref<Color>(
  637. () => colourVal,
  638. t => colourVal = t
  639. );
  640. CommandQueue queue = new CommandQueue();
  641. queue.Enqueue(
  642. Commands.Repeat(2,
  643. Commands.TintTo(colourRef, colourEnd, 1.0)
  644. )
  645. );
  646. queue.Update(0.2);
  647. TestRunner.AssertApprox(colourVal, colourStart * 0.8f + colourEnd * 0.2f, 0.001f);
  648. colourVal = colourStart;
  649. queue.Update(0.8);
  650. TestRunner.AssertApprox(colourVal, colourEnd, 0.001f);
  651. queue.Update(0.5);
  652. TestRunner.AssertApprox(colourVal, colourEnd, 0.001f);
  653. }
  654. [Test]
  655. public static void TestTintFrom()
  656. {
  657. Color colourStart = new Color(0.4f, 0.2f, 0.7f, 0.5f);
  658. Color colourEnd = new Color(0.3f, 0.4f, 0.15f, 0.25f);
  659. Color colourVal = colourStart;
  660. Ref<Color> colourRef = new Ref<Color>(
  661. () => colourVal,
  662. t => colourVal = t
  663. );
  664. CommandQueue queue = new CommandQueue();
  665. queue.Enqueue(
  666. Commands.Repeat(2,
  667. Commands.Sequence(
  668. Commands.TintFrom(colourRef, colourEnd, 1.0),
  669. Commands.WaitForFrames(1)
  670. )
  671. )
  672. );
  673. queue.Update(0.2);
  674. TestRunner.AssertApprox(colourVal, colourStart * 0.2f + colourEnd * 0.8f, 0.001f);
  675. colourVal = colourStart;
  676. queue.Update(0.8);
  677. TestRunner.AssertApprox(colourVal, colourStart, 0.001f);
  678. queue.Update(0.0);
  679. queue.Update(0.5);
  680. TestRunner.AssertApprox(colourVal, colourStart * 0.5f + colourEnd * 0.5f, 0.001f);
  681. }
  682. }
  683. }