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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Easiest way to do direct OpenGL commands in Unity 5?

Discussion in 'General Discussion' started by yoonitee, Mar 5, 2015.

  1. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    OK, so I know you can do limited things like GL.LINE in Unity, but if I wanted to access some OpenGL commands directly, what is the easiest way of doing this? And would I need Unity 5 Professional edition to get the source code?

    For example, I might want to access things like glLine or do something procedurally with OpenGL that can't quite be done with the C# scripting?

    Or perhaps I have an OpenGL game that I want to port into Unity that builds levels procedurally using OpenGL commands? And I might not want to convert everything into Unity Meshes.

    Any thoughts?
     
  2. AaronClark

    AaronClark

    Joined:
    Oct 14, 2013
    Posts:
    39
    Have you looked into native plugins?
     
  3. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    Yeah but I wanted something that could run on all platforms.
     
  4. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    in that case if you need mobile supported too then you are out of luck unfortunately. btw renderingcallback dont help? i guess you need some advanced technique maybe rendering callback will be enough?

    EDIT btw native plugins are supported on mobile too but they havent native graphic events so they are barely used on mobile, but you still could marshal between c# and c++ as workaround i think
     
  5. Pix10

    Pix10

    Joined:
    Jul 21, 2012
    Posts:
    850
    If by source you mean Unity source, I think the cost of a Pro license would be the last of your concerns :)

    Native plugins can run on all platforms except web (AFAIK)..they just need recompiling? You'd probably still be creating your geometry with UnityEngine.GL (I don't know if there's more available through reflection, but runtime reflection also may not be around forever).
     
  6. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    But the UnityEngine.GL commands seem to be based on OpenGL 1.0

    e.g. A separate function call for each vertex.

    I thought there might be an easier way to port OpenGL 1.1 to OpenGL 2 commands into Unity. Maybe not then.
     
  7. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    I presume you could create a dll which gives access
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The source code is a separate license; you don't get it with Pro. The GL class isn't OpenGL (which is evident since it runs fine using Direct3D). I can't think of anything that you'd do with OpenGL that you can't already do with Unity as it is. Aside from giving yourself headaches when trying to port to different platforms.

    --Eric
     
  9. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    The beauty of unity is not having to worry about the graphics driver command stuff... although I love OpenGL and used it a lot directly in other systems.... I'd say you're probably better off just converting your generator to work within Unity's design. What GL commands do you need that aren't supported?
     
  10. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    But some things like changing a Mesh, to get the vertices it makes a complete copy of the vertex array and then you change it and send it back. I think if I had access to a function such as glVertexPointer, I could optimize things a bit better for certain tasks. Or if I just wanted to draw a random triangle strip somewhere.
    Basically the Unity functions are optimised for the particular case of having persistent models.
    But, for example, making a game like Minecraft (sorry for mentioning that!) it is quite hard to make a game like that in Unity without combining things into a persistent Mesh which isn't really what you want to do!
    It would be nice to access the raw OpenGL functions but also have the power of Unity to import models and port to multiple platforms.
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There are plenty of Minecraft-type assets made with Unity that get very good performance; don't worry about speed. You do want to use persistent meshes for that, so frustum culling will work. It's true that uploading the entire mesh isn't 100% ideal, but the solution is for Unity to implement an improvement in that area, not to implement OpenGL access ("access raw OpenGL" and "port to multiple platforms" are incompatible ideas). There are signs some improvement may be coming, since the CanvasRenderer.SetVertices function allows for partial uploads, using either a List, or an array with an index. So they're obviously aware of it.

    --Eric
     
  12. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    That sounds exciting. I wonder if it is due to Minecraft in particular that they might be adding things like this?
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's not due to Minecraft, since you wouldn't need to upload meshes all that often so it doesn't really matter.

    --Eric
     
  14. LtKelleyUSMC

    LtKelleyUSMC

    Joined:
    Oct 6, 2012
    Posts:
    44
    Just thought I would add my TWO CENTS on doing the OpenGL commands,
    or JUST TO BE CLEAR for some people, using the GL CLASS!!!
    Finally got them working in my SCENE VIEW window.
    I had to attach this script to the camera...
    (I put a plane gameobject behind the location of where the lines will be, just so they would be easier to see)


    The OpenGL lines from behind (As you know, the PLAIN is invisible from this perspective)...

    Code (CSharp):
    1. [FONT=Courier New]
    2. // Attach this script to the active camera
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class DoOpenGLcommands : MonoBehaviour
    7. {
    8.     public static Material lineMaterial;
    9.     static void CreateLineMaterial()
    10.     {
    11.         if (!lineMaterial)
    12.         {
    13.             lineMaterial = new Material("Shader \"Lines/Colored Blended\"              " +
    14.                                         "{                                             " +
    15.                                         "    SubShader                                 " +
    16.                                         "    {    Pass                                 " +
    17.                                         "         {                                    " +
    18.                                         "             Blend                            " +
    19.                                         "             SrcAlpha                         " +
    20.                                         "             OneMinusSrcAlpha                 " +
    21.                                         "             ZWrite Off                       " +
    22.                                         "             Cull Off                         " +
    23.                                         "             Fog                              " +
    24.                                         "             {                                " +
    25.                                         "                 Mode Off                     " +
    26.                                         "             }                                " +
    27.                                         "             BindChannels                     " +
    28.                                         "             {                                " +
    29.                                         "                 Bind \"vertex\",             " +
    30.                                         "                 vertex Bind \"color\", color " +
    31.                                         "             }                                " +
    32.                                         "         }                                    " +
    33.                                         "    }                                         " +
    34.                                         "}                                             " );
    35.             lineMaterial.hideFlags = HideFlags.HideAndDontSave;
    36.             lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
    37.         }
    38.     }
    39.     void OnPostRender()
    40.     {
    41.         CreateLineMaterial();
    42.         lineMaterial.SetPass(0);
    43.         // GL.LineWidth(1.0f); // Command NOT FOUND
    44.         GL.Begin(GL.LINES);
    45.             GL.Color(new Color(1, 0, 0, 1.0f));                    // RED LINE - The alpha was originally 0.5f
    46.                 GL.Vertex3(0, 0, 0);    GL.Vertex3(1, 0, 0);
    47.             GL.Color(new Color(0, 0, 1, 1.0f));                    // BLUE Line
    48.                 GL.Vertex3(0, 1, 0);    GL.Vertex3(1, 1, 0);
    49.             GL.Color(new Color(0, 1, 0, 1.0f));                    // GREEN LINE
    50.                 GL.Vertex3(0, 0, 0);    GL.Vertex3(0, 1, 0);
    51.             GL.Color(new Color(1, 0, 1, 1.0f));                    // PURPLE LINE (Red and Blue)
    52.                 GL.Vertex3(1, 0, 0);    GL.Vertex3(1, 1, 0);
    53.             GL.Color (new Color(1, 1, 0, 1.0f));                   // YELLOW LINE
    54.                 GL.Vertex3(0,0,0);   GL.Vertex3 (1,1,0);
    55.             GL.Color (new Color(0, 1, 1, 1.0f));                   // GREEN AND BLUE LINE
    56.                 GL.Vertex3(1,0,0);   GL.Vertex3(0,1,0);
    57.         GL.End();
    58.     }
    59. }
    60. [/FONT]
    61.  
     
    Last edited: Mar 7, 2015
  15. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Those aren't OpenGL commands, as mentioned earlier. That's just the standard Unity GL class.

    --Eric
     
  16. LtKelleyUSMC

    LtKelleyUSMC

    Joined:
    Oct 6, 2012
    Posts:
    44
    Let me see if can clarify. When programming in OpenGL, these are the commands to create the lines,
    In Unity, they are defined as a class, but techncally, the commands and the class are the same thing.
    Maybe, I should have said the SYNTAX, but the point is, that I am using the
    correct programming to get the OpenGL code, to work with Unity3D.
    I think that Yoonitee, (like that name), was simply talking about using OpenGL code to work
    in the Unity3D environment, which my code, hopefully, was beneficial to him...
    Geezzus, this is the problem when talking to computer people...
    "EVERYBODY IS AN EXPERT", as the saying goes.
     
    Last edited: Mar 7, 2015
  17. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Technically they aren't; they use Unity's backend which uses various APIs (Direct3D etc.). It's deliberately OpenGL-like (the name is a hint), but it's not, technically, OpenGL. This topic is specifically about using actual OpenGL...read the first few posts—yoonitee was already aware of the GL class.

    --Eric
     
    Ryiah likes this.
  18. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    Well, yeah, but a GL class that covered more of OpenGL 1.1 instead of OpenGL 1.0 would also be useful. It wouldn't bother me if it got translated to DirectX instead of OpenGL.

    I remember writing an OpenGL wrapper for XNA once. (It was similar to the Unity GL commands). It was very useful for porting an OpenGL game into XNA.
     
  19. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    If anything, you don't want to use GL class for "optimizing", as the performance of it is terrible due to the fact that it has to emulate fixed function pipeline via programmable shaders.

    To access OpenGL, you'll need a native plugin. That will require separate code for different platforms due to a single fact that there's no glVertexPointer on non-OpenGL platforms.
     
  20. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    Out of interest why when you want to change one vertex in a Mesh do you have to copy the whole vertex array. That doesn't sound very efficient? Or am I missing something?
     
  21. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    You don't have to copy it. Just keep your own array and assign it to mesh once you want to upload the vertices rather than getting a new copy from a mesh every time.
     
  22. AbhimanyuAryan

    AbhimanyuAryan

    Joined:
    Apr 22, 2015
    Posts:
    32
    @Eric5h5 @Tautvydas-Zilys is it possible to extend Unity Engine if you know DirectX? Like you can write HLSL(Shader Language) & let unity port it for you? Doesn't Unity allow DirectX commands?

    & now it's not OpenGL
     
  23. rajanerve

    rajanerve

    Joined:
    Nov 17, 2017
    Posts:
    17
    hello.. how to use glblendfunc and glhint of opengl in unity?
    Is there any plug in or packages available for these functions? if so please mention it..
     
  24. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,141
    Why do you need these?
     
  25. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,327
    You pretty much don't. platform-specific api is abstracted away from you via GL class:
    https://docs.unity3d.com/ScriptReference/GL.html

    You can call functions directly from native plugins via native plugins interface - if you really insist.
    https://docs.unity3d.com/Manual/NativePluginInterface.html

    But in general this is not what you're supposed to be doing.Unity isn't guaranteed to start in opengl mode anyway.
     
  26. rajanerve

    rajanerve

    Joined:
    Nov 17, 2017
    Posts:
    17
  27. rajanerve

    rajanerve

    Joined:
    Nov 17, 2017
    Posts:
    17
    Can a circle or ellipse be drawn using shader? If so please share the sample code or links to find the same..