暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Light2DTests.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Collections;
  2. using NUnit.Framework;
  3. using UnityEngine.Rendering.Universal;
  4. using UnityEngine.TestTools;
  5. namespace UnityEngine.Rendering.Universal.Tests
  6. {
  7. [TestFixture]
  8. class MultipleObjectLight2DTests
  9. {
  10. GameObject m_TestObject1;
  11. GameObject m_TestObject2;
  12. GameObject m_TestObject3;
  13. GameObject m_TestObject4;
  14. GameObject m_TestObjectCached;
  15. [SetUp]
  16. public void Setup()
  17. {
  18. m_TestObject1 = new GameObject("Test Object 1");
  19. m_TestObject2 = new GameObject("Test Object 2");
  20. m_TestObject3 = new GameObject("Test Object 3");
  21. m_TestObject4 = new GameObject("Test Object 4");
  22. m_TestObjectCached = new GameObject("Test Object Cached");
  23. }
  24. [TearDown]
  25. public void Cleanup()
  26. {
  27. Object.DestroyImmediate(m_TestObjectCached);
  28. Object.DestroyImmediate(m_TestObject4);
  29. Object.DestroyImmediate(m_TestObject3);
  30. Object.DestroyImmediate(m_TestObject2);
  31. Object.DestroyImmediate(m_TestObject1);
  32. }
  33. [Test]
  34. public void LightsAreSortedByLightOrder()
  35. {
  36. var light1 = m_TestObject1.AddComponent<Light2D>();
  37. var light2 = m_TestObject2.AddComponent<Light2D>();
  38. var light3 = m_TestObject3.AddComponent<Light2D>();
  39. light1.lightOrder = 1;
  40. light2.lightOrder = 2;
  41. light3.lightOrder = 0;
  42. var camera = m_TestObject4.AddComponent<Camera>();
  43. var cameraPos = camera.transform.position;
  44. light1.transform.position = cameraPos;
  45. light2.transform.position = cameraPos;
  46. light3.transform.position = cameraPos;
  47. light1.UpdateMesh(true);
  48. light1.UpdateBoundingSphere();
  49. light2.UpdateMesh(true);
  50. light2.UpdateBoundingSphere();
  51. light3.UpdateMesh(true);
  52. light3.UpdateBoundingSphere();
  53. var cullResult = new Light2DCullResult();
  54. var cullingParams = new ScriptableCullingParameters();
  55. camera.TryGetCullingParameters(out cullingParams);
  56. cullResult.SetupCulling(ref cullingParams, camera);
  57. Assert.AreSame(light3, cullResult.visibleLights[0]);
  58. Assert.AreSame(light1, cullResult.visibleLights[1]);
  59. Assert.AreSame(light2, cullResult.visibleLights[2]);
  60. }
  61. [Test]
  62. public void LightIsInVisibleListIfInCameraView()
  63. {
  64. var camera = m_TestObject1.AddComponent<Camera>();
  65. var light = m_TestObject2.AddComponent<Light2D>();
  66. light.transform.position = camera.transform.position;
  67. light.UpdateMesh(true);
  68. light.UpdateBoundingSphere();
  69. var cullResult = new Light2DCullResult();
  70. var cullingParams = new ScriptableCullingParameters();
  71. camera.TryGetCullingParameters(out cullingParams);
  72. cullResult.SetupCulling(ref cullingParams, camera);
  73. Assert.Contains(light, cullResult.visibleLights);
  74. }
  75. [Test]
  76. public void LightIsNotInVisibleListIfNotInCameraView()
  77. {
  78. var camera = m_TestObject1.AddComponent<Camera>();
  79. var light = m_TestObject2.AddComponent<Light2D>();
  80. light.transform.position = camera.transform.position + new Vector3(9999.0f, 0.0f, 0.0f);
  81. light.UpdateMesh(true);
  82. light.UpdateBoundingSphere();
  83. var cullResult = new Light2DCullResult();
  84. var cullingParams = new ScriptableCullingParameters();
  85. camera.TryGetCullingParameters(out cullingParams);
  86. cullResult.SetupCulling(ref cullingParams, camera);
  87. Assert.IsFalse(cullResult.visibleLights.Contains(light));
  88. }
  89. [Test]
  90. public void CachedMeshDataIsUpdatedOnChange()
  91. {
  92. var shapePath = new Vector3[4] { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 0), new Vector3(0, 1, 0) };
  93. var light = m_TestObjectCached.AddComponent<Light2D>();
  94. light.lightType = Light2D.LightType.Freeform;
  95. light.SetShapePath(shapePath);
  96. light.UpdateMesh(true);
  97. Assert.AreEqual(true, light.hasCachedMesh);
  98. }
  99. [Test]
  100. public void CachedMeshDataIsOverriddenByRuntimeChanges()
  101. {
  102. var shapePath = new Vector3[4] { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 0), new Vector3(0, 1, 0) };
  103. var light = m_TestObjectCached.AddComponent<Light2D>();
  104. light.lightType = Light2D.LightType.Freeform;
  105. light.SetShapePath(shapePath);
  106. light.UpdateMesh(true);
  107. int vertexCount = 0, triangleCount = 0;
  108. // Check if Cached Data and the actual data are the same.
  109. Assert.AreEqual(true, light.hasCachedMesh);
  110. vertexCount = light.lightMesh.triangles.Length;
  111. triangleCount = light.lightMesh.vertices.Length;
  112. // Simulate Runtime Behavior.
  113. var shapePathChanged = new Vector3[5] { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 0), new Vector3(0.5f, 1.5f, 0), new Vector3(0, 1, 0) };
  114. light.SetShapePath(shapePathChanged);
  115. light.UpdateMesh(true);
  116. // Check if Cached Data and the actual data are no longer the same. (We don't save cache on Runtime)
  117. Assert.AreNotEqual(vertexCount, light.lightMesh.triangles.Length);
  118. Assert.AreNotEqual(triangleCount, light.lightMesh.vertices.Length);
  119. }
  120. [Test]
  121. public void EnsureShapeMeshGenerationDoesNotOverflowAllocation()
  122. {
  123. var shapePath = new Vector3[4] { new Vector3(-76.04548f, 7.522535f, 0f), new Vector3(-66.52518f, 18.88778f, 0f), new Vector3(-66.35441f, 24.34475f, 0), new Vector3(-75.15407f, 33.0358f, 0) };
  124. var light = m_TestObjectCached.AddComponent<Light2D>();
  125. light.lightType = Light2D.LightType.Freeform;
  126. LightUtility.GenerateShapeMesh(light, shapePath, 180.0f, 0);
  127. Assert.AreEqual(true, light.hasCachedMesh);
  128. }
  129. }
  130. }