Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by michael 4 · Jul 27, 2011 at 05:44 PM · linesgl

Creating GL Lines

Id like to set up a function that draw lines exactly like the Debug.DrawLine() function works.

I just want to say DrawLine(point1, point2) and it will draw a line between point 1 and two.

I I tried to use GL Lines (I have Pro)

the line get generated when its called. Could someone tell me what Im doing wrong? (I really just copied this from the documentation because I dont fully understand how it works.

     public static void DrawGLLine(Vector3 P1, Vector3 P2)
 {
     GL.PushMatrix();
     GL.LoadOrtho();
     GL.Begin(GL.LINES);
     GL.Color(Color.red);
     GL.Vertex(P1);
     GL.Vertex(P2);
     GL.End();
     GL.PopMatrix();        
 }

Im then trying to call it in the part of the script that gets updated every frame

 DrawGLLine(cam.transform.TransformPoint(v[0]),hit_point4.point);
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image michael 4 · Jul 27, 2011 at 07:22 PM 0
Share

Does it have to be OnPostRender()? That would make it difficult to call the function at anytime when I have two points I need to draw between.

That statement just says "usual" does that imply it has to be there or not necessarily?

avatar image Chris D · Jul 27, 2011 at 07:29 PM 0
Share

I take it to mean that there are other functions you could use to get it to render after everything else, but OnPostRender is the most convenient. I haven't tried using this myself as I don't have Pro, unfortunately.

I'm not sure I understand how it makes it any harder, could you explain? Isn't it just a matter of setting up your calculations outside of the function, creating a queue of lines to be drawn, waiting for PostRender, then calling them all?

avatar image Graham-Dunnett ♦♦ · Jul 27, 2011 at 07:32 PM 0
Share

Interpret "usual" as "call GL drawing functions in OnPostRender() unless you know exactly what you are doing". If you have the code in an Update() then it won't work. Can't you just add an OnPostRender, and store your two points in variables set in your update or where-ever. You can also set a boolean value that controls whether the line is drawn or not.

avatar image michael 4 · Jul 27, 2011 at 07:43 PM 0
Share

The thing that makes this all difficult is that the points are stored inside of a static class that extends the capabilities of the built in Camera class. Im currently using debug.DrawLine() inside this class once the points are calculated. Everything works exactly as it should except Debug.DrawLine only works in the editor. I need to replace it with someway to draw a vectored line.

Extension classes are somewhat new to me, which is why the usual techniques are giving me a hard time.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Bunny83 · Jul 27, 2011 at 07:41 PM

You can't call it in Update since the first thing that happens in the render step (which comes after Update) is to clear the framebuffer. If you want to show something above the rendered cameras you need to use one of the post-events (OnPostRender or OnGUI for example). Also keep in mind that LoadOrtho will load a totally different projectionmatrix than your camera is using. You should also set a modelview matrix to transform the world coordinates into camera coordinates or your line won't appear at the desired world coordinates.

You can set the perspective matrix from your camera.

Next thing is that you need a material to render with. If you don't set one it's quite random what material is used.

http://unity3d.com/support/documentation/ScriptReference/GL.LINES.html

I've used GL.LINES a lot but i have no time at the moment to show a full example.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image michael 4 · Jul 28, 2011 at 02:37 PM 0
Share

Great explanation, However I broke down and just purchased vectrosity. Trying to use OnPostRender() would have required me to restructure my code.

avatar image
0

Answer by Chris D · Jul 27, 2011 at 07:16 PM

From the docs:

GL immediate drawing functions use whatever is the "current material" set up right now. The material controls how the rendering is done (blending, textures, etc.), so unless you explicitly set it to something before using GL draw functions, the material can happen to be anything. Also, if you call any other drawing commands from inside GL drawing code, they can set material to something else, so make sure it's under control as well.

GL drawing commands execute immediately. That means if you call them in Update(), they will be executed before the camera is rendered (and the camera will most likely clear the screen, making the GL drawing not visible).

The usual place to call GL drawing is most often in OnPostRender() from a script attached to a camera

Edited to include all relevant details. Docs here.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chris D · Jul 27, 2011 at 07:36 PM 0
Share

For an example of drawing to screen with GL, check out this question and the link it has to this wiki post.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I change GL LINES color? 1 Answer

Drawing 3D Lines with GL? 2 Answers

Draw 2D polygon 1 Answer

Use Gl.Lines to connect Vector3 points in world space 1 Answer

Does Unity iPhone support calls to the GL Class? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges