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

Drawing 1 pixel orbits

Discussion in 'Scripting' started by GoodNight9, Jul 27, 2016.

  1. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Hello Everyone!

    I'm trying to draw a 1 pixel circle to represent planet orbits. Right now I'm using a really big .png image for a texture. It looks OK, but the problem is that the texture gets bigger as the orbit gets bigger.

    I was wondering is there any better way to go about doing this? Here is a picture of what I have so far:



    Thank you so much for your time!
     
  2. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    It's possible to dynamically create geometry, which could give you a ring of any desired size/thickness. The tools I know of for this are Vectrosity (used it with no issues, but that was for straight lines, so not sure how it looks for smooth curves), and Ragespline (not used it myself, but others have said it works well).

    If you're familiar with how meshes work in Unity, you could probably roll your own as well.
     
  3. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Yes, I think drawing it geometrically is what I need! In my searches I've stumbled upon Vectrosity, Ragespline, and GL. But like you said, the only instructions I can find with them are how to create lines. I haven't been able to find a tutorial or simple basic code to make a circle so far :(
     
  4. Fissll

    Fissll

    Joined:
    Jan 30, 2015
    Posts:
    70
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    One of the Vectrosity demos is exactly what you're trying to do (well, they're using 2 pixel wide lines).
    https://starscenesoftware.com/vectrositydemo8.html

    Depending on your camera setup (ie: do you ever allow the camera to see your current texture quad based lines "edge on") you could also do this with a shader to draw the circle instead of a texture.
     
    GoodNight9 likes this.
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    Code (CSharp):
    1. Shader "Custom/Circle" {
    2. Properties {
    3.     _Color ("Color", Color) = (1.0, 1.0, 1.0, 1.0)
    4.     _LineWidth ("Line Width", Range(0.0, 10.0)) = 2.0
    5.     _Gutter ("Edge Inset", Range(0.0, 0.5)) = 0.1
    6. }
    7.  
    8. SubShader {
    9.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "DisableBatching"="True"}
    10.  
    11.     Blend SrcAlpha OneMinusSrcAlpha
    12.     ZWrite Off
    13.     Cull Off
    14.  
    15.     Pass {
    16.         CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             #pragma multi_compile_fog
    20.          
    21.             #include "UnityCG.cginc"
    22.  
    23.             struct appdata_t {
    24.                 float4 vertex : POSITION;
    25.                 float2 texcoord : TEXCOORD0;
    26.             };
    27.  
    28.             struct v2f {
    29.                 float4 vertex : SV_POSITION;
    30.                 half2 texcoord : TEXCOORD0;
    31.                 UNITY_FOG_COORDS(1)
    32.             };
    33.  
    34.             fixed4 _Color;
    35.             float _LineWidth;
    36.             float _Gutter;
    37.          
    38.             v2f vert (appdata_t v)
    39.             {
    40.                 v2f o;
    41.                 o.vertex = UnityObjectToClipPos(v.vertex);
    42.  
    43.                 // center uvs
    44.                 o.texcoord = v.texcoord * 2.0 - 1.0;
    45.  
    46.                 // adjustment to prevent line from spilling over quad edge
    47.                 o.texcoord /= 1.0 - _Gutter;
    48.  
    49.                 UNITY_TRANSFER_FOG(o,o.vertex);
    50.                 return o;
    51.             }
    52.          
    53.             fixed4 frag (v2f i) : SV_Target
    54.             {
    55.                 float circularGradient = sqrt(dot(i.texcoord, i.texcoord));
    56.                 float screenPixelWidth = length(float2(ddx(circularGradient), ddy(circularGradient)));
    57.                 float lineCenteredGradient = abs(circularGradient - 1.0);
    58.  
    59.                 float lineWidth = max(1.0, _LineWidth) * 0.5;
    60.                 float lineAlpha = saturate(_LineWidth);
    61.  
    62.                 float circle = smoothstep(-0.5, 1.0, lineWidth - lineCenteredGradient / screenPixelWidth) * lineAlpha;
    63.  
    64.                 fixed4 col = _Color;
    65.                 col.a *= circle;
    66.  
    67.                 UNITY_APPLY_FOG(i.fogCoord, col);
    68.                 return col;
    69.             }
    70.         ENDCG
    71.     }
    72. }
    73.  
    74. }
    The "Edge Inset" is how much to inset the line by to prevent it from spilling over the edge of the quad. If you use a high width line with no edge inset you'll see the problem quickly. Also if you look at this edge on it'll collapse into a mess, but so does a texture based solution.

    When you're placing these you'll want to scale up your quads by 1.0 / (1.0 - Edge Inset) to keep the line centered.

    note: for Unity 5.3 and earlier replace:
    o.vertex = UnityObjectToClipPos(v.vertex);
    with
    o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
     
    Last edited: Jul 28, 2016
    GoodNight9 likes this.
  7. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Wow, thank you very much and yes that appears to be exactly what I'm looking for!

    The code that you just sent, is that something I copy and paste in the update method?
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    That's a shader. Copy it into a file called Circle.shader and make a new material using that shader. The Vectrosity demo and that shader are unrelated.
     
    GoodNight9 likes this.
  9. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Thank you very much for the information and I do believe this answers everything I needed to know.

    Much appreciated bro!