Light2DEditorUtility.cs 4.5 KB

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