Light2DEditorUtility.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.Universal;
  4. namespace UnityEditor.Rendering.Universal
  5. {
  6. internal static class Light2DEditorUtility
  7. {
  8. static Material s_TexCapMaterial = CoreUtils.CreateEngineMaterial(Shader.Find("Hidden/Internal-GUITexture"));
  9. static internal void GUITextureCap(int controlID, Texture texture, Vector3 position, Quaternion rotation, float size, EventType eventType, bool isAngleHandle)
  10. {
  11. switch (eventType)
  12. {
  13. case (EventType.Layout):
  14. {
  15. Vector2 size2 = Vector2.one * size * 0.5f;
  16. if (isAngleHandle)
  17. size2.x = 0.0f;
  18. HandleUtility.AddControl(controlID, DistanceToRectangle(position, rotation, size2));
  19. break;
  20. }
  21. case (EventType.Repaint):
  22. {
  23. s_TexCapMaterial.mainTexture = texture;
  24. s_TexCapMaterial.SetPass(0);
  25. float w = texture.width;
  26. float h = texture.height;
  27. float max = Mathf.Max(w, h);
  28. Vector3 scale = new Vector2(w / max, h / max) * size * 0.5f;
  29. if (Camera.current == null)
  30. scale.y *= -1f;
  31. Matrix4x4 matrix = new Matrix4x4();
  32. matrix.SetTRS(position, rotation, scale);
  33. Graphics.DrawMeshNow(RenderingUtils.fullscreenMesh, matrix);
  34. }
  35. break;
  36. }
  37. }
  38. static float DistanceToRectangle(Vector3 position, Quaternion rotation, Vector2 size)
  39. {
  40. Vector3[] points = { Vector3.zero, Vector3.zero, Vector3.zero, Vector3.zero, Vector3.zero };
  41. Vector3 sideways = rotation * new Vector3(size.x, 0, 0);
  42. Vector3 up = rotation * new Vector3(0, size.y, 0);
  43. points[0] = HandleUtility.WorldToGUIPoint(position + sideways + up);
  44. points[1] = HandleUtility.WorldToGUIPoint(position + sideways - up);
  45. points[2] = HandleUtility.WorldToGUIPoint(position - sideways - up);
  46. points[3] = HandleUtility.WorldToGUIPoint(position - sideways + up);
  47. points[4] = points[0];
  48. Vector2 pos = Event.current.mousePosition;
  49. bool oddNodes = false;
  50. int j = 4;
  51. for (int i = 0; i < 5; ++i)
  52. {
  53. if ((points[i].y > pos.y) != (points[j].y > pos.y))
  54. {
  55. if (pos.x < (points[j].x - points[i].x) * (pos.y - points[i].y) / (points[j].y - points[i].y) + points[i].x)
  56. oddNodes = !oddNodes;
  57. }
  58. j = i;
  59. }
  60. if (!oddNodes)
  61. {
  62. // Distance to closest edge (not so fast)
  63. float dist, closestDist = -1f;
  64. j = 1;
  65. for (int i = 0; i < 4; ++i)
  66. {
  67. dist = HandleUtility.DistancePointToLineSegment(pos, points[i], points[j++]);
  68. if (dist < closestDist || closestDist < 0)
  69. closestDist = dist;
  70. }
  71. return closestDist;
  72. }
  73. else
  74. return 0;
  75. }
  76. public static Renderer2DData GetRenderer2DData()
  77. {
  78. UniversalRenderPipelineAsset pipelineAsset = UniversalRenderPipeline.asset;
  79. if (pipelineAsset == null)
  80. return null;
  81. // try get the default
  82. Renderer2DData rendererData = pipelineAsset.scriptableRendererData as Renderer2DData;
  83. if (rendererData == null)
  84. {
  85. foreach (Camera camera in Camera.allCameras)
  86. {
  87. UniversalAdditionalCameraData additionalCameraData = camera.GetComponent<UniversalAdditionalCameraData>();
  88. ScriptableRenderer renderer = additionalCameraData?.scriptableRenderer;
  89. Renderer2D renderer2D = renderer as Renderer2D;
  90. if (renderer2D != null)
  91. return renderer2D.GetRenderer2DData();
  92. }
  93. }
  94. return rendererData;
  95. }
  96. public static bool IsUsing2DRenderer()
  97. {
  98. return GetRenderer2DData() != null;
  99. }
  100. }
  101. }