Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rotating UVs around pivot

Discussion in 'Scripting' started by PROE_, Apr 26, 2020.

  1. PROE_

    PROE_

    Joined:
    Feb 20, 2016
    Posts:
    32
    Hi, I was making decal system lately and everything was going fine until I got some UVs errors and got a need for proper rotation. I'm totally new to matrices so maybe that's why I'm having such a problems :/

    The problem is with the Z component rotation. It looks like this without any rotation:
    upload_2020-4-26_11-26-58.png
    And rotating it by 5 degrees make UVs go in the opposite direction...
    upload_2020-4-26_11-32-21.png
    And rotating it by 90 degrees makes it even smaller :confused:
    upload_2020-4-26_11-34-54.png

    Here's the code for that part:
    Code (CSharp):
    1.         public Vector2[] CalculateUVs(Mesh mesh)
    2.         {
    3.             var vertices = mesh.vertices;
    4.             var normals = mesh.normals;
    5.             var uvs = new Vector2[vertices.Length];
    6.  
    7.             var rect = DecalSprite.rect;
    8.             Vector2 texSize = new Vector2(DecalSprite.texture.width, DecalSprite.texture.height);
    9.  
    10.             Rect correctedRect = new Rect(rect.x / texSize.x, rect.y / texSize.y, rect.width / texSize.x, rect.height / texSize.y);
    11.             Vector3 offset = new Vector2(correctedRect.x + correctedRect.width / 2, correctedRect.y + correctedRect.height / 2);
    12.             Vector3 size = new Vector3(correctedRect.width / transform.lossyScale.x, correctedRect.height / transform.lossyScale.y, correctedRect.width / transform.lossyScale.z);
    13.  
    14.             for (var i = 0; i < uvs.Length; i++)
    15.             {
    16.                 if (Mathf.Abs(normals[i].x) > Mathf.Abs(normals[i].y) && Mathf.Abs(normals[i].x) > Mathf.Abs(normals[i].z))
    17.                 {
    18.                     uvs[i] = new Vector3(vertices[i].z * size.x, vertices[i].y * size.y) + offset; // just without matrix
    19.                 }
    20.  
    21.                 if (Mathf.Abs(normals[i].y) > Mathf.Abs(normals[i].x) && Mathf.Abs(normals[i].y) > Mathf.Abs(normals[i].z))
    22.                 {
    23.                     uvs[i] = new Vector3(vertices[i].x * size.x, vertices[i].z * size.y) + offset; // just without matrix
    24.                 }
    25.  
    26.                 if (Mathf.Abs(normals[i].z) > Mathf.Abs(normals[i].x) && Mathf.Abs(normals[i].z) > Mathf.Abs(normals[i].y))
    27.                 {
    28.                     uvs[i] = Matrix4x4.TRS(offset, transform.rotation, Vector3.one).MultiplyPoint(new Vector2(vertices[i].x * size.x, vertices[i].y * size.y));
    29.                 }
    30.             }
    31.  
    32.             return uvs;
    33.         }
    Thanks in advance :)
     

    Attached Files:

  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi,

    I'm not sure how that code is exactly related to that box you're rotating in your images, looks like you have a rect in your code and I don't have time to reconstruct something to debug that code either. But maybe you could just work your way around by using quaternions and rotate your points around a pivot, for example.

    Here's a code snippet that will do that:
    https://answers.unity.com/questions/532297/rotate-a-vector-around-a-certain-point.html
     
  3. PROE_

    PROE_

    Joined:
    Feb 20, 2016
    Posts:
    32
    Box just tells the dimensions of the decal and that rect in code is used to assign sprite UVs and that wasn't a problem because it was working, just added full method to give some reference, but nevermind. I did some testing with matrices and now I understand some of it and got this to work properly now so that's nice :)
    If anyone ever have this problem - I had a problem with rotation because to get it to working properly I had to reverse quaternion and move UVs to the center for the calculation. Sometimes you gotta take a break, because I don't really know how I haven't thought about it earlier... but at least it working now
     
    Olmi likes this.