Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes, you need to define some kind of Z position, 0 won't work.

    --Eric
     
  2. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    i did this:

    Code (csharp):
    1.     originVector=Vector3(0,0,1);
    2.     destinationVector=Vector3(1,10,1);
    i guess you are reffering to vectors and still the same thing happens, line draws far away from the camera view.
    in the hierarchy view there is VectorSetLine3D with zero transformation
    and there is VectorCam which has the position x:398.5 and y:239.5

    at this point line draws where it should be, as soon as i touch the screen line dissapears but in the scene view i see
    that it draws correctly on the input but at the wrong position.

    also the thing i noticed is that if i changed slightly z position of my camera (not VectorCam) line appears where it should be. so i guess z is a problem? any ideas how to make it work as it should be?

    thanks eric!
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I mean in ScreenToWorldPoint.

    --Eric
     
  4. appleunited

    appleunited

    Joined:
    Dec 25, 2010
    Posts:
    103
    I am unable to run the demo. It is prompting to download the unity3d webplayer plugin which I already have it installed. Or does it need an older version of the webplayer to run?
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Which demo are you trying to run? Some of them were made with Unity 2.6, some with Unity 3.x. Theoretically it should just automatically use the appropriate plugin, however it seems at least with Safari that this doesn't necessarily always happen. You can try a different browser; e.g. with Firefox that doesn't seem to be an issue.

    --Eric
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    appleunited: I see this with some of my webprojects when I'm using Safari (Mac), but not when using Firefox. I've had one session with UT TS, and stumped them... so it's not a trivial issue. So far it seems to be limited to 2.x content in Safari browsers... but I've not done extensive testing.
     
  7. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    is it possible to have antialiasing lines in android with vectrosity?
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I assume standard antialiasing is possible with Android like it is with iOS, so yes, but aside from MSAA there's also the "line texture with transparent edges" trick that will always work on any platform regardless of AA settings.

    --Eric
     
  9. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    Whats the best way performance wise to update a line following an object in Update?
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Update the point(s) for the VectorLine and call Vector.DrawLine in LateUpdate (to make sure the object in Update has moved first).

    --Eric
     
  11. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    So this is what I have now

    Code (csharp):
    1. public void Update()
    2.     {
    3.         if (draw)
    4.         {
    5.             if (myTransform  myTrail != null)
    6.             {
    7.                 myTrail.points3[1] = myTransform.position;
    8.                 Vector.DrawLine(myTrail);
    9.             }
    10.         }
    11.     }
    it should be this?

    Code (csharp):
    1. public void Update()
    2.     {
    3.         if (draw)
    4.             if (myTransform  myTrail != null)
    5.                 myTrail.points3[1] = myTransform.position;
    6.     }
    7.    
    8.     public void LateUpdate()
    9.     {
    10.         if (draw)
    11.             if (myTransform  myTrail != null)
    12.                 Vector.DrawLine(myTrail);
    13.     }
     
  12. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    when I change it to that Im still getting about a 23% hit in LateUpdate in the profiler
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, take your first version and change Update to LateUpdate. Splitting the code between Update and LateUpdate doesn't accomplish anything; the point is to get transform.position after whatever else is moving the object has finished.

    --Eric
     
  14. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    alright thanks!
     
  15. masterchafe

    masterchafe

    Joined:
    Oct 2, 2011
    Posts:
    59
    Just picked up Vectrosity this week, and having plenty of fun with the examples, its great to see so many! I'm currently playing with the SplineFollow3d example and was wondering; how would I go about getting the cube to also orient to the line while it is following it around?
     
  16. Xantec

    Xantec

    Joined:
    Aug 2, 2011
    Posts:
    39
    How can I replicate exactly the Debug.DrawLine. Ive looked through the documentation but I cant get it to work correctly in this loop.
     
  17. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That might be rather tricky; I think to do it properly you'd need to store normals for each point on the line, rather than just have a series of points. I can't think of a way offhand to do it easily with the built-in functions I'm afraid.

    SetLine or SetLine3D; see pages 3 and 4 in the docs.

    --Eric
     
  18. Xantec

    Xantec

    Joined:
    Aug 2, 2011
    Posts:
    39
    I need it to render for a single frame then disappear/delete.
     
  19. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can do

    var line = SetRay
    yield
    DestroyLine(line)

    --Eric
     
  20. wbl1

    wbl1

    Joined:
    Apr 22, 2009
    Posts:
    159
    Eric,

    Creating a simple display with lines and points (code below). I am getting the following error. Any thoughts?

    "Shader wants normals, but the mesh Points doesn't have them"

    Code (csharp):
    1. var lineMaterial : Material;
    2. var controlPointMaterial : Material;
    3. private var line : VectorLine;
    4. private var points : VectorPoints;
    5. private var linePoints : Vector2[];
    6.  
    7. function Start ()
    8. {
    9.     var vectorCam : Camera = Vector.SetCamera(camera,true);
    10.     vectorCam.transform.position=Vector3.zero;
    11.     vectorCam.orthographicSize=120;
    12.     linePoints = new Vector2[5];
    13.     linePoints[0] = Vector2(0,50);
    14.     linePoints[1] = Vector2(100,50);
    15.     linePoints[2] = Vector2(130,35);
    16.     linePoints[3] = Vector2(130,0);
    17.     linePoints[4] = Vector2(0,0);
    18.     SetLine();
    19. }
    20.  
    21. function SetLine () {
    22.     Vector.DestroyLine(line);
    23.     line = new VectorLine("Line", linePoints, Color.yellow, lineMaterial, 2.0, LineType.Continuous, Joins.None);
    24.     points = new VectorPoints("Points", linePoints, controlPointMaterial, 4.0);
    25.     Vector.DrawPoints(points);
    26.     Vector.DrawLine(line);
    27. }
     
  21. wbl1

    wbl1

    Joined:
    Apr 22, 2009
    Posts:
    159
    Regarding last thread, I see that shader Unlit does the trick ...
     
  22. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yeah, as the error implies, you need to use a shader which doesn't use normals.

    --Eric
     
  23. wightwhale

    wightwhale

    Joined:
    Jul 28, 2011
    Posts:
    397
    Hi Eric,
    First off I would like to say Vectrosity is great and your documentation was some of the best documentation I've seen for any product anywhere. I've run into my first real problem recently, I'm having a 3D line update which is killing my performance on ios. I'm getting hit pretty hard on the CreateVBOs() call. I think I may be abusing something about the Vectrosity and maybe you could recommend the proper way to do what I'm trying to do. Basicly I'm trying to draw a line hanging from a balloon.

    Code (csharp):
    1.  
    2.     VectorLine line;
    3.     Vector3[] vecArray = { Vector3.zero, new Vector3 (0, -.75f, 0), new Vector3 (0, -.75f, 0), new Vector3 (0, -1.5f, 0) };
    4.     Material lineMat;
    5.     float lineWidth = 3.0f;
    6.     ConfigurableJoint cj;
    7.    
    8.     void Start ()
    9.     {
    10.         lineMat = (Material)Resources.Load ("Color Line", typeof(Material));
    11.         cj = GetComponentInChildren<ConfigurableJoint> ();
    12.    
    13.         line = new VectorLine ("balloon line", vecArray, lineMat, lineWidth);
    14.     }
    15.  
    16.    
    17.     void LateUpdate ()
    18.     {
    19.         if (cj.connectedBody != null  cj.connectedBody.gameObject.active)
    20.         {
    21.             Vector3 stringVec = cj.connectedBody.position - rigidbody.position;
    22.            
    23.             //bot of line
    24.             line.points3[0] = cj.connectedBody.position;
    25.             line.points3[1] = cj.connectedBody.position - stringVec / 2f;
    26.             line.points3[2] = cj.connectedBody.position - stringVec / 2f;
    27.             //top of line
    28.             line.points3[3] = rigidbody.position;
    29.            
    30.             Vector.DrawLine3D (line);
    31.             //;, transform);
    32.         }
    33.         else
    34.         {
    35.             line.points3[0] = Vector3.zero;
    36.             line.points3[1] = new Vector3 (0, -.75f, 0);
    37.             line.points3[2] = new Vector3 (0, -.75f, 0);
    38.             line.points3[3] = new Vector3 (0, -1.5f, 0);
    39.            
    40.             Vector.DrawLine3D (line, transform);
    41.         }
    42.     }
    43.    
    44.  
     
  24. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    3D lines are slower than 2D lines, however I still get pretty good performance here. Your code looks fine to me. What device are you running it on? Is it possible that this script is attached to many objects?

    --Eric
     
  25. wightwhale

    wightwhale

    Joined:
    Jul 28, 2011
    Posts:
    397
    It's running on the ipad, it seems to me that the line mesh is getting rebuilt on every late update.
     
  26. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Which iPad? (Although even the iPad 1 should be OK.) The vertices for the line mesh are uploaded when calling DrawLine3D, but that's about it.

    --Eric
     
  27. wightwhale

    wightwhale

    Joined:
    Jul 28, 2011
    Posts:
    397
    Ipad 1. I thought it might be a problem with the shader or the material but it doesn't seem to matter what it is set as. I'm pretty sure this is a bug in unity which is supposed to be fixed in the next patch. I guess I can try and update it less frequently to save some performance.
     
  28. wightwhale

    wightwhale

    Joined:
    Jul 28, 2011
    Posts:
    397
  29. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Good to know, thanks...I was running out of ideas. :)

    --Eric
     
  30. masterchafe

    masterchafe

    Joined:
    Oct 2, 2011
    Posts:
    59
    I think I just realised how I could do it. I'm going to put another object on the line a little bit further along the dist value and look towards its position. Thanks for Vectrosity again, its been a real inspiration-shot in the arm!
     
  31. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I actually originally wrote that you could use the current point and the next point on the line for a directional vector, but then I realized that there's no concept of what the up vector should be. Maybe that would work well enough in some cases though.

    --Eric
     
  32. danien

    danien

    Joined:
    Jun 16, 2009
    Posts:
    71
    Hi Eric, I'm trying to do something similar except using maxDrawIndex. According to the documentation, the parts of the line that was previously drawn won't be erased.

    Basically, I allocate an array of points and use them with Vectrosity to draw a flight path (DrawPoints with transform) that is attached to a space ship. As the length of the flight path changes (not every frame), I modify the points and use maxDrawIndex to tell Vectrosity that I only want the points up to that index, to avoid reallocating a new array. However, the segments/points after the new maxDrawIndex are not erased, as per the docs. Any way to actually erase those? (I've tried ZeroPointsInLine but that just draws the points in the center of my object, as expected.)

    Thanks.
     
    Last edited: Feb 2, 2012
  33. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    Hey Eric,

    vectrosity is pretty awesome, just picked it up. I've tweaked some code I found that you wrote to sample the position of a rigid body and draw a line for it's trajectory. I'm using this on these sphere prefabs in this scene.

    the problem:



    when I instantiate one or duplicate one in the editor it creates that fast moving streak before drawing the line, any idea what causing that behavior?
     
  34. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Not quite sure I follow that...is this like line-drawing, except the line is controlled by the flight path of the space ship? If so, the DrawLinesMouse example script shows the basic concept.

    I see it's connecting to the same point every time, although apparently not Vector2.zero, which is interesting. I would guess there's some initial data in the points array which gets overwritten after the first frame?

    --Eric
     
  35. danien

    danien

    Joined:
    Jun 16, 2009
    Posts:
    71
    Not quite.

    Let's say I draw 4 points horizontally.

    . . . .

    Next, I set minDrawIndex from 0 to 1 and then call DrawPoints again. All 4 points are still visible, as the documentation states that anything drawn previously is not erased. How I can get the 1st point to be erased while keeping the other 3 points in the same position as shown below without reallocating a new array of points or new VectorPoints/VectorLine?

    _ . . .

    where the _ indicates nothing should be seen there (previous point is erased).

    On a somewhat related note, I think GetLineLength() and GetLinePoint*() does not seem to check minDrawIndex and maxDrawIndex. Is this by design?

    Thank you.
     
  36. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can set the first point to Vector2.zero (or Vector3.zero).

    Yes, minDrawIndex and maxDrawIndex are for drawing only and don't affect the contents of the line.

    --Eric
     
  37. arkon

    arkon

    Joined:
    Jun 27, 2011
    Posts:
    1,122
    Hi Eric5h5

    I've recently bought Vectrosity and all wokrs great, but I need to draw 2d lines behind GUITextures. I can't find how to do it in the docs, although I've probably just missed the info.

    To sum up, I have a 3d plane at a Z of 0 with GUITextures on top, but I want the lines drawn on top of the Plane but under the GUITextures.

    Thanks.
     
  38. danien

    danien

    Joined:
    Jun 16, 2009
    Posts:
    71
    Hi Eric, I think my understanding of how this works is fundamentally flawed. I've uploaded a unitypackage with an example to explain how I'm thinking about this and hope you can help me understand this better.

    In the DrawIndexTest scene, I create 4 points (index 0, 1, 2, 3) for a line that should have 3 segments. I set the minDrawIndex to 1 and maxDrawIndex to 2 (via the Inspector) to try to draw only the middle line segment. When I call DrawLine, I was expecting to see . .-. . but instead, it shows .-.-. .

    I'm not sure why it's drawing the line segment from points 0-1 as well. Can you help to clarify this for me? Thank you, and apologies for taking up more of your time.
     

    Attached Files:

  39. arkon

    arkon

    Joined:
    Jun 27, 2011
    Posts:
    1,122
    I had a simular problem, but it turned out to be a bug in my logic. I was calline DrawLine for 1 frame without the correct points set up.
    Double check your logic.
     
  40. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's a bug in my logic in this case. ;) Specifically, the Line3DContinuous function. Line 670 (assuming Vectrosity 1.5) of Vector.cs should read:

    Code (csharp):
    1.         var pos2 = doTransform? cam3D.WorldToScreenPoint(thisMatrix.MultiplyPoint3x4(line.points3[line.minDrawIndex])) :
    2.                                 cam3D.WorldToScreenPoint(line.points3[line.minDrawIndex]);
    Sorry about that...apparently I missed that particular combination when testing, since minDrawIndex works properly with the other functions.

    --Eric
     
  41. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I removed a post that was off-topic. Please post only Vectrosity-related question in this topic, thanks.

    --Eric
     
  42. danien

    danien

    Joined:
    Jun 16, 2009
    Posts:
    71
    That fixed it. Thanks, Eric.

    I've uploaded a new version of the test sample to show my original problem. When it first runs, it draws the middle segment (points index 1-2) of the line. This shows . .-. . correctly after applying your fix.

    What I would like is to draw the next segment (points index 2-3), while erasing the current segment (points index 1-2) after I call DrawLine again. I've added a GUI button to increment the minDrawIndex and maxDrawIndex for this. So, I would like it to show . . .-. but instead, it draws the new segment but still shows the previous segment . .-.-.

    Technically, this is as per your documentation for minDrawIndex and maxDrawIndex, which says that " If the line has been drawn previously, the segments before minDrawIndex are untouched, rather than being erased."

    Could you recommend how I could achieve the behaviour I want, without having to call DestroyLine and recreate it whenever minDrawIndex or maxDrawIndex changes? Thanks.
     

    Attached Files:

  43. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You would set the desired points to Vector3.zero and re-draw the line, before changing minDrawIndex. Although I actually wouldn't bother with minDrawIndex and maxDrawIndex unless you have a lot of points. The DrawLinesMouse.js example illustrates that well, where you could potentially have thousands of points, and it would be kind of a waste to re-compute every point of the line when the line is updated, when only the last segment is actually changing.

    --Eric
     
  44. danien

    danien

    Joined:
    Jun 16, 2009
    Posts:
    71
    Ah, got it. Thanks again for your help and for a great tool, Eric!
     
  45. Dev.D1

    Dev.D1

    Joined:
    Dec 29, 2010
    Posts:
    99
    Hi Eric,

    We got Vectrosity a few weeks ago, but I've started playing with it just now. Pretty nifty ! Now for the issues :)

    Problem:
    It seems like the UVs for the line geometry are not being calculated correctly if Joins.Weld option is chosen. This might need some extra vertices around the corners to fix.

    Code (csharp):
    1. var lineMaterial : Material;
    2. var lineWidth = 8.0;
    3. var textureScale = 1.0;
    4.  
    5. function Start () {
    6.    
    7.     // Make a VectorLine object using the above points and material
    8.     var line = new VectorLine("line", new Vector2[5], Color.white, lineMaterial, lineWidth, LineType.Continuous,Joins.Weld);       
    9.     Vector.MakeRectInLine (line, Vector2(100, 100),Vector2(200, 200));
    10.  
    11.     // Draw the line and set the texture scale
    12.     Vector.DrawLine(line);
    13.     Vector.SetTextureScale(line, textureScale);
    14. }
    Just associate the "DotLine" material from Vectrosity samples to see the UV distortion . Enclosing screencaps of Joins.Weld and Joins.Fill as comparison. What do you think?
     

    Attached Files:

  46. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The UVs are actually correct as they can be given the geometry; the same thing happens if you recreate the mesh in other apps like Blender. Unfortunately "fixing" it creates other problems; in most cases it was best to leave it this way (so yes, I've been through this already ;) ). It will look better if you add some more points for each side of the rectangle, so that the texture isn't stretched over only 2 triangles, although that does mean making your own function rather than using MakeRectInLine.

    --Eric
     
  47. Dev.D1

    Dev.D1

    Joined:
    Dec 29, 2010
    Posts:
    99
    Thank you for your reply. That was exactly what I was suggesting in my initial post. I've introduced 2 points close to the each corner of the rectangle, one on each side. I have to say though I'm not really very happy with the initial result. I guess what I'm after is similar to the corner split sprites that most UI solutions use to render dynamically re sizable smooth rectangles. The trick is to get a consistent UV layout in the side as well as the corner sections, while not adding enormous number of geometry.
     
  48. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It would certainly be possible to create a better result by building a mesh specifically for rectangles, but that geometry wouldn't apply to any other shape.

    --Eric
     
  49. Dev.D1

    Dev.D1

    Joined:
    Dec 29, 2010
    Posts:
    99
    Now that you mention it, I think I have solved this before using, for want of a better word, an adaptive tessellation scheme . You're doing the same for curves, where, I think you're creating more polygons where the derivative / slope changes more drastically.

    I have used this to create procedurally created roads geometry where it was imperative to get the UVs flowing correct. Otherwise the road markings applied via texture(s) would be distorted. I distinctly remember there where some special case considerations with perpendicular roads and multi road junction - but I digress.

    I'll sort this mess out in my head first, will share if I find something interesting. :)
     
    Last edited: Feb 15, 2012
  50. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The number of polygons is always the same. This is in the interest of speed, because rebuilding the mesh every frame is much slower than simply updating the vertices.

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