Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Limbo · Jun 13, 2017 at 08:39 PM · editorsceneviewgl

Drawing with GL on SceneView bad performance

I've search the forums and in here also but nothing has help me so far.

I'm drawing a simple grid with GL on the Editor SceneView with a size of 100x100 and it's really laggy I'm guessing there's something wrong with my code but I can't find what, I would appreciate any help.

Here's the code

 Material lineMaterial;
 
     void CreateLineMaterial()
     {
         if (!lineMaterial)
         {
             // Unity has a built-in shader that is useful for drawing
             // simple colored things.
             var shader = Shader.Find("Hidden/Internal-Colored");
             lineMaterial = new Material(shader);
             lineMaterial.hideFlags = HideFlags.HideAndDontSave;
             // Turn on alpha blending
             lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
             lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
             // Turn backface culling off
             lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
             // Turn off depth writes
             lineMaterial.SetInt("_ZWrite", 0);
         }
     }
 
     protected void DrawGrid()
     {
         // Tile size
         float size = positionSnap;
         // Grid Size
         int gridSizeX = 100;
         int gridSizeZ = 100;
         // Half Size
         float halfSizeX = (gridSizeX * 0.5f) * size;
         float halfSizeZ = (gridSizeZ * 0.5f) * size;
         float offset = positionSnap * 0.5f;
         halfSizeX -= offset;
         halfSizeZ -= offset;
 
         GL.PushMatrix();
         lineMaterial.SetPass(0);
         GL.Begin(GL.LINES);
         GL.Color(Color.grey);
 
         for(int x = 0; x < gridSizeX + 1; x++)
         {
             for(int z = 0; z < gridSizeZ + 1; z++)
             {
                 // X
                 Vector3 start   = new Vector3(x * size, height, z * size);
                 Vector3 end     = new Vector3(gridSizeX, height, z * size);
                 start           += Vector3.left * halfSizeX;
                 end             += Vector3.left * halfSizeX;
                 start           += Vector3.back * halfSizeZ;
                 end             += Vector3.back * halfSizeZ;
 
                 GL.Vertex3(start.x, start.y, start.z);
                 GL.Vertex3(end.x, end.y, end.z);
 
                 // Z
                 start           = new Vector3(x * size, height, z * size);
                 end             = new Vector3(x * size, height, gridSizeZ);
                 start           += Vector3.left * halfSizeX;
                 end             += Vector3.left * halfSizeX;
                 start           += Vector3.back * halfSizeZ;
                 end             += Vector3.back * halfSizeZ;
                 GL.Vertex3(start.x, start.y, start.z);
                 GL.Vertex3(end.x, end.y, end.z);
             }
         }
 
         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
1

Answer by Okari-Draconis · Oct 25, 2017 at 03:30 AM

The main issue is the way you are drawing the grid... in your code example you are pushing 10,000+ Vertexes when you only need to push like 404..

Also.. You are making 4 new vectors inside of each iteration.. this is generally a bad idea, and its better to re-use vectors having them be outside the loop.

Finally to move something to the Left... its WAY cheaper to do:

 start.x -= halfSizeX

than this:

 start += Vector3.left * halfSizeX

because the above is the same as doing the below:

        Vector3 leftVector = new Vector3(-1,0,0);
         leftVector.x *= halfSizeX;
         leftVector.y *= halfSizeX;
         leftVector.z *= halfSizeX;
 
         start.x += leftVector.x;
         start.y += leftVector.y;
         start.z += leftVector.z;

As you can see.. a lot of useless calculations wasting CPU time... Normally not an issue.. but remember your doing the above basically 80,000+ times...

Anyways I made your code more efficient, I think it should do same as above.

     protected void DrawGrid()
     {
         float tileSize = 0.25f;
         int gridSizeX = 100;
         int gridSizeZ = 100;
 
         // Half Size
         float halfGridSizeX = gridSizeX * 0.5f * tileSize;
         float halfGridSizeZ = gridSizeZ * 0.5f * tileSize;
 
         GL.PushMatrix();
         lineMaterial.SetPass(0);
         GL.Begin(GL.LINES);
         GL.Color(Color.grey);
 
         int i;
         Vector3 start = new Vector3(-halfGridSizeX, 0, -halfGridSizeZ);
         Vector3 end = new Vector3(-halfGridSizeX, 0, halfGridSizeZ);
 
         for (i = 0; i < gridSizeX + 1; i++)
         {
             GL.Vertex(start);
             GL.Vertex(end);
             end.x = start.x = start.x + tileSize;
         }
 
         start.x = -halfGridSizeX;
         end.x = halfGridSizeX;
         end.z = start.z = -halfGridSizeZ;
 
         for (i = 0; i < gridSizeZ + 1; i++)
         {
             GL.Vertex(start);
             GL.Vertex(end);
             end.z = start.z = start.z + tileSize;
         }
 
         GL.End();
         GL.PopMatrix();
     }

@Limbo

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

122 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

Related Questions

How to change scene view flythrough WASD key bindings ? 1 Answer

How to change axis of rotation when holding Alt? [SceneView in Editor, NOT in-game] 1 Answer

Scene view resolution 0 Answers

Command Buffers for the SceneView Camera 0 Answers

How to create a curve editor? 0 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