• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by pajamajama · Dec 05, 2013 at 06:52 PM · texturetexture2ddrawingsetpixelssetpixel

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

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);
 
             }
         }    
     }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Dec 06, 2013 at 09:07 PM 0
Share
  • and accepted :)

$$anonymous$$eep in $$anonymous$$d 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.

avatar image pajamajama · Dec 07, 2013 at 09:25 AM 1
Share

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 ();
     }
avatar image lvictorino pajamajama · May 17, 2016 at 02:21 PM 1
Share

Well... for those passing here... note that the 1024 value @$$anonymous$$majama 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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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.



Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

For different texture sizes, which is faster? SetPixel or SetPixels? 2 Answers

Fastest rendering of 2D world with lots of changes to screen every frame? 3 Answers

Setting pixels on a texture? 2 Answers

Draw one texture on to another? Get Set Pixels. 0 Answers

Setpixels creates gradient texture instead of raw colored pixels 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges