ImageAppearanceProviderHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. namespace TheraBytes.BetterUi
  8. {
  9. public static class ImageAppearanceProviderHelper
  10. {
  11. public static void SetMaterialType(string value,
  12. Graphic graphic, VertexMaterialData materialProperties, ref MaterialEffect effect, ref string materialType)
  13. {
  14. if (materialType == value && graphic.material != null)
  15. return;
  16. materialType = value;
  17. UpdateMaterial(graphic, materialProperties, ref effect, ref materialType);
  18. }
  19. public static void SetMaterialEffect(MaterialEffect value,
  20. Graphic graphic, VertexMaterialData materialProperties, ref MaterialEffect effect, ref string materialType)
  21. {
  22. if (effect == value && graphic.material != null)
  23. return;
  24. effect = value;
  25. UpdateMaterial(graphic, materialProperties, ref effect, ref materialType);
  26. }
  27. private static void UpdateMaterial(
  28. Graphic graphic, VertexMaterialData materialProperties, ref MaterialEffect effect, ref string materialType)
  29. {
  30. var info = Materials.Instance.GetMaterialInfo(materialType, effect);
  31. materialProperties.Clear();
  32. if (info != null)
  33. {
  34. graphic.material = info.Material;
  35. info.Properties.CopyTo(materialProperties);
  36. }
  37. else
  38. {
  39. graphic.material = null;
  40. }
  41. }
  42. public static void SetMaterialProperty(int propertyIndex, float value,
  43. Graphic graphic, VertexMaterialData materialProperties,
  44. ref float materialProperty1, ref float materialProperty2, ref float materialProperty3)
  45. {
  46. if (propertyIndex < 0 || propertyIndex >= 3)
  47. throw new ArgumentException("the propertyIndex can have the value 0, 1 or 2.");
  48. switch (propertyIndex)
  49. {
  50. case 0: materialProperty1 = value; break;
  51. case 1: materialProperty2 = value; break;
  52. case 2: materialProperty3 = value; break;
  53. }
  54. if (materialProperties.FloatProperties.Length > propertyIndex)
  55. {
  56. materialProperties.FloatProperties[propertyIndex].Value = value;
  57. graphic.SetVerticesDirty();
  58. }
  59. }
  60. public static float GetMaterialPropertyValue(int propertyIndex,
  61. ref float materialProperty1, ref float materialProperty2, ref float materialProperty3)
  62. {
  63. if (propertyIndex < 0 || propertyIndex >= 3)
  64. throw new ArgumentException("the propertyIndex can have the value 0, 1 or 2.");
  65. switch (propertyIndex)
  66. {
  67. case 0: return materialProperty1;
  68. case 1: return materialProperty2;
  69. case 2: return materialProperty3;
  70. default: return 0;
  71. }
  72. }
  73. public static void AddQuad(
  74. VertexHelper vertexHelper, Rect bounds,
  75. Vector2 posMin, Vector2 posMax,
  76. ColorMode mode, Color colorA, Color colorB,
  77. Vector2 uvMin, Vector2 uvMax,
  78. VertexMaterialData materialProperties)
  79. {
  80. int cnt = vertexHelper.currentVertCount;
  81. Color32[] colors = new Color32[4];
  82. colors[0] = GetColor(mode, colorA, colorB, bounds, posMin.x, posMin.y);
  83. colors[1] = GetColor(mode, colorA, colorB, bounds, posMin.x, posMax.y);
  84. colors[2] = GetColor(mode, colorA, colorB, bounds, posMax.x, posMax.y);
  85. colors[3] = GetColor(mode, colorA, colorB, bounds, posMax.x, posMin.y);
  86. float uvX = 0, uvY = 0, tangentW = 0;
  87. materialProperties.Apply(ref uvX, ref uvY, ref tangentW);
  88. Vector2 uv1 = new Vector2(uvX, uvY);
  89. Vector3 normal = Vector3.back;
  90. Vector4 tangent = new Vector4(1, 0, 0, tangentW);
  91. vertexHelper.AddVert(new Vector3(posMin.x, posMin.y, 0f), colors[0], new Vector2(uvMin.x, uvMin.y),
  92. uv1, normal, tangent);
  93. vertexHelper.AddVert(new Vector3(posMin.x, posMax.y, 0f), colors[1], new Vector2(uvMin.x, uvMax.y),
  94. uv1, normal, tangent);
  95. vertexHelper.AddVert(new Vector3(posMax.x, posMax.y, 0f), colors[2], new Vector2(uvMax.x, uvMax.y),
  96. uv1, normal, tangent);
  97. vertexHelper.AddVert(new Vector3(posMax.x, posMin.y, 0f), colors[3], new Vector2(uvMax.x, uvMin.y),
  98. uv1, normal, tangent);
  99. vertexHelper.AddTriangle(cnt, cnt + 1, cnt + 2);
  100. vertexHelper.AddTriangle(cnt + 2, cnt + 3, cnt);
  101. }
  102. static Color32 GetColor(ColorMode mode, Color a, Color b, Rect bounds, float x, float y)
  103. {
  104. switch (mode)
  105. {
  106. case ColorMode.Color:
  107. return a;
  108. case ColorMode.HorizontalGradient:
  109. {
  110. float amount = (x - bounds.xMin) / bounds.size.x;
  111. return Color.Lerp(a, b, amount);
  112. }
  113. case ColorMode.VerticalGradient:
  114. {
  115. float amount = 1 - (y - bounds.yMin) / bounds.size.y;
  116. return Color.Lerp(a, b, amount);
  117. }
  118. default: throw new NotImplementedException();
  119. }
  120. }
  121. }
  122. }