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
1
Question by Saitodepaula · Aug 20, 2012 at 06:45 PM · transformpositionmatrixcombinemeshes

Modify position in transform matrix

I'm trying to use CombineMeshes, but I think the code in the docs assumes a previous position (maybe world origin, which is not the case with my meshes) so the resulting mesh is not located where I want it to be.

I think I should modify the transform matrix from CombineInstance using SetTRS, but I don't know how to do it, could someone explain and, if possible, show me an example?

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

3 Replies

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

Answer by DaveA · Aug 20, 2012 at 06:52 PM

It looks like the code in the docs converts all submesh's vertices to world coordinates:

combine[i].transform = meshFilters[i].transform.localToWorldMatrix;

I imagine it does this because if it used local coords, they might all appear in seemingly random places near the local origin. You could try removing that localToWorldMatrix part, or you could use InverseTransformPoint as a post-process. That is, after all submeshes are combined into world space, inverse-transform them back to local space (of the parent object) using that.

Comment
Add comment · Show 6 · 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 Saitodepaula · Aug 20, 2012 at 07:40 PM 0
Share

Thanks @DaveA, but if I just write

combine[i].transform = meshFilters[i].transform;

I get an error "Cannot convert 'UnityEngine.Transform' to 'UnityEngine.$$anonymous$$atrix4x4'. That's why I've asked about how to change the transform matrix.

To use InverseTransformPoint, I think I have to make some conversion, because if I try

combine[i].transform.position = transform.InverseTransformPoint(combine[i].transform.position);

I get the error that 'position' is not a member of 'UnityEngine4x4'.

I'm using #pragma strict if that matters.

avatar image DaveA · Aug 20, 2012 at 08:38 PM 0
Share

Ah right it needs that 4x4 thing. So I would go with the second suggestion. Use the code as-is, but at the bottom of the routine, after the Combine has happened, then loop through the resulting vertices and invert the coords. This likely means making a whole new vertex list (same length) and using the transform.InverseTransformPoint on each vertex which was the result of the Combine. That new list will be in local coords (of the transform you gave it, presumably the parent object). Then set that vertex list to the mesh and it should be ok.

avatar image Saitodepaula · Aug 20, 2012 at 09:12 PM 0
Share

Thanks it worked. I was trying to set the position back to local for each combined mesh inside the for loop. Looping through the resulting vertices after the combine was easy.

avatar image jialoveli · Jan 28, 2015 at 02:32 PM 0
Share

coule you tell me the solution? I come with the same question.

avatar image MFKJ · Sep 23, 2015 at 07:13 AM 0
Share

so what is the final code for retaining the postion ?? please share

Show more comments
avatar image
5

Answer by Almar · May 02, 2015 at 08:04 PM

This did the trick for me, worked instantly:

 Matrix4x4 pTransform = parent.transform.worldToLocalMatrix;
 
 for (int i = 0; i < meshFilters.Length; i++)
 {
    combine[i].mesh = meshFilters[i].sharedMesh;
    combine[i].transform = pTransform * meshFilters[i].transform.localToWorldMatrix;
 }

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 ABerlemont · Dec 02, 2015 at 04:10 PM 0
Share

That did the trick for me. Thanks.

avatar image Bunny83 · Dec 02, 2015 at 04:58 PM 0
Share

Yes, this is the usual usecase. By combining the two matrices you can directly transform a localspace point from one child object into the local space of the parent object.

The same procedure actually happens in the shader where the "mvp" matrix is the combination of the objects localToWorld matrix (the **$$anonymous$$**odel matrix), the cameras worldToLocal matrix (the **V*iew matrix) and finally the the cameras P*rojection matrix which projects the camera-local 3d point into a 2d point on the screen. So by multiplying a local point of a mesh with the objects mvp matrix you directly convert it from local space into viewport space.

avatar image
1

Answer by MLM · May 04, 2014 at 05:06 AM

Since there doesn't seem to be a solution in either this thread or this related one:

  • Instanced Meshes are being offset to weird positions

The snippet below will keep your mesh from translating or rotating. Even works with scale.

Here is my solution:

 CombineInstance[] combine = new CombineInstance[meshs.Length];
 for(int meshIndex = 0; meshIndex < meshs.Length; meshIndex++) 
 {
     combine[meshIndex].mesh = meshs[meshIndex];
     combine[meshIndex].transform = Matrix4x4.TRS(gameObject.transform.InverseTransformPoint(gameObject.transform.position), Quaternion.Inverse(gameObject.transform.rotation), Vector3.one);
 }
 
 if(mFilter)
 {
     mFilter.mesh.CombineMeshes(combine);
 
     mFilter.mesh.RecalculateBounds();
     mFilter.mesh.RecalculateNormals();
 }
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

14 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

Related Questions

Match position of two object without making one a child of the other 4 Answers

Transform.position assign attempt error - NaN 0 Answers

transform.position question 1 Answer

Dragging prefab into hierarchy reset position now? 0 Answers

Use methods of Transform without linked gameObject 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