• 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 /
avatar image
0
Question by trevk84 · Feb 13, 2016 at 08:15 PM · combinemeshes

Combined meshes migrate farther and farther away from parent

Procedurally, I'm creating a world of zones, each one 50x50 units. Each zone is simply a quad. It looks like a chess board, where the bottom right zone is (0, 0), the top right is (x*10, x*10), there are no negative positioned zones. I next randomly place objects(trees) on each zone as a child to that zone. I then use the following code to combine all the objects(trees) into one mesh as a child under their respective zone.

 [RequireComponent(typeof(MeshFilter))]
 [RequireComponent(typeof(MeshRenderer))]
 public class CombineMeshes : MonoBehaviour {
 
    void Start()
    {
        MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
        CombineInstance[] combine = new CombineInstance[meshFilters.Length];
        int i = 0;
        while (i < meshFilters.Length)
        {
             combine[i].mesh = meshFilters[i].sharedMesh;
             combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
             meshFilters[i].gameObject.SetActive(false);
             i++;
        }
         transform.GetComponent<MeshFilter>().mesh = new Mesh();
         transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
         transform.gameObject.SetActive(true);
     }
 }

Zone (0, 0) works perfectly, but the farther away the zone is from origin, the farther away the gameObjects(trees) get placed. As you can see from the picture by placing each gameObject(tree) in the center of the zone, there an obvious a pattern to it, but I clearly don't understand why. Tree "A" is correct, Tree "B" is 50 units off, Tree "C" is 100 and so on...

alt text

unitypick.jpg (49.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

Answer by trevk84 · Feb 13, 2016 at 08:50 PM

So the answer came to me in a dream...and some trial and error over breakfast. Here is a working code that combines meshes one at a time (each frame in a coroutine) in the correct locations. Evidently the main problem was that combining meshes need to be done around world origin (0, 0, 0). This isn't cut-and-paste code, rather written to make more sense, and I'm sure my comments aren't completely accurate, but it does work as intended for me.

         while numberOfTreesToBePlaced > 0)
         {
             //Random placement of Tree inside Zone boundaries
             posX = Random.Range(0, zoneSize);
             posZ = Random.Range(0, zoneSize);
 
             //Raycast at random position created above
             Ray rayTarget = new Ray(new Vector3(transform.position.x + posX, rayStartPoint, transform.position.z + posZ), Vector3.down);
             if (Physics.Raycast(rayTarget, out rayHit, rayBeamLength))
             {
                 //To make sure raycast hit the ground so our tree isn't floating in air
                 if (rayHit.collider.tag == "Zone")
                 {
                     //Retrieve Tree mesh (Probably can be simplified since this gameObject will be destroyed soon)
                     GameObject newObj = (GameObject)Instantiate(Resources.Load("Prefabs/Pine1.1High"));
 
                     //The center of each Zone is the bottom left corner, so place newObj as if the Zone was positioned at (0, 0), but use the rayCast Y value
                     newObj.transform.position = new Vector3(posX, rayHit.point.y, posZ);
 
                     //Retrieve Tree mesh to be copied
                     MeshFilter copyMesh = newObj.GetComponent<MeshFilter>();
                     CombineInstance[] combine = new CombineInstance[2];
 
                     //"single Mesh" is the main mesh that new meshes are combined with, it is a GameObject
                     combine[0].mesh = singleMesh.GetComponent<MeshFilter>().sharedMesh;
                     //Instead of moving singleMesh to world center, I already have a gameObject at (0, 0, 0), called WorldCenter.  I just steal it's coordinates
                     //and pretend that is where singleMesh is at.
                     combine[0].transform = worldCenter.transform.localToWorldMatrix;
 
                     //Grab my Tree mesh and it's coordinates
                     combine[1].mesh = copyMesh.sharedMesh;
                     combine[1].transform = copyMesh.transform.localToWorldMatrix;
 
                     //Deletes original singleMesh
                     singleMesh.transform.GetComponent<MeshFilter>().mesh = new Mesh();
 
                     //Combines singleMesh in CombineInstance[] with Tree mesh
                     singleMesh.transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
 
                     //Destroys gameObject that had the Tree mesh I needed
                     Destroy(newObj);
                 }
             }
             numberOfTreesToBePlaced--;
         }
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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how to combine SkinnedMeshRendera with part of same materials and renmain animation 0 Answers

Unity Editor Crash when trying to combine meshes 0 Answers

Mesh combining makes an incomplete mesh? 0 Answers

Render lot of Game Object 0 Answers

Combined meshes are invisible ("Combine mesh instance 0 is null") 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