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 /
avatar image
0
Question by seandolan · Jul 02, 2018 at 04:41 PM · 3doptimizationprocedural mesh

Procedural mesh redrawing textures causes in game fps drop. Is there a way to optimise this?

I have a procedural mesh drawing a grid of floor textures on in a 3D environment. The player can change the tiles by drawing on them. I am experiencing a significant drop in fps just doing the updating of the new textures. I know enough to understand that this is creating a lot of work, but not enough to understand how to optimise this. I added 2 boolean's that just flag instead of running the update in one whole chunk and this has helped out with about a 10% reduction in the lag it's all causing. Any help would be greatly appreciated. I feel that there should be a more efficient way to run the UpdateMesh as it feels like it's doing more work than it would need to, just to update the new floor tiles.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BuildGrid:MonoBehaviour {
 
     public int floor = 0;
 
     public bool buildPending = false;
     public bool updatePending = false;
 
     List<Vector3> newVertices = new List<Vector3>();
     List<int> newTriangles = new List<int>();
     List<Vector2> newUV = new List<Vector2>();
 
     List<Vector3> colVertices = new List<Vector3>();
     List<int> colTriangles = new List<int>();
     int colCount;
 
     Mesh mesh;
     MeshCollider col;
 
     float tUnit = 0.1f;
 
     int squareCount;
 
     void Start() {
         mesh = GetComponent<MeshFilter>().mesh;
         col = GetComponent<MeshCollider>();
         buildPending = true;
     }
 
     void FixedUpdate() {
         if(buildPending) {
             buildPending = false;
             BuildMesh();
         }
         if(updatePending) {
             updatePending = false;
             UpdateMesh();
         }
     }
 
     void BuildMesh() {
         for(int px = 0; px < FloorManager.Instance.width; px++) {
             for(int py = 0; py < FloorManager.Instance.height; py++) {
                 GenCollider(px,py);
                 GenSquare(px,py,TextureMap(StoreManager.Instance.GetStoreColor(FloorManager.Instance.floorLayout[floor,px,py])));
             }
         }
         updatePending = true;
     }
     void UpdateMesh() {
         mesh.Clear();
         mesh.vertices = newVertices.ToArray();
         mesh.triangles = newTriangles.ToArray();
         mesh.uv = newUV.ToArray();
         mesh.RecalculateNormals();
 
         newVertices.Clear();
         newTriangles.Clear();
         newUV.Clear();
         squareCount = 0;
 
         Mesh newMesh = new Mesh {
             vertices = colVertices.ToArray(),
             triangles = colTriangles.ToArray()
         };
         col.sharedMesh = newMesh;
 
         colVertices.Clear();
         colTriangles.Clear();
         colCount = 0;
     }
 
     Vector2 TextureMap(int _number) {
         return new Vector2((_number - (_number % 10)) / 10,_number % 10);
 
     }
 
     void GenCollider(int x,int y) {
         colVertices.Add(new Vector3(x,0,y - 1));
         colVertices.Add(new Vector3(x,0,y));
         colVertices.Add(new Vector3(x + 1,0,y));
         colVertices.Add(new Vector3(x + 1,0,y - 1));
 
         ColliderTriangles();
 
         colCount++;
     }
 
     void ColliderTriangles() {
         colTriangles.Add(colCount * 4);
         colTriangles.Add((colCount * 4) + 1);
         colTriangles.Add((colCount * 4) + 3);
         colTriangles.Add((colCount * 4) + 1);
         colTriangles.Add((colCount * 4) + 2);
         colTriangles.Add((colCount * 4) + 3);
     }
 
     void GenSquare(int x,int y,Vector2 texture) {
         newVertices.Add(new Vector3(x,0,y));
         newVertices.Add(new Vector3(x + 1.0f,0,y));
         newVertices.Add(new Vector3(x + 1.0f,0,y - 1.0f));
         newVertices.Add(new Vector3(x,0,y - 1.0f));
 
         newTriangles.Add(squareCount * 4);
         newTriangles.Add((squareCount * 4) + 1);
         newTriangles.Add((squareCount * 4) + 3);
         newTriangles.Add((squareCount * 4) + 1);
         newTriangles.Add((squareCount * 4) + 2);
         newTriangles.Add((squareCount * 4) + 3);
 
         newUV.Add(new Vector2(tUnit * texture.x + 0.01f,tUnit * texture.y + tUnit - 0.01f));
         newUV.Add(new Vector2(tUnit * texture.x + tUnit - 0.01f,tUnit * texture.y + tUnit - 0.01f));
         newUV.Add(new Vector2(tUnit * texture.x + tUnit - 0.01f,tUnit * texture.y + 0.01f));
         newUV.Add(new Vector2(tUnit * texture.x + 0.01f,tUnit * texture.y + 0.01f));
 
         squareCount++;
     }
 
 }


Texture for Colours

tilesheet.png (40.3 kB)
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 seandolan · Jul 03, 2018 at 06:58 AM 0
Share

Would breaking the following code into parts (like 0 to 9, then 10 to 19, etc) improve the performance?

 void Build$$anonymous$$esh() {
          for(int px = 0; px < Floor$$anonymous$$anager.Instance.width; px++) {
              for(int py = 0; py < Floor$$anonymous$$anager.Instance.height; py++) {
                  GenCollider(px,py);
                  GenSquare(px,py,Texture$$anonymous$$ap(Store$$anonymous$$anager.Instance.GetStoreColor(Floor$$anonymous$$anager.Instance.floorLayout[floor,px,py])));
              }
          }
          updatePending = true;
      }

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

105 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

Related Questions

Texture2D consuming too much memory 1 Answer

How Would I Find Out if a Player can See a Certain Face of a Mesh? 1 Answer

How to optimize 3D game on mobile? 0 Answers

Determining where a mesh triangle faces 1 Answer

What are the best shaders for mobile platforms ? 2 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