TMP_ShaderUtilities.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. using UnityEngine;
  2. using System.Linq;
  3. using System.Collections;
  4. namespace TMPro
  5. {
  6. public static class ShaderUtilities
  7. {
  8. // Shader Property IDs
  9. public static int ID_MainTex;
  10. public static int ID_FaceTex;
  11. public static int ID_FaceColor;
  12. public static int ID_FaceDilate;
  13. public static int ID_Shininess;
  14. public static int ID_UnderlayColor;
  15. public static int ID_UnderlayOffsetX;
  16. public static int ID_UnderlayOffsetY;
  17. public static int ID_UnderlayDilate;
  18. public static int ID_UnderlaySoftness;
  19. public static int ID_WeightNormal;
  20. public static int ID_WeightBold;
  21. public static int ID_OutlineTex;
  22. public static int ID_OutlineWidth;
  23. public static int ID_OutlineSoftness;
  24. public static int ID_OutlineColor;
  25. public static int ID_Padding;
  26. public static int ID_GradientScale;
  27. public static int ID_ScaleX;
  28. public static int ID_ScaleY;
  29. public static int ID_PerspectiveFilter;
  30. public static int ID_Sharpness;
  31. public static int ID_TextureWidth;
  32. public static int ID_TextureHeight;
  33. public static int ID_BevelAmount;
  34. public static int ID_GlowColor;
  35. public static int ID_GlowOffset;
  36. public static int ID_GlowPower;
  37. public static int ID_GlowOuter;
  38. public static int ID_LightAngle;
  39. public static int ID_EnvMap;
  40. public static int ID_EnvMatrix;
  41. public static int ID_EnvMatrixRotation;
  42. //public static int ID_MaskID;
  43. public static int ID_MaskCoord;
  44. public static int ID_ClipRect;
  45. public static int ID_MaskSoftnessX;
  46. public static int ID_MaskSoftnessY;
  47. public static int ID_VertexOffsetX;
  48. public static int ID_VertexOffsetY;
  49. public static int ID_UseClipRect;
  50. public static int ID_StencilID;
  51. public static int ID_StencilOp;
  52. public static int ID_StencilComp;
  53. public static int ID_StencilReadMask;
  54. public static int ID_StencilWriteMask;
  55. public static int ID_ShaderFlags;
  56. public static int ID_ScaleRatio_A;
  57. public static int ID_ScaleRatio_B;
  58. public static int ID_ScaleRatio_C;
  59. public static string Keyword_Bevel = "BEVEL_ON";
  60. public static string Keyword_Glow = "GLOW_ON";
  61. public static string Keyword_Underlay = "UNDERLAY_ON";
  62. public static string Keyword_Ratios = "RATIOS_OFF";
  63. //public static string Keyword_MASK_OFF = "MASK_OFF";
  64. public static string Keyword_MASK_SOFT = "MASK_SOFT";
  65. public static string Keyword_MASK_HARD = "MASK_HARD";
  66. public static string Keyword_MASK_TEX = "MASK_TEX";
  67. public static string Keyword_Outline = "OUTLINE_ON";
  68. public static string ShaderTag_ZTestMode = "unity_GUIZTestMode";
  69. public static string ShaderTag_CullMode = "_CullMode";
  70. private static float m_clamp = 1.0f;
  71. public static bool isInitialized = false;
  72. /// <summary>
  73. /// Returns a reference to the mobile distance field shader.
  74. /// </summary>
  75. internal static Shader ShaderRef_MobileSDF
  76. {
  77. get
  78. {
  79. if (k_ShaderRef_MobileSDF == null)
  80. k_ShaderRef_MobileSDF = Shader.Find("TextMeshPro/Mobile/Distance Field");
  81. return k_ShaderRef_MobileSDF;
  82. }
  83. }
  84. static Shader k_ShaderRef_MobileSDF;
  85. /// <summary>
  86. /// Returns a reference to the mobile bitmap shader.
  87. /// </summary>
  88. internal static Shader ShaderRef_MobileBitmap
  89. {
  90. get
  91. {
  92. if (k_ShaderRef_MobileBitmap == null)
  93. k_ShaderRef_MobileBitmap = Shader.Find("TextMeshPro/Mobile/Bitmap");
  94. return k_ShaderRef_MobileBitmap;
  95. }
  96. }
  97. static Shader k_ShaderRef_MobileBitmap;
  98. /// <summary>
  99. ///
  100. /// </summary>
  101. static ShaderUtilities()
  102. {
  103. GetShaderPropertyIDs();
  104. }
  105. /// <summary>
  106. ///
  107. /// </summary>
  108. public static void GetShaderPropertyIDs()
  109. {
  110. if (isInitialized == false)
  111. {
  112. //Debug.Log("Getting Shader property IDs");
  113. isInitialized = true;
  114. ID_MainTex = Shader.PropertyToID("_MainTex");
  115. ID_FaceTex = Shader.PropertyToID("_FaceTex");
  116. ID_FaceColor = Shader.PropertyToID("_FaceColor");
  117. ID_FaceDilate = Shader.PropertyToID("_FaceDilate");
  118. ID_Shininess = Shader.PropertyToID("_FaceShininess");
  119. ID_UnderlayColor = Shader.PropertyToID("_UnderlayColor");
  120. ID_UnderlayOffsetX = Shader.PropertyToID("_UnderlayOffsetX");
  121. ID_UnderlayOffsetY = Shader.PropertyToID("_UnderlayOffsetY");
  122. ID_UnderlayDilate = Shader.PropertyToID("_UnderlayDilate");
  123. ID_UnderlaySoftness = Shader.PropertyToID("_UnderlaySoftness");
  124. ID_WeightNormal = Shader.PropertyToID("_WeightNormal");
  125. ID_WeightBold = Shader.PropertyToID("_WeightBold");
  126. ID_OutlineTex = Shader.PropertyToID("_OutlineTex");
  127. ID_OutlineWidth = Shader.PropertyToID("_OutlineWidth");
  128. ID_OutlineSoftness = Shader.PropertyToID("_OutlineSoftness");
  129. ID_OutlineColor = Shader.PropertyToID("_OutlineColor");
  130. ID_Padding = Shader.PropertyToID("_Padding");
  131. ID_GradientScale = Shader.PropertyToID("_GradientScale");
  132. ID_ScaleX = Shader.PropertyToID("_ScaleX");
  133. ID_ScaleY = Shader.PropertyToID("_ScaleY");
  134. ID_PerspectiveFilter = Shader.PropertyToID("_PerspectiveFilter");
  135. ID_Sharpness = Shader.PropertyToID("_Sharpness");
  136. ID_TextureWidth = Shader.PropertyToID("_TextureWidth");
  137. ID_TextureHeight = Shader.PropertyToID("_TextureHeight");
  138. ID_BevelAmount = Shader.PropertyToID("_Bevel");
  139. ID_LightAngle = Shader.PropertyToID("_LightAngle");
  140. ID_EnvMap = Shader.PropertyToID("_Cube");
  141. ID_EnvMatrix = Shader.PropertyToID("_EnvMatrix");
  142. ID_EnvMatrixRotation = Shader.PropertyToID("_EnvMatrixRotation");
  143. ID_GlowColor = Shader.PropertyToID("_GlowColor");
  144. ID_GlowOffset = Shader.PropertyToID("_GlowOffset");
  145. ID_GlowPower = Shader.PropertyToID("_GlowPower");
  146. ID_GlowOuter = Shader.PropertyToID("_GlowOuter");
  147. //ID_MaskID = Shader.PropertyToID("_MaskID");
  148. ID_MaskCoord = Shader.PropertyToID("_MaskCoord");
  149. ID_ClipRect = Shader.PropertyToID("_ClipRect");
  150. ID_UseClipRect = Shader.PropertyToID("_UseClipRect");
  151. ID_MaskSoftnessX = Shader.PropertyToID("_MaskSoftnessX");
  152. ID_MaskSoftnessY = Shader.PropertyToID("_MaskSoftnessY");
  153. ID_VertexOffsetX = Shader.PropertyToID("_VertexOffsetX");
  154. ID_VertexOffsetY = Shader.PropertyToID("_VertexOffsetY");
  155. ID_StencilID = Shader.PropertyToID("_Stencil");
  156. ID_StencilOp = Shader.PropertyToID("_StencilOp");
  157. ID_StencilComp = Shader.PropertyToID("_StencilComp");
  158. ID_StencilReadMask = Shader.PropertyToID("_StencilReadMask");
  159. ID_StencilWriteMask = Shader.PropertyToID("_StencilWriteMask");
  160. ID_ShaderFlags = Shader.PropertyToID("_ShaderFlags");
  161. ID_ScaleRatio_A = Shader.PropertyToID("_ScaleRatioA");
  162. ID_ScaleRatio_B = Shader.PropertyToID("_ScaleRatioB");
  163. ID_ScaleRatio_C = Shader.PropertyToID("_ScaleRatioC");
  164. // Set internal shader references
  165. if (k_ShaderRef_MobileSDF == null)
  166. k_ShaderRef_MobileSDF = Shader.Find("TextMeshPro/Mobile/Distance Field");
  167. if (k_ShaderRef_MobileBitmap == null)
  168. k_ShaderRef_MobileBitmap = Shader.Find("TextMeshPro/Mobile/Bitmap");
  169. }
  170. }
  171. // Scale Ratios to ensure property ranges are optimum in Material Editor
  172. public static void UpdateShaderRatios(Material mat)
  173. {
  174. //Debug.Log("UpdateShaderRatios() called.");
  175. float ratio_A = 1;
  176. float ratio_B = 1;
  177. float ratio_C = 1;
  178. bool isRatioEnabled = !mat.shaderKeywords.Contains(Keyword_Ratios);
  179. // Compute Ratio A
  180. float scale = mat.GetFloat(ID_GradientScale);
  181. float faceDilate = mat.GetFloat(ID_FaceDilate);
  182. float outlineThickness = mat.GetFloat(ID_OutlineWidth);
  183. float outlineSoftness = mat.GetFloat(ID_OutlineSoftness);
  184. float weight = Mathf.Max(mat.GetFloat(ID_WeightNormal), mat.GetFloat(ID_WeightBold)) / 4.0f;
  185. float t = Mathf.Max(1, weight + faceDilate + outlineThickness + outlineSoftness);
  186. ratio_A = isRatioEnabled ? (scale - m_clamp) / (scale * t) : 1;
  187. //float ratio_A_old = mat.GetFloat(ID_ScaleRatio_A);
  188. // Only set the ratio if it has changed.
  189. //if (ratio_A != ratio_A_old)
  190. mat.SetFloat(ID_ScaleRatio_A, ratio_A);
  191. // Compute Ratio B
  192. if (mat.HasProperty(ID_GlowOffset))
  193. {
  194. float glowOffset = mat.GetFloat(ID_GlowOffset);
  195. float glowOuter = mat.GetFloat(ID_GlowOuter);
  196. float range = (weight + faceDilate) * (scale - m_clamp);
  197. t = Mathf.Max(1, glowOffset + glowOuter);
  198. ratio_B = isRatioEnabled ? Mathf.Max(0, scale - m_clamp - range) / (scale * t) : 1;
  199. //float ratio_B_old = mat.GetFloat(ID_ScaleRatio_B);
  200. // Only set the ratio if it has changed.
  201. //if (ratio_B != ratio_B_old)
  202. mat.SetFloat(ID_ScaleRatio_B, ratio_B);
  203. }
  204. // Compute Ratio C
  205. if (mat.HasProperty(ID_UnderlayOffsetX))
  206. {
  207. float underlayOffsetX = mat.GetFloat(ID_UnderlayOffsetX);
  208. float underlayOffsetY = mat.GetFloat(ID_UnderlayOffsetY);
  209. float underlayDilate = mat.GetFloat(ID_UnderlayDilate);
  210. float underlaySoftness = mat.GetFloat(ID_UnderlaySoftness);
  211. float range = (weight + faceDilate) * (scale - m_clamp);
  212. t = Mathf.Max(1, Mathf.Max(Mathf.Abs(underlayOffsetX), Mathf.Abs(underlayOffsetY)) + underlayDilate + underlaySoftness);
  213. ratio_C = isRatioEnabled ? Mathf.Max(0, scale - m_clamp - range) / (scale * t) : 1;
  214. //float ratio_C_old = mat.GetFloat(ID_ScaleRatio_C);
  215. // Only set the ratio if it has changed.
  216. //if (ratio_C != ratio_C_old)
  217. mat.SetFloat(ID_ScaleRatio_C, ratio_C);
  218. }
  219. }
  220. // Function to calculate padding required for Outline Width & Dilation for proper text alignment
  221. public static Vector4 GetFontExtent(Material material)
  222. {
  223. // Revised implementation where style no longer affects alignment
  224. return Vector4.zero;
  225. /*
  226. if (material == null || !material.HasProperty(ShaderUtilities.ID_GradientScale))
  227. return Vector4.zero; // We are using an non SDF Shader.
  228. float scaleRatioA = material.GetFloat(ID_ScaleRatio_A);
  229. float faceDilate = material.GetFloat(ID_FaceDilate) * scaleRatioA;
  230. float outlineThickness = material.GetFloat(ID_OutlineWidth) * scaleRatioA;
  231. float extent = Mathf.Min(1, faceDilate + outlineThickness);
  232. extent *= material.GetFloat(ID_GradientScale);
  233. return new Vector4(extent, extent, extent, extent);
  234. */
  235. }
  236. // Function to check if Masking is enabled
  237. public static bool IsMaskingEnabled(Material material)
  238. {
  239. if (material == null || !material.HasProperty(ShaderUtilities.ID_ClipRect))
  240. return false;
  241. if (material.shaderKeywords.Contains(ShaderUtilities.Keyword_MASK_SOFT) || material.shaderKeywords.Contains(ShaderUtilities.Keyword_MASK_HARD) || material.shaderKeywords.Contains(ShaderUtilities.Keyword_MASK_TEX))
  242. return true;
  243. return false;
  244. }
  245. // Function to determine how much extra padding is required as a result of material properties like dilate, outline thickness, softness, glow, etc...
  246. public static float GetPadding(Material material, bool enableExtraPadding, bool isBold)
  247. {
  248. //Debug.Log("GetPadding() called.");
  249. if (isInitialized == false)
  250. GetShaderPropertyIDs();
  251. // Return if Material is null
  252. if (material == null) return 0;
  253. int extraPadding = enableExtraPadding ? 4 : 0;
  254. // Check if we are using a non Distance Field Shader
  255. if (material.HasProperty(ID_GradientScale) == false)
  256. {
  257. if (material.HasProperty(ID_Padding))
  258. extraPadding += (int)material.GetFloat(ID_Padding);
  259. return extraPadding;
  260. }
  261. Vector4 padding = Vector4.zero;
  262. Vector4 maxPadding = Vector4.zero;
  263. //float weight = 0;
  264. float faceDilate = 0;
  265. float faceSoftness = 0;
  266. float outlineThickness = 0;
  267. float scaleRatio_A = 0;
  268. float scaleRatio_B = 0;
  269. float scaleRatio_C = 0;
  270. float glowOffset = 0;
  271. float glowOuter = 0;
  272. float uniformPadding = 0;
  273. // Iterate through each of the assigned materials to find the max values to set the padding.
  274. // Update Shader Ratios prior to computing padding
  275. UpdateShaderRatios(material);
  276. string[] shaderKeywords = material.shaderKeywords;
  277. if (material.HasProperty(ID_ScaleRatio_A))
  278. scaleRatio_A = material.GetFloat(ID_ScaleRatio_A);
  279. //weight = 0; // Mathf.Max(material.GetFloat(ID_WeightNormal), material.GetFloat(ID_WeightBold)) / 2.0f * scaleRatio_A;
  280. if (material.HasProperty(ID_FaceDilate))
  281. faceDilate = material.GetFloat(ID_FaceDilate) * scaleRatio_A;
  282. if (material.HasProperty(ID_OutlineSoftness))
  283. faceSoftness = material.GetFloat(ID_OutlineSoftness) * scaleRatio_A;
  284. if (material.HasProperty(ID_OutlineWidth))
  285. outlineThickness = material.GetFloat(ID_OutlineWidth) * scaleRatio_A;
  286. uniformPadding = outlineThickness + faceSoftness + faceDilate;
  287. // Glow padding contribution
  288. if (material.HasProperty(ID_GlowOffset) && shaderKeywords.Contains(Keyword_Glow)) // Generates GC
  289. {
  290. if (material.HasProperty(ID_ScaleRatio_B))
  291. scaleRatio_B = material.GetFloat(ID_ScaleRatio_B);
  292. glowOffset = material.GetFloat(ID_GlowOffset) * scaleRatio_B;
  293. glowOuter = material.GetFloat(ID_GlowOuter) * scaleRatio_B;
  294. }
  295. uniformPadding = Mathf.Max(uniformPadding, faceDilate + glowOffset + glowOuter);
  296. // Underlay padding contribution
  297. if (material.HasProperty(ID_UnderlaySoftness) && shaderKeywords.Contains(Keyword_Underlay)) // Generates GC
  298. {
  299. if (material.HasProperty(ID_ScaleRatio_C))
  300. scaleRatio_C = material.GetFloat(ID_ScaleRatio_C);
  301. float offsetX = material.GetFloat(ID_UnderlayOffsetX) * scaleRatio_C;
  302. float offsetY = material.GetFloat(ID_UnderlayOffsetY) * scaleRatio_C;
  303. float dilate = material.GetFloat(ID_UnderlayDilate) * scaleRatio_C;
  304. float softness = material.GetFloat(ID_UnderlaySoftness) * scaleRatio_C;
  305. padding.x = Mathf.Max(padding.x, faceDilate + dilate + softness - offsetX);
  306. padding.y = Mathf.Max(padding.y, faceDilate + dilate + softness - offsetY);
  307. padding.z = Mathf.Max(padding.z, faceDilate + dilate + softness + offsetX);
  308. padding.w = Mathf.Max(padding.w, faceDilate + dilate + softness + offsetY);
  309. }
  310. padding.x = Mathf.Max(padding.x, uniformPadding);
  311. padding.y = Mathf.Max(padding.y, uniformPadding);
  312. padding.z = Mathf.Max(padding.z, uniformPadding);
  313. padding.w = Mathf.Max(padding.w, uniformPadding);
  314. padding.x += extraPadding;
  315. padding.y += extraPadding;
  316. padding.z += extraPadding;
  317. padding.w += extraPadding;
  318. padding.x = Mathf.Min(padding.x, 1);
  319. padding.y = Mathf.Min(padding.y, 1);
  320. padding.z = Mathf.Min(padding.z, 1);
  321. padding.w = Mathf.Min(padding.w, 1);
  322. maxPadding.x = maxPadding.x < padding.x ? padding.x : maxPadding.x;
  323. maxPadding.y = maxPadding.y < padding.y ? padding.y : maxPadding.y;
  324. maxPadding.z = maxPadding.z < padding.z ? padding.z : maxPadding.z;
  325. maxPadding.w = maxPadding.w < padding.w ? padding.w : maxPadding.w;
  326. float gradientScale = material.GetFloat(ID_GradientScale);
  327. padding *= gradientScale;
  328. // Set UniformPadding to the maximum value of any of its components.
  329. uniformPadding = Mathf.Max(padding.x, padding.y);
  330. uniformPadding = Mathf.Max(padding.z, uniformPadding);
  331. uniformPadding = Mathf.Max(padding.w, uniformPadding);
  332. return uniformPadding + 1.25f;
  333. }
  334. // Function to determine how much extra padding is required as a result of material properties like dilate, outline thickness, softness, glow, etc...
  335. public static float GetPadding(Material[] materials, bool enableExtraPadding, bool isBold)
  336. {
  337. //Debug.Log("GetPadding() called.");
  338. if (isInitialized == false)
  339. GetShaderPropertyIDs();
  340. // Return if Material is null
  341. if (materials == null) return 0;
  342. int extraPadding = enableExtraPadding ? 4 : 0;
  343. // Check if we are using a Bitmap Shader
  344. if (materials[0].HasProperty(ID_Padding))
  345. return extraPadding + materials[0].GetFloat(ID_Padding);
  346. Vector4 padding = Vector4.zero;
  347. Vector4 maxPadding = Vector4.zero;
  348. float faceDilate = 0;
  349. float faceSoftness = 0;
  350. float outlineThickness = 0;
  351. float scaleRatio_A = 0;
  352. float scaleRatio_B = 0;
  353. float scaleRatio_C = 0;
  354. float glowOffset = 0;
  355. float glowOuter = 0;
  356. float uniformPadding = 0;
  357. // Iterate through each of the assigned materials to find the max values to set the padding.
  358. for (int i = 0; i < materials.Length; i++)
  359. {
  360. // Update Shader Ratios prior to computing padding
  361. ShaderUtilities.UpdateShaderRatios(materials[i]);
  362. string[] shaderKeywords = materials[i].shaderKeywords;
  363. if (materials[i].HasProperty(ShaderUtilities.ID_ScaleRatio_A))
  364. scaleRatio_A = materials[i].GetFloat(ShaderUtilities.ID_ScaleRatio_A);
  365. if (materials[i].HasProperty(ShaderUtilities.ID_FaceDilate))
  366. faceDilate = materials[i].GetFloat(ShaderUtilities.ID_FaceDilate) * scaleRatio_A;
  367. if (materials[i].HasProperty(ShaderUtilities.ID_OutlineSoftness))
  368. faceSoftness = materials[i].GetFloat(ShaderUtilities.ID_OutlineSoftness) * scaleRatio_A;
  369. if (materials[i].HasProperty(ShaderUtilities.ID_OutlineWidth))
  370. outlineThickness = materials[i].GetFloat(ShaderUtilities.ID_OutlineWidth) * scaleRatio_A;
  371. uniformPadding = outlineThickness + faceSoftness + faceDilate;
  372. // Glow padding contribution
  373. if (materials[i].HasProperty(ShaderUtilities.ID_GlowOffset) && shaderKeywords.Contains(ShaderUtilities.Keyword_Glow))
  374. {
  375. if (materials[i].HasProperty(ShaderUtilities.ID_ScaleRatio_B))
  376. scaleRatio_B = materials[i].GetFloat(ShaderUtilities.ID_ScaleRatio_B);
  377. glowOffset = materials[i].GetFloat(ShaderUtilities.ID_GlowOffset) * scaleRatio_B;
  378. glowOuter = materials[i].GetFloat(ShaderUtilities.ID_GlowOuter) * scaleRatio_B;
  379. }
  380. uniformPadding = Mathf.Max(uniformPadding, faceDilate + glowOffset + glowOuter);
  381. // Underlay padding contribution
  382. if (materials[i].HasProperty(ShaderUtilities.ID_UnderlaySoftness) && shaderKeywords.Contains(ShaderUtilities.Keyword_Underlay))
  383. {
  384. if (materials[i].HasProperty(ShaderUtilities.ID_ScaleRatio_C))
  385. scaleRatio_C = materials[i].GetFloat(ShaderUtilities.ID_ScaleRatio_C);
  386. float offsetX = materials[i].GetFloat(ShaderUtilities.ID_UnderlayOffsetX) * scaleRatio_C;
  387. float offsetY = materials[i].GetFloat(ShaderUtilities.ID_UnderlayOffsetY) * scaleRatio_C;
  388. float dilate = materials[i].GetFloat(ShaderUtilities.ID_UnderlayDilate) * scaleRatio_C;
  389. float softness = materials[i].GetFloat(ShaderUtilities.ID_UnderlaySoftness) * scaleRatio_C;
  390. padding.x = Mathf.Max(padding.x, faceDilate + dilate + softness - offsetX);
  391. padding.y = Mathf.Max(padding.y, faceDilate + dilate + softness - offsetY);
  392. padding.z = Mathf.Max(padding.z, faceDilate + dilate + softness + offsetX);
  393. padding.w = Mathf.Max(padding.w, faceDilate + dilate + softness + offsetY);
  394. }
  395. padding.x = Mathf.Max(padding.x, uniformPadding);
  396. padding.y = Mathf.Max(padding.y, uniformPadding);
  397. padding.z = Mathf.Max(padding.z, uniformPadding);
  398. padding.w = Mathf.Max(padding.w, uniformPadding);
  399. padding.x += extraPadding;
  400. padding.y += extraPadding;
  401. padding.z += extraPadding;
  402. padding.w += extraPadding;
  403. padding.x = Mathf.Min(padding.x, 1);
  404. padding.y = Mathf.Min(padding.y, 1);
  405. padding.z = Mathf.Min(padding.z, 1);
  406. padding.w = Mathf.Min(padding.w, 1);
  407. maxPadding.x = maxPadding.x < padding.x ? padding.x : maxPadding.x;
  408. maxPadding.y = maxPadding.y < padding.y ? padding.y : maxPadding.y;
  409. maxPadding.z = maxPadding.z < padding.z ? padding.z : maxPadding.z;
  410. maxPadding.w = maxPadding.w < padding.w ? padding.w : maxPadding.w;
  411. }
  412. float gradientScale = materials[0].GetFloat(ShaderUtilities.ID_GradientScale);
  413. padding *= gradientScale;
  414. // Set UniformPadding to the maximum value of any of its components.
  415. uniformPadding = Mathf.Max(padding.x, padding.y);
  416. uniformPadding = Mathf.Max(padding.z, uniformPadding);
  417. uniformPadding = Mathf.Max(padding.w, uniformPadding);
  418. return uniformPadding + 0.25f;
  419. }
  420. }
  421. }