• 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 /
This question was closed Feb 14, 2016 at 02:44 PM by joshua-lyness for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by joshua-lyness · Feb 13, 2016 at 08:14 PM · errorcombinemeshesexistscombineinstance

Combine Meshes combine with current mesh

Ok so I have a tree script that generates trees in a random location wit$$anonymous$$n a circle. To stop the trees from generating ontop of each other, when the raycast to generate the tree is fired downwards, if it collides with the tree collider it will re-raycast until it finds a point without an exisiting tree. For performance reasons I needed to merge the tree meshes together (each tree was making 3 draw calls, one for shadow, one for the light, and one for the object itself) and with up to 40^2 trees placeable, was making waaaaay too many batches. So long story short I made a mesh merge script that gets the mesh from every object with the tag "firTree" and then put those into the combineInstance variable. Then I use the combineMeshes function to put that into an empty gameObjects mesh filter, and disable all of the meshRenderers of the individual trees. Heres the Problem: Whats going on is every frame it is getting the mesh data from the individual trees and combining them. If I was to make thousands of trees, the fps would die from merging all of them every frame. Therefore I wanted to Destroy() the individual trees once they had been combined, however the trees disappear. So.....I made another combineInstance slot in its array for the current combined mesh, so that what would happen is it would combine, then destroy the original objects, and only combine new trees. However Unity throws up the error :

 Cannot combine into a mesh that is also in the CombineInstances input: 5,0_firTree
 UnityEngine.Mesh:CombineMeshes(CombineInstance[], Boolean, Boolean)
 meshMerge:Update() (at Assets/treeTool/firTree/meshMerge.cs:50)

so I assume that means it is already combining it to the current mesh?? but if so then why do the trees disappear when I destroy the individual ones? They should still be visible after I remove the original mesh data, right? Or does it need to continue referencing it? I don't even know what unity is doing xD

Anyway, just in case its helpful, ive got the combine script. If you are going to run it, remember to tag your objects as "firTree" for it to work ;)

 using System.Collections;
 
 public class meshMerge : MonoBehaviour {
 
     //the meshFilter of the combined tree object
     private MeshFilter filter;
 
     //the meshFilter array of gameObects to combine
     private Component[] meshFilterArray;
 
     //objects to combine 
     public GameObject[] toCombine;
 
     //the mesh instance to apply, special type for meshcombine function
     public CombineInstance[] toApply;
 
     void Start () {
 
         //sets combined filter to the current mesh filter (for reference optimisation)
         filter = GetComponent<MeshFilter>();
 
         //makes a plain mesh, with no current data (for merging purposes)
         //there always needs to be a mesh in t$$anonymous$$s location, otherwise when merging, the first time will generate
         //an error for no current mesh to combine with
         filter.mesh = new Mesh();
     }
 
     void Update () {
         //will put all gameObjects with the tag firTree in the array of gameObjects to combine
         toCombine = GameObject.FindGameObjectsWithTag ("firTree");
         int i = 0;
         //the combine instance for the combineMeshes, sets length to the gameObject array,  +1 for existing mesh 
         toApply = new CombineInstance[toCombine.Length + 1];
         //repeats for every gameObject
         foreach(GameObject tree in toCombine){
             //passes in the mesh data
             toApply[i].mesh = tree.GetComponent<MeshFilter> ().mesh;
             //converts the local positions to world positions in a matrix, so the combineMesh can use it (no idea why)
             toApply[i].transform = tree.GetComponent<MeshFilter> ().transform.localToWorldMatrix;
             Destroy (tree.gameObject);
             //disables the mesh renderer (commented out for experimentation purposes)
             //tree.GetComponent<MeshRenderer>().enabled = false;
             //increments i for the array location
             i ++;
         }
         //does the same process to the existing mesh (adds it to the combine instance
         toApply[i].mesh = filter.mesh;
         toApply[i].transform = filter.transform.localToWorldMatrix;
 
         //joins all meshes, set other parameters to true so it uses the matrix positions and make them a single mesh
         filter.mesh.CombineMeshes (toApply, true, true);
     }
 }

if you can enlighten me as to what I'm doing wrong, I'm very grateful. Thanks ;)

Comment
Add comment · Show 4
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 joshua-lyness · Feb 13, 2016 at 08:20 PM 0
Share
avatar image joshua-lyness joshua-lyness · Feb 13, 2016 at 08:37 PM 0
Share
avatar image joshua-lyness · Feb 13, 2016 at 10:24 PM 0
Share
avatar image joshua-lyness · Feb 14, 2016 at 01:54 PM 0
Share

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by joshua-lyness · Feb 14, 2016 at 02:43 PM

Hmm I figured it out myself. All that I needed to do was add a line before combining, to make a new mesh. So: filter.mesh = new mesh();

that way I wasn't writing to a mesh I had just read from, no idea why unity doesn't let you do that. If anyone could explain that would be great, but otherwise, its sorted.

Oh and a little tip if anyone uses my code, don't delete the destroy part in the foreach, otherwise the mesh size increases exponentially and causes unity to crash xD

Comment
Add comment · Show 3 · 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
avatar image string_username · Mar 07, 2017 at 09:32 AM 0
Share
avatar image joshua-lyness string_username · Mar 12, 2017 at 06:24 PM 0
Share
avatar image joshua-lyness string_username · Mar 18, 2017 at 09:22 PM 0
Share

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

39 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

Related Questions

New project doesn't start up 2 Answers

Enumaration error CS1025: Single-line comment or end-of-line expected 1 Answer

force close on new update 0 Answers

Showing Error CS1525: Unexpected symbol `)', expecting `;' or '{' ? 1 Answer

Error CS0246: The type or namespace name 'Condition' could not be found (are you missing a using directive or an assembly reference?) (CS0246) (Assembly-CSharp) 1 Answer


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