Search Unity

Vectrosity - fast and easy line drawing

Discussion in 'Assets and Asset Store' started by Eric5h5, May 26, 2010.

Thread Status:
Not open for further replies.
  1. MatthewM7

    MatthewM7

    Joined:
    Mar 16, 2014
    Posts:
    1
    I'm having trouble figuring out 2D depth with other game objects. I need to keep some VectorLine objects behind normal GameObjects. I am in orthographic. I've tried the depth property, sortingLayer, and changing shaders such as unlit. I see it discussed in the docs but am clearly lacking the magic setup to do it. E.g. Put a VectorLine at depth 20 with objects at z10 and camera z-1. It seems like sorting layer would be do it and perhaps I simply have failed to put the VectorLine on the lower sorting layer.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    You would need to use Draw3D rather than Draw...if you use Draw, then the lines are drawn with a separate overlay camera and wouldn't be sorted with the other objects.

    --Eric
     
  3. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    272
    Hi Eric,

    I've got a 1 pixel line that (obviously) looks quite jagged at angles as I rotate it around, I'm wondering if there's any kind of aliasing or smoothing I can apply outside of increasing the width and smoothing that?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    You can turn on anti-aliasing in the Unity quality settings, or else use a texture for the line, so that bilinear filtering essentially does anti-aliasing for you (see page 12 in the docs).

    --Eric
     
  5. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    272
    Ah, my humblest apologies. I've even read the section on free anti-aliasing before, and got excited about lightsabers instead of retaining actual application benefits.
     
  6. Danirey

    Danirey

    Joined:
    Apr 3, 2013
    Posts:
    525
    Hi Eric!

    Awesome asset. Iuse it to make a visual reference of a character path with navmesh. Finally i have it working with a dotted line. But i've a question for you. Is it possible to make the line flat over the floor surface instead of rotate each segment to face the camera? I want it to stay flat over the floor and as the camera rotates the line stay in the same position and orientation.

    Cheers!
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    You can do that, although currently it involves a bit of a hack where you have to use SetCamera with a different camera oriented so the line will be drawn facing it.

    --Eric
     
  8. Danirey

    Danirey

    Joined:
    Apr 3, 2013
    Posts:
    525
    Ok! Thanks. I'll try it!
     
  9. LifeArtist

    LifeArtist

    Joined:
    Mar 17, 2014
    Posts:
    17
    Hey,

    i have a question about the collide function.

    Is it possible to act with the collision of the lines ?
    So with OnCollisionEnter or something ?

    I cant check it.


    Regards

    LifeArtist
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    You need to use OnCollisionEnter2D.

    --Eric
     
  11. LifeArtist

    LifeArtist

    Joined:
    Mar 17, 2014
    Posts:
    17
    Wow ty :) But how i can add a tag to a line ? :S
     
  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    You can use vectorObject.tag.

    --Eric
     
  13. Doompanther

    Doompanther

    Joined:
    Aug 11, 2012
    Posts:
    24
    I've been having an issue drawing a simple circle. I've tried both MakeCircle and MakeEllipse. But the line is horribly jagged. Most of the segments aren't even connecting to one another. I feel like I'm missing a step. Here's the relevant code:

    Code (csharp):
    1.  
    2.  
    3. Circle= new VectorLine("CircleLine", new Vector3[segments+1], LineMaterial, (width), LineType.Continuous, Joins.Weld);
    4. //Circle.MakeCircle (transform.position, Vector3.up, (gameObject.transform.localScale.x * 4f));
    5. Circle.MakeEllipse(transform.position, Vector3.up, (gameObject.transform.localScale.x * 4f), (gameObject.transform.localScale.x * 4f), segments, 0.0f);
    6.  
    7.  
     
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    Seems fine, and I don't see anything that could be called "horribly jagged" when I try that code.

    --Eric
     
  15. LifeArtist

    LifeArtist

    Joined:
    Mar 17, 2014
    Posts:
    17
    What do u mean ?
    i defined myLine = new VectorLine(...). And then myLine is the vectorObject ?
     
  16. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    myLine.vectorObject.tag = ...

    --Eric
     
  17. LifeArtist

    LifeArtist

    Joined:
    Mar 17, 2014
    Posts:
    17
    Great ty :)

    One last question, is it possible to check if the line collides with his self ?
     
  18. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    Not that I know of, sorry.

    --Eric
     
  19. Doompanther

    Doompanther

    Joined:
    Aug 11, 2012
    Posts:
    24
    Eric, this is what I get when I use the above code:
    $Badline.jpg

    The material used is the VectorLineGlow included in Vectrosity.
     
  20. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    I'm sorry but I don't see anything like that, nor can I tell what would cause that. I'd need more info and code.

    $circle.png

    --Eric
     
  21. sasuke908

    sasuke908

    Joined:
    Mar 28, 2013
    Posts:
    1
    Last edited: Mar 24, 2014
  22. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    You could try some appropriate texture, with VectorLine.textureScale, plus a particle shader.

    --Eric
     
  23. Danirey

    Danirey

    Joined:
    Apr 3, 2013
    Posts:
    525
    Hi,

    Here is the thread of my game. I'm using Vectrosity for some uses, so you guys can see it there.

    Cheers!
     
  24. Xbelt

    Xbelt

    Joined:
    Nov 27, 2013
    Posts:
    8
    I have a small problem. Is there an easy way of translating all vectors currently drawn? My approach was to just move the vectorCamera but this seems problematic since all lines need to be aligned with the background. I tried moving the MainCamera by the same value but then all lines are slightly shifted regarding the other gameObjects (the gameObjects and the vectorLines all have the same z-value since it is 2D so perspective should not be a problem)

    I hope my question is clear enough. If I can provide you with more information, which could lead to a solution, don't hesitate two ask!
     
  25. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    I'm not quite sure what you mean, but if you use .drawTransform, you can basically tie a VectorLine to a transform, so moving the transform moves the VectorLine.

    --Eric
     
  26. Xbelt

    Xbelt

    Joined:
    Nov 27, 2013
    Posts:
    8
    Thanks. I think this should resolve my problem
     
  27. Xbelt

    Xbelt

    Joined:
    Nov 27, 2013
    Posts:
    8
    Well it did not actuall work. I will try to give a more detailed explanation of my problem. I have a gameObject (named turtle) which is at position [0,0,300]. Then I have multiple VectorLines which all have a z-value of 300. At this point all vectorLines are perfectly aligned with the gameObject, (All end at [0,0,300])
    Now when I move the VectorCam lets say by 100 in x and set my gameObject at position [100, 0, 300] the vectorLines are not aligned anymore with the gameObject. I tried to use the .drawTransform and created a gameObject (named VectorPositions) which had the coordinates [0, 0, 300] now when I move this gameObject and my original gameObject by the same amount, the vectorLines are still not aligned.

    here is the code I used to translate my objects. It is executed within the first (turtle) gameObject:
    transform.Translate(x,y,z);
    GameObject.Find("VectorPositions").transform.Translate(x, y, z);
    _mainCamera.DrawLines(_currentDrawnLines);

    _mainCamera.DrawLines just iterates through all lines and calls draw on them and attaches the .drawTransform

    Your help is greatly appreciated.
    Well I finally fixed it by just iterating through all vectorLines and manually update their positions. Do you have any idea why the previous approaches didnt work?
     
    Last edited: Mar 27, 2014
  28. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    I doubt you should be moving the VectorCam; Vectrosity isn't designed for that. You should only move the lines, either by altering the points or by using .drawTransform and moving the transform. If you want the lines to be drawn in the scene instead of as an overlay, use Draw3D/Draw3DAuto instead of Draw.

    --Eric
     
  29. Battleb

    Battleb

    Joined:
    Feb 23, 2012
    Posts:
    85
    Hi,
    i want to make 3d lines pointing up and not facing the camera. Also i don't want to put an additional camera as it would complicate my level making process and will introduce a new item to be careful about.
    would you be so kind to provide me some tips on how to modify the source to make this happen?
    Thanks for your help!
     
  30. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    It's not something that can be done trivially I'm afraid. Using a second camera, while not ideal, is actually fairly simple and doesn't have to complicate making a level since it's just there for SetCamera and wouldn't be used for anything else (you can just disable it).

    --Eric
     
  31. Battleb

    Battleb

    Joined:
    Feb 23, 2012
    Posts:
    85
    Yes it sounds trivial, but i would have to precisely position the camera above the lines and some lines still will not be facing up, but facing the camera, increasing the angle as they move away from it.
    The thing is among other things the line traspasses the terrain polygons when it's rotated to face the camera and looks bad :S
    What would be my best second choice in your opinion?
    Thanks for your help
     
  32. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    As far as I know that's pretty much the only choice at this time. If you have the camera pointing straight down, the lines will face straight up. They don't change angle no matter how far away from the camera, as you can see:

    $line-up2.png

    --Eric
     
  33. Battleb

    Battleb

    Joined:
    Feb 23, 2012
    Posts:
    85
    Thanks i just tried and it works. For future reference:
    -Your camera must be orthogonal, otherwise the line will orient to the camera, varying the angle.
    -The line width is dependent of the orthogonal size, so you must change it to fit the intended width (even if you use setWidths)

    Thanks for your help!
     
    Last edited: Mar 28, 2014
  34. Liviuss

    Liviuss

    Joined:
    Apr 2, 2014
    Posts:
    88
    Hi Eric,

    I'm helping a friend to implement Vectrocity in his project and i faced a nasty behavior. We need to draw some lines with colliders at run-time to be used as a "ground" for moving objects. We have an Vector2 array where we store/append new coordinates provided by user input. The problem is in the way how collider is "extending" from a origin, which in most cases, is behind the moving object(the object is moving to right). Our "ground" is generated in advance so we face the situation when objects cannot reach the lowest point of the line because collider just make a kind of loop. You can see this in attached image.
    The question is if it's a normal behavior or maybe there is a workaround except making splines.

    Thanks.

    $vectrosity_issue.png
     
    Last edited: Apr 2, 2014
  35. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    I'd suggest using a discrete line instead of continuous.

    --Eric
     
  36. Liviuss

    Liviuss

    Joined:
    Apr 2, 2014
    Posts:
    88
    Hi Eric,

    Thanks for the hint but it does not work as expected. I have attached a image captured from your test scene "DrawLinesTouch" just changed the type of line to "Discrete". As you can see, with discrete it's even worse, it's a flickering effect and you can see the line caming from the bottom left corner in "Play Mode".

    Thanks.

    $vectrosity_issue2.png
     
  37. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    Well, it's more involved than just changing the type to Discrete; you'd have to change the logic as well in order to work with the different line type.

    --Eric
     
  38. fieldrequired

    fieldrequired

    Joined:
    Feb 26, 2014
    Posts:
    108
    Quick question for anyone in the know: Does Vectrosity support filled shapes yet?
     
  39. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    Nope.

    --Eric
     
  40. fieldrequired

    fieldrequired

    Joined:
    Feb 26, 2014
    Posts:
    108
    How annoying, shame really because it's pretty good at everything else...

    Any intentions of including it do you know? or are they just going to leave a decent library of functions with a pretty glaring omission?
     
    Last edited: Apr 3, 2014
  41. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    Well, honestly it is a line-drawing utility, and never claimed to be anything else, so I don't think that not having filled shapes counts as an omission.

    --Eric
     
  42. memetic-arts

    memetic-arts

    Joined:
    Feb 27, 2011
    Posts:
    82
    Hi Eric -

    First, I just upgraded to 3.0, and wanted to say a huge 'thank you' for implementing the GetColor and Set/GetWidth features -- huge gain for me, with the particular work I've been doing lately, so can't thank you enough for listening and making it happen.

    So, while the new "get" functions seem to be working fine, I'm having an issue with SetWidth. Here is a code snippet:

    Code (csharp):
    1.  
    2. lineGrp[curGrp].SetColor(Color(0,1,0,1),ls);
    3. lineGrp[curGrp].SetWidth(50.0,ls);
    4. Debug.Log("just set green line width to 50.0; retrieving value . . . width = "+lineGrp[curGrp].GetWidth(ls));
    5.  
    you can see in the screenshot below that I'm successfully setting the color of the segment to green, and that the Debug message in the status line is printing the Width value being retrieved through GetWidth, after I've set it with SetWidth. Problem is, the green line is not 50px wide!

    $screenshot_setWidth_issue.png

    Any ideas? I tried it without a linematerial , and the result is the same . . .

    Many thanks . . .

    Richard
     
  43. memetic-arts

    memetic-arts

    Joined:
    Feb 27, 2011
    Posts:
    82
    Figured it out . . . need to call VectoLine.Draw after resetting the width.

    Need to see how this will impact performance, as I need to do this once per minute, if not more.
     
    Last edited: Apr 4, 2014
  44. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    Redrawing the line once per minute is pretty much nothing, surely? Seeing as how Draw is designed to be called every frame if necessary.

    --Eric
     
  45. Bic Pentameter

    Bic Pentameter

    Joined:
    Aug 24, 2012
    Posts:
    7
    Hi Eric,

    Just picked up your awesome utility. What would be, in your experience, the simplest way to draw a rounded rectangle?

    Thanks!
     
  46. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    I'd probably use MakeCircle and then move the points as needed. Something like this, which is hard-coded to 17 points in the line and a 5 pixel radius for the corners, but you could use it as a starting point if you need something more flexible:

    Code (csharp):
    1. function Start () {
    2.     var roundedRectLine = new VectorLine("Rect", new Vector2[17], null, 2.0, LineType.Continuous);
    3.     RoundedRectangle (roundedRectLine, Vector2(50, 50), Vector2(500, 300));
    4.     roundedRectLine.Draw();
    5. }
    6.  
    7. function RoundedRectangle (line : VectorLine, p1 : Vector2, p2 : Vector2) {
    8.     line.MakeCircle (p1 + Vector2(5.0, 5.0), 5.0, 16, 11.25);
    9.     var size = p2 - p1 - Vector2(10.0, 10.0);
    10.     for (var i = 1; i <= 4; i++) {
    11.         line.points2[i] += size;
    12.     }
    13.     for (i = 5; i <= 8; i++) {
    14.         line.points2[i].y += size.y;
    15.     }
    16.     for (i = 13; i <= 16; i++) {
    17.         line.points2[i].x += size.x;
    18.     }
    19.     line.points2[0].x += size.x;
    20. }
    --Eric
     
  47. Bic Pentameter

    Bic Pentameter

    Joined:
    Aug 24, 2012
    Posts:
    7
    Thanks Eric! I'll try this. I really appreciate your quick response.
     
  48. Bic Pentameter

    Bic Pentameter

    Joined:
    Aug 24, 2012
    Posts:
    7
    Eric, here's a class I created based on your sample code in case others find it useful. So far it works. Also, it looks like the point ordering for drawing the 4 corners of a circle differ depending on if your VectorLine uses Vector2's or Vector3's. This difference is reflected in the RoundedRectangle and RoundedRectangle3D functions.

    Two things I've noticed however.

    1) I am using your DashLine material on my VectorLine for the rounded rectangle. I am causing the dashed lines to move using the line

    line.textureOffset = -Time.time * 2.0f % 1f;

    in Update(). This works fine. The dashes flow counter-clockwise when using RoundedRectangle, and flow clockwise when using RoundedRectangle3D. So my guess is that the code in RoundedRectangle3D must reversing the direction the VectorLine is being created, or does line.MakeCircle reverse the direction depending on if a Vector2 or Vector3 is used?

    2) I'm using the UnlitAlpha shader on the DashLine material. This looks great when using line.Draw(), however, when using line.Draw3D(), the sprites I have behind the lines are not visible through the alpha regions of the material (the scene/camera background color shows instead). If I use the Sprites/Default shader on the DashLine material, Draw() works fine as well, however with Draw3D() the lines render before the sprites, so even if the z value of the Vector3's in the VectorLine are closer to the camera (as is clear in the scene view), the sprites appear to render on top of them in the game view. Any suggestions on the proper shader to use when my 2D scene has sprites?

    Thanks again.


    Code (csharp):
    1. public class LineHelper
    2. {
    3.     public static void RoundedRectangle(VectorLine line, Vector2 p1, Vector2 p2, float cornerRadius, int cornerSegments = 4, float pointRotation = 11.25f)
    4.     {
    5.         Vector2 radiusVector = new Vector2(cornerRadius, cornerRadius);
    6.  
    7.         line.MakeCircle (p1 + radiusVector, cornerRadius, cornerSegments * 4, pointRotation);
    8.         Vector2 size = (p2 - (radiusVector * 2)) - p1;
    9.  
    10.         for (int i = 1; i <= cornerSegments; i++)
    11.             line.points2[i] += size;
    12.  
    13.         for (int i = (cornerSegments + 1); i <= (cornerSegments * 2); i++)
    14.             line.points2[i].y += size.y;
    15.  
    16.         for (int i = ((cornerSegments * 3) + 1); i <= (cornerSegments * 4); i++)
    17.             line.points2[i].x += size.x;
    18.  
    19.         line.points2[0].x += size.x;
    20.     }
    21.  
    22.     public static void RoundedRectangle3D(VectorLine line, Vector3 p1, Vector3 p2, float cornerRadius, int cornerSegments = 4, float pointRotation = 11.25f)
    23.     {
    24.         Vector3 radiusVector = new Vector3(cornerRadius, cornerRadius, 0f);
    25.  
    26.         line.MakeCircle (p1 + radiusVector, cornerRadius, cornerSegments * 4, pointRotation);
    27.         Vector3 size = (p2 - (radiusVector * 2)) - p1;
    28.  
    29.         for (int i = 1; i <= cornerSegments; i++)
    30.             line.points3[i].x += size.x;
    31.  
    32.         for (int i = ((cornerSegments * 2) + 1); i <= (cornerSegments * 3); i++)
    33.             line.points3[i].y += size.y;
    34.  
    35.         for (int i = ((cornerSegments * 3) + 1); i <= (cornerSegments * 4); i++)
    36.             line.points3[i] += size;
    37.  
    38.         line.points3[0] += size;
    39.     }
    40. }
     
  49. ichini

    ichini

    Joined:
    Oct 13, 2012
    Posts:
    23
    Hi,

    just playing around with Unity 4's Blend Shapes. Wondering if there's a way to make these mesh deforms work with Vectrosity?

    Thanks + best,

    georg.-
     
  50. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    Looking at the code, I don't see any fundamental difference between Vector2 or Vector3 in terms of how the circle is calculated, so I would guess that which way is "backwards" for Vector3 depends on which way the camera is facing?

    You should be able to use VectorLine.sortingLayerID (and/or .sortingOrder) to control drawing order with sprites.

    I haven't actually used blendshapes, but I'm guessing Mesh.BakeMesh + MakeWireframe would work? Although it probably wouldn't be very fast.

    --Eric
     
Thread Status:
Not open for further replies.
unityunity