• 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
Question by KristofferH · Aug 19, 2018 at 11:02 AM · combinemeshes

How to combine a mesh with a copy of itself?

I'm trying to write a script that scatters grass turfs on the ground at runtime, but I don't want to use Instantiate all of the time, since it is a slow process and it creates separate gameobjects. So what I would like to do is to use one Instantiate for the first turf and then simply copy that mesh (from the Mesh Filter) and offset it to a new position (determined using a raycast to the ground) and then combine them back together to one mesh, attached to only one game object. Here is my code so far:

     public GameObject grass;
     void Start()
     {
         Mesh orgGrassMesh = grass.GetComponent<MeshFilter>().sharedMesh;
         Mesh finalGrassMesh = new Mesh();
         finalGrassMesh.name = "All Grass";
 
         for (int i = 0; i < 36; i++) // scatter 36 grass turfs
         {
             Vector3 pos = new Vector3(Random.Range(-3f, 3f), 500f, Random.Range(-3f, 3f));
 
             RaycastHit hit;
             Ray ray = new Ray(pos, Vector3.down);
 
             if (Physics.Raycast(ray, out hit, 600f))
             {
                 Mesh newMesh = orgGrassMesh;
                 //newMesh = MoveNewMesh(newMesh, grass.transform.position, hit.point);
 
                 CombineInstance[] combine = new CombineInstance[1];
 
                 combine[0].mesh = newMesh;
                 combine[0].subMeshIndex = 0;
                 combine[0].transform = hit.transform.localToWorldMatrix;
 
                 finalGrassMesh.CombineMeshes(combine);
             }
         }
 
         grass.GetComponent<MeshFilter>().sharedMesh = finalGrassMesh;
     }

I also tried using a separate method for offsetting the turf mesh by moving all the vertices in the mesh. This method looks like this:

     private Mesh MoveNewMesh(Mesh mesh, Vector3 pivot, Vector3 newPos)
     {
         Vector3 posDif = newPos - pivot;
 
         for (int v = 0; v < mesh.vertices.Length; v++)
         {
             mesh.vertices[v] += posDif;
         }
 
         return mesh;
     }

In the first code example I have commented the call to this out, since I've tried using both methods, unfortunately none of the methods work. What happens is that I don't get any new mesh at all. Either the mesh is just the original mesh that is showing up, or all the meshes disappears and I don't see anything. Even when I try to inspect the mesh it still just says that it is 224 triangles (which is the count for the original mesh. So the mesh does not appear to be combined at all, nothing happens, or it just disappears.

So just to iterate. I want to Instantiate one GameObject with a mesh, and then just duplicate and offset that mesh in the scene (without creating new gameobjects) and then have all the duplicated meshes combined into one mesh with the original mesh.

Comment

People who like this

0 Show 2
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 myzzie · Aug 19, 2018 at 11:27 AM 0
Share

then have all the duplicated meshes combined into one mesh with the original mesh.

Then why are you trying to modify the vertices of the mesh directly? That is not possible.
avatar image KristofferH myzzie · Aug 19, 2018 at 11:38 AM 0
Share

Well, that was something I tried. I don't want the grass turfs to be in the same place. So I need some way to make a copy of the original mesh (in memory, without creating a new gameobject) and move it to a new position (and ultimately also a new random rotation along the Y axis) and then combine this modified copy with the original mesh.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by rainChu · Aug 19, 2018 at 11:54 AM

You're on the right track! You definitely should be using CombineMeshes. However, you're using it wrong. What you're doing is iterating through a for loop, and 36 times, you create the same mesh over and over, in a different position each time. CombineMeshes doesn't add onto what's already there. It combines the array of CombineInstance into a single mesh, using their transform.

Try something more like this:

 var combine = new List<CombineInstance>();
 
 for (int i = 0; i < 36; i++) // scatter 36 grass turfs
 {
     Vector3 pos = new Vector3(Random.Range(-3f, 3f), 500f, Random.Range(-3f, 3f));
     Ray ray = new Ray(pos, Vector3.down);
     RaycastHit hit;
 
     if (Physics.Raycast(ray, out hit, 600f))
     {
         var combineInstance = new CombineInstance();
         combineInstance.mesh = orgGrassMesh;
         combineInstance.subMeshIndex = 0;
         combineInstance.transform = hit.transform.localToWorldMatrix;
 
         combine.Add( combineInstance );
     }
 }

 finalGrassMesh.CombineMeshes(combine.ToArray());

Good luck!

Edit: I didn't spot this at first, because I was paying more attention to CombineInstance, but the scattering won't work because hit.transform is the Transform of the ground you hit, which is always the same. To get the 3D position of the point you hit, you should instead do this:

 combineInstance.transform = Matrix4x4.Translate( hit.point );

You can combine rotation as well, but I'm not the best with Matrices as I've never needed to use them. You can do this:

 // Edited to be in the correct order (See comments)
 combineInstance.transform = Matrix4x4.Translate( hit.point ) * Matrix4x4.Rotate( Quaternion.identity ) ;

And replace Quaternion.identity with your actual rotation. However, I'm sure this is not an optimized way of doing things. I'd ask a new question on how to efficiently create a matrix with a rotation, followed by a translation, if you want to do this.

Comment
KristofferH

People who like this

1 Show 4 · 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 KristofferH · Aug 19, 2018 at 12:12 PM 0
Share

Thanks! This solves the copying thing! :) I now have a mesh of 8064 triangles, but the grass turfs are all stacked on each other, so the scattering part is not working. So I get all the copies combined together, but they are still all in the same place. Any ideas on how to fix that issue? I tried modifying the vertex list with my MoveNewMesh() method, but that didn't help.

avatar image rainChu KristofferH · Aug 19, 2018 at 12:26 PM 0
Share

Edited my answer! I hope this fixes it!

avatar image KristofferH rainChu · Aug 19, 2018 at 12:54 PM 0
Share

Oops, my bad! :) Of course it should be hit.point, that was a typo :) But thanks for the matrix stuff! The scattering part now works as well! :) The rotation works if I swap the multiplication order around :) Anyway huge thanks for your help! :D

Show more comments

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

89 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

Related Questions

Combined Meshes for Dynamic Objects 2 Answers

combining littles sprites into one big sprite ? 1 Answer

CombineMeshes Error on Run-time Meshes 2 Answers

Combine Mesh for TextMeshPro 0 Answers

Combine meshes? 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