Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

DrawLine with GL

Discussion in 'Assets and Asset Store' started by devLapinou, Mar 22, 2014.

  1. devLapinou

    devLapinou

    Joined:
    Mar 22, 2014
    Posts:
    3
    EDIT: Look like someone did it before, more optimized than my script and with more functions ;)
    http://pastebin.com/69QP1s45
    author: artemisart

    Hello, I'm currently working on some external ballistic stuff, and Debug.DrawRay and Debug.DrawLine were VERY useful.

    I've made some research to get these lines into the game for whatever reason, and I've figured that using the GL library was the way to do it. But impossible to find scripts to do it simple.

    So I've made these 2 scripts:

    Code (csharp):
    1. /*
    2. HOW TO USE, Put DrawLineGLViewer.js on the cameras you want the lines to be rendered
    3.  
    4. THIS SCRIPT SHOULD BE IN YOUR PLUGINS FOLDER
    5.  
    6. DrawLineGL.DrawLine(StartPos:Vector3, EndPos:Vector3, Color:Color, LifeTime:float);
    7.  
    8. LifeTime is optional, if none then it'll be rendered 1 frame
    9. */
    10.  
    11. static var lineMaterial : Material;
    12. static var linesGL:lineGL[];
    13.  
    14. class lineGL
    15. {
    16.     var lt: float;
    17.     var st: Vector3;
    18.     var ed: Vector3;
    19.     var c: Color;
    20.    
    21.     function lineGL(nst:Vector3, ned:Vector3, nc:Color, nlt:float)
    22.     {
    23.         lt = Time.time + nlt;
    24.         st = nst;
    25.         ed = ned;
    26.         c = nc;
    27.     }
    28. }
    29.  
    30. static function CreateLineMaterial() {
    31.     if( !lineMaterial ) {
    32.         lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
    33.             "SubShader { Pass { " +
    34.             "    Blend SrcAlpha OneMinusSrcAlpha " +
    35.             "    ZWrite Off Cull Off Fog { Mode Off } " +
    36.             "    BindChannels {" +
    37.             "      Bind \"vertex\", vertex Bind \"color\", color }" +
    38.             "} } }" );
    39.         lineMaterial.hideFlags = HideFlags.HideAndDontSave;
    40.         lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
    41.     }
    42. }
    43.  
    44. static function CreateLinesGL()
    45. {
    46.     if(!linesGL)
    47.     {
    48.         linesGL = new lineGL[0];
    49.     }
    50. }
    51.  
    52. static public function DrawLine(st:Vector3, ed:Vector3, c:Color, lt:float)
    53. {
    54.     var nlGL : lineGL = new lineGL(st,ed,c,lt);
    55.    
    56.     var nlinesGL = new lineGL[linesGL.length + 1];
    57.    
    58.     for(var i : int = 0; i<nlinesGL.length;i++)
    59.     {
    60.         if(i<linesGL.length)
    61.         {
    62.             nlinesGL[i] = linesGL[i];
    63.         }else
    64.         {
    65.             nlinesGL[i] = nlGL;
    66.         }
    67.     }
    68.    
    69.     linesGL = nlinesGL;
    70. }
    71.  
    72. static public function DrawLine(st:Vector3, ed:Vector3, c:Color)
    73. {
    74.     var nlGL : lineGL = new lineGL(st,ed,c,0);
    75.    
    76.     var nlinesGL = new lineGL[linesGL.length + 1];
    77.    
    78.     for(var i : int = 0; i<nlinesGL.length;i++)
    79.     {
    80.         if(i<linesGL.length)
    81.         {
    82.             nlinesGL[i] = linesGL[i];
    83.         }else
    84.         {
    85.             nlinesGL[i] = nlGL;
    86.         }
    87.     }
    88.    
    89.     linesGL = nlinesGL;
    90. }
    91.  
    92. static public function ResizeLines(index:float)
    93. {
    94.     var nlinesGL = new lineGL[linesGL.length - 1];
    95.    
    96.     for(var i : int = 0; i<linesGL.length; i++)
    97.     {
    98.         if(i < index)
    99.         {
    100.             nlinesGL[i] = linesGL[i];
    101.         }else
    102.         if(i < nlinesGL.length)
    103.         {
    104.             nlinesGL[i] = linesGL[i+1];
    105.         }
    106.     }
    107.    
    108.     linesGL = nlinesGL;
    109. }
    110.  
    Code (csharp):
    1. #pragma strict
    2.  
    3. function Awake()
    4. {
    5.     DrawLineGL.CreateLinesGL();
    6. }
    7.  
    8. function OnPostRender() {
    9.     DrawLineGL.CreateLineMaterial();
    10.     // set the current material
    11.     DrawLineGL.lineMaterial.SetPass( 0 );
    12.    
    13.     GL.Begin( GL.LINES );
    14.  
    15.     if(DrawLineGL.linesGL.Length > 0)
    16.     {
    17.         for(var i : int = 0; i<DrawLineGL.linesGL.length; i++)
    18.         {
    19.             GL.Color (DrawLineGL.linesGL[i].c);
    20.             GL.Vertex3 (DrawLineGL.linesGL[i].st.x,DrawLineGL.linesGL[i].st.y,DrawLineGL.linesGL[i].st.z);
    21.             GL.Vertex3 (DrawLineGL.linesGL[i].ed.x,DrawLineGL.linesGL[i].ed.y,DrawLineGL.linesGL[i].ed.z);
    22.            
    23.             if(Time.time > DrawLineGL.linesGL[i].lt)
    24.             {
    25.                 DrawLineGL.ResizeLines(i);
    26.             }
    27.         }
    28.     }
    29.  
    30.     GL.End();
    31. }
    So put the first script on your Plugins folder, and the second script on whatever camera you will use.
    Then just use DrawLineGL.DrawLine(StartPosition:Vector3, EndPosition:Vector3, Color:Color, LifeTime:Float);

    Code (csharp):
    1. DrawLineGL.DrawLine(this.transform.position, target.transform.position, Color.Blue,2);
    2. DrawLineGL.DrawLine(this.transform.position, target.transform.position + , Color.Blue);//If no lifetime line is rendered 1 frame
    3.  
    Dunno if it'll be useful, (maybe someone made something like that but I didn't find it so...), just tell me if it was useful for you :)

    Thanks,
    Sylvain
     

    Attached Files:

    Last edited: Mar 23, 2014
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    6,245
    Thanks, very much needed! (maybe should have it in unifywiki or asset store as free also..)
     
  3. devLapinou

    devLapinou

    Joined:
    Mar 22, 2014
    Posts:
    3
    Thanks, I've requested an account on unifywiki to post it, and I'll upload it to the asset store asap ;).
     
    Last edited: Mar 23, 2014
  4. artemisart

    artemisart

    Joined:
    Sep 16, 2012
    Posts:
    8
  5. devLapinou

    devLapinou

    Joined:
    Mar 22, 2014
    Posts:
    3
    Nice! I couldn't find it when I needed it! You should spread it everywhere o/

    [Effectivement je pense jamais à chercher côté fr]
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    6,245
    Needed to draw simple circle with artemisart script, added this code: (circle part taken from some unity answers post)

    Code (CSharp):
    1. public static void DrawCircle (Vector3 center, float radius, Color? color = null, float duration = 0, bool depthTest = false)
    2.   {
    3.   //float degRad = Mathf.PI / 180;
    4.        for(float theta = 0.0f; theta < (2*Mathf.PI); theta += 0.2f)
    5.        {
    6.          Vector3 ci = (new Vector3(Mathf.Cos(theta) * radius + center.x, Mathf.Sin(theta) * radius + center.y, center.z));
    7.          DrawLine (ci, ci+new Vector3(0,0.02f,0), color, duration, depthTest);
    8.        }
    9.   }
     
    Last edited: Oct 22, 2014
    Dazzid likes this.
  7. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    8,660
    Very nice, will be really useful
     
  8. Dazzid

    Dazzid

    Joined:
    Apr 15, 2014
    Posts:
    61
    Thanks a lot for this info!!!
    I'm trying to implement it, but I'm getting some errors calling GL_Line.DrawLine (startLine, endLine, myColor ,0.0f,true);
    "cannot be accessed with an instance reference, qualify it with a type name instead"
    Do you have any example on how to use it?
    Thnaaaks!
     
  9. artemisart

    artemisart

    Joined:
    Sep 16, 2012
    Posts:
    8
    If you use this script http://pastebin.com/69QP1s45 then you should call, for instance : GLDebug.DrawLine(new Vector3(0, 0, 0), new Vector3(1, 1, 1), Color.red, 1);
     
  10. Dazzid

    Dazzid

    Joined:
    Apr 15, 2014
    Posts:
    61
    And using unity5 could affect in something? Don't know why I can't make it work :(
     
  11. artemisart

    artemisart

    Joined:
    Sep 16, 2012
    Posts:
    8
    Unfortunately, yes. This code was made for Unity 3.x (and compatible with 4.x if i remember correctly).
    The error comes from this script ? or the one that use it ?
     
  12. Dazzid

    Dazzid

    Joined:
    Apr 15, 2014
    Posts:
    61
    Hi
    Actually I don't have errors right now. But it only paint the lines on the Gizmos or in the editor window, but nothing on the scene. What could cause this?
    Thanks for the help
     
  13. artemisart

    artemisart

    Joined:
    Sep 16, 2012
    Posts:
    8
    Sorry, I forgot to say that the script has to be on a camera for OnPostRender to be called. Can you retry with the script attached to the GameObject of your main camera ?
     
  14. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    6,245
    Updated that GLDebug.cs for unity5 (it was giving some obsolete warnings on the new Material())
     

    Attached Files:

    Muckel likes this.
  15. Geminior

    Geminior

    Joined:
    Feb 12, 2014
    Posts:
    322
    These line draw shaders look nice and crisp in Unity 4 if DX11 is turned off, but with DX11 active and in Unity 5 they are fuzzy.

    Any shader wizards that have any idea how to make a line shader that draws correctly under DX11?
     
  16. Bartec

    Bartec

    Joined:
    Jun 20, 2015
    Posts:
    12
    Hey,
    im using apex path and i want to show this
    debugging path in game view.
    have tried with a linerenderer but still not working

    how to enable gizmo drawing in game?

    thanks

    Code (CSharp):
    1. /* Copyright © 2014 Apex Software. All rights reserved. */
    2. namespace Apex.Debugging
    3. {
    4.     //using Apex;
    5.     using Apex.PathFinding;
    6.     using Apex.Steering;
    7.     using Apex.Steering.Components;
    8.     using Apex.Units;
    9.     using UnityEngine;
    10.     //using System;
    11.     using System.Collections.Generic;
    12.  
    13.  
    14.  
    15.     /// <summary>
    16.     ///
    17.     //List<Vector3> posSpheres = new List<Vector3>();
    18.     /// Visualization component that draws gizmos to represent a moving unit's current path.
    19.     /// </summary>
    20.     [AddComponentMenu("Apex/Unit/Debugging/Path Visualizer", 1024)]
    21.     [ApexComponent("Debugging")]
    22.     public partial class PathVisualizer : Visualizer
    23.     //public partial class PathVisualizer : MonoBehaviour
    24.     {
    25.  
    26.         public List<Vector3> posSpheres = new List<Vector3>();
    27.  
    28.         /// <summary>
    29.         /// The route color
    30.         /// </summary>
    31.         public Color routeColor = new Color(148f / 255f, 214f / 255f, 53f / 255f);
    32.  
    33.         /// <summary>
    34.         /// The way point color
    35.         /// </summary>
    36.         public Color waypointColor = new Color(0f, 150f / 255f, 211f / 255f);
    37.  
    38.         /// <summary>
    39.         /// Whether to show segment markers
    40.         /// </summary>
    41.         public bool showSegmentMarkers = false;
    42.  
    43.         private IUnitFacade _unit;
    44.         //public IUnitFacade _unit;
    45.  
    46.         /// <summary>
    47.         /// Called on start
    48.         /// </summary>
    49.         protected override void Start()
    50.         //void Start()
    51.         {
    52.             _unit = this.GetUnitFacade();
    53.         }
    54.  
    55.      
    56.      
    57.         void Update()
    58.     {
    59.          LineRenderer lineRenderer = GetComponent<LineRenderer>();
    60.         // Also, what is the value of lengthOfLineRenderer?  Ensure it is more than 1.
    61.         var points = new Vector3[lengthOfLineRenderer];
    62.         var t = Time.time;
    63.         for (int i = 0; i < lengthOfLineRenderer; i++)
    64.         {
    65.             points[i] = new Vector3(i * 0.5f, Mathf.Sin(i + t), 0.0f);
    66.         }
    67.         lineRenderer.positionCount = points.Length;
    68.         lineRenderer.SetPositions(points);
    69.     }
    70.         /// <summary>
    71.         /// Partial method for draw visualization.
    72.         /// </summary>
    73.         partial void DrawVisualizationPartial();
    74.  
    75.         /// <summary>
    76.         /// Draws the actual visualization.
    77.         /// </summary>
    78.         protected override void DrawVisualization()
    79.         {
    80.             DrawVisualizationPartial();
    81.  
    82.             if (_unit == null || !_unit.isMovable)
    83.             {
    84.                 return;
    85.             }
    86.  
    87.             Vector3 heightAdj = new Vector3(0.0f, 0.2f, 0.0f);
    88.             Gizmos.color = this.routeColor;
    89.  
    90.             var prev = _unit.position;
    91.             if (_unit.nextNodePosition.HasValue)
    92.             {
    93.                 prev = _unit.nextNodePosition.Value;
    94.                 Gizmos.DrawLine(_unit.position + heightAdj, prev + heightAdj);
    95.                 //VectorLine.SetLine (_unit.position + heightAdj, prev + heightAdj);
    96.                 //VectorLine.SetLine (Color.yellow, Vector3(0, 0, 0), Vector3(100, 200, 0));
    97.             }
    98.  
    99.             if (_unit.currentPath != null)
    100.             {
    101.                 foreach (var n in _unit.currentPath)
    102.                 {
    103.                     if (n is IPortalNode)
    104.                     {
    105.                         continue;
    106.                     }
    107.  
    108.                     if (showSegmentMarkers)
    109.                     {
    110.                         Gizmos.DrawSphere(prev, 0.2f);
    111.                     }
    112.  
    113.                     Gizmos.DrawLine(prev + heightAdj, n.position + heightAdj);
    114.                     prev = n.position;
    115.                 }
    116.             }
    117.  
    118.             Gizmos.color = this.waypointColor;
    119.             if (_unit.currentWaypoints != null)
    120.             {
    121.                 heightAdj.y = 1.0f;
    122.  
    123.                 foreach (var wp in _unit.currentWaypoints)
    124.                 {
    125.                     var pinHead = wp + heightAdj;
    126.                     Gizmos.DrawLine(wp, pinHead);
    127.                     Gizmos.DrawSphere(pinHead, 0.3f);
    128.  
    129.                     posSpheres.Add(pinHead);
    130.  
    131.                 }
    132.             }
    133.         }
    134.     }
    135. }
     
    Last edited: Jun 11, 2018
unityunity