- Home /
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?
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.
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.
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.
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.
coule you tell me the solution? I come with the same question.
so what is the final code for retaining the postion ?? please share
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;
}
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.
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:
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();
}
Your answer
