Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

procedural filled circles

Discussion in '2D' started by jimmyo, Mar 20, 2014.

  1. jimmyo

    jimmyo

    Joined:
    Mar 20, 2014
    Posts:
    20
    Hi all - Thanks in advance for any help someone can provide. I am building a game and would like flexibility for deployment on different screen sizes. To accommodate this I would like to procedurally create a few circles (rather than drawing them as fixed size on a sprite sheet and scaling them as that produces artefacts).

    One way I have found to do this is to use LineRenderer to draw a number of lines from the center of the circle to the edge. eg something like:

    Code (csharp):
    1. int i = 0;
    2. for (float angle=0f; float<2*PI; float += 0.1) {
    3.   float x = radius*cos(angle);
    4.   float y = radius*sin(angle);
    5.   Vector3 pos1 = new Vector3(0,0,0);
    6.   Vector3 pos2 = new Vector3(x,y,0);
    7.   lineRenderer.SetPosition(i, pos1);
    8.   lineRenderer.SetPosition(i, pos2);
    9.   i+=1;
    10. }
    Question 1: Is this the best way to do this?

    Question 2: I don't want to do this in every frame. How can I create one of the above 'circles', store it as if were on a spritesheet and then add and remove them from my scene programmatically and efficiently?

    Thanks for any help anyone can provide.
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    1: Dunno, you'd have to test performance between different methods.

    2: Do it in start function once, just to set the points. And after that, the LineRenderer component will continue to render based on those initial set points. You can easily modify them at any time, while running, if you feel like it as well.

    Another way would be to draw it directly through the GPU, using some shader code. There's an HLSL example here that you could adapt to Unity with a little bit of research:
    http://missingbytes.blogspot.ca/2012/05/circles_12.html
     
  3. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    I decided to do a little research into CG shader writing and managed to whip up a Circle shader since I thought it might come in handy. It keeps all the existing Sprite material capabilities, it also still dynamically batches, saving you render calls, but it also has a radius slider to decide how much to cut away.

    I've attached it for download.

    I also attached a regular shader version, that can work without a texture input or sprite, just apply it to a polygon Quad.


    No matter how close you get with this shader, it will always be per-screen-pixel smooth. So you can size it up and down to your heart's content, without extra aliasing.

    Currently it's not anti-aliased though, so it's as smooth as the screen resolution allows. Here's a gif of it:
    http://i.imgur.com/Hf5f24W.gif
     

    Attached Files:

  4. jimmyo

    jimmyo

    Joined:
    Mar 20, 2014
    Posts:
    20
    Thank you Invertex - this is the kind of thing I have been struggling to come up with!
     
  5. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    No problem! Also, I'll have an anti-aliased version uploaded later today/tomorrow.
     
  6. jimmyo

    jimmyo

    Joined:
    Mar 20, 2014
    Posts:
    20
    Hi Invertex - Any chance you got around to the anti-alias version (or can provide hints on how to implement)? I have the shader working (I actually needed partial circles (eg "pizza slices" of various sizes) and modified it to do that) but the lines look quite rough at the edges so would like to have it smoothed. Thanks for any info you can provide.
     
  7. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    Yeah I had, as well as added some more controls to it, but my main hard drive with a lot of my important files died on the weekend. Trying to recover my files right now, but it's not looking good :C
     
  8. jimmyo

    jimmyo

    Joined:
    Mar 20, 2014
    Posts:
    20
    Invertex - Sorry to hear about your drive crash. Just wondering if you were able to recover the anti-aliased version. Jim