説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ApplyCircleMask.cs 2.0KB

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