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 thisisprojectemerald · Nov 09, 2020 at 02:42 PM · optimizationcombinemeshescombineinstance

Combing Meshes not working as expected?

I'm working on a Uni project where I use 3d noise to create terrain. I'm currently trying to speed up my meshes by combining them using mesh.CombineMeshes. I created one script that uses a lot of .AddComponent and .GetComponent which is obviously quite slow but works. The code for which is:

 private void CreateMesh()
     {
         int meshCount = 0;
         List<MeshFilter> meshFilters = new List<MeshFilter>();
 
         for (int i = 0; i < chunkWidth; i++) //x
         {
             for (int j = 0; j < chunkHeight; j++) //y
             {
                 for (int k = 0; k < chunkWidth; k++) //z
                 {
                     if (_thisChunk.ChunkData[i, j, k] > threshold)
                     {
                         List<Direction> dirs = new List<Direction>();
                         for (int l = 0; l < 6; l++)
                         {
                             if (!GetIfNeighbour(new Vector3(i, j, k), (Direction) l))
                             {
                                 dirs.Add((Direction) l);
                             }
                         }
 
                         if (dirs.Count > 0)
                         {
                             GameObject terrainObj = CreateTerrainFace(
                                 new Vector3(_thisChunk.chunkPos.x + i, j, _thisChunk.chunkPos.y + k),
                                 dirs);
                             meshFilters.Add(terrainObj.GetComponent<MeshFilter>());
                             meshCount++;
                             Destroy(terrainObj);
                         }
                     }
                 }
             }
         }
         
         CombineInstance[] combine = new CombineInstance[meshCount];
         int count = 0;
         foreach (var meshsF in meshFilters)
         {
             combine[count].mesh = meshsF.sharedMesh;
             combine[count].transform = meshsF.transform.localToWorldMatrix;
             count++;
         }
         
         MeshFilter tempFilter = new MeshFilter();
 
         tempFilter = _thisChunk.gameObject.AddComponent<MeshFilter>();
         tempFilter.mesh = new Mesh();
         tempFilter.mesh.CombineMeshes(combine);
 
         _thisChunk.gameObject.GetComponent<MeshFilter>().mesh = tempFilter.mesh;
         _thisChunk.gameObject.AddComponent<MeshRenderer>().material = basicMat;
         
         _thisChunk.firstBlockInChunk = true;
         
         _thisChunk.isRendered = true;
     }
 
 private GameObject CreateTerrainFace(Vector3 pos, List<Direction> dirs)
     {
         GameObject terrainObj = new GameObject(pos.ToString());
         MeshFilter[] meshFilters = new MeshFilter[6];
         terrainObj.AddComponent<MeshFilter>().mesh = new Mesh();
         CombineInstance[] CombineMeshes = new CombineInstance[6];
         int meshCount = 0;
         foreach (var dir in dirs)
         {
             Mesh mesh = new Mesh();
             mesh.vertices = _planes[(int) dir].vertices;
             mesh.triangles = _planes[(int) dir].planeTriangles;
             mesh.uv = _planes[(int) dir].uvs;
             mesh.RecalculateNormals();
 
             GameObject meshObj = new GameObject(dir.ToString());
             meshObj.AddComponent<MeshFilter>().mesh = mesh;
             meshFilters[meshCount] = meshObj.GetComponent<MeshFilter>();
 
             Destroy(meshObj);
             //meshObj.transform.SetParent(terrainObj.transform);
 
             meshCount++;
         }
 
         //meshFilters = terrainObj.GetComponentsInChildren<MeshFilter>();
         for (int i = 0; i < meshCount; i++)
         {
             CombineMeshes[i].mesh = meshFilters[i].sharedMesh;
             CombineMeshes[i].transform = meshFilters[i].transform.localToWorldMatrix;
         }
 
         MeshFilter tempFilter = new MeshFilter();
 
         tempFilter = terrainObj.transform.GetComponent<MeshFilter>();
         tempFilter.mesh = new Mesh();
         tempFilter.mesh.CombineMeshes(CombineMeshes);
 
         terrainObj.GetComponent<MeshFilter>().mesh = tempFilter.mesh;
         terrainObj.AddComponent<MeshRenderer>().material = basicMat;
 
         terrainObj.transform.position = pos;
         terrainObj.transform.eulerAngles = new Vector3(0, -90, 0);
         terrainObj.transform.SetParent(_thisChunk.transform);
         return terrainObj;
         //_thisChunk.blockList.Add(terrainObj);
     }

This produces a result that looks like this: alt text

My "improved" script looks like this:

 private void CreateCombinedMesh()
     {
         int meshCount = 0;
         List<Mesh> meshFilters = new List<Mesh>();
 
         for (int i = 0; i < chunkWidth; i++) //x
         {
             for (int j = 0; j < chunkHeight; j++) //y
             {
                 for (int k = 0; k < chunkWidth; k++) //z
                 {
                     if (_thisChunk.ChunkData[i, j, k] > threshold)
                     {
                         List<Direction> dirs = new List<Direction>();
                         for (int l = 0; l < 6; l++)
                         {
                             if (!GetIfNeighbour(new Vector3(i, j, k), (Direction) l))
                             {
                                 dirs.Add((Direction) l);
                             }
                         }
 
                         if (dirs.Count > 0)
                         {
                             meshFilters.Add(CreateCombinedTerrain(
                                 new Vector3(_thisChunk.chunkPos.x + i, j, _thisChunk.chunkPos.y + k),
                                 dirs));
                             
                             meshCount++;
                             
                         }
                     }
                 }
             }
         }
         
         CombineInstance[] combine = new CombineInstance[meshCount];
         int count = 0;
         
         foreach (var meshsF in meshFilters)
         {
             combine[count].mesh = meshsF;
             combine[count].transform = _thisChunk.gameObject.transform.localToWorldMatrix;
             count++;
         }
 
         MeshFilter tempFilter = _thisChunk.gameObject.AddComponent<MeshFilter>();
         
         tempFilter.mesh = new Mesh();
         tempFilter.mesh.CombineMeshes(combine);
         
         _thisChunk.gameObject.AddComponent<MeshRenderer>().material = basicMat;
         
         
         _thisChunk.isRendered = true;
     }
 
     private Mesh CreateCombinedTerrain(Vector3 pos, List<Direction> dirs)
     {
         Mesh[] meshesToCombine = new Mesh[dirs.Count];
         CombineInstance[] combine = new CombineInstance[dirs.Count];
         Mesh mesh = new Mesh();
         int meshCount = 0;
         
         foreach (var dir in dirs)
         {
             mesh.vertices = _planes[(int) dir].vertices;
             mesh.triangles = _planes[(int) dir].planeTriangles;
             mesh.uv = _planes[(int) dir].uvs;
             mesh.RecalculateNormals();
 
             meshesToCombine[meshCount] = mesh;
 
             meshCount++;
         }
 
         for (int i = 0; i < meshCount; i++)
         {
             combine[i].mesh = meshesToCombine[i];
             combine[i].transform = Matrix4x4.TRS(pos, Quaternion.Euler(0,-90,0), Vector3.one);
         }
         
         Mesh returnMesh = new Mesh();
         returnMesh.CombineMeshes(combine);
         return returnMesh;
     }

Which produces:

alt text

It seems like my new code is getting confused at the edges of the mesh but i don't know why. Is there something im missing when I avoid using mesh filters and components? Any ideas would be appreciated.

I can provide the Git repository as an edit, if that would help.

Thanks,

Blackclaw_

working.png (256.8 kB)
notworking.png (234.1 kB)
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
Best Answer

Answer by thisisprojectemerald · Nov 09, 2020 at 04:18 PM

Ive fixed the issue. Inside:

 foreach (var dir in dirs)
             {
                 mesh.vertices = _planes[(int) dir].vertices;
                 mesh.triangles = _planes[(int) dir].planeTriangles;
                 mesh.uv = _planes[(int) dir].uvs;
                 mesh.RecalculateNormals();
     
                 meshesToCombine[meshCount] = mesh;
                 
                 meshCount++;
             }
 

I didn't include:

 mesh = new Mesh();

And each time I was going through the foreach loop i was replacing all the meshes in meshesToCombine with the latest mesh.

TL;DR: Don't be an idiot and think about references.

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

144 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

Related Questions

MeshFilter and CombineMesh Doesn't Build Properly 1 Answer

Using Mesh.CombienMeshes. 0 Answers

Mesh Combine Increases Triangle Count 0 Answers

Combining meshes doesn't reduce triangle count 0 Answers

Combine (skinned) submeshes causes increased vertcount(for each submesh the whole vertcount) 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