Search Unity

★ LINEFY ★ GPU powered drawing: lines ╳ polylines ≋ dots ⠇

Discussion in 'Assets and Asset Store' started by polyflow3d, Apr 23, 2020.

  1. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    Hello.

    When an opaque material is used (transparent = false), sorting cannot be controlled. The object is always drawn based on its distance from the camera.

    For transparent = true use the property .renderOrder. It is an wrapper for material renderQueue

    There is also a .viewOffset parameter. It actually shfts in or out of the geometry along the from the camera view direction, so you can avoid surface intersection. How this property works is clearly seen in the demo scenes with polygonal meshes. There, transparent lines are drawn on top of the standart material with small amount of .viewOffset.
     
  2. oddreams

    oddreams

    Joined:
    Dec 3, 2016
    Posts:
    17
    Ok, thanks for the helpful information.

    One more question: How can one disable the line at once. Is there a similar method as Draw(), except for disabling/clearing the line?
     
  3. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    Set its vertices width = 0 . SetWidth(lineIdx, 0,0)
    This may seem inconvenient at first, but it will avoid memory allocation.
    May I ask for what purpose you need to hide some lines?
     
  4. oddreams

    oddreams

    Joined:
    Dec 3, 2016
    Posts:
    17
    The lines are dynamic, and are part of a bigger system. Some get activated, some get deactivated at times.
    Can I set count = 0 ?

    And btw, I am trying some code right now, where I initially set a default capacity for a Polyline (using constructor), then during runtime I need more points, then I have:
    polyLine.Count = newCount
    // Set points...
    polyLine.Draw();

    But this seems to not change the capacity nor the count. In fact the line is not even drawn. I managed to draw lines before using simple API from your documentation, but for some reason this is not giving any visible output (width != 0, color != transparent, widthMultiplier = 1, feather = 0, isClosed = false, transparent = true)
     
    polyflow3d likes this.
  5. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    Do you want to dynamically hide the lines or points of the polyline? I did a little test about visibility management.



    scripts
     
    Last edited: Mar 18, 2021
  6. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    copy these scripts here...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Linefy;
    5.  
    6. [ExecuteInEditMode]
    7. public class PolylineVisiblityTest : MonoBehaviour
    8. {
    9.     public int count;
    10.     public bool[] visiblity;
    11.  
    12.     Polyline polyline;
    13.  
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         if (polyline == null || polyline.count != count) {
    19.             polyline = new Polyline(count);
    20.             for (int i = 0; i<polyline.count; i++) {
    21.                 float xPos = i * 0.1f;
    22.                 polyline[i] = new PolylineVertex(new Vector3(xPos, 0, 0), Color.white, 1);
    23.             }
    24.             polyline.widthMultiplier = 4;
    25.         }
    26.  
    27.         if (visiblity == null || visiblity.Length != count) {
    28.             visiblity = new bool[count];
    29.             for (int i = 0; i < visiblity.Length; i++) {
    30.                 visiblity[i] = true;
    31.             }
    32.         }
    33.  
    34.         for (int i = 0; i<polyline.count; i++) {
    35.             polyline.SetWidth(i, visiblity[i] ? 1 : 0);
    36.         }
    37.  
    38.         polyline.Draw(transform.localToWorldMatrix);
    39.     }
    40. }

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Linefy;
    5.  
    6. [ExecuteInEditMode]
    7. public class LinesVisiblityTest : MonoBehaviour
    8. {
    9.  
    10.     public int count;
    11.     public bool[] visiblity;
    12.     Lines lines;
    13.  
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         if (lines == null || lines.count != count) {
    19.             lines = new Lines(count);
    20.  
    21.             for (int i = 0; i<count; i++) {
    22.                 float xPos = i * 0.1f;
    23.                 lines[i] = new Line(new Vector3(xPos, 0, 0), new Vector3(xPos, 1, 0), Color.white, 1);
    24.             }
    25.             lines.widthMultiplier = 4;
    26.         }
    27.  
    28.         if (visiblity == null || visiblity.Length != count) {
    29.             visiblity = new bool[count];
    30.             for (int i = 0; i<visiblity.Length; i++) {
    31.                 visiblity[i] = true;
    32.             }
    33.         }
    34.  
    35.  
    36.         for (int i = 0; i<lines.count; i++) {
    37.             lines.SetWidth(i, visiblity[i] ? 1 : 0);
    38.         }
    39.  
    40.         lines.Draw(transform.localToWorldMatrix);
    41.  
    42.     }
    43. }
    44.  
     
  7. oddreams

    oddreams

    Joined:
    Dec 3, 2016
    Posts:
    17
    Hey, thanks for the snippets. They are really helpful.

    Have you got an answer or any idea what I might be missing for the quoted situation too?
     
  8. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    If you do not see lines or part of the polyline, then here are the possible reasons:
    1) the points are in the same position.
    2) color alpha is zero
    3) the width is zero
    4) a texture with transparency is applied and all texture coordinates are in the transparent part

    To quickly check for transparency issues, turn on the display of the warframe and look at the lines in the editor window.
     
  9. oddreams

    oddreams

    Joined:
    Dec 3, 2016
    Posts:
    17
    Hmmm, all this is already there to be honest. I debugged each point.
    However I saw something: Is a single Draw() call in start enough to draw the line persistently?, or do we need to do a Draw() every single update? Because I am doing it every once in a while (as to not do unneccessary mesh rendering for a consistent state of line)
     
  10. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    Make sure to call Draw() every frame in Update() or LateUpdate() as long as you want it to be drawn )

    Also, do not forget about the [ExecuteInEditMode] attribute so that it is drawn in non-play mode.
     
  11. oddreams

    oddreams

    Joined:
    Dec 3, 2016
    Posts:
    17
    I see, thanks!

    Side question: Is there any implication in performance if such a not-changing mesh is redrawn every frame? I am saying this because I am targetting mobile devices.
     
    polyflow3d likes this.
  12. maciekfreak

    maciekfreak

    Joined:
    Sep 21, 2015
    Posts:
    23
    I'm trying to use Lines with very long distance between points and I have a problem with rendering of this lines. If I use orthographic camera everything is all right but in perspective mode it looks that line change it width or perspective with camera and are very very bold.

    I'm using just simple:
    Code (CSharp):
    1. new Line(new Vector3(0,0,-5000), new Vector3(0, 0, 5000), Color.red, 2) ;
     
    polyflow3d likes this.
  13. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    Hi, I have already solved this problem in a new version 1.1 that will be available soon.
     
  14. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    There is no CPU implication of Linefy.Draw() except frames during which properties are changed.
    If the parameters do not change, then absolutely all calculations are performed on the GPU
     
  15. alb-lab

    alb-lab

    Joined:
    Feb 2, 2019
    Posts:
    11
    Exactly what I need! Thank you!
    Just bought Linefy.
     
    polyflow3d likes this.
  16. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    Lars-Steenhoff likes this.
  17. UmbraFidelis

    UmbraFidelis

    Joined:
    Mar 16, 2015
    Posts:
    8
    Hey there, just bought this plugin, real neat stuff! But how would I go about creating dashed lines? I tried the example you posted earlier, but it seems to be broken, the texture does not repeat, it just stretches.
     
    polyflow3d likes this.
  18. UmbraFidelis

    UmbraFidelis

    Joined:
    Mar 16, 2015
    Posts:
    8
    Nevermind! I didn't set the texture wrapmode to repeat!
     
    polyflow3d likes this.
  19. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    You can also enable .autoTextureOffset for proportional uv-mapping.
     
  20. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    In Linefy there is an attribute for drawing Matrix4x4 inspector property . [Matrix4x4Inspector]

     
  21. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    This code example shows how to draw gizmo arrow connections between transforms.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Linefy;
    5.  
    6. public class ArrowConnection : MonoBehaviour
    7. {
    8.     [System.Serializable]
    9.     public class Connection {
    10.         public Transform a;
    11.         public Transform b;
    12.     }
    13.  
    14.     public Connection[] connections;
    15.     Lines clines;
    16.     public float width = 16;
    17.     public float texScale = 1;
    18.     public Texture tex;
    19.     public Color color;
    20.     public float margin = 0.2f;
    21.  
    22.     void OnDrawGizmos() {
    23.         if (clines == null) {
    24.             clines = new Lines(connections.Length);
    25.         }
    26.      
    27.         clines.count = connections.Length;
    28.         for (int i = 0; i< connections.Length; i++) {
    29.             clines.SetTextureOffset(i, 0, 0);
    30.             Vector3 _a = connections[i].a.position;
    31.             Vector3 _b = connections[i].b.position;
    32.             Vector3 m = (_a - _b);
    33.             float l = m.magnitude;
    34.  
    35.             m = m/l * margin;
    36.  
    37.             clines.SetPosition(i, _a + m, _b - m );
    38.             clines.SetTextureOffset(i, 0, l);
    39.         }
    40.         clines.widthMultiplier = width;
    41.         clines.texture = tex;
    42.         clines.textureScale = texScale;
    43.         clines.colorMultiplier = color;
    44.         clines.DrawNow(Matrix4x4.identity);
    45.      
    46.     }
    47. }
    48.  
    arrow texture https://polyflow.xyz/content/linefy/downloads/arrowTexture.png
     
    Last edited: Mar 23, 2021
  22. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    Linefy uses a reliable and stable algorithm for solving polyline corners.

     
  23. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
  24. UmbraFidelis

    UmbraFidelis

    Joined:
    Mar 16, 2015
    Posts:
    8
    Hi, is there a way to orient lines with a widthmode set to worldXY upwards world space? I want the lines not to distort based on the camera position, and worldXY widthmode seems to solve this, but I can't figure out how to align them properly.
     
  25. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    If I understand you correctly, you need to provide the transformation matrix that looks up its forward axis .
    .Draw( Matrix4x4.Rotate(Quaternion.Euler(-90,0,0)));
     
  26. UmbraFidelis

    UmbraFidelis

    Joined:
    Mar 16, 2015
    Posts:
    8

    Hey there, I figured out what the issue was, though I still don't understand it. The issue was that the camera in the scene was too close to the lines in world space, and when it would move so that the lines would exit the view (intersecting the view frustum), the lines would scale and shrink inwards, when they were supposed to be a static size.

    I had a zoom function in the scene that would adjust the orthographic size of the camera for a zoom effect, but it would also move the camera closer to a focus point at the same time, which was unnecessary. After putting the camera in a fixed position a sufficient distance away from the lines - and only adjusting the orthographic size of the camera when zooming - the line shrinking went away.

    I don't know if this is intentional or just a result of how drawing the lines work. Anyways, thanks for the fast reply, really liking Linefy so far!
     
    polyflow3d likes this.
  27. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    Hmm, that looks like a bug. It would help me a lot if I could see your scene. Could you send it to me by mail?


    Thank you so much!
     
  28. UmbraFidelis

    UmbraFidelis

    Joined:
    Mar 16, 2015
    Posts:
    8
    I can't send you the scene where I first encountered this in, but I'm setting up a similar scene around this issue right now, which mail should I send it to?
     
  29. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
  30. UmbraFidelis

    UmbraFidelis

    Joined:
    Mar 16, 2015
    Posts:
    8
  31. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    The line is clipped along the near plane of the camera. This is normal behavior, just like any other geometry. If you need special behavior of the line outside the camera, then you can draw it in the screen space.

    You use auto-texture offset to calculate texture coordinates based on line length. But the calculated texture coordinates will be summed up with those of the line structure.
    Assign texture coordinates 0,0 for the line if you want the automatic coordinates to exactly match its length.

    For your code it is


    Code (CSharp):
    1.             Vector3 _a = _origin.position - (_origin.position - _destination.position).normalized * _pointCutOffDistance;
    2.             Vector3 _b = _destination.position - (_destination.position - _origin.position).normalized * _pointCutOffDistance;
    3.             _lines[0] = new Line(_a, _b, Color.white, Color.white, _width, _width, 0, 0);
     
  32. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    I am using Linefy itself for debugging when developing a new version of Linefy :cool:
     
    Polaaa likes this.
  33. Polaaa

    Polaaa

    Joined:
    Sep 29, 2017
    Posts:
    18
    This looks cool! but I imported the script with texture in Unity 2020.3.0f and no line segments seem to be rendered
     

    Attached Files:

    polyflow3d likes this.
  34. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    Based on the screenshot, it look like the color is completely transparent?
     
  35. Polaaa

    Polaaa

    Joined:
    Sep 29, 2017
    Posts:
    18
    I found a bug that the selection box in the editor is misaligned when the zoom is not 100% in win 10 scale and layout greyed out.
    Also I have a question, how do I select an EditableLines in the Unity Editor Scene view instead of from the Hierarchy panel?


    Snipaste_2021-04-29_13-09-11.png Snipaste_2021-04-29_13-12-52.png
     
  36. Polaaa

    Polaaa

    Joined:
    Sep 29, 2017
    Posts:
    18
    Thanks for the tip! The line can now be rendered correctly, but the texture seems a bit strange
     

    Attached Files:

  37. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    Yes, I have already discovered this UI scaling bug, I will fix it shortly.

    Change the clamp parameters of the texture.
     
    Polaaa likes this.
  38. Betaman

    Betaman

    Joined:
    Mar 10, 2015
    Posts:
    4
    Hello, I would like to draw lines in the world space, over a plane. I'm lost about what parameters I should use in the Draw command or if NearClipPlaneMatrix is needed and how to use it (docs are a bit sparse about this). I checked the Clews example but could not figure out the use of transforsms there.
    Thanks for your help.
     
  39. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    Hi, NearClipPlaneMatrix is used to calculate the matrix that is in front of the camera (screen space).
    In the clews example, one polyline is created, and then it is drawn repeatedly using different matrices that describe the positions and rotation of the clews .

    To draw in world space, you need to provide an identity matrix or use empty Draw() overload . This way positions of polyline vertices will be dispayed in worldspace.
     
  40. Betaman

    Betaman

    Joined:
    Mar 10, 2015
    Posts:
    4
    That worked great, thank you very much. Very happy with Linefy.
     
    polyflow3d likes this.
  41. vanyfilatov

    vanyfilatov

    Joined:
    Dec 2, 2020
    Posts:
    33
    How to add rounded corners or custom background to labels?(solved with edited texture alpha)
    And how convert font to dot atlas?
    Why use anything other than a font at all?
    In which script is the texture generated that is assigned to the label material?
    Its not genereated at all?
     
    Last edited: Jun 30, 2021
    polyflow3d likes this.
  42. vanyfilatov

    vanyfilatov

    Joined:
    Dec 2, 2020
    Posts:
    33
    I have a TextToTexture script
    and there a regular font(TTF) generates a texture on request (RequestCharactersInTexture)
    with which you can use GetCharacterInfo to pick up "UV"for letters quads
    and not use a custom DotsAtlas texture. which need to be manual created and have limited size(font texture can have 256pix one letter size)
    The only problem is that not all fonts have a background square and I/you need to do something with it
    I can share script if needed
     
    polyflow3d likes this.
  43. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    I have not planned on adding non-square quads for the glyphs yet, as this makes setting up the font so difficult. Labels Renderer is not a replacement for other powerful text tools - but it does have such advantages:

    1) allows you to quickly set up any texture as a font,
    2) draw thousands of labels in one dracall without CPU load
    3) draw pixel perfect text

    To create a bitmap font I use two methods -
    1) draw texture in Photoshop from scratch

    or

    2) create a texture using Codehead-Bitmap-Font-Generator tool(you can also add effects in Photoshop to the resulting texture).

    To create rounded background draw rounded quad on onused space of atlas then define it indices in atlas settings.
    For example - in this font background located in left-top 3x3 rect space . You can find this atlas here \Assets\Plugins\Linefy\LinefyResources\ColoredFont

     
    vanyfilatov likes this.
  44. vanyfilatov

    vanyfilatov

    Joined:
    Dec 2, 2020
    Posts:
    33
    Ok. I understood.
    Another question is how to draw a polyline with an upward vector? Or different rotations in vertexes?
    To look up and not at the camera.
     
  45. vambier

    vambier

    Joined:
    Oct 1, 2012
    Posts:
    102
    Is it also possible to draw pixelated lines for 2D pixelart games like this:

     
  46. Innovine

    Innovine

    Joined:
    Aug 6, 2017
    Posts:
    522
    I have created a CircularPolyline and I notice when I move my scene camera away from the line by a small amount, my log window starts to fill up with:

    Expanding invalid MinMaxAABB
    UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)


    What is causing this? Is it related to the Gizmos.matrix in OnDrawGizmos, because if I remove that, and only draw in Update in play mode then the problem doesn't occur.
     
    Last edited: Sep 12, 2021
    polyflow3d likes this.
  47. Innovine

    Innovine

    Joined:
    Aug 6, 2017
    Posts:
    522
    What is boundSize, what is it used for? The documentation describes this as "The bound size."
    That's helpful.
     
    polyflow3d likes this.
  48. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    Bound size is a parameter that is responsible for the mesh's bound, if it is less than or equal to zero then the bound is calculated automatically ,based on object form, otherwise it is fixed and equal to the specified size. This (Expanding invalid MinMaxAABB) error usually occurs if the mesh is too far from the center of the world, or the bound is too large. Try to reduce the size of the bound. If this does not help, could you send me a package with the scene in which this error is observed?
     
  49. Innovine

    Innovine

    Joined:
    Aug 6, 2017
    Posts:
    522
    Is there some kind of clipping distance in the shader?

    My lines are at the world origin, and my camera is at -86603 on Z. The lines are drawn just fine.
    If I move the camera back more, to -86603.6 the line becomes faint, and at -86603.7 the lines are not drawn at all.
    The camera draw distance is set higher than this so the lines should be visible. Other objects at the origin are drawn ok.
    The lines are still drawn in the scene view, and visible by another camera which is not so far away.
     
  50. polyflow3d

    polyflow3d

    Joined:
    Oct 6, 2014
    Posts:
    297
    Could you send on polyflow3d @ gmail.com the scene in which this issue happens?