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 /
  • Help Room /
avatar image
0
Question by Maverick283 · Nov 08, 2018 at 08:53 AM · meshrendering

Improve Mesh generation and rendering performance

Hello everyone,

since I just recently started looking into meshes, how they work, what they do and so on, I decided to use my own calculations to create a mesh of a circle. Unfortunately though, this is really, really slow! So I am looking for tips on improvements, to make it slow only (because that's probably the best it will get...)

Here is the code I use to generate a circle:

 public static void createCircle(MeshFilter meshFilter, float innerRadius, float outerRadius, Color color, float xPosition = 0, float yPosition = 0, float startDegree = 0, float endDegree = 360, int points = 100)
         {
             Mesh mesh = meshFilter.mesh;
             mesh.Clear();
             //These values will result in no (or very ugly in the case of points < 10) circle, so let's safe calculation and just return an empty mesh!
             if (startDegree == endDegree || points < 10 || innerRadius >= outerRadius || innerRadius < 0 || outerRadius <= 0)
             {
                 return;
             }
             //The points for the full circle shall be whatever is given but if its not the full circle we dont need all the points!
             points = (int)(Mathf.Abs(endDegree - startDegree) / 360f * points);
             //We always need an uneven number of points!
             if (points % 2 != 0) { points++; }
             Vector3[] vertices = new Vector3[points];
             float degreeStepSize = (endDegree - startDegree) * 2 / (points - 3);
             float halfRadStepSize = (degreeStepSize) * Mathf.Deg2Rad / 2f;
             float startRad = Mathf.Deg2Rad * startDegree;
             float endRad = Mathf.Deg2Rad * endDegree;
             //Let's save the vector at the beginning and the one on the end to make a perfectly straight line
             vertices[0] = new Vector3(Mathf.Sin(startRad) * outerRadius + xPosition, Mathf.Cos(startRad) * outerRadius + yPosition, 0);
             vertices[vertices.Length - 1] = new Vector3(Mathf.Sin(endRad) * innerRadius + xPosition, Mathf.Cos(endRad) * innerRadius + yPosition, 0);
             for (int i = 1; i < vertices.Length - 1; i++)
             {
                 //Pure coinsidence that saved some calculatons. Half Step Size is the same as what would needed to be calculated here!
                 float rad = (i - 1) * halfRadStepSize + startRad;
                 if (i % 2 == 0)
                 {
                     vertices[i] = new Vector3(Mathf.Sin(rad) * outerRadius + xPosition, Mathf.Cos(rad) * outerRadius + yPosition, 0);
                 }
                 else
                 {
                     vertices[i] = new Vector3(Mathf.Sin(rad) * innerRadius + xPosition, Mathf.Cos(rad) * innerRadius + yPosition, 0);
                 }
             }
             mesh.vertices = vertices;
 
             int[] tri = new int[(vertices.Length - 2) * 3];
             for (int i = 0; i < (vertices.Length - 2); i++)
             {
                 int index = i * 3;
                 if (i % 2 == 0)
                 {
                     tri[index + 0] = i + 0;
                     tri[index + 1] = i + 2;
                     tri[index + 2] = i + 1;
                 }
                 else
                 {
                     tri[index + 0] = i + 0;
                     tri[index + 1] = i + 1;
                     tri[index + 2] = i + 2;
                 }
             }
             mesh.triangles = tri;
             Vector3[] normals = new Vector3[vertices.Length];
             Color[] colors = new Color[vertices.Length];
             for (int i = 0; i < vertices.Length; i++)
             {
                 normals[i] = Vector3.forward;
                 colors[i] = color;
             }
             mesh.normals = normals;
             mesh.colors = colors;
 
             meshFilter.mesh = mesh;
         }
 

I know I "could just use the LineRenderer shipped with Unity, it is faster then anything you'll ever write", but that's not the point here. I am trying to understand meshes and see where I can tweak my code to improve it's performance

Thanks for your help in advance!

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

0 Replies

· Add your reply
  • Sort: 

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

179 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Additive based material not rendering on some objects 0 Answers

Trying to get a completely transparent mesh to hide parts of another visible mesh it overlaps. 0 Answers

Create 3D object from external camera view 0 Answers

Obj model invisible in game 0 Answers

Black suface glitch 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