Light2DTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using NUnit.Framework;
  2. using System.Collections;
  3. using UnityEngine.Experimental.Rendering.Universal;
  4. using UnityEngine.TestTools;
  5. namespace UnityEngine.Rendering.Universal.Tests
  6. {
  7. [TestFixture]
  8. class SingleObjectLight2DTests
  9. {
  10. Light2DManager m_LightManager;
  11. GameObject m_TestObject;
  12. Light2D m_TestLight;
  13. [SetUp]
  14. public void Setup()
  15. {
  16. m_LightManager = new Light2DManager();
  17. m_TestObject = new GameObject("Test Object");
  18. m_TestLight = m_TestObject.AddComponent<Light2D>();
  19. }
  20. [TearDown]
  21. public void Cleanup()
  22. {
  23. Object.DestroyImmediate(m_TestObject);
  24. m_LightManager.Dispose();
  25. }
  26. [Test]
  27. public void SetupCullingSetsBoundingSpheresAndCullingIndices()
  28. {
  29. Light2D.SetupCulling(default(ScriptableRenderContext), Camera.main);
  30. Assert.NotNull(Light2DManager.boundingSpheres);
  31. Assert.AreEqual(1024, Light2DManager.boundingSpheres.Length);
  32. Assert.AreEqual(0, m_TestLight.lightCullingIndex);
  33. }
  34. [UnityTest]
  35. public IEnumerator ChangingBlendStyleMovesTheLightToTheCorrectListInLightManager()
  36. {
  37. m_TestLight.blendStyleIndex = 1;
  38. // LightManager update happens in LateUpdate(). So we must test the result in the next frame.
  39. yield return null;
  40. Assert.AreEqual(0, Light2DManager.lights[0].Count);
  41. Assert.AreSame(m_TestLight, Light2DManager.lights[1][0]);
  42. }
  43. }
  44. [TestFixture]
  45. class MultipleObjectLight2DTests
  46. {
  47. Light2DManager m_LightManager;
  48. GameObject m_TestObject1;
  49. GameObject m_TestObject2;
  50. GameObject m_TestObject3;
  51. [SetUp]
  52. public void Setup()
  53. {
  54. m_LightManager = new Light2DManager();
  55. m_TestObject1 = new GameObject("Test Object 1");
  56. m_TestObject2 = new GameObject("Test Object 2");
  57. m_TestObject3 = new GameObject("Test Object 3");
  58. }
  59. [TearDown]
  60. public void Cleanup()
  61. {
  62. Object.DestroyImmediate(m_TestObject3);
  63. Object.DestroyImmediate(m_TestObject2);
  64. Object.DestroyImmediate(m_TestObject1);
  65. m_LightManager.Dispose();
  66. }
  67. [UnityTest]
  68. public IEnumerator LightsAreSortedByLightOrder()
  69. {
  70. var light1 = m_TestObject1.AddComponent<Light2D>();
  71. var light2 = m_TestObject2.AddComponent<Light2D>();
  72. var light3 = m_TestObject3.AddComponent<Light2D>();
  73. light1.lightOrder = 1;
  74. light2.lightOrder = 2;
  75. light3.lightOrder = 0;
  76. // Sorting happens in LateUpdate() after light order has changed.
  77. // So we must test the result in the next frame.
  78. yield return null;
  79. Assert.AreSame(light3, Light2DManager.lights[0][0]);
  80. Assert.AreSame(light1, Light2DManager.lights[0][1]);
  81. Assert.AreSame(light2, Light2DManager.lights[0][2]);
  82. }
  83. [Test]
  84. public void IsLightVisibleReturnsTrueIfInCameraView()
  85. {
  86. var camera = m_TestObject1.AddComponent<Camera>();
  87. var light = m_TestObject2.AddComponent<Light2D>();
  88. light.transform.position = camera.transform.position;
  89. Light2D.SetupCulling(default(ScriptableRenderContext), camera);
  90. // We can only verify the results after culling is done on this camera.
  91. camera.Render();
  92. Assert.IsTrue(light.IsLightVisible(camera));
  93. }
  94. [Test]
  95. public void IsLightVisibleReturnsFalseIfNotInCameraView()
  96. {
  97. var camera = m_TestObject1.AddComponent<Camera>();
  98. var light = m_TestObject2.AddComponent<Light2D>();
  99. light.transform.position = camera.transform.position + new Vector3(9999.0f, 0.0f, 0.0f);
  100. Light2D.SetupCulling(default(ScriptableRenderContext), camera);
  101. // We can only verify the results after culling is done on this camera.
  102. camera.Render();
  103. Assert.IsFalse(light.IsLightVisible(camera));
  104. }
  105. [Test]
  106. public void DestroyingLastLightAlsoDestroysCullingGroup()
  107. {
  108. var light1 = m_TestObject1.AddComponent<Light2D>();
  109. var light2 = m_TestObject2.AddComponent<Light2D>();
  110. Assert.IsNotNull(Light2DManager.cullingGroup);
  111. Object.Destroy(light2);
  112. Assert.IsNotNull(Light2DManager.cullingGroup);
  113. Object.Destroy(light1);
  114. Assert.IsNull(Light2DManager.cullingGroup);
  115. }
  116. }
  117. }