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.

How to draw 2D lines?

Discussion in 'Editor & General Support' started by DavidC02, Jul 4, 2012.

  1. DavidC02

    DavidC02

    Joined:
    Jul 19, 2011
    Posts:
    152
    What do you guys think would be the best to draw 2D lines in a game? I have been looking around but haven't found something.

    It would probably act as the brush or pencil tool in Paint or something like that. Been using RageSpline but before iterating on trying to make it look better I wanted to ask if there's anything out there I could look at, or if you guys had any suggestions.

    Thanks.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,334
  3. DavidC02

    DavidC02

    Joined:
    Jul 19, 2011
    Posts:
    152
    Thanks for your reply. Is there a demo for something like I'm looking for?
     
  4. spinaljack

    spinaljack

    Joined:
    Mar 18, 2010
    Posts:
    896
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,334
    Yes, it comes with a demo script for simple line drawing (using a mouse or touch input).

    --Eric
     
  6. DavidC02

    DavidC02

    Joined:
    Jul 19, 2011
    Posts:
    152

    I'd like to see it before buying. A YouTube video or something would do. Or maybe a web demo.
     
    Last edited: Jul 5, 2012
  7. DavidC02

    DavidC02

    Joined:
    Jul 19, 2011
    Posts:
    152
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,334
    Oh, a demo of Vectrosity. There are a number of them on the page I linked to. As for a demo of the line-drawing itself, see here (also here).

    --Eric
     
  9. DavidC02

    DavidC02

    Joined:
    Jul 19, 2011
    Posts:
    152
    How intensive is the line drawing demo (demo 9) if I keep previous lines while drawing new ones? It's going to be used for iPhone, and according to this, you're supposed to keep it under 7.5k tris, or at least try.
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,334
    Not very intensive, since each line is a mesh that takes no CPU time (aside from being a draw call) if it's not actively being updated.

    That's years old and only applies to the 1st-gen iPhone. The latest hardware is much faster.

    --Eric
     
  11. DavidC02

    DavidC02

    Joined:
    Jul 19, 2011
    Posts:
    152
    Thanks so much Eric, I guess my last question regarding Vectrosity is just to confirm if lines can have z-depth (by this I mean, I need the collider to have depth), and physics attached to them, like a rigidbody and stuff?

    Now, what would you suggest to do this with, if I didn't use Vectrosity. I know I might save time, but just wondering, and maybe if there's a demo out there.
     
    Last edited: Jul 12, 2012
  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,334
    No, sorry, lines in Vectrosity are 2D only. If you're going for a "Max and the Magic Marker"-like effect, that would be fairly complex and not something that Vectrosity is really meant to do.

    --Eric
     
  13. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    I'll say that I messed with this as well, if you're interested my method was to use the unity procedural mesh code to extrude the mesh planes that vectrosity generates, then I generated a mesh collider from that extrusion.
     
  14. DavidC02

    DavidC02

    Joined:
    Jul 19, 2011
    Posts:
    152
    Wow! you know I hadn't checked Max and the Magic Marker. Quite impressive.

    Thanks for your reply. I'll keep updating as I advance. I'm giving it a shot with RageSpline.
     
  15. DavidC02

    DavidC02

    Joined:
    Jul 19, 2011
    Posts:
    152
    Ok basically this is what I'm doing

    First, when I click I instantiate a new line prefab, which is a very simple 2 point ragespline, with free outline.
    This I use on a plane, but I guess you could find many ways to do this, basically you just need to instantiate a new object.
    Code (csharp):
    1.  
    2. //LineDrawing.js
    3. var point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    4. var line = Instantiate (linePrefab, new Vector3(point.x-20, point.y+15, -10), Quaternion.identity);
    5. line.name = "line" + lineCounter++;
    6. currentLine = line.name; //this is an static variable
    7.  
    For some reason I had to add those 20 and 15 points because it wouldn't instantiate exactly where I was clicking, and I check so I only run this once.

    On the line, I use the following
    Code (csharp):
    1.  
    2. //line.js
    3. if (this.gameObject.name == LineDrawing.currentLine) {
    4.         if (LineDrawing.RefreshMeshBool) {
    5.             this.GetComponent(RageSpline).RefreshMesh(false, false, true); //to update the physics on release, in case you are using true, true, false
    6.             LineDrawing.currentLine = "";
    7.         }
    8.        
    9.         if (Input.GetMouseButton(0)) {
    10.             if (previousPosition != Input.mousePosition) { //avoid adding points on top of each other
    11.                
    12.                 if (pointIndex <= 80) { //to avoid infinite lines, int value
    13.                     distance = Vector3.Distance (previousPosition, Input.mousePosition);
    14.                    
    15.                     if (distance > 10) {
    16.                         this.GetComponent(RageSpline).AddPointWorldSpace(pointIndex++, new Vector3(point.x,point.y,-10));
    17.                        
    18.                         this.GetComponent(RageSpline).SetOutControlPosition(pointIndex-1, this.GetComponent(RageSpline).GetPosition(pointIndex-1));
    19.                         previousPosition = Input.mousePosition;
    20.                        
    21.                         this.GetComponent(RageSpline).RefreshMesh(true, true, true); //using (true, true, false) makes it run smoothly, but has no physics
    22.                     }
    23.                 }
    24.             }
    25.         }
    26.     }
    27.  
    And finally just add on some mouseUp, depending on what you did on the first part, for instance, to the same plane:
    Code (csharp):
    1.  
    2. LineDrawing.RefreshMeshBool = true;
    3.  
    So it on the Line script, sets the currentLine to "", next time you click, you'll get a new line, otherwise, it just snaps them together, I guess it depends on what you want to do.

    Hope this helps someone else :)
     
  16. graphicDNA

    graphicDNA

    Joined:
    Jun 26, 2017
    Posts:
    47
    Hi guys, I just published a package that does this, and much more for both OnGui and OnPostRender events, allowing drawing all kinds of stuff in 2D with OpenGL (including, custom shapes, textures, dashed lines, all with variable width and orientation, and also allowing drawing text with Bitmap Fonts).

    It's 4.99$ but if there's any student around, or anyone that can't afford it, I´ll be glad to provide free vouchers. Just feel free to contact me...

    Link: https://assetstore.unity.com/packages/tools/gui/2d-shaper-library-105973

    Some examples below...
    Cheers


     
  17. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    LineRender is alternative for GL.Lines.
    If you use GL.Lines you can not control it thickness, GL.Lines have thickness=1 pixel. It's good for make wireframes.
    If you use LineRender, lines have scale thickness.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Main : MonoBehaviour {
    5.  
    6.     // Simple "Paint" based on LineRenderer (Unity 5)
    7.     // New properties appeared for class LineRender in version 2017.1
    8.     public LineRenderer l;
    9.  
    10.     int len=2;
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         Vector2 mousePos = Input.mousePosition;
    15.         Vector3 pos3d = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x,mousePos.y,Camera.main.nearClipPlane));
    16.         l.SetVertexCount(len);
    17.         l.SetPosition(len-1,pos3d);
    18.         len++;
    19.     }
    20. }
     
    Last edited: Jan 13, 2018
  18. graphicDNA

    graphicDNA

    Joined:
    Jun 26, 2017
    Posts:
    47
    Hi Gold_Rush, thanks for your sample.

    LineRenderer is a good alternative if you want/need to deal with game components, but not so good if you just want to draw stuff without creating components or adding complexity to your scene. It doesn't work either for Editor customization stuff.

    All shapes supported by 2D shaper include the option to be drawn with thickness > 1.

    You are right, GL.Lines only support lines with thickness = 1. That's why 2D Shaper draws lines using GL.Quads (not GL.Lines) when line thickness is greater than 1.

    It basically does the same as the LineRender: it creates quads to mimic the shape of continuous, thick lines, but exposes those features as a script library than can be used anywhere, instead of game components.

    Thanks!
     
unityunity