CamerasBlendingTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Collections;
  2. using NUnit.Framework;
  3. using UnityEngine;
  4. using UnityEngine.TestTools;
  5. using Cinemachine;
  6. [TestFixture]
  7. [Ignore("Instability in blending tests")]
  8. public class CamerasBlendingTests
  9. {
  10. private Camera cam;
  11. private CinemachineVirtualCamera sourceVCam;
  12. private CinemachineVirtualCamera targetVCam;
  13. private CinemachineBrain brain;
  14. private GameObject followObject;
  15. private const float BlendingTime = 1;
  16. [SetUp]
  17. public void Setup()
  18. {
  19. // Camera
  20. var cameraHolder = new GameObject("MainCamera");
  21. cam = cameraHolder.AddComponent<Camera>();
  22. brain = cameraHolder.AddComponent<CinemachineBrain>();
  23. // Blending
  24. brain.m_DefaultBlend = new CinemachineBlendDefinition(
  25. CinemachineBlendDefinition.Style.Linear,
  26. BlendingTime);
  27. followObject = new GameObject("Follow Object");
  28. // Source vcam
  29. var sourceVCamHolder = new GameObject("Source CM Vcam");
  30. sourceVCam = sourceVCamHolder.AddComponent<CinemachineVirtualCamera>();
  31. sourceVCam.Priority = 2;
  32. sourceVCam.Follow = followObject.transform;
  33. // target vcam
  34. var targetVCamHolder = new GameObject("Target CM Vcam");
  35. targetVCam = targetVCamHolder.AddComponent<CinemachineVirtualCamera>();
  36. targetVCam.Priority = 1;
  37. targetVCam.Follow = followObject.transform;
  38. CinemachineCore.UniformDeltaTimeOverride = 0.1f;
  39. }
  40. [TearDown]
  41. public void Teardown()
  42. {
  43. Object.Destroy(cam.gameObject);
  44. Object.Destroy(sourceVCam.gameObject);
  45. Object.Destroy(targetVCam.gameObject);
  46. Object.Destroy(followObject);
  47. CinemachineCore.UniformDeltaTimeOverride = -1f;
  48. }
  49. [UnityTest]
  50. public IEnumerator BlendingBetweenCameras()
  51. {
  52. targetVCam.Priority = 3;
  53. yield return null;
  54. yield return new WaitForSeconds(BlendingTime + 0.1f);
  55. Assert.IsTrue(!brain.IsBlending);
  56. }
  57. [UnityTest]
  58. public IEnumerator InterruptedBlendingBetweenCameras()
  59. {
  60. // Start blending
  61. targetVCam.Priority = 3;
  62. yield return null;
  63. // Wait for 90% of blending duration
  64. yield return new WaitForSeconds(BlendingTime * 0.9f);
  65. // Blend back to source
  66. targetVCam.Priority = 1;
  67. yield return null;
  68. yield return new WaitForSeconds(BlendingTime * 0.1f);
  69. // Quickly blend to target again
  70. targetVCam.Priority = 3;
  71. yield return null;
  72. // We went 90%, then got 10% back, it means we are 20% away from the target
  73. yield return new WaitForSeconds(BlendingTime * 0.21f);
  74. Assert.IsTrue(!brain.IsBlending);
  75. // Start blending
  76. targetVCam.Priority = 3;
  77. yield return null;
  78. // Wait for 90% of blending duration
  79. yield return new WaitForSeconds(BlendingTime * 0.9f);
  80. // Blend back to source
  81. targetVCam.Priority = 1;
  82. yield return null;
  83. yield return new WaitForSeconds(BlendingTime * 0.1f);
  84. // Quickly blend to target again
  85. targetVCam.Priority = 3;
  86. yield return null;
  87. // We went 90%, then got 10% back, it means we are 20% away from the target - wait only 10% worth
  88. yield return new WaitForSeconds(BlendingTime * 0.1f);
  89. // Blend back to source
  90. targetVCam.Priority = 1;
  91. yield return null;
  92. yield return new WaitForSeconds(BlendingTime * 0.1f);
  93. // Quickly blend to target again
  94. targetVCam.Priority = 3;
  95. yield return null;
  96. // We went 90%, then got 10% back, it means we are 20% away from the target
  97. yield return new WaitForSeconds(BlendingTime * 0.21f);
  98. Assert.IsTrue(!brain.IsBlending);
  99. }
  100. [UnityTest]
  101. public IEnumerator DoesInterruptedBlendingBetweenCamerasTakesDoubleTime()
  102. {
  103. // Start blending
  104. targetVCam.Priority = 3;
  105. yield return null;
  106. // Wait for 90% of blending duration
  107. yield return new WaitForSeconds(BlendingTime * 0.9f);
  108. // Blend back to source
  109. targetVCam.Priority = 1;
  110. yield return null;
  111. yield return new WaitForSeconds(BlendingTime * 0.1f);
  112. // Quickly blend to target again
  113. targetVCam.Priority = 3;
  114. yield return null;
  115. // We went 90%, then got 10% back, it means we are 20% away from the target
  116. yield return new WaitForSeconds(BlendingTime + 0.01f);
  117. Assert.IsTrue(!brain.IsBlending);
  118. }
  119. }