VFXContextBorder.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. using System;
  6. using UnityObject = UnityEngine.Object;
  7. namespace UnityEditor.VFX.UI
  8. {
  9. class VFXContextBorderFactory : UxmlFactory<VFXContextBorder>
  10. { }
  11. class VFXContextBorder : ImmediateModeElement, IDisposable
  12. {
  13. Material m_Mat;
  14. static Mesh s_Mesh;
  15. public VFXContextBorder()
  16. {
  17. RecreateResources();
  18. RegisterCallback<CustomStyleResolvedEvent>(OnCustomStyleResolved);
  19. }
  20. void RecreateResources()
  21. {
  22. if (s_Mesh == null)
  23. {
  24. s_Mesh = new Mesh();
  25. int verticeCount = 16;
  26. var vertices = new Vector3[verticeCount];
  27. var uvsBorder = new Vector2[verticeCount];
  28. for (int ix = 0; ix < 4; ++ix)
  29. {
  30. for (int iy = 0; iy < 4; ++iy)
  31. {
  32. vertices[ix + iy * 4] = new Vector3(ix < 2 ? -1 : 1, iy < 2 ? -1 : 1, 0);
  33. uvsBorder[ix + iy * 4] = new Vector2(ix == 0 || ix == 3 ? 1 : 0, iy == 0 || iy == 3 ? 1 : 0);
  34. }
  35. }
  36. var indices = new int[4 * 8];
  37. for (int ix = 0; ix < 3; ++ix)
  38. {
  39. for (int iy = 0; iy < 3; ++iy)
  40. {
  41. int quadIndex = (ix + iy * 3);
  42. if (quadIndex == 4)
  43. continue;
  44. else if (quadIndex > 4)
  45. --quadIndex;
  46. int vertIndex = quadIndex * 4;
  47. indices[vertIndex] = ix + iy * 4;
  48. indices[vertIndex + 1] = ix + (iy + 1) * 4;
  49. indices[vertIndex + 2] = ix + 1 + (iy + 1) * 4;
  50. indices[vertIndex + 3] = ix + 1 + iy * 4;
  51. }
  52. }
  53. s_Mesh.vertices = vertices;
  54. s_Mesh.uv = uvsBorder;
  55. s_Mesh.SetIndices(indices, MeshTopology.Quads, 0);
  56. }
  57. if (m_Mat == null)
  58. m_Mat = new Material(Shader.Find("Hidden/VFX/GradientBorder"));
  59. }
  60. void IDisposable.Dispose()
  61. {
  62. UnityObject.DestroyImmediate(m_Mat);
  63. }
  64. Color m_StartColor;
  65. public Color startColor
  66. {
  67. get { return m_StartColor; }
  68. }
  69. Color m_EndColor;
  70. public Color endColor
  71. {
  72. get { return m_EndColor; }
  73. }
  74. static readonly CustomStyleProperty<Color> s_StartColorProperty = new CustomStyleProperty<Color>("--start-color");
  75. static readonly CustomStyleProperty<Color> s_EndColorProperty = new CustomStyleProperty<Color>("--end-color");
  76. private void OnCustomStyleResolved(CustomStyleResolvedEvent e)
  77. {
  78. var customStyle = e.customStyle;
  79. customStyle.TryGetValue(s_StartColorProperty, out m_StartColor);
  80. customStyle.TryGetValue(s_EndColorProperty, out m_EndColor);
  81. }
  82. protected override void ImmediateRepaint()
  83. {
  84. RecreateResources();
  85. VFXView view = GetFirstAncestorOfType<VFXView>();
  86. if (view != null && m_Mat != null)
  87. {
  88. float radius = resolvedStyle.borderTopLeftRadius;
  89. float realBorder = style.borderLeftWidth.value * view.scale;
  90. Vector4 size = new Vector4(layout.width * .5f, layout.height * 0.5f, 0, 0);
  91. m_Mat.SetVector("_Size", size);
  92. m_Mat.SetFloat("_Border", realBorder < 1.75f ? 1.75f / view.scale : style.borderLeftWidth.value);
  93. m_Mat.SetFloat("_Radius", radius);
  94. m_Mat.SetColor("_ColorStart", (QualitySettings.activeColorSpace == ColorSpace.Linear) ? startColor.gamma : startColor);
  95. m_Mat.SetColor("_ColorEnd", (QualitySettings.activeColorSpace == ColorSpace.Linear) ? endColor.gamma : endColor);
  96. m_Mat.SetPass(0);
  97. Graphics.DrawMeshNow(s_Mesh, Matrix4x4.Translate(new Vector3(size.x, size.y, 0)));
  98. }
  99. }
  100. }
  101. }