Search Unity

how can I rotate a vertex array around the transform pivot?

Discussion in 'Scripting' started by dennik, Nov 3, 2014.

  1. dennik

    dennik

    Joined:
    Nov 23, 2011
    Posts:
    101
    I'm writing a script that moves the vertices of an object around, conforming them to the surface below, and everything works well, except when the transform gets rotated.
    In that case very weird things happen.
    Is there any way I can add the transform.rotation on top of the position coordinates for each vertex?
     
  2. dennik

    dennik

    Joined:
    Nov 23, 2011
    Posts:
    101
    Here is a sample script to make my point more clear. Apply this on a plane, parented to something (an empty game object would do fine), and put another object (with mesh collider) under the plane to deform to its surface.
    As you move the parent, everything goes well, the plane slides and deforms to any curve as expected.
    But if you start rotating, all hell breaks lose.
    Code (CSharp):
    1.     Mesh mesh;
    2.     Vector3[] vertices;
    3.     int i;
    4.     Vector3 tempPos;
    5.     RaycastHit hit;
    6.     Ray heightrayUp;
    7.     Vector3 oldPosition;
    8.     float ScaleX,ScaleY,ScaleZ;
    9.     public float hover;
    10.     void Start () {
    11.         mesh = GetComponent<MeshFilter>().mesh;
    12.         vertices = mesh.vertices;
    13.         ScaleX=transform.localScale.x;
    14.         ScaleY=transform.localScale.y;
    15.         ScaleZ=transform.localScale.z;
    16.     }
    17.     void Update() {
    18.  
    19.         if (transform.root.position!=oldPosition)//if object has moved, update vertices positions.
    20.         {
    21.             while (i < vertices.Length)
    22.             {
    23.             tempPos = transform.TransformPoint(vertices[i])+new Vector3(0,1,0);
    24.             heightrayUp = new Ray (tempPos, Vector3.down);
    25.                 if (Physics.Raycast (heightrayUp , out hit, 500))
    26.                 {vertices[i]=hit.point-transform.localPosition-transform.root.position+new Vector3(0,hover,0);
    27.                     vertices[i].x=vertices[i].x/ScaleX;//invert transform scale on vertices
    28.                     vertices[i].y=vertices[i].y/ScaleY;
    29.                     vertices[i].z=vertices[i].z/ScaleZ;
    30.                 }
    31.             i++;
    32.             }
    33.             if (i>=vertices.Length){i = 0;}
    34.             mesh.vertices = vertices;
    35.             mesh.RecalculateBounds();
    36.         }
    37.         oldPosition=transform.root.position;
    38.  
    39.     }
     
    Last edited: Nov 4, 2014
  3. dennik

    dennik

    Joined:
    Nov 23, 2011
    Posts:
    101
    Still haven't figured it out. Any ideas?
     
  4. dennik

    dennik

    Joined:
    Nov 23, 2011
    Posts:
    101
    Well... It took me three days but i figured it out. No point to post the updated code cause it has game specific tweaks and references to other stuff that won't make sense without posting everything.

    But just in case anyone is interested for a hint, I used Matrix4x4.TRS to rotate the vertices array and I un parented the plane and selectively drove its transforms from another object (for game specific reasons), as reading world transformations of a child object and applying those back to its vertices won't work properly, at least for rotation.