x


Change mesh translation for future placements

Hi,

I have an object that I imported with several meshes. Some of them have nontrivial translations, and I would like to bake those translations in. The code I wrote was this :

private static void NormalizeMesh(MeshFilter mf)
    {
        Transform transform = mf.transform;
        if (transform.localPosition != Vector3.zero ||
            transform.localRotation != Quaternion.identity ||
            transform.localScale != Vector3.one)
        {
            Vector3[] vertices = mf.sharedMesh.vertices;
            Vector3[] normals = mf.sharedMesh.normals;
            for (int i = 0; i < mf.sharedMesh.vertexCount; i++)
            {
                vertices[i] = transform.TransformPoint(vertices[i]);
                normals[i] = transform.TransformDirection(normals[i]);
                //Tangents? UVs?
            }

            mf.sharedMesh.vertices = vertices;
            mf.sharedMesh.normals = normals;
            mf.transform.localPosition = Vector3.zero;
            mf.transform.localRotation = Quaternion.identity;
            mf.transform.localScale = Vector3.one;
        }

I want to be able to do this only once per mesh, and that the next time that I add the object to the scene hierarchy it will already be fixed. The code that I wrote takes care of the mesh for good, but it only takes care of the translation for the instance that I added. This is because the sharedMesh gets saved back to the model, but the changes to the mesh filter don't get changed - the next time I will add the model, the vertices will be baked but the transform will remain the original one.

Is there a way to modify the mesh's transform for future insertions into the scene?

more ▼

asked Sep 20 '10 at 02:16 PM

Noam gravatar image

Noam
282 24 24 33

(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

Call your function in AssetPostprocessor.OnPostprocessModel and modify the shared mesh.

more ▼

answered Sep 21 '10 at 07:12 AM

Paulius Liekis gravatar image

Paulius Liekis
7.3k 16 24 45

This sounds useful. Where is this documented?

Oct 03 '10 at 01:24 PM Noam
(comments are locked)
10|3000 characters needed characters left

You could save it as a prefab at the end of your script.

more ▼

answered Sep 20 '10 at 04:06 PM

StephanK gravatar image

StephanK
6k 39 53 93

The problem with that is that it is still possible to add the normalized mesh with the un-zeroed transformed afterwards, and the script will run again and transform the already transformed vertices.

If what I want to do is impossible, I'll try to solve the issue in the exporting software instead.

Sep 21 '10 at 11:08 AM Noam
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1357
x970
x556
x491

asked: Sep 20 '10 at 02:16 PM

Seen: 1210 times

Last Updated: Sep 20 '10 at 02:16 PM