Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 gRntaus · May 08, 2014 at 04:18 AM · c#linesgl

Drawing 3D Lines with GL?

I've seen a bunch of example answers here for drawing lines in 3D using GL in Unity and I am just having no luck at all, I typically only get one line run across my screen, I can never get more than one line and it also doesn't appear to be running in 3D rather it looks orthographic on my screen (although I am not loading ortho).

Can anyone explain if I need to draw my lines on a 3D surface (such as a plane) or can they simply be drawn in world space?

As an example to test what can or can't be done I am attempting to draw a square in world space from 0,0,0 that is 10 units in x and 10 units in y.

I'd post my code but there isn't a lot of point as I've typically been copying other threads such as http://answers.unity3d.com/questions/482128/draw-grid-lines-in-game-view.html and http://answers.unity3d.com/questions/149210/creating-gl-lines.html.

Comment
Add comment · Show 1
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 flamy · May 08, 2014 at 04:41 AM 0
Share

still posting code might help because 1 line change might give a totally different output in gl...

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Bunny83 · May 08, 2014 at 11:20 AM

I think you expect too much from GL lines ;). OpenGL is a very mathematic / physic based API. A line has actually no width at all it's infinite thin, otherwise it would have an area and therefore is not a line anymore. Of course to see / render a line it has to have some width. In OpenGL you could set the linewidth, in Unity it's always 1. So each line you draw always is 1 pixel wide, no matter if it's 10000 units away or 0.01 infront of your camera.

Actually everything the GPU renders is rendered in 2d-screen-space. Things rendered in distance appear smaller because the projected vertex-positions are closer to each other. A line however never look thicker since it doesn't have any thickness. All the GPU does is projecting the two endpoints into screenspace and draw a 1-pixel-line between them.

If you want a line to appear thicker when it's closer to the screen you have to draw them as quad-billoards like the LineRenderer does. There are several assets in the asset store which can draw lines, one of the best know is eric's Vectrosity.

Keep in mind that billboard lines (quads) always look / behave a bit "strange" when you view them from different angles due to the billboarding. If the view is always the same you don't notice this. Here's an example of Quake3

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 flamy · May 08, 2014 at 04:33 PM 0
Share

the answer is pretty good but to add to it, if you still want to draw a line of certain thickness you have to create a polygon around the line render it as a quad.

look at this image alt text

or to avoid confusion you can just use this script from here this wiki which does nearly the same!

awesomedrawing.png (5.5 kB)
avatar image
0

Answer by Kaz_Yamof · May 08, 2014 at 12:07 PM

You just have to set the position of the initial point and the final point. You also need to write two commands of draw lines in sequence. If you have a vector with your points, do something like this:

 //Script attached on camera main
     private void OnPostRender()
     {
         Vector3[] points = new Vector3[10];
         //points = initialize some values to array...
 
         for (int i = 1; i < points.Length; i++)
         {
             GL.Vertex(points[i - 1]);
             GL.Vertex(points[i]);
         }    
     }

Instead to do this (will not work):

 [...]  
 for (int i = 0; i < points.Length; i++)
     {
         GL.Vertex(points[i]);
     }

But keep in mind that it will just draw one line on screen each time. If you want more than that, create a vector/list of Vector3[] array, with the coordinates of each line, and iterate by the list, and then iterate by the coordinates of the actual array to draw.

 foreach (Vector3[] points in ListPoints)
     {
         for( int i =0; i < points.Length; i++)
         {
                 GL.Vertex(points[i - 1]);
                 GL.Vertex(points[i]);
         }
     }
Comment
Add comment · 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

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to create a script for dynamic 2d sprite object that is controlled by 2 vector points? 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Renderer on object disabled after level reload 1 Answer


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