File size: 1,998 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | using UnityEngine;
namespace Unity.PlasticSCM.Editor.UI.Avatar
{
internal static class ApplyCircleMask
{
internal static Texture2D For(Texture2D sourceImage)
{
int centerx = sourceImage.width / 2;
int centery = sourceImage.height / 2;
int radius = sourceImage.width / 2;
Texture2D result = Images.GetNewTextureFromTexture(sourceImage);
for (int i = (centerx - radius); i < centerx + radius; i++)
{
for (int j = (centery - radius); j < centery + radius; j++)
{
float dx = i - centerx;
float dy = j - centery;
float d = Mathf.Sqrt(dx * dx + dy * dy);
float borderSize = 1f;
if (d <= (radius - borderSize))
{
result.SetPixel(
i - (centerx - radius),
j - (centery - radius),
sourceImage.GetPixel(i, j));
continue;
}
Color color = sourceImage.GetPixel(i, j);
result.SetPixel(
i - (centerx - radius),
j - (centery - radius),
Color.Lerp(Color.clear, color,
GetAntialiasAlpha(radius, d, borderSize)));
}
}
result.Apply();
return result;
}
static float GetAntialiasAlpha(float radius, float d, float borderSize)
{
if (d >= (radius + borderSize))
return 0f;
if (d - radius - borderSize == 0)
return 0;
float proportion =
Mathf.Abs(d - radius - borderSize) /
(radius + borderSize) - (radius - borderSize);
return Mathf.Max(0, 1.0f - proportion);
}
}
} |