Search Unity

OpenGL lines with no depth buffer

Discussion in 'General Graphics' started by Todd-Wasson, Nov 27, 2014.

  1. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    I'm basically trying to somewhat duplicate the Debug.DrawLine function with an OpenGL implementation so these kinds of lines can be visible in the actual application instead of just the editor preview. This will come in handy for drawing force vectors and so on which might be a neat part of the simulator I'm working on.

    I've got line segments rendering through OpenGL, but would like a way to have them render over everything else in the scene. I.e., not be blocked by anything in the scene, the same way Debug.DrawLine typically works. Anyone know a relatively quick and easy way of doing this? Here's what I've got now:

    Code (csharp):
    1.  
    2. Material lineMaterial;
    3.  
    4. void Start()
    5. {
    6.     CreateLineMaterial();
    7. }
    8.  
    9. void CreateLineMaterial()
    10. {
    11.         if (!lineMaterial)
    12.         {
    13.             lineMaterial = new Material("Shader \"Lines/Colored Blended\" {" +
    14.                 "SubShader { Pass { " +
    15.                 "    Blend SrcAlpha OneMinusSrcAlpha " +
    16.                 "    ZWrite Off Cull Off Fog { Mode Off } " +
    17.                 "    BindChannels {" +
    18.                 "      Bind \"vertex\", vertex Bind \"color\", color }" +
    19.                 "} } }");
    20.             lineMaterial.hideFlags = HideFlags.HideAndDontSave;
    21.             lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
    22.         }
    23. }
    24.  
    25. void OnPostRender()
    26.     {
    27.         Vector3 startPoint;
    28.         Vector3 endPoint;
    29.         GL.PushMatrix();
    30.         lineMaterial.SetPass(0);
    31.         GL.Begin(GL.LINES);
    32.         GL.Color(Color.white);
    33.         for (int iMesh = 0; iMesh < hydroMesh.Length; iMesh++)
    34.         {
    35.             for (int iForce = 0; iForce < hydroMesh[iMesh].forcesBuoyancy.numberOfForces; iForce++)
    36.             {
    37.                 startPoint = hydroMesh[iMesh].forcesBuoyancy.position[iForce];
    38.                 endPoint = startPoint + hydroMesh[iMesh].forcesBuoyancy.force[iForce] * forceScale;
    39.                 GL.Vertex(startPoint);
    40.                 GL.Vertex(endPoint);
    41.             }
    42.         }
    43.         GL.End();
    44.         GL.PopMatrix();
    45.     }
    46.  
    Basically what I want is for there to be no depth checking during this part of the rendering, like maybe drawing all this at the very end over everything else in the scene, overwriting the pixels that are already there. It doesn't seem to matter if ZWrite is on or off though. I must not be understanding how this works very well.

    Also: This script appears to work if it's attached to a camera rather than any old game object, which is not how I'm used to working with OpenGL. Is it a requirement with Unity that OpenGL drawing must be done on a script attached to a camera? Isn't there a way to operate on the current view/camera without having to do that? If not, it's not a huge deal, it just complicates things a little bit more by making it necessary to attach this script to every single camera in the scene, which means I have redundant data storage and extra loading time unless I split up a couple things. Again, it's not a huge deal, it'd just be nice not to have to do this on every camera object.

    Thanks for any ideas.
     
    Last edited: Nov 27, 2014
  2. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Nevermind, some wonderful person told me to use the ZTest Always option. Did that and it worked. One extra line is stuck in here:

    Code (csharp):
    1.  
    2. lineMaterial = new Material("Shader \"Lines/Colored Blended\" {" +
    3.                 "SubShader { Pass { " +
    4.                 "    Blend SrcAlpha OneMinusSrcAlpha " +
    5.                 "    ZWrite Off Cull Off Fog { Mode Off } " +
    6.                 "    ZTest Always " + //<------------------------------Added this
    7.                 "    BindChannels {" +
    8.                 "      Bind \"vertex\", vertex Bind \"color\", color }" +
    9.                 "} } }");
    10.  
     
    WILEz1975 likes this.