Search Unity

Question about support for lines and antialiasing

Discussion in 'Editor & General Support' started by ejhong, Jul 16, 2008.

  1. ejhong

    ejhong

    Joined:
    Jul 16, 2008
    Posts:
    16
    Hi,

    I am considering unity and would like to know whether it supports my situation.

    Here are my two main requirements:

    - Support for lines, curves and circles in 3D space (not made of polygons but always a specified thickness in relation to the screen).
    - Ensuring that the lines are all smoothly antialiased.

    Does unity support this? If anybody can point me to any demos that have examples of this, would love to see what they look like.
     
  2. 64746c

    64746c

    Joined:
    Oct 21, 2007
    Posts:
    181
    You can draw lines with the GL class. You can control antialiasing from quality settings.
    For circles and curves, I expect you'll have to take a look at something like polynomial interpolation or Bezier curves and break curves up into segments to render.
    Unity doesn't support this kind of thing out of the box (yet?), but it is pretty easy to create your own solutions.

    HTH
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Here is a quick demo. Try pressing the up/down arrow keys, hit "r" to reset.

    --Eric
     
  4. ejhong

    ejhong

    Joined:
    Jul 16, 2008
    Posts:
    16
    Thanks for the demo and all the info - it looks great! I think this should work out fine for what I need. I notice also that this line demo doesn't flicker when I use my browser scroll bar (firefox on mac) like some of the other demos do (the jungle). If it's not too much trouble, would you mind making the source available? I t will give me a great place to start...
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Sure:

    Code (csharp):
    1. @script RequireComponent(Camera)
    2.  
    3. static var lineMaterial : Material;
    4. var numberOfPoints = 400;
    5. private var points : Vector3[];
    6. var rotateSpeed = 10.0;
    7. var amount = .02;
    8. var extent = 15.0;
    9. var sineNumber = .025;
    10. var sineAddSpeed = .05;
    11. var moveAmount = .02;
    12.  
    13. function Start () {
    14.     transform.position = Vector3.forward*-20;
    15.     points = new Vector3[numberOfPoints];
    16.     var sineCount = 0.0;
    17.     for (i = 0; i < numberOfPoints; i++) {
    18.         points[i] = Vector3(Mathf.Lerp(-extent, extent, Mathf.InverseLerp(0, numberOfPoints, i)), Mathf.Sin(sineCount), 0.0);
    19.         sineCount += sineNumber;
    20.     }
    21.     CreateLineMaterial();
    22. }
    23.  
    24. function Update () {
    25.     transform.Rotate(Vector3.forward*Time.deltaTime*rotateSpeed);
    26.     transform.Translate(Vector3.forward*Mathf.Sin(Time.time*2.0)*moveAmount);
    27.    
    28.     for (i = 0; i < numberOfPoints; i++) {
    29.         points[i].y += Mathf.Sin(Time.time+sineNumber*i)*amount;
    30.         points[i].z += Mathf.Cos(Time.time+sineNumber*i)*amount;
    31.     }
    32.     sineNumber += Input.GetAxis("Vertical") * sineAddSpeed * Time.deltaTime;
    33.     if (Input.GetKeyDown("r")) {Application.LoadLevel(0);}
    34. }
    35.  
    36. function CreateLineMaterial () {
    37.     lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
    38.         "SubShader { Pass {" +
    39.         "   BindChannels { Bind \"Color\",color }" +
    40.         "   Blend SrcAlpha OneMinusSrcAlpha" +
    41.         "   ZWrite Off Cull Off Fog { Mode Off }" +
    42.         "} } }");
    43.     lineMaterial.hideFlags = HideFlags.HideAndDontSave;
    44.     lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
    45. }
    46.  
    47. function OnPostRender () {
    48.     lineMaterial.SetPass(0);
    49.     GL.Begin(GL.LINES);
    50.     GL.Color(Color.yellow);
    51.     for (i = 0; i < numberOfPoints-1; i++) {
    52.         GL.Vertex3(points[i].x, points[i].y, points[i].z);
    53.         GL.Vertex3(points[i+1].x, points[i+1].y, points[i+1].z);
    54.     }
    55.     GL.End();
    56. }
    You would attach that to a camera (or an empty game object). BTW, just to see what would happen, I bumped the number of points up to 10,000 and it still ran smoothly in the editor. It wasn't until I upped it to 30,000 that it started to get visibly a bit choppy. Just so you can get an idea of the speed.

    --Eric
     
  6. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    Does any one have a solution for indie users making single pixel anti aliased lines? Im actually a little surprised that it seems we can not. Single pixel anti aliased lines and circles I dont think fall under nice looking features. I have occasional been stopped from making a certain functional features including some maps or screen indications from the lack of this feature in indie. The line renderer does not work in most of my situations as I require is be one pixel not matter the resolution. This is the only complaint I have about the separation of features between unity indie and unity pro. Any ways just thought i would ask if any one had a solution.

    Thanks, Bill
     
  7. David-Helgason

    David-Helgason

    Moderator

    Joined:
    Mar 29, 2005
    Posts:
    1,104
    You can use the GUI system and generate your own textures with Texture2D.

    d.