Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am attempting to transform the components of a mesh directly using a 4x4 matrix. This is working for the vertex positions, but it is not working for the normals (and probably not the tangents either).

Here is what I have:

// Transform vertex positions - Works like a charm!
vertices = mesh.vertices;
for (int i = 0; i < vertices.Length; ++i)
    vertices[i] = transform.MultiplyPoint(vertices[i]);

// Does not work, lighting is messed up on mesh
normals = mesh.normals;
for (int i = 0; i < normals.Length; ++i)
    normals[i] = transform.MultiplyVector(normals[i]).normalized;

// Tangents?? I really do not understand these guys
tangents = mesh.tangents;
for (int i = 0; i < tangents.Length; ++i) {
    Vector4 temp = transform.MultiplyVector(tangents[i]).normalized;
    temp.w = tangents[i].w;
    tangents[i] = temp;
}

Note: The input matrix converts from local to world space and is needed to combine multiple meshes together.

share|improve this question
    
If your normal matrix does not have uniform scale (ie same amount of scale in x,y,z directions), then it will screw up the normals. –  bobobobo Nov 26 '12 at 23:11
    
@bobobobo Should I have a different matrix for normals then? The built-in Unity Mesh.CombineMeshes seems to work fine with the same input matrix. But obviously it could be creating a normal matrix or something. Are you suggesting that I should be using transform.inverse.transpose.MultiplyVector instead? –  Lea Hayes Nov 26 '12 at 23:14
2  
Definitely don't do it like that! You'll be inverting the matrix for every vector you need to multiply. Try creating a second matrix normalMatrix = transform.inverse().transpose() and see if that solves your problem. A tangent is just a vector that "grazes" the surface. –  bobobobo Nov 27 '12 at 1:03
1  
@bobobobo: you should probably write that as an answer. –  Sean Middleditch Nov 27 '12 at 1:35
    
Awesome that seems to work! The lighting behaves a little differently from the original meshes in that they detect the light at different stages. But the normals appear to behave correctly and I am guessing that the tangents are also (though the w component might be messed up)... cheers! –  Lea Hayes Nov 27 '12 at 1:41
add comment

1 Answer 1

up vote 3 down vote accepted

If your normal matrix does not have uniform scale (ie same amount of scale in x,y,z directions), then it will screw up the normals.

Try creating a second matrix normalMatrix = transform.inverse().transpose() and see if that solves your problem. A tangent is just a vector that "grazes" the surface.

share|improve this answer
    
Thanks for your help! –  Lea Hayes Nov 27 '12 at 3:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.