# Vectrosity - Fast and Easy Line Drawing

Discussion in 'Assets and Asset Store' started by Eric5h5, Sep 26, 2014.

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
You can use VectorLine.rectTransform.parent, after the line is drawn.

--Eric

2. ### foxes

Joined:
Oct 9, 2015
Posts:
10
You need more optimization, it's possible.
If you first replace WorldToScreenPoint with
Code (CSharp):
1. Vector4 v = (Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix) * new Vector4(tv.x, tv.y, tv.z, 1.0f);
2.         v.x = (v.x/v.z + 1.0f) * Camera.main.pixelWidth * 0.5f;
3.         v.y = (v.y/v.z + 1.0f) * Camera.main.pixelHeight * 0.5f;
and Camera.ScreenToWorldPoint with
Code (CSharp):
1.
2. Vector4 tv.x = (v.x * (2.0f / Camera.main.pixelWidth) - 1.0f) * v.z;         tv.y = (v.y * (2.0f / Camera.main.pixelHeight) - 1.0f) * v.z;         tv.z = v.z;         tv.w = v.z;         tv = (Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix).inverse * tv;
3.
some expressions can be taken out of the loop.
Code (CSharp):
1. (Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix)
2. (Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix).inverse
and to make some changes to the "if"
Code (CSharp):
1. if (useTransformMatrix) {
2.   for (int i = start; i < end; i += add) {
3.     p1 = thisMatrix.MultiplyPoint3x4 (m_points3[i]);
4.     p2 = thisMatrix.MultiplyPoint3x4 (m_points3[i+1]);
5.     ...
6.   }
7. }
8. else {
9.   for (int i = start; i < end; i += add) {
10.     p1 = m_points3[i];
11.     p2 = m_points3[i+1];
12.     ...
13.   }
14. }

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
I actually did something similar some time ago, and it made no performance difference. The fastest thing would be to have it done in a shader, but that prevents people from just being able to use whatever material they want, which has been pretty common.

--Eric

4. ### foxes

Joined:
Oct 9, 2015
Posts:
10
We tested this on larger arrays with a polygon of 15,000 positions on Unity 2018. The difference is 40mc and 20mc. Perhaps for some other tests it does not make a difference or it is a feature of the device. Аnd perhaps this difference is visible only in the Profiler

It is also possible that this gives a particular version of unity. Do you use any improvements in the compiler?

in fact, the transfer of calculations to the GPU does not give a big performance gain, because you have to transfer additional data. Filling in these data even without any calculations takes a lot of time.

Last edited: Jan 11, 2019

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
I don't see where I'd need to transfer additional data, though as I mentioned, doing it in a shader has other drawbacks.

--Eric

6. ### foxes

Joined:
Oct 9, 2015
Posts:
10
To implement this on the Shader side, it is not necessary to change all functionality. Can be done as an additional or alternative feature set.

But for the calculations on the Shader I needed to send some idea about the neighboring points of the line. Because for WebGl and other platforms, the geometric Shader does not work as well as some useful properties.

Joined:
Apr 13, 2014
Posts:
6
Looking for some help - I'm trying to use Vectrosity to draw lines on top of my 3d unity terrain object. I started by drawing onto a flat terrain - 1 unit above where the terrain is. I then moved the camera around, and noticed the lines appeared correctly from various angles, except that some portions clipped though the terrain mesh.

I don't understand why this is because every Vector3 that makes up the line floats above the terrain by 1 unit. At no angle should the corner of the line be below the terrain.

here is a GIF that shows what I'm seeing as I rotate around the location with the lines
What am I doing incorrectly here?

Code (CSharp):
1. var y = 16f;
2.             var s = 10;
3.             var line = new List<Vector3>(new[]
4.             {
5.                 new Vector3(0,y,0), new Vector3(0,y,s),
6.                 new Vector3(0,y,s), new Vector3(s,y,s),
7.                 new Vector3(s,y,s), new Vector3(s,y,0),
8.                 new Vector3(s,y,0), new Vector3(0,y,0)
9.             });
10.
11.             selectVectorLine = new VectorLine("selectVectorLine", line, 5f);
12.
13.             VectorLine.SetCamera3D(Camera.main);
14.             selectVectorLine.Draw3DAuto();
15.             selectVectorLine.joins = Joins.Weld;
16.             selectVectorLine.capLength = selectVectorLine.GetWidth(0) / 2f;
17.             selectVectorLine.material = this.Material;

UPDATE: It's this code line that is causing the odd behavior:
Code (CSharp):
1. selectVectorLine.joins = Joins.Weld;
Removing that, and seeing gaps in the joins between lines, fixes the issue.

#### Attached Files:

• ###### Vectrosity-Question-01.gif
File size:
3.7 MB
Views:
292
Last edited: Jan 13, 2019

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
Joins.Weld can alter the vertex position in order to look correct from the camera viewpoint, so try turning that off. Other than that, it could be a shader issue.

--Eric

Joined:
Apr 13, 2014
Posts:
6
Thank you!

10. ### Re2Go

Joined:
Jan 17, 2019
Posts:
7
@Eric5h5 Do you mind checking the emails from your StarScene site? I have sent multiple emails asking for you to send the download links to the email address associated with my account (detailed in the emails I sent you). I would like to use the software I purchased from you, surprise surprise.

If you are unwilling, or for some reason unable to send me the downloads for something I paid for, at least tell me for crying out loud. (Maybe I forgot the correct account email, which would be on me. But it would be nice to get a response.).

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
Hi, there's nothing from you, assuming you're using a similar name. I've been in communication with several customers using that email this week, so I know it's working. There's also no payment from you that I can see. If you bought from the Unity asset store, you don't need any download links from me, and can use the store download functions. If you want to give me more information that could help resolve this, I'd suggest using a PM.

--Eric

12. ### Re2Go

Joined:
Jan 17, 2019
Posts:
7
Update: eric5h5 helped me over PM. Excuse my fed up tone (I had been waiting for a response for weeks lol) and get back to discussing lines

13. ### Nosaru

Joined:
Feb 26, 2017
Posts:
11
Same problem as JRayP, sent support mail via the website. Waited 2 days... no answer. Sent a direct email, waited 2 days.... nothing.

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
I just got a few days' worth of mail this morning, so it looks like something got backed up.

--Eric

Joined:
Feb 26, 2017
Posts:
11

16. ### 5thPartial

Joined:
Dec 20, 2014
Posts:
2
Hi, I have 2 small questions.

1. I'm making a grid using one discrete Vectorline. I want to make the entire grid one translucent color, but when I try, the parts where the lines overlap end up like this:

How can I make the overlapping parts remain the same color as the rest of the grid?

2. I'm having trouble getting the free anti-aliasing to work with draw3D lines. I'm using a material with Unlit/Transparent shader and ThickLine texture, but the lines still look jagged. Is there any way to fix this?
Thanks!

File size:
17.7 KB
Views:
243

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
1. That could be done with a custom shader, though I don't have any examples offhand. Other than that, you could construct the line so as to prevent overlapping, where either the horizontal or vertical lines would be non-continuous and skip over the intersections.

2. Did you assign the texture to the line? Since with Vectrosity, the material and texture are separate. Looks like that combo does work:

--Eric

18. ### 5thPartial

Joined:
Dec 20, 2014
Posts:
2
I was able to fix the anti-aliasing to work! But now I can't change the color of the line. Looks like I'll have to look into shaders for both my problems...
Anyway, thanks for the help!

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
Line segment colors require a shader that uses vertex colors.

--Eric

20. ### Nosaru

Joined:
Feb 26, 2017
Posts:
11
So I have this weird bug....
I am drawing a VectorLine with
VectorManager.useDraw3D = true;

I then enable and disable it using
line.active = false;

And everything is fine, however.... When I turn my camera away from where the VectorLine shape was and then look back at it, it comes back
This is happening because it Dynamically activates and deactivates the lines when out of sight.

How do I get the newly created Vectorline gameobject so that I can toggle SetActive on it? That would solve the problem I think.

err..... Help?

Last edited: Feb 10, 2019

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
Not a bug...when you're using VectorManager, Vectrosity is like a renderer, so setting the GameObject active or not is what controls the line rendering being disabled. If you don't want that, then you should create lines directly and not use VectorManager.

--Eric

22. ### Nosaru

Joined:
Feb 26, 2017
Posts:
11
Its not a bug in Vectrocity, its a bug in my game...

Anyways I just realized that its a recTransform not a gameObject, thats y I couldn't pull it out from the VectorLine

Last edited: Feb 10, 2019
23. ### keez-duyves

Joined:
May 25, 2015
Posts:
3
Hi Eric,

I was trying to import a .obj file with polylines into Unity.

This is not possible, similar to:

Dreamora suggested to use your asset (which looks great).
My question - is Vectrocity capable of importing any poly line formats or do I have to script some import ?
Couldn't find this in the description or forum..

Thanks,
Keez

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
Hi, Vectrosity doesn't import anything, so you'd need an importer.

--Eric

25. ### apexsoftworks

Joined:
Oct 5, 2016
Posts:
29
Hey Eric,

Quick question. I'm storing a VectorObject3D in a class. In another class I'm changing the points and calling Draw() on that VectorObject3D.

The issue I have is that after I call Draw(), the gameObject gets destroyed. Does redrawing a line cause the GameObject to be replaced by a new one? If so, how do I keep track of my VectorObject3D while redrawing it each frame?

Note: I'm able to set the points3 but when I call Draw3D() the gameObject seems to get destroyed. Weird.

Thanks.

Last edited: Feb 14, 2019

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
Calling Draw won't cause a GameObject to be destroyed. Certain components can be created/destroyed if you change some line options and re-draw, but in normal use nothing is created/destroyed and no garbage is created. I'd recommend storing VectorLine in a class, and calling Draw3D on that.

--Eric

27. ### circus3d

Joined:
Nov 9, 2016
Posts:
3
Hi there. I have a need to render a rather complex network of road centerlines in 3D space in as few draw calls as possible. Ideally rendering them as a line with some width. My dataset is a series of individual line segments (eg. two verts per line). I have approximately 13,000 line segments in this dataset below. Due to the way the data is structured and where it is coming from (geojson), I don't have the luxury of welding adjacent lines together. I'm not too worried about joining segments. Is Vectrosity the right tool for this from a ease and performance point of view (if I was to use Unity Line segments, I'd need to have 13,000 individual game objects rendering each segment)

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
Hi, you can do that in Vectrosity with a single object. Lines can either be continuous or discrete, and for discrete lines, the segments are defined separately, though still with a single list. Just supply a list of points and Vectrosity will draw the line. e.g. (0, 0, 0), (10, 0, 0), (0, 5, 0), (0, 15, 0) as a discrete line would draw a segment from 0-10 on the X axis, and another from 5-15 on the Y axis. There's a max of ~16K segments per line. Each segment can also have its own width, though it looks like in this case the width would be uniform.

--Eric

29. ### circus3d

Joined:
Nov 9, 2016
Posts:
3
Thanks for the update Eric. I've purchased and tested and appears to be working well. One question that I couldn't see answered in the documentation. Is there an option to set the up direction for the initial render of lines in Draw3D? It looks like by default all lines are drawn to face the camera, however in my use case I just need all lines facing a consistent up direction (eg. Vector3.up). I can see MakeCircle has an up vector option, so I guess I'm looking for something similar for regular lines. Cheers

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
The up vector for MakeCircle is for the circle itself, not the lines. They're meant to face the camera and there isn't strictly an option to change that, however a sort of hack is to use another camera with SetCamera3D to make the lines face that (such as a camera pointing straight down).

--Eric

31. ### labandunga

Joined:
Aug 1, 2012
Posts:
28
just wanna say how impressive it is that you've been supporting this package over the years, with very fast responses.

Thanks Eric.

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
You're welcome, glad I can help!

--Eric

33. ### apexsoftworks

Joined:
Oct 5, 2016
Posts:
29
Yes, also thanks for answering my question the other day regarding objects being destroyed as it helped me locate my bug.

34. ### th3flyboy

Joined:
May 28, 2016
Posts:
7
How does one create a line between two moving game objects using vectrosity?

I'm trying to create a force graph and I'm having problems figuring out how to update the position of the line render in 3d when the game update tick happens.

This is a relevant snippet showing what I'm doing.

Code (CSharp):
1. // Use this for initialization
2.     void Start()
3.     {
5.         springJoint1.connectedBody = startGameObject.GetComponent(typeof(Rigidbody)) as Rigidbody;
6.
8.         springJoint2.connectedBody = endGameObject.GetComponent(typeof(Rigidbody)) as Rigidbody;
9.
10.         vl = VectorLine.SetLine3D(lineColor, startGameObject.transform.position, endGameObject.transform.position);
11.         vl.Draw3DAuto();
12.     }
13.
14.     // Update is called once per frame
15.     void Update()
16.     {
17.         // Check if the GameObjects are not null
18.         if (startGameObject != null && endGameObject != null)
19.         {
20.             vl.points3[0] = startGameObject.transform.position;
21.             vl.points3[1] = endGameObject.transform.position;
22.             vl.Draw();
23.         }
24.     }
25. }
26.

Last edited: Feb 25, 2019

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
You're mixing a lot of draw commands...SetLine3D already does Draw3DAuto, so you should remove the Draw3DAuto call (and when using Draw3DAuto, don't call Draw manually). It's enough to just call SetLine3D, then update the two points in Update.

--Eric

36. ### th3flyboy

Joined:
May 28, 2016
Posts:
7
Thanks, I appreciate it. What function do I use to update the two points in the update? I'm trying with vl.points3 and it doesn't seem to be working.

*edit, it works now. Turns out I needed to keep draw3dauto in the start loop and get rid of all draw calls in update except for vl.points3.

Last edited: Feb 25, 2019

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
If you're using SetLine3D, get rid of Draw3DAuto. Since the line is updated every frame, all you need to do is update the points.

--Eric

38. ### apexsoftworks

Joined:
Oct 5, 2016
Posts:
29
I have a VectorLine with several vertices. I want to subdivide one of the edges of the VectorLine into 2 lines right down the middle. What's the best way to go about doing this?

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
Subdivide which way, length-wise or cross-wise?

--Eric

40. ### apexsoftworks

Joined:
Oct 5, 2016
Posts:
29

I think I have it pretty much figured out. I'm copying the list of points in the line, creating a new point located at the center point of the two vectors, then inserting that new point into the list, then setting the vectorline's list of points equal to the new list, then calling Draw.

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
You can avoid most of those steps just by inserting the point in the line points list directly, no need for copying anything.

--Eric

42. ### apexsoftworks

Joined:
Oct 5, 2016
Posts:
29
Great point. Thank you.

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
I'd probably need to see some code to make any guesses, as I've never seen anything like this. Are they separate lines? Is there anything funky going on with cameras?

--Eric

44. ### apexsoftworks

Joined:
Oct 5, 2016
Posts:
29
Hi Eric,

I have an issue with mesh colliders on my VectorLine3D. The line has a mesh collider but I don't actually see the green border unless I manually enable then disable the collider in editor. Any ideas? I tried to enable and disable the collider in code and that didn't solve it.

Also, I just noticed that we are doing an AddComponent on the line for a mesh collider. I just tried to use line.collider = true and it gave the error "There is no collider attached to *gameobject name here*". According to the docs it looks like that only works for 2D lines. Is adding a mesh component the best way to go about adding collision to 3D lines?

Thanks,

- Tate

Last edited: Feb 28, 2019

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
Do you mean a polygon collider or edge collider? Line don't have mesh colliders. You can use line.collider for 3D lines, but the collider will always be a 2D polygon or edge collider. You can try adding a mesh collider manually, but due to the nature of how lines are made, I can't see it working very well.

--Eric

46. ### metaphysician

Joined:
May 29, 2012
Posts:
128
hi Eric! i'm thinking of buying this tool, but i have a pretty specific use case. i want to be able to enable a user to draw a waveform line that progresses horizontally, and then be able to read back that waveform data at any time position. so i need some kind of way of editing and reading back data values horizontally and vertically. it seems the demos allow the drawing option, but not sure of the data reading/writing situation. is this possible at all?

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
Vectrosity just takes a list of Vector2 or Vector3 data and draws a line from that, so it seems like that's your waveform data right there. Thank you for your interest!

As a side note, I think that's the most common misunderstanding with Vectrosity: you just have a standard List and that's it. No SetPosition functions or anything are needed, it's just...a List. Do your usual Add/Remove/Insert/Sort/Whatever directly and it's fine. (Well, there is a GetPoint function if you want to interpolate between points in the list.)

--Eric

48. ### smalleye

Joined:
Apr 18, 2018
Posts:
9
Hi Eric,

I finally fixed the bug by myself. It's a bug in my own code, not in your library.
So I deleted my post in order not to confuse others.

Thank you very much for the support!

49. ### am_jooz

Joined:
Jun 12, 2018
Posts:
7
Hi All,
I have issues with line flickering and I wondered if I could get some help here.
I am trying to draw 3D lines in a scene. My scene is a very large plane and my object moves on this plane. The lines I want to draw are supposed to show boundaries on this plane, a bit like lines we could have on a football field. When my object is very close to the line, it works ok, but when I am far away (and I guess the angle to the line is very small) I have a lot of flicker.
To have the lines well drawn on the plane, I setup my camera as orthographic pointing perfectly down.

My code is the following:
Code (CSharp):
1.     private void DrawBoundary(List<Vector3> vertices)
2.     {
3.         VectorLine.SetCamera3D(LinesCamera);
4.         VectorLine line = new VectorLine("Boundary", vertices, widthBoundaries, LineType.Continuous, Joins.Fill);
5.         line.SetColor(colorBoundaries);
6.         line.Draw3DAuto();
7. }
8.
Any idea how I could remove the flickering?

Thanks a lot for your help!

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,238
Probably z-fighting, in which case you can move the lines farther from the plane, or use a shader that draws lines always on top.

--Eric

unityunity