This site uses strictly necessary cookies. More Information

X- Home /

# Drawing a solid circle onto texture

Has anyone modified Eric Haines' (Eric5h5) TextureDrawCircle script to draw a solid circle? It currently draws only the perimeter. Any suggestions on how a solid circle drawing algorithm would go?

Here's a link to Eric's lovely script: TextureDrawCircle - Unify Wiki

and here's a snippet:

```
static function Circle (tex : Texture2D, cx : int, cy : int, r : int, col : Color) {
var y = r;
var d = 1/4 - r;
var end = Mathf.Ceil(r/Mathf.Sqrt(2));
for (x = 0; x <= end; x++) {
tex.SetPixel(cx+x, cy+y, col);
tex.SetPixel(cx+x, cy-y, col);
tex.SetPixel(cx-x, cy+y, col);
tex.SetPixel(cx-x, cy-y, col);
tex.SetPixel(cx+y, cy+x, col);
tex.SetPixel(cx-y, cy+x, col);
tex.SetPixel(cx+y, cy-x, col);
tex.SetPixel(cx-y, cy-x, col);
d += 2*x+1;
if (d > 0) {
d += 2 - 2*y--;
}
}
}
```

Thank you very much!

**Answer** by pajamajama
·
Dec 06, 2013 at 08:58 PM

Problem solved! Below is a modified function for creating a solid circle!

```
public void Circle(Texture2D tex, int cx, int cy, int r, Color col)
{
int x, y, px, nx, py, ny, d;
for (x = 0; x <= r; x++)
{
d = (int)Mathf.Ceil(Mathf.Sqrt(r * r - x * x));
for (y = 0; y <= d; y++)
{
px = cx + x;
nx = cx - x;
py = cy + y;
ny = cy - y;
tex.SetPixel(px, py, col);
tex.SetPixel(nx, py, col);
tex.SetPixel(px, ny, col);
tex.SetPixel(nx, ny, col);
}
}
}
```

and accepted :)

$$anonymous$$eep in mind that SetPixel for filling a large area is quite slow. You might want to use GetPixels32, work on the array and assign it back to SetPixels32.

Thanks Bunny83!

Here's a modified Circle function that uses Get/SetPixels, in case it helps anyone.

```
public void Circle(Texture2D tex, int cx, int cy, int r, Color col)
{
int x, y, px, nx, py, ny, d;
Color[] tempArray = tex.GetPixels32;
for (x = 0; x <= r; x++)
{
d = (int)$$anonymous$$athf.Ceil($$anonymous$$athf.Sqrt(r * r - x * x));
for (y = 0; y <= d; y++)
{
px = cx + x;
nx = cx - x;
py = cy + y;
ny = cy - y;
tempArray[py*1024 + px] = col;
tempArray[py*1024 + nx] = col;
tempArray[ny*1024 + px] = col;
tempArray[ny*1024 + nx] = col;
}
}
tex.SetPixels32(tempArray);
tex.Apply ();
}
```

Well... for those passing here... note that the `1024`

value @pajamajama uses in the example above seems to be the size of his texture... to make it work with your texture replace `1024`

by `tex.width`

.

**Answer** by ProNoob2
·
Jul 16, 2020 at 12:01 PM

@pajamajama @Bunny83 I want to draw rectangles in the same way . I dont know how to do that? I also would like to know that how can I draw rectangles with round edges or maybe add some more shapes. I'm currently stuck here and any help would be great. Thanks in advance.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.