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
0
Question by GameDevPadawan_ · Jun 20, 2020 at 09:09 PM · renderingopengl

Why does GL not render a line when I attach all my vertices in one go?

I want to draw several lines that are related to one game object. So I can't use LineRenderer since you can only have one of it attached to an object. I opted to use GL instead. I have working code, but I don't understand why a particular modification breaks it?


This code works and draws my line as desired. However, it seems wasteful to begin and end for every pair in my line, which can be thousands of points long.

 // Loop through each point and connect it to the previous one
 for (int i = 1; i < line.points.Length; i++)
 {
     Vector3 start = line.points[i - 1];
     Vector3 end = line.points[i];
 
     GL.Begin(GL.LINES);
     line.lineMat.SetPass(0);
     GL.Color(line.lineMat.color);
     GL.Vertex3(start.x, start.y, start.z);
     GL.Vertex3(end.x, end.y, end.z);
     GL.End();
 }



So I changed to this. Which doubled my framerate in my simple scene from ~600 to ~1200 fps. So I got the improvement that I was expecting. However, this made my lines dashed.

 GL.Begin(GL.LINES);
 line.lineMat.SetPass(0);
 GL.Color(line.lineMat.color);
 
 foreach (Vector3 point in line.points)
 {
     GL.Vertex3(point.x, point.y, point.z);
     
 }
 GL.End();



I did some googling and I found this nearly decade old post that showed a fix. https://answers.unity.com/questions/155803/gllines-dashed-stippled-why.html

However, when I make that modification I get no line at all! No modifications I make fix this. Below is the final attempt at this. It still does not render anything. Can anyone point me to why that might be?

 GL.PushMatrix();
 GL.LoadOrtho();
 line.lineMat.SetPass(0);
 GL.Color(line.lineMat.color);
 GL.Begin(GL.LINES);
 
 int i = 0;
 foreach (Vector3 point in line.points)
 {
     GL.Vertex(point);
     if (i > 0 && i < line.points.Length - 1)
     {
         GL.Vertex(point);
     }
     i++;
 
 }
 GL.End();
 GL.PopMatrix();
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jun 21, 2020 at 04:12 AM

The drawing mode "LINES" will draw several line segments. Each segment requires TWO vertices. However you only emit all your points once. So the first and second point will form a line and the third and forth will form another line. However you would have a gap every second segment that way. You should do something like this:

 GL.Begin(GL.LINES);
 line.lineMat.SetPass(0);
 GL.Color(line.lineMat.color);
 for (int i = 1; i < line.points.Length; i++)
 {
     Vector3 start = line.points[i - 1];
     Vector3 end = line.points[i];
  
     GL.Vertex3(start.x, start.y, start.z);
     GL.Vertex3(end.x, end.y, end.z);
 }
 GL.End();

This is your original code just with the begin and end lines outside the loop. However since Unity also supports a line strip you could use:

 GL.Begin(GL.LINES);
 line.lineMat.SetPass(0);
 GL.Color(line.lineMat.color);
 foreach(var p in line.points)
 {
     GL.Vertex(p);
 }
 GL.End();


Note that GL:LoadOrtho will change the projection to a 0 to 1 range for x and y and -1 to 100 on z. It's not clear in which space you want to specify your points. Keep in mind that drawing with GL directly draws to the screen. There is no camera involved. You have to setup all wanted transformations yourself, including the projection.

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

153 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 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 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 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 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 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 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

Texture lookup in Surface Shader in pixel space 0 Answers

GL.LINES renders in a single quadrant of the screen 0 Answers

OpenGL ES 1/2 Jagged edges issue 1 Answer

general question regarding flexibility of unity3d rendering 0 Answers

OpenGL ES 3 VR 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