- Home /
Calculate Rotation based on mesh normals
i have a plane that i want to rotate to face a certain direction, but not using transform.rotation
but by rotating the vertices. i already have the code for rotating mesh vertices arround a center :
var path = GetPath();
var angle = Quaternion.FromToRotation(mf.mesh.normals[0], path[0].rotation.eulerAngles);
var center = mf.mesh.bounds.center;
for (int i = 0; i < mf.mesh.vertices.Length; i++)
{
mf.mesh.vertices[i] = RotateVertex(center, mf.mesh.vertices[i], angle);
}
public Vector3 RotateVertex(Vector3 center, Vector3 vert, Quaternion angle)
{
// rotates the vertex
vert = angle * (vert - center) + center;
return vert;
}
i only need to find a way to calculate the rotation of the mesh based on the direction and the mesh normals like this :
Answer by bdubbert · Sep 29, 2021 at 10:46 PM
Given only mesh normals you cannot calculate the rotation of the mesh fully, because you can always rotate the mesh about the normal and have a new mesh rotation but the same normal. You can calculate a mesh rotation by using Quaternion.LookRotation and feed in the normal. You will also have to feed in another vector, but you could feed in any vector that is orthogonal to the normal vector, because as I mentioned before there are really infinite rotations given a normal vector.
Also the order that you feed the vectors in depends on how your plane set up relative to the x/y/z axis. If "up" for the plane is in the same direction as the normals, then feed in the normal vector as up and the orthogonal vector as forward.
If you really want to get the ACTUAL EXACT rotation for the plane, you will need more information.
is there a way to find out which way my mesh is facing so i can know how to rotate it ?
I suppose you could always look at the vertex data too... but I guess at this point I would be curious to know why you can't just use transform.rotation?
Anyway, if you really want to figure out the orientation of the plane you can look at the 3 vertices comprising the first triangle of your mesh. If you look at the vectors that represent the shorter of the two sides of the triangle, then you have 2 of your 3 local axes.
For example, say you know that the first triangle in your mesh always corresponds to the upper left corner of the plane (looking at it head on), and that the forward vector of the plane object is parallel with the normal of the plane. Then, looking at the triangle, you know that the two sides of the triangle will be the right and down vectors. Then, using the normal vector and down vector you can get the rotation using Quaternion.LookRotation.
This requires you to know some data about the plane before hand. How is the plane oriented with respect to its local axes (does the forward vector point normal to the plane or parallel)? Where is the first triangle in the mesh, and which sides of the triangle point in which directions? Once you figure this stuff out though then as long as you are always using the same mesh you can figure out the orientation of the mesh.
Your answer
Follow this Question
Related Questions
Comparing a quaternion to a contact point normal 0 Answers
Rotate Player to Match Surface Normal While Still Maintaining Y Rotation 0 Answers
Rotating with a preferred driection. 0 Answers
Rotating an object to equal normals of object below 1 Answer
How to handler rotation of a Gameobject while aligning it with normals 1 Answer