• 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
2
Question by AaronG · Mar 24, 2012 at 12:03 AM · combinemeshes

Instanced Meshes are being offset to weird positions

I'm using the CombineMeshes script from the scripting reference pages...

 public class CombineMeshes : MonoBehaviour

{ void Start() { Component[] meshFilters = GetComponentsInChildren(); CombineInstance[] combine = new CombineInstance[meshFilters.Length]; int i = 0; while (i < meshFilters.Length) {

         combine[i].mesh = meshFilters[i].GetComponent<MeshFilter>().sharedMesh;
         combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
         //combine[i].transform = meshFilters[i].transform.worldToLocalMatrix;
         meshFilters[i].gameObject.active = false;
         i++;
     }
     transform.GetComponent<MeshFilter>().mesh = new Mesh();
     transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
     transform.gameObject.active = true;
 }

}

...and all is fine and dandy except for this really odd problem.

I have two GameObjects; one with CombineMeshes and another with a model called MeshInstance. MeshInstance sits inside CombineMeshes as a child object.

If I put CombineMeshes at a position of 0, 0, 0 and throw all my meshes inside it, all the instance meshes will be correctly positioned when the game is run. As you would expect...

However.

When I move CombineMeshes to anything other than 0, 0, 0 and run the game, all the instanced meshes are offset and I have no idea why. Take a look at this pic.

alt text

In this example, CombineMeshes is now at 32, 0, 0 and MeshInstance is at -32, 0, 0. When I run the game, MeshInstance is moved x axis by 32 units (to the right).

Why does it do this?

Comment
Add comment · Show 3
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 hijinxbassist · Mar 24, 2012 at 05:30 AM 0
Share

You double checked the childs position is set to 0,0,0 ?? And that you are moving the Combine$$anonymous$$eshes only and not the $$anonymous$$eshInstance. First thots when glancing at this.

Id be happy to help you debug your prob.

avatar image GameFreak · Mar 24, 2012 at 01:57 PM 0
Share

Check where your camera. It just might be in the wrong place

Try align with view?.

avatar image AaronG · Mar 24, 2012 at 02:54 PM 0
Share

@hijinxbassist: Yes. The child's position is at 0, 0, 0 relative to the parent object.

As far as I can tell, the child object is being translated by twice that of the position of the parent object.

IE:

Parent is 32, 0, 0. Child becomes 64, 0, 0 in world space.

Parent is 64, 0, 0. Child becomes 128, 0, 0 in world space.

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Zerot · Mar 24, 2012 at 09:27 PM

The reason why the child instances move, is because of the meshFilters[i].transform.localToWorldMatrix. This returns a world matrix, which includes the position and rotation of the parent. After combining, the parent matrix is applied again and therefore moves the children further along.

What you want, is or to combine the meshes when the parent is at 0,0,0, or build a different matrix using Matrix4x4.TRS and the localPosition, localRotation and localScale of the child object.

Comment
Add comment · Show 2 · 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 AaronG · Mar 25, 2012 at 03:52 PM 1
Share

I couldn't quite figure out how to use TRS so, ins$$anonymous$$d, I did the other thing you suggested by caching the position of the parent, setting it to 0, 0, 0, combining the meshes, then setting the transform.position to the cached value.

Danke.

avatar image g4borg · Jun 20, 2016 at 02:38 PM 7
Share

As pointed out by xdaniel0's answer,his linked script shows the alternative use, and as I stumbled on it aswell, I wanted to give a note on how to do it without moving:

  1. save parents' / gameobjects worldToLocal$$anonymous$$atrix: $$anonymous$$atrix4x4 myTransform = transform.worldToLocal$$anonymous$$atrix;

  2. use this "myTransform" wherever you use the opposite, localToWorld$$anonymous$$atrix to eli$$anonymous$$ate the difference in a multiplication: combine[i].transform = myTransform * meshFilters[i].transform.localToWorld$$anonymous$$atrix;

this will essentially relieve you of moving the object for the same effect, or having to understand anything about those $$anonymous$$atrices, and the occurences of using localToWorld$$anonymous$$atrix are usually easy to spot. Just wanted to note this for anyone stumbling on this.

avatar image
4

Answer by Squoktapus · Feb 24, 2015 at 03:45 AM

Take your parent transform.worldToLocalMatrix and cross it with each mesh transform.localToWorldMatrix that you're going to combine.

Good example here http://wiki.unity3d.com/index.php/CombineSkinnedMeshes

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
avatar image
0

Answer by Zerot · Mar 24, 2012 at 09:27 PM

The reason that is happening, is because of the meshFilters[i].transform.localToWorldMatrix. This matrix is a world matrix and thus includes the position and rotation of the parent. After the combining the parent matrix is applied again.

To solve this, you can either combine the meshes with the parent at position 0,0,0, or create a different matrix using Matrix4x4.TRS with the localPosition, localRotation and localScale of the child.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dynamic Batching, Combine Meshes? 1 Answer

Combining two mesh doesn't work 1 Answer

A way to combine 2 or more objects to make 1? 1 Answer

mesh.CombineMeshes messing with lighting and framerate 1 Answer

Mesh.CombineMesh() results in different heights between objects 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