Search Unity

Rotate vertices around pivot

Discussion in 'Scripting' started by Precursor13, Feb 19, 2012.

  1. Precursor13

    Precursor13

    Joined:
    Jun 22, 2010
    Posts:
    142
    Hey all, for a particular project I'm doing, I need to create a dynamic mesh to emulate the affects of linerenderers on a large scale. The problem I'm running into is that I'm not really sure how to go about have the 4 vertices rotate around the origin so the 2 triangles are oriented at the camera. Basically, I'm just trying to come up with a function that takes 4 vertices and an origin, than transforms them so that the faces created by them are at the camera (like the line renderer does). Any help would be greatly appreciated!

    No, I can't just use the linerender class itself, because this will need to be highly optimized on a mobile device to limit both drawcalls and function mesh update calls.

    Edit: I'm inquiring as to the quaternion math itself, not the theory or technicality involved in rotating points in 3d space.
     
    Last edited: Feb 20, 2012
  2. The Pirate Duck

    The Pirate Duck

    Joined:
    Apr 30, 2013
    Posts:
    1
    Code (CSharp):
    1. for (int n = 0; n < vertices.Length; n++) {
    2.            
    3.    vertices[n] = rotation * (vertices[n] - centerPoint) + centerPoint;
    4.            
    5. }
    Where vertices[n] is the vertex you want to rotate, rotation is a quaternion describing the rotation (duh) and the centerPoint is the point you want to rotate around.

    Yes, I know this is close to three years old :D
     
    Ryan900, Fly81, N4ma3 and 4 others like this.
  3. Max-Bot

    Max-Bot

    Joined:
    Sep 25, 2013
    Posts:
    83
    Good stuff.

    1) Reversing Matrices operation "Transform Move" by (vertices[n] - centerPoint).
    2) Applying rotation via rotation *.
    3) Applying "Transform Move" back again with + centerPoint.

    Very good and useful.
     
  4. BergOnTheJob

    BergOnTheJob

    Joined:
    Oct 9, 2019
    Posts:
    10