- Home /
Real pivot rotation of all vertices in a mesh?
I need to rotate a mesh by actually moving all the vertices, NOT by changing the transform.rotation of the gameobject. In other words, the mesh should rotate by the vertices, but the tranform.rotation of the mesh remains unchanged at 0,0,0.
So, say I have a vertex located at x,y,z of 50,0,50... how would I rotate that vertex by 60 degrees clockwise around the y axis? Assuming the pivot point of the mesh is centered at 0,0,0?
You can use the Rotate$$anonymous$$esh() editor script in the following question as a basis for your code:
http://answers.unity3d.com/questions/561786/how-to-export-obj-from-editor-with-rescaled-mesh.html
Actually, it looks like there is a unity function just for this http://docs.unity3d.com/ScriptReference/$$anonymous$$atrix4x4.TRS.html
I've converted my answer to a comment because for the actual rotation, @Cherno's answer is right and the code I pointed you towards does not do what you asked. You don't need a $$anonymous$$atrix4x4 to solve your problem. You only have to to multiply the vertices by a Quaternion representing your rotation.
But there are a few other things to consider that are handled in the code above. For example, you will need to either rotate the normals or recalculate the normals, and recalculate the bounds. And if your object has a collider, you need to address the changed geometry.
Answer by Cherno · Nov 23, 2014 at 05:41 PM
I do it like this:
Vector3 center = new Vector3(x, y, z);//any V3 you want as the pivot point.
Quaternion newRotation = new Quaternion();
newRotation.eulerAngles = new Vector3(0,0,0);//the degrees the vertices are to be rotated, for example (0,90,0)
for(int i = 0; i < vertices.Count; i++) {//vertices being the array of vertices of your mesh
vertices[i] = newRotation * (vertices[i] - center) + center;
}
Answer by BergOnTheJob · Jul 10, 2020 at 05:04 PM
if anyone is looking, this tool can move, rotate, and scale a mesh's vertices without changing the object's transform.
https://assetstore.unity.com/packages/slug/166155
it also has a bunch of other modeling tools if you want to further edit your mesh.
Your answer
Follow this Question
Related Questions
Pivot modifying script 0 Answers
Help with gun accuracy in degrees. 3 Answers
Constrain object rotation set by mouse? 1 Answer