Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GL.LINES and GL functionality with LWRP

Discussion in 'General Graphics' started by nickfourtimes, Jan 25, 2019.

  1. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    217
    I'm hoping to use some of the GL.*() class of functions, in particular rendering with GL.LINES. However, I tried upgrading my project to the LWRP, and those functions no longer seem to work/render.

    Is that in the roadmap for LWRP (or any of the custom render pipelines), is there a workaround for using GL.LINES currently, or is that something that I'd have to create custom on top of the LWRP?
     
  2. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    217
    Actually I imagine it's something related to this issue, which I'd posted earlier, or will have a similar response.
     
  3. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    How are you drawing the GL.LINES?
     
  4. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    217
    Previously, I was rending the lines in OnPostRender(), on the main camera:
    Code (CSharp):
    1. void OnPostRender() {
    2.     GL.PushMatrix();
    3.     lineMaterial.SetPass(0);
    4.     GL.Begin(GL.LINES);
    5.     foreach(var l in linesList) {
    6.         GL.Vertex3(l.p0.x, l.p0.y, l.p0.z);
    7.         GL.Vertex3(l.p1.x, l.p1.y, l.p1.z);
    8.     }
    9.     GL.End();
    10.     GL.PopMatrix();
    11.  
    12.     return;
    13. }
    I've since migrated rendering to the objects themselves, using OnRenderObject():

    Code (CSharp):
    1. public void OnRenderObject() {
    2.     GL.PushMatrix();
    3.     lineMaterial.SetPass(0);
    4.     GL.Begin(GL.LINES);
    5.     foreach (var l in linesList) {
    6.         GL.Vertex3(l.p0.x, l.p0.y, l.p0.z);
    7.         GL.Vertex3(l.p1.x, l.p1.y, l.p1.z);
    8.     }
    9.     GL.End();
    10.     GL.PopMatrix();
    11.     return;
    12. }
    In both cases, the lines render properly in the legacy rendering system, but they don't render at all with the LWRP.
     
  5. retrophil

    retrophil

    Joined:
    Mar 16, 2013
    Posts:
    46
    I have the same issue with GL.LINE_STRIP, worked with normal post processing, LW only displays the lines in scene view not game view. Same installs as nickfourtimes. I've tried 2018.3.3f1 and the latest beta 2019.1.0b1.
     
  6. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    Could you guys open a bug report for this? We'll take a look at it.
     
  7. retrophil

    retrophil

    Joined:
    Mar 16, 2013
    Posts:
    46
    Sent.
     
    phil_lira likes this.
  8. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    217
    Shall I provide the same or is there/will there be an open issue # to contribute to?
     
  9. retrophil

    retrophil

    Joined:
    Mar 16, 2013
    Posts:
    46
    I recommend you send yours in. We can compare answers :D
     
    nickfourtimes likes this.
  10. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    217
    Alrighty, I've submitted a bug along with a repro project; I can provide a case ID if that would help.
     
    phil_lira and retrophil like this.
  11. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    217
    Looks like they've been able to reproduce the problem and have sent it along for investigation.
     
    retrophil likes this.
  12. retrophil

    retrophil

    Joined:
    Mar 16, 2013
    Posts:
    46
    Are you winning? Doesn't look good my end, I'm told that GL is going to be deprecated in LWRP :/ Which pushes me back to the normal post processing.
     
  13. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    217
    I haven't heard back from them at all, but this was pretty much what I was expecting. Luckily using LWRP isn't a dealbreaker for me, and I can run fine with legacy rendering, but I was hoping to use LWRP to future-proof the project at least a little bit.

    Hopefully folks will come up with a custom RP in the future that supports GL commands, but that's way outside my wheelhouse.
     
  14. retrophil

    retrophil

    Joined:
    Mar 16, 2013
    Posts:
    46
    It's not the same but worth looking at if you haven't already...

    http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/

    scene: linesGR
     

    Attached Files:

  15. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    Hi, regarding the two issues:

    1) Debug.DrawLines work as expected. However the rendering callbacks are deprecated in SRP. We are adding atm a callback to render after camera is finished.
    2) GL.* API can be replaced with CommandBuffer.DrawMesh and CommandBuffer.DrawProcedural https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.html
     
    hugokostic likes this.
  16. retrophil

    retrophil

    Joined:
    Mar 16, 2013
    Posts:
    46
    Ok well thanks for everything phil. For the time being I'm going back to the regular v2 PostProcessing. When I have the time I'll likely implement the drawmesh for LW. :)
     
  17. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    217
    ~ update ~

    I've migrated along to Unity 2019.3.0b6, and it seems like the GL.Lines are working!
     
    richardkettlewell likes this.
  18. hchlqlz

    hchlqlz

    Joined:
    Feb 21, 2020
    Posts:
    5
    I've the same problem. When using LWRP OnRenderObject / OnPostRender is not called in game. My unity's edition is 2019.3.2f1,
     
  19. hchlqlz

    hchlqlz

    Joined:
    Feb 21, 2020
    Posts:
    5
    what should i set up ?
     
  20. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    217
    Hm. I'm essentially calling this code in 2019.3.1f1:

    Code (CSharp):
    1.  
    2.     void OnRenderObject() {
    3.         myMaterial.SetPass(0);
    4.         GL.Begin(GL.LINES);
    5.         {
    6.             GL.Vertex(from);
    7.             GL.Vertex(to);
    8.         }
    9.         GL.End();
    10.  
    11.         return;
    12.     }
    ...and it's working just fine. I'm using URP since the upgrade/rename, rather than LWRP, not sure if that changes anything.
     
  21. hchlqlz

    hchlqlz

    Joined:
    Feb 21, 2020
    Posts:
    5
    Do you use 2D render ? I'm developing a 2D project, when i switch URP's render list to 2D render, OnRenderObject is not called.
     
  22. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    217
    I just tested using a 2D Renderer asset, and as you say it seems that OnRenderObject isn't called. It's marked as "Experimental" so maybe that's not implemented yet, or maybe it's not present by design, but there might be other threads about that.
     
  23. hchlqlz

    hchlqlz

    Joined:
    Feb 21, 2020
    Posts:
    5
    All right, thanks
     
  24. Andrew900460

    Andrew900460

    Joined:
    Aug 9, 2017
    Posts:
    39
    I understand this is an old thread. But this is very relevant to something I'm trying to do in URP/SRP.
    I've asked many different people and I couldn't get the answer I need, for whatever reason.

    I did a test on my end to see if GL.Begin(GL.LINES); (etc, etc) works, and it doesn't seem to be working for me. Although, I only tested it in a custom ScriptableRenderPass I am making. and not in "OnRenderObject".

    But what I wanted to ask you about "CommandBuffer.DrawProcedural". Since you brought it up here. And I'm assuming you brought that function up because it is the only way to draw single-pixel-lines in URP/SRP using command buffers.
    I am actually trying to do this as well, drawing basic lines. But I am having a hard time getting anything to show up on screen. This could be because I have very little experience with graphics API's and I'm making many amateur mistakes.

    This is my code I have so far. It sits inside a ScriptableRenderPass and uses CommandBuffers. I am also leaving out other code so it's easier to read:

    In the Frame Debugger of Unity, it does show that the "Draw Procedural" is happening, but under the "preview" button. It says "Vertices: 3" but "Indices: 0"
    Even though it should say "Indices: 3"

    Other people have told me that I need to supply the buffers to the material with the Material.SetBuffer function. But I have no clue how that's supposed to happen. I tested "myMaterial.SetBuffer(0,vertexBuffer)" as if the shader semantic "POSITION" is held in the "first index" of something. But that didn't change anything.

    Code (CSharp):
    1. public MyCustomPass() {
    2.             myMaterial = new Material(Shader.Find("Sprites/Default"));
    3.             ib = new GraphicsBuffer(GraphicsBuffer.Target.Index, 3, 4); // index buffer for vertices
    4.             vb = new GraphicsBuffer(GraphicsBuffer.Target.Vertex, 3, 12); // vertex buffer
    5.             vb.SetData(new List<Vector3> { new Vector3(0, 0, 0), new Vector3(1, 2, 1), new Vector3(2, 1, 2) });
    6.             ib.SetData(new List<int>() { 0, 1, 2 });
    7.          
    8.             //myMaterial.SetBuffer("vertex", vb); NOT SURE IF THIS LINE IS NEEDED, OR CORRECT
    9. }
    10.  
    11. public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData) {
    12.     RenderTextureDescriptor descriptor = renderingData.cameraData.cameraTargetDescriptor;
    13.     descriptor.depthBufferBits = 0;
    14.     colorBuffer = renderingData.cameraData.renderer.cameraColorTarget;
    15. }
    16.  
    17. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) {
    18.     var cmd = CommandBufferPool.Get();
    19.     Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one); //renderingData.cameraData.GetProjectionMatrix();
    20.  
    21.     using(new ProfilingScope(cmd, profilingTest)) {
    22.         cmd.SetRenderTarget(colorBuffer,
    23.             RenderBufferLoadAction.DontCare,
    24.             RenderBufferStoreAction.Store,
    25.             RenderBufferLoadAction.DontCare,
    26.             RenderBufferStoreAction.DontCare);
    27.         cmd.ClearRenderTarget(false, true, new Color(0f, 0.2f, 0.8f)); // just some color I picked
    28.  
    29.         cmd.DrawProcedural(ib, matrix, myMaterial, 0, MeshTopology.Lines, 3); // draw lines?
    30.         // somehow, the index buffer has to be paired up with the vertex buffer
    31.         // and I have no clue where that happens.
    32.     }
    33.  
    34.     context.ExecuteCommandBuffer(cmd);
    35.  
    36.     CommandBufferPool.Release(cmd);
    37. }

    It's kinda sad also that you can't just do this with "DrawMesh". Because one of the things I might want to do with this is add a single-pixel outline around meshes. As you can see in the attached image. Maybe if there is a more effective way to achieve this, I would resort to that. But I understand that other graphics APIs like OpenGL do have support for drawing lines from vertex arrays, and it's disappointing that the support isn't as obvious.
     

    Attached Files:

  25. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    291

    CommandBuffer... reminds me on DirectX in the early 90's.
    Back to the roots, Unity, back to the roots ;-)

    Fun fact, came here after migrating my little Play Mode Editor, developed in Unity 2018 in Unity 2019, but Gizmo.Drawline() didn't read zBuffer anymore (as it does in 2018). So I tried Debug.Drawline() which has a zTest flag. This does exactly nothing (2019).

    So I found an old GL.xxx solution to draw a line. Which seems to work fine.

    But not in URP, it seems... Needs command buffers to draw a line now.

    Didn't migrate just for fun, but because my editor needs Undo (Ctrl+Z). That behaved oddly. Reason is, even in Play Mode, when focused window is Game window, Unity catches Hotkeys and uses them in Editor. Not kidding. So you can do two things with one key stroke! Testing your game and edit it in the Editor!
    So I found this new HotkeyManager- thinggy, not available in 2018, but in 2019 to solve the problem. But to go full circle, then the line render problem came up.

    Yeah. 'nough said.
     
    Last edited: May 26, 2022
    cagatayasar likes this.