C# 使用纹理2D创建平滑的圆角矩形

C# 使用纹理2D创建平滑的圆角矩形,c#,unity3d,textures,smoothing,C#,Unity3d,Textures,Smoothing,我正在尝试绘制圆形矩形,它可以用作任何UI组件的纹理。我的目标是通过使用函数设置像素,使用Texture2D类创建这个圆形纹理。我不想使用着色器执行此操作 这样做是因为XNA而不是Unity。我把它移植到Unity中,但边缘参差不齐 这是Unity中的外观: 下面是移植的代码: public int width = 256; public int height = 140; public int borderThickness = 1; //Cannot be < 1 //Border

我正在尝试绘制圆形矩形,它可以用作任何UI组件的纹理。我的目标是通过使用函数设置像素,使用
Texture2D
类创建这个圆形纹理。我不想使用着色器执行此操作

这样做是因为XNA而不是Unity。我把它移植到Unity中,但边缘参差不齐

这是Unity中的外观:

下面是移植的代码:

public int width = 256;
public int height = 140;
public int borderThickness = 1;  //Cannot be < 1
//Border shadow cannot be more than Border Radius
public int borderRadius = 40; //Cannot be < 1
public int borderShadow = 2;
public List<Color32> backgroundColors = new List<Color32>();
public List<Color32> borderColors = new List<Color32>();
public float initialShadowIntensity = 5f;
public float finalShadowIntensity = 5f;


private Texture2D resultTex;
public RawImage display;

void Start()
{
    backgroundColors.Add(new Color32(171, 0, 0, 255));
    backgroundColors.Add(new Color32(9, 48, 173, 255));

    borderColors.Add(new Color32(111, 8, 99, 255));
    borderColors.Add(new Color32(171, 4, 161, 255));

    resultTex = RectangleCreator.
        CreateRoundedRectangleTexture(width, height, borderThickness,
        borderRadius, borderShadow, backgroundColors, borderColors,
        initialShadowIntensity, finalShadowIntensity);

    display.texture = resultTex;
    display.SetNativeSize();
}



public class RectangleCreator
{
    public static Texture2D CreateRoundedRectangleTexture(int width, int height, int borderThickness, int borderRadius, int borderShadow, List<Color32> backgroundColors, List<Color32> borderColors, float initialShadowIntensity, float finalShadowIntensity)
    {
        if (backgroundColors == null || backgroundColors.Count == 0) throw new ArgumentException("Must define at least one background color (up to four).");
        if (borderColors == null || borderColors.Count == 0) throw new ArgumentException("Must define at least one border color (up to three).");
        if (borderRadius < 1) throw new ArgumentException("Must define a border radius (rounds off edges).");
        if (borderThickness < 1) throw new ArgumentException("Must define border thikness.");
        if (borderThickness + borderRadius > height / 2 || borderThickness + borderRadius > width / 2) throw new ArgumentException("Border will be too thick and/or rounded to fit on the texture.");
        if (borderShadow > borderRadius) throw new ArgumentException("Border shadow must be lesser in magnitude than the border radius (suggeted: shadow <= 0.25 * radius).");

        Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
        Color32[] color = new Color32[width * height];

        for (int x = 0; x < texture.width; x++)
        {
            for (int y = 0; y < texture.height; y++)
            {
                switch (backgroundColors.Count)
                {
                    case 4:
                        Color32 leftColor0 = Color32.Lerp(backgroundColors[0], backgroundColors[1], ((float)y / (width - 1)));
                        Color32 rightColor0 = Color32.Lerp(backgroundColors[2], backgroundColors[3], ((float)y / (height - 1)));
                        color[x + width * y] = Color32.Lerp(leftColor0, rightColor0, ((float)x / (width - 1)));
                        break;
                    case 3:
                        Color32 leftColor1 = Color32.Lerp(backgroundColors[0], backgroundColors[1], ((float)y / (width - 1)));
                        Color32 rightColor1 = Color32.Lerp(backgroundColors[1], backgroundColors[2], ((float)y / (height - 1)));
                        color[x + width * y] = Color32.Lerp(leftColor1, rightColor1, ((float)x / (width - 1)));
                        break;
                    case 2:
                        color[x + width * y] = Color32.Lerp(backgroundColors[0], backgroundColors[1], ((float)x / (width - 1)));
                        break;
                    default:
                        color[x + width * y] = backgroundColors[0];
                        break;
                }

                color[x + width * y] = ColorBorder(x, y, width, height, borderThickness, borderRadius, borderShadow, color[x + width * y], borderColors, initialShadowIntensity, finalShadowIntensity);
            }
        }

        texture.SetPixels32(color);
        texture.Apply();
        return texture;
    }

    private static Color32 ColorBorder(int x, int y, int width, int height, int borderThickness, int borderRadius, int borderShadow, Color32 initialColor, List<Color32> borderColors, float initialShadowIntensity, float finalShadowIntensity)
    {
        Rect internalRectangle = new Rect((borderThickness + borderRadius), (borderThickness + borderRadius), width - 2 * (borderThickness + borderRadius), height - 2 * (borderThickness + borderRadius));


        Vector2 point = new Vector2(x, y);
        if (internalRectangle.Contains(point)) return initialColor;

        Vector2 origin = Vector2.zero;

        if (x < borderThickness + borderRadius)
        {
            if (y < borderRadius + borderThickness)
                origin = new Vector2(borderRadius + borderThickness, borderRadius + borderThickness);
            else if (y > height - (borderRadius + borderThickness))
                origin = new Vector2(borderRadius + borderThickness, height - (borderRadius + borderThickness));
            else
                origin = new Vector2(borderRadius + borderThickness, y);
        }
        else if (x > width - (borderRadius + borderThickness))
        {
            if (y < borderRadius + borderThickness)
                origin = new Vector2(width - (borderRadius + borderThickness), borderRadius + borderThickness);
            else if (y > height - (borderRadius + borderThickness))
                origin = new Vector2(width - (borderRadius + borderThickness), height - (borderRadius + borderThickness));
            else
                origin = new Vector2(width - (borderRadius + borderThickness), y);
        }
        else
        {
            if (y < borderRadius + borderThickness)
                origin = new Vector2(x, borderRadius + borderThickness);
            else if (y > height - (borderRadius + borderThickness))
                origin = new Vector2(x, height - (borderRadius + borderThickness));
        }

        if (!origin.Equals(Vector2.zero))
        {
            float distance = Vector2.Distance(point, origin);

            if (distance > borderRadius + borderThickness + 1)
            {
                return Color.clear;
            }
            else if (distance > borderRadius + 1)
            {
                if (borderColors.Count > 2)
                {
                    float modNum = distance - borderRadius;

                    if (modNum < borderThickness / 2)
                    {
                        return Color32.Lerp(borderColors[2], borderColors[1], (float)((modNum) / (borderThickness / 2.0)));
                    }
                    else
                    {
                        return Color32.Lerp(borderColors[1], borderColors[0], (float)((modNum - (borderThickness / 2.0)) / (borderThickness / 2.0)));
                    }
                }


                if (borderColors.Count > 0)
                    return borderColors[0];
            }
            else if (distance > borderRadius - borderShadow + 1)
            {
                float mod = (distance - (borderRadius - borderShadow)) / borderShadow;
                float shadowDiff = initialShadowIntensity - finalShadowIntensity;
                return DarkenColor(initialColor, ((shadowDiff * mod) + finalShadowIntensity));
            }
        }

        return initialColor;
    }

    private static Color32 DarkenColor(Color32 color, float shadowIntensity)
    {
        return Color32.Lerp(color, Color.black, shadowIntensity);
    }
}

但这并没有用透明的颜色使它平滑。它去除了黑色,但使其更加参差不齐

下面是它的外观:


如何平滑纹理边缘?

当我将此代码放在UICanvas中的原始图像上时,它看起来很好。这是通过游戏对象->用户界面->原始图像获得的默认设置

我只是将组件附加到rawimage并将Display属性设置为自身

如果希望unity进行一些抗锯齿处理,可以提高原始图像的分辨率,并删除对
display.SetNativeSize()的调用
但是您需要使用矩形的变换组件设置矩形的实际大小

   using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour {


public int width = 256;
public int height = 140;
public int borderThickness = 1;  //Cannot be < 1
//Border shadow cannot be more than Border Radius
public int borderRadius = 40; //Cannot be < 1
public int borderShadow = 2;
public List<Color32> backgroundColors = new List<Color32>();
public List<Color32> borderColors = new List<Color32>();
public float initialShadowIntensity = 5f;
public float finalShadowIntensity = 5f;

public float resolutionmultiplier = 4f;


private Texture2D resultTex;
public RawImage display;

public RectTransform rt;

void Start()
{
    backgroundColors.Add(new Color32(171, 0, 0, 255));
    backgroundColors.Add(new Color32(9, 48, 173, 255));

    borderColors.Add(new Color32(111, 8, 99, 255));
    borderColors.Add(new Color32(171, 4, 161, 255));

    resultTex = RectangleCreator.
        CreateRoundedRectangleTexture(4, width, height, borderThickness,
        borderRadius, borderShadow, backgroundColors, borderColors,
        initialShadowIntensity, finalShadowIntensity);

    display.texture = resultTex;
    rt.sizeDelta = new Vector2(width, height);
}



public class RectangleCreator
{
    public static Texture2D CreateRoundedRectangleTexture(int resolutionmultiplier, int width, int height, int borderThickness, int borderRadius, int borderShadow, List<Color32> backgroundColors, List<Color32> borderColors, float initialShadowIntensity, float finalShadowIntensity)
    {
       // if (backgroundColors == null || backgroundColors.Count == 0) throw new ArgumentException("Must define at least one background color (up to four).");
      //  if (borderColors == null || borderColors.Count == 0) throw new ArgumentException("Must define at least one border color (up to three).");
      //  if (borderRadius < 1) throw new ArgumentException("Must define a border radius (rounds off edges).");
      //  if (borderThickness < 1) throw new ArgumentException("Must define border thikness.");
     //   if (borderThickness + borderRadius > height / 2 || borderThickness + borderRadius > width / 2) throw new ArgumentException("Border will be too thick and/or rounded to fit on the texture.");
     //   if (borderShadow > borderRadius) throw new ArgumentException("Border shadow must be lesser in magnitude than the border radius (suggeted: shadow <= 0.25 * radius).");

        width = width * resolutionmultiplier;
        height = height * resolutionmultiplier;

        Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
        Color32[] color = new Color32[width * height];

        for (int x = 0; x < texture.width; x++)
        {
            for (int y = 0; y < texture.height; y++)
            {
                switch (backgroundColors.Count)
                {
                    case 4:
                        Color32 leftColor0 = Color32.Lerp(backgroundColors[0], backgroundColors[1], ((float)y / (width - 1)));
                        Color32 rightColor0 = Color32.Lerp(backgroundColors[2], backgroundColors[3], ((float)y / (height - 1)));
                        color[x + width * y] = Color32.Lerp(leftColor0, rightColor0, ((float)x / (width - 1)));
                        break;
                    case 3:
                        Color32 leftColor1 = Color32.Lerp(backgroundColors[0], backgroundColors[1], ((float)y / (width - 1)));
                        Color32 rightColor1 = Color32.Lerp(backgroundColors[1], backgroundColors[2], ((float)y / (height - 1)));
                        color[x + width * y] = Color32.Lerp(leftColor1, rightColor1, ((float)x / (width - 1)));
                        break;
                    case 2:
                        color[x + width * y] = Color32.Lerp(backgroundColors[0], backgroundColors[1], ((float)x / (width - 1)));
                        break;
                    default:
                        color[x + width * y] = backgroundColors[0];
                        break;
                }

                color[x + width * y] = ColorBorder(x, y, width, height, borderThickness, borderRadius, borderShadow, color[x + width * y], borderColors, initialShadowIntensity, finalShadowIntensity);
            }
        }

        texture.SetPixels32(color);
        texture.Apply();
        return texture;
    }

    private static Color32 ColorBorder(int x, int y, int width, int height, int borderThickness, int borderRadius, int borderShadow, Color32 initialColor, List<Color32> borderColors, float initialShadowIntensity, float finalShadowIntensity)
    {
        Rect internalRectangle = new Rect((borderThickness + borderRadius), (borderThickness + borderRadius), width - 2 * (borderThickness + borderRadius), height - 2 * (borderThickness + borderRadius));


        Vector2 point = new Vector2(x, y);
        if (internalRectangle.Contains(point)) return initialColor;

        Vector2 origin = Vector2.zero;

        if (x < borderThickness + borderRadius)
        {
            if (y < borderRadius + borderThickness)
                origin = new Vector2(borderRadius + borderThickness, borderRadius + borderThickness);
            else if (y > height - (borderRadius + borderThickness))
                origin = new Vector2(borderRadius + borderThickness, height - (borderRadius + borderThickness));
            else
                origin = new Vector2(borderRadius + borderThickness, y);
        }
        else if (x > width - (borderRadius + borderThickness))
        {
            if (y < borderRadius + borderThickness)
                origin = new Vector2(width - (borderRadius + borderThickness), borderRadius + borderThickness);
            else if (y > height - (borderRadius + borderThickness))
                origin = new Vector2(width - (borderRadius + borderThickness), height - (borderRadius + borderThickness));
            else
                origin = new Vector2(width - (borderRadius + borderThickness), y);
        }
        else
        {
            if (y < borderRadius + borderThickness)
                origin = new Vector2(x, borderRadius + borderThickness);
            else if (y > height - (borderRadius + borderThickness))
                origin = new Vector2(x, height - (borderRadius + borderThickness));
        }

        if (!origin.Equals(Vector2.zero))
        {
            float distance = Vector2.Distance(point, origin);

            if (distance > borderRadius + borderThickness + 1)
            {
                return Color.clear;
            }
            else if (distance > borderRadius + 1)
            {
                if (borderColors.Count > 2)
                {
                    float modNum = distance - borderRadius;

                    if (modNum < borderThickness / 2)
                    {
                        return Color32.Lerp(borderColors[2], borderColors[1], (float)((modNum) / (borderThickness / 2.0)));
                    }
                    else
                    {
                        return Color32.Lerp(borderColors[1], borderColors[0], (float)((modNum - (borderThickness / 2.0)) / (borderThickness / 2.0)));
                    }
                }


                if (borderColors.Count > 0)
                    return borderColors[0];
            }
            else if (distance > borderRadius - borderShadow + 1)
            {
                float mod = (distance - (borderRadius - borderShadow)) / borderShadow;
                float shadowDiff = initialShadowIntensity - finalShadowIntensity;
                return DarkenColor(initialColor, ((shadowDiff * mod) + finalShadowIntensity));
            }
        }

        return initialColor;
    }

    private static Color32 DarkenColor(Color32 color, float shadowIntensity)
    {
        return Color32.Lerp(color, Color.black, shadowIntensity);
    }
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类NewBehaviourScript:MonoBehavior{
公共整数宽度=256;
公共内部高度=140;
public int borderThickness=1;//不能小于1
//边框阴影不能大于边框半径
public int borderRadius=40;//不能小于1
公共int-borderShadow=2;
公共列表背景颜色=新列表();
公共列表边框颜色=新列表();
公共交通强度=5f;
公共浮动最终阴影强度=5f;
公共浮点解析乘法器=4f;
私有纹理2dresulttex;
公共图像显示;
公共关系;
void Start()
{
添加(新颜色32(171,0,025));
添加(新颜色32(9,48,173255));
添加(新颜色32(111、8、99、255));
添加(新颜色32(171,4161255));
resultTex=矩形创建器。
CreateRoundedRectangleTexture(4,宽度、高度、边界厚度、,
borderRadius、borderShadow、BackgroundColor、BorderColor、,
初始阴影强度,最终阴影强度);
display.texture=resultTex;
rt.sizeDelta=新矢量2(宽度、高度);
}
公共类矩形生成器
{
公共静态纹理2D CreateRoundedRectangleTexture(int resolutionmultiplier、int width、int height、int borderThickness、int borderRadius、int borderShadow、List BackgroundColor、List BorderColor、float initialShadowIntensity、float finalShadowIntensity)
{
//if(backgroundColors==null | | backgroundColors.Count==0)抛出新的ArgumentException(“必须定义至少一种背景色(最多四种)”;
//if(borderColors==null | | borderColors.Count==0)抛出新的ArgumentException(“必须定义至少一种边框颜色(最多三种)”;
//如果(borderRadius<1)抛出新的ArgumentException(“必须定义边界半径(舍入边)”;
//如果(borderThickness<1)抛出新的ArgumentException(“必须定义边框厚度”);
//如果(borderThickness+borderRadius>height/2 | | borderThickness+borderRadius>width/2)抛出新的ArgumentException(“边框太厚和/或太圆,无法适应纹理”);

//如果(borderShadow>borderRadius)抛出新的ArgumentException(“borderShadow”的大小必须小于borderRadius(suggeted:shadow)Texture2D的FilterMode是什么?可以将其设置为“point”吗然后再试一次,或者你已经试过了吗?听起来是个好主意,我刚做了,它看起来仍然更好。锯齿问题仍然存在。它只是稍微改进了一点。如果你想离开深层,你可以实现这个着色器:@LeoBartkus是的,我知道我可以使用着色器,但我在问题中提到我不想使用shader.我更喜欢使用纹理类。啊,我会尝试将分辨率加倍,并在材质设置为剪切模式的情况下渲染。你也可能会遇到半像素问题或你原来的代码库的解决方法。我在第一个屏幕截图中看到了圆形锯齿边。你的代码在屏幕上排列像素空间完全一样。就像在XNA中一样。等等,你的“游戏视图”选项卡中的视口比例设置为1x了吗?没有。在发布这篇文章时,这不是问题。当我回到电脑时,我会做你建议的更改。我不敢相信我在这方面浪费了这么多时间。Unity中有一个新的复选框,名为“低分辨率纵横比”“默认情况下,它是启用的。我只是简单地禁用了它,在按照您的建议增加纹理大小后,它工作正常。自从我更改分辨率后,我还必须将
边界半径
更改为大约
300
。现在它很平滑。谢谢。”。
return Color32.Lerp(color, Color.clear, shadowIntensity);
   using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour {


public int width = 256;
public int height = 140;
public int borderThickness = 1;  //Cannot be < 1
//Border shadow cannot be more than Border Radius
public int borderRadius = 40; //Cannot be < 1
public int borderShadow = 2;
public List<Color32> backgroundColors = new List<Color32>();
public List<Color32> borderColors = new List<Color32>();
public float initialShadowIntensity = 5f;
public float finalShadowIntensity = 5f;

public float resolutionmultiplier = 4f;


private Texture2D resultTex;
public RawImage display;

public RectTransform rt;

void Start()
{
    backgroundColors.Add(new Color32(171, 0, 0, 255));
    backgroundColors.Add(new Color32(9, 48, 173, 255));

    borderColors.Add(new Color32(111, 8, 99, 255));
    borderColors.Add(new Color32(171, 4, 161, 255));

    resultTex = RectangleCreator.
        CreateRoundedRectangleTexture(4, width, height, borderThickness,
        borderRadius, borderShadow, backgroundColors, borderColors,
        initialShadowIntensity, finalShadowIntensity);

    display.texture = resultTex;
    rt.sizeDelta = new Vector2(width, height);
}



public class RectangleCreator
{
    public static Texture2D CreateRoundedRectangleTexture(int resolutionmultiplier, int width, int height, int borderThickness, int borderRadius, int borderShadow, List<Color32> backgroundColors, List<Color32> borderColors, float initialShadowIntensity, float finalShadowIntensity)
    {
       // if (backgroundColors == null || backgroundColors.Count == 0) throw new ArgumentException("Must define at least one background color (up to four).");
      //  if (borderColors == null || borderColors.Count == 0) throw new ArgumentException("Must define at least one border color (up to three).");
      //  if (borderRadius < 1) throw new ArgumentException("Must define a border radius (rounds off edges).");
      //  if (borderThickness < 1) throw new ArgumentException("Must define border thikness.");
     //   if (borderThickness + borderRadius > height / 2 || borderThickness + borderRadius > width / 2) throw new ArgumentException("Border will be too thick and/or rounded to fit on the texture.");
     //   if (borderShadow > borderRadius) throw new ArgumentException("Border shadow must be lesser in magnitude than the border radius (suggeted: shadow <= 0.25 * radius).");

        width = width * resolutionmultiplier;
        height = height * resolutionmultiplier;

        Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
        Color32[] color = new Color32[width * height];

        for (int x = 0; x < texture.width; x++)
        {
            for (int y = 0; y < texture.height; y++)
            {
                switch (backgroundColors.Count)
                {
                    case 4:
                        Color32 leftColor0 = Color32.Lerp(backgroundColors[0], backgroundColors[1], ((float)y / (width - 1)));
                        Color32 rightColor0 = Color32.Lerp(backgroundColors[2], backgroundColors[3], ((float)y / (height - 1)));
                        color[x + width * y] = Color32.Lerp(leftColor0, rightColor0, ((float)x / (width - 1)));
                        break;
                    case 3:
                        Color32 leftColor1 = Color32.Lerp(backgroundColors[0], backgroundColors[1], ((float)y / (width - 1)));
                        Color32 rightColor1 = Color32.Lerp(backgroundColors[1], backgroundColors[2], ((float)y / (height - 1)));
                        color[x + width * y] = Color32.Lerp(leftColor1, rightColor1, ((float)x / (width - 1)));
                        break;
                    case 2:
                        color[x + width * y] = Color32.Lerp(backgroundColors[0], backgroundColors[1], ((float)x / (width - 1)));
                        break;
                    default:
                        color[x + width * y] = backgroundColors[0];
                        break;
                }

                color[x + width * y] = ColorBorder(x, y, width, height, borderThickness, borderRadius, borderShadow, color[x + width * y], borderColors, initialShadowIntensity, finalShadowIntensity);
            }
        }

        texture.SetPixels32(color);
        texture.Apply();
        return texture;
    }

    private static Color32 ColorBorder(int x, int y, int width, int height, int borderThickness, int borderRadius, int borderShadow, Color32 initialColor, List<Color32> borderColors, float initialShadowIntensity, float finalShadowIntensity)
    {
        Rect internalRectangle = new Rect((borderThickness + borderRadius), (borderThickness + borderRadius), width - 2 * (borderThickness + borderRadius), height - 2 * (borderThickness + borderRadius));


        Vector2 point = new Vector2(x, y);
        if (internalRectangle.Contains(point)) return initialColor;

        Vector2 origin = Vector2.zero;

        if (x < borderThickness + borderRadius)
        {
            if (y < borderRadius + borderThickness)
                origin = new Vector2(borderRadius + borderThickness, borderRadius + borderThickness);
            else if (y > height - (borderRadius + borderThickness))
                origin = new Vector2(borderRadius + borderThickness, height - (borderRadius + borderThickness));
            else
                origin = new Vector2(borderRadius + borderThickness, y);
        }
        else if (x > width - (borderRadius + borderThickness))
        {
            if (y < borderRadius + borderThickness)
                origin = new Vector2(width - (borderRadius + borderThickness), borderRadius + borderThickness);
            else if (y > height - (borderRadius + borderThickness))
                origin = new Vector2(width - (borderRadius + borderThickness), height - (borderRadius + borderThickness));
            else
                origin = new Vector2(width - (borderRadius + borderThickness), y);
        }
        else
        {
            if (y < borderRadius + borderThickness)
                origin = new Vector2(x, borderRadius + borderThickness);
            else if (y > height - (borderRadius + borderThickness))
                origin = new Vector2(x, height - (borderRadius + borderThickness));
        }

        if (!origin.Equals(Vector2.zero))
        {
            float distance = Vector2.Distance(point, origin);

            if (distance > borderRadius + borderThickness + 1)
            {
                return Color.clear;
            }
            else if (distance > borderRadius + 1)
            {
                if (borderColors.Count > 2)
                {
                    float modNum = distance - borderRadius;

                    if (modNum < borderThickness / 2)
                    {
                        return Color32.Lerp(borderColors[2], borderColors[1], (float)((modNum) / (borderThickness / 2.0)));
                    }
                    else
                    {
                        return Color32.Lerp(borderColors[1], borderColors[0], (float)((modNum - (borderThickness / 2.0)) / (borderThickness / 2.0)));
                    }
                }


                if (borderColors.Count > 0)
                    return borderColors[0];
            }
            else if (distance > borderRadius - borderShadow + 1)
            {
                float mod = (distance - (borderRadius - borderShadow)) / borderShadow;
                float shadowDiff = initialShadowIntensity - finalShadowIntensity;
                return DarkenColor(initialColor, ((shadowDiff * mod) + finalShadowIntensity));
            }
        }

        return initialColor;
    }

    private static Color32 DarkenColor(Color32 color, float shadowIntensity)
    {
        return Color32.Lerp(color, Color.black, shadowIntensity);
    }
}
}