title: Create smoothly rounded rectangle with Texture2D
body: <p>I am trying to draw rounded rectangles which can be used as a Texture for any UI component. My goal is to create this rounded Texture with the <code>Texture2D</code> class by setting the pixels with the <a href="https://docs.unity3d.com/ScriptReference/Texture2D.SetPixels32.html" rel="nofollow noreferrer"><code>SetPixels32</code></a> function. I do not want to do this with a shader.</p>
  
  <p>This <a href="https://stackoverflow.com/a/17260476/3785314">post</a> did that but with XNA instead of Unity. I ported it into Unity <em>but the Edge is jaggy</em>. </p>
  
  <p>This is what it looks like in Unity:</p>
  
  <p><a href="https://i.stack.imgur.com/pTFWF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pTFWF.png" alt="enter image description here"></a></p>
  
  <p>Below is the ported code:</p>
  
  <pre><code>public int width = 256;
  public int height = 140;
  public int borderThickness = 1;  //Cannot be &lt; 1
  //Border shadow cannot be more than Border Radius
  public int borderRadius = 40; //Cannot be &lt; 1
  public int borderShadow = 2;
  public List&lt;Color32&gt; backgroundColors = new List&lt;Color32&gt;();
  public List&lt;Color32&gt; borderColors = new List&lt;Color32&gt;();
  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&lt;Color32&gt; backgroundColors, List&lt;Color32&gt; 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 &lt; 1) throw new ArgumentException("Must define a border radius (rounds off edges).");
          if (borderThickness &lt; 1) throw new ArgumentException("Must define border thikness.");
          if (borderThickness + borderRadius &gt; height / 2 || borderThickness + borderRadius &gt; width / 2) throw new ArgumentException("Border will be too thick and/or rounded to fit on the texture.");
          if (borderShadow &gt; borderRadius) throw new ArgumentException("Border shadow must be lesser in magnitude than the border radius (suggeted: shadow &lt;= 0.25 * radius).");
  
          Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
          Color32[] color = new Color32[width * height];
  
          for (int x = 0; x &lt; texture.width; x++)
          {
              for (int y = 0; y &lt; 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&lt;Color32&gt; 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 &lt; borderThickness + borderRadius)
          {
              if (y &lt; borderRadius + borderThickness)
                  origin = new Vector2(borderRadius + borderThickness, borderRadius + borderThickness);
              else if (y &gt; height - (borderRadius + borderThickness))
                  origin = new Vector2(borderRadius + borderThickness, height - (borderRadius + borderThickness));
              else
                  origin = new Vector2(borderRadius + borderThickness, y);
          }
          else if (x &gt; width - (borderRadius + borderThickness))
          {
              if (y &lt; borderRadius + borderThickness)
                  origin = new Vector2(width - (borderRadius + borderThickness), borderRadius + borderThickness);
              else if (y &gt; height - (borderRadius + borderThickness))
                  origin = new Vector2(width - (borderRadius + borderThickness), height - (borderRadius + borderThickness));
              else
                  origin = new Vector2(width - (borderRadius + borderThickness), y);
          }
          else
          {
              if (y &lt; borderRadius + borderThickness)
                  origin = new Vector2(x, borderRadius + borderThickness);
              else if (y &gt; height - (borderRadius + borderThickness))
                  origin = new Vector2(x, height - (borderRadius + borderThickness));
          }
  
          if (!origin.Equals(Vector2.zero))
          {
              float distance = Vector2.Distance(point, origin);
  
              if (distance &gt; borderRadius + borderThickness + 1)
              {
                  return Color.clear;
              }
              else if (distance &gt; borderRadius + 1)
              {
                  if (borderColors.Count &gt; 2)
                  {
                      float modNum = distance - borderRadius;
  
                      if (modNum &lt; 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 &gt; 0)
                      return borderColors[0];
              }
              else if (distance &gt; 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);
      }
  }
  </code></pre>
  
  <p>I tried fixing the black jagged edge by replacing the</p>
  
  <pre><code>return Color32.Lerp(color, Color.black, shadowIntensity);
  </code></pre>
  
  <p>with</p>
  
  <pre><code>return Color32.Lerp(color, Color.clear, shadowIntensity);
  </code></pre>
  
  <p>but that didn't smooth it with a transparent color. It removed the black color but made it more jagged. </p>
  
  <p>Here is what it looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/jwmlg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jwmlg.png" alt="enter image description here"></a></p>
  
  <p>How can I smooth out the texture edge?</p>