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 /
This question was closed Apr 10, 2014 at 11:21 AM by Benproductions1 for the following reason:

The question is answered on stackoverflow

avatar image
0
Question by eduaglz · Apr 08, 2014 at 06:39 AM · c#meshrendering

Procedural mesh slows down scene

Im creating a pretty simple game where I need to create multiple Meshes. The code that I create is pretty simple and yet after having more than 8 Meshes at the same time, the peerformance reduces considerably to just a couple of fps (~8 fps). The Mesh that I create is just a simple square so I really don´t know where the problem is, here´s my code:

 using UnityEngine;
 using System.Collections;
 
 public class TetraGenerator : MonoBehaviour {
     public int slices;
     public GameObject forceSource;
     void OnMouseDown(){
         var arcLength = Mathf.PI / slices;
         var distance = 10;
         var height = 1;
         var origin = Random.Range(-slices,slices);
 
         Vector3[] vertices = new Vector3[4];
         vertices [0] = new Vector3 (Mathf.Cos(origin*arcLength),Mathf.Sin(origin*arcLength));
         vertices [1] = new Vector3 (Mathf.Cos(origin*arcLength),Mathf.Sin(origin*arcLength));
         vertices [2] = new Vector3 (Mathf.Cos((origin+1)*arcLength),Mathf.Sin((origin+1)*arcLength));
         vertices [3] = new Vector3 (Mathf.Cos((origin+1)*arcLength),Mathf.Sin((origin+1)*arcLength));
 
         vertices [0] *= distance;
         vertices [1] *= (distance+height);
         vertices [2] *= (distance+height);
         vertices [3] *= distance;
 
         Vector3 frameRef = new Vector3(Mathf.Cos(origin*arcLength+(arcLength/2)),Mathf.Sin(origin*arcLength+(arcLength/2)));
         frameRef *= distance;
 
         vertices [0] -= frameRef;
         vertices [1] -= frameRef;
         vertices [2] -= frameRef;
         vertices [3] -= frameRef;
 
         int[] triangles = new int[]{0,1,2,2,3,0};
 
         Mesh mesh = new Mesh ();
         mesh.vertices = vertices;
         mesh.triangles = triangles;
 
         GameObject tile = new GameObject("tile",typeof(MeshFilter),typeof(MeshRenderer));
         tile.transform.position = frameRef;
 
         MeshFilter meshFilter = tile.GetComponent<MeshFilter> ();
         meshFilter.mesh = mesh;
 
     }
 }
Comment
Add comment · Show 7
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 CodeElemental · Apr 08, 2014 at 07:42 AM 0
Share

It could be that the mesh is missing the normals required by the shaders. You should use the RecalculateNormals() method after you specify the meshes vertices and triangles.

        $$anonymous$$esh mesh = new $$anonymous$$esh ();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();



 
avatar image eduaglz CodeElemental · Apr 08, 2014 at 03:45 PM 0
Share

Thanks, I tried it, but it still stays the same =(

avatar image CodeElemental CodeElemental · Apr 09, 2014 at 07:02 AM 0
Share

You could also try adding the UV parameters of the mesh. Do you get any warnings in the console when you run your project ?

avatar image CodeElemental · Apr 09, 2014 at 07:10 AM 0
Share

You could try to add material and UV data to the gameobject that has the mesh renderer:

 mesh.uv = uvList; // uvList is an array of uv data for the mesh.
 
 GameObject tile = new GameObject("tile",typeof($$anonymous$$eshFilter),typeof($$anonymous$$eshRenderer));
 tile.renderer.material = m$$anonymous$$aterial; // You set m$$anonymous$$aterial to be a material instance.
 
 
avatar image Benproductions1 · Apr 09, 2014 at 10:14 AM 0
Share

The problem is you're constantly creating new vertex arrays and meshes. This is much more expensive than simply reusing the same mesh with the same vertex array, again and again. Every new mesh is a new OpenGL or DirectX object.

avatar image thinkwayne · Apr 09, 2014 at 10:49 AM 0
Share

As the comments have said constantly creating new objects on the fly like this will be expensive. If you absolutely have to generate meshes like this, then the first thing I would suggest is reduce the amount of trigonometry calls you are making, Sin and Cos functions can be costly and you are sometimes using the results twice so you could store the results in a variable and recycle.

avatar image eduaglz · Apr 09, 2014 at 04:42 PM 0
Share

The problem was that I wasn´t assigning the material, and the shader. Here´s the answer:

http://stackoverflow.com/questions/22926739/procedural-mesh-slows-down-unity-scene/22927214#22927214

Thanks everyone!

0 Replies

  • Sort: 

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

Fixing mesh normals 1 Answer

Where is the submesh Reference of vertex/triangle? 1 Answer

Distribute terrain in zones 3 Answers

Mesh creation by code not working? 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