Ingen beskrivning
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.

ParametricToFreeformLightUpgrader.cs 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Rendering.Universal;
  6. using UnityEditor.SceneManagement;
  7. namespace UnityEditor.Rendering.Universal
  8. {
  9. internal sealed class ParametricToFreeformLightUpgrader : RenderPipelineConverter
  10. {
  11. const float k_EnscribedSquareDiagonalLength = 0.70710678118654752440084436210485f;
  12. public override string name => "Parametric to Freeform Light Upgrade";
  13. public override string info => "This will upgrade all parametric lights to freeform lights.";
  14. public override int priority => -1000;
  15. public override Type container => typeof(UpgradeURP2DAssetsContainer);
  16. List<string> m_AssetsToConvert = new List<string>();
  17. string m_Light2DId;
  18. public static void UpgradeParametricLight(Light2D light)
  19. {
  20. if (light.lightType == (Light2D.LightType)Light2D.DeprecatedLightType.Parametric)
  21. {
  22. light.lightType = Light2D.LightType.Freeform;
  23. // Parametric radius has to be > 0 in order mesh tessellation to be valid
  24. if (light.shapeLightParametricRadius == 0)
  25. light.shapeLightParametricRadius = 0.01f;
  26. float radius = light.shapeLightParametricRadius;
  27. float angle = light.shapeLightParametricAngleOffset;
  28. int sides = light.shapeLightParametricSides;
  29. var angleOffset = Mathf.PI / 2.0f + Mathf.Deg2Rad * angle;
  30. if (sides < 3)
  31. {
  32. radius = k_EnscribedSquareDiagonalLength * radius;
  33. sides = 4;
  34. }
  35. if (sides == 4)
  36. {
  37. angleOffset = Mathf.PI / 4.0f + Mathf.Deg2Rad * angle;
  38. }
  39. var radiansPerSide = 2 * Mathf.PI / sides;
  40. var min = new Vector3(float.MaxValue, float.MaxValue, 0);
  41. var max = new Vector3(float.MinValue, float.MinValue, 0);
  42. Vector3[] shapePath = new Vector3[sides];
  43. for (var i = 0; i < sides; i++)
  44. {
  45. var endAngle = (i + 1) * radiansPerSide;
  46. var extrudeDir = new Vector3(Mathf.Cos(endAngle + angleOffset), Mathf.Sin(endAngle + angleOffset), 0);
  47. var endPoint = radius * extrudeDir;
  48. shapePath[i] = endPoint;
  49. }
  50. light.shapePath = shapePath;
  51. light.UpdateMesh();
  52. EditorSceneManager.MarkSceneDirty(light.gameObject.scene);
  53. }
  54. }
  55. void UpgradeGameObject(GameObject go)
  56. {
  57. Light2D[] lights = go.GetComponentsInChildren<Light2D>();
  58. foreach (Light2D light in lights)
  59. {
  60. if (light.lightType == Light2D.LightType.Parametric && !PrefabUtility.IsPartOfPrefabInstance(light))
  61. UpgradeParametricLight(light);
  62. }
  63. }
  64. public override void OnInitialize(InitializeConverterContext context, Action callback)
  65. {
  66. string[] allAssetPaths = AssetDatabase.GetAllAssetPaths();
  67. foreach (string path in allAssetPaths)
  68. {
  69. if (URP2DConverterUtility.IsPrefabOrScenePath(path, "m_LightType: 0"))
  70. {
  71. ConverterItemDescriptor desc = new ConverterItemDescriptor()
  72. {
  73. name = Path.GetFileNameWithoutExtension(path),
  74. info = path,
  75. warningMessage = String.Empty,
  76. helpLink = String.Empty
  77. };
  78. // Each converter needs to add this info using this API.
  79. m_AssetsToConvert.Add(path);
  80. context.AddAssetToConvert(desc);
  81. }
  82. }
  83. callback.Invoke();
  84. }
  85. public override void OnRun(ref RunItemContext context)
  86. {
  87. string result = string.Empty;
  88. string ext = Path.GetExtension(context.item.descriptor.info);
  89. if (ext == ".prefab")
  90. result = URP2DConverterUtility.UpgradePrefab(context.item.descriptor.info, UpgradeGameObject);
  91. else if (ext == ".unity")
  92. URP2DConverterUtility.UpgradeScene(context.item.descriptor.info, UpgradeGameObject);
  93. if (result != string.Empty)
  94. {
  95. context.didFail = true;
  96. context.info = result;
  97. }
  98. else
  99. {
  100. context.hasConverted = true;
  101. }
  102. }
  103. public override void OnClicked(int index)
  104. {
  105. EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(m_AssetsToConvert[index]));
  106. }
  107. public override void OnPostRun()
  108. {
  109. Resources.UnloadUnusedAssets();
  110. }
  111. }
  112. }