rotate normal map

Hello! I try to rotate normal map for terrain by using getPixe and setPixel functions. With texture this works ok, but with normal map some trouble. Resulting texture not looking as normal map and Ive got no idea why. Thanks for any help.

public Texture2D RotateTexture2d(Texture2D tex)
{
Texture2D tex1 = new Texture2D(tex.width, tex.height);
int wid = tex.width; int height = tex.height;

        tex1.Resize(height, wid);
        for (int a = 0; a < wid; a++)
        {
            for (int b = 0; b < height; b++)
            {
                Color color = tex1.GetPixel(a, b);
                tex1.SetPixel(b, a, color);
            }            
    }

}

Your read and write from / to the same texture:

// read
Color color = tex1.GetPixel(a, b);
//write
tex1.SetPixel(b, a, color);

You want to read from tex not tex1. Also note that using GetPixel and SetPixel are quite low. It’s way faster to use GetPixels and SetPixels.

edit

Also note that you do not rotate the image but you flip it along the diagonal. To actually rotate the image you have to inverse one of the coordinates. Here are two methods to create a rotated texture, either clockwise (CW) or counter clockwise (CCW):

public static Texture2D RotateTexture2dCW(Texture2D tex)
{
    int w = tex.width;
    int h = tex.height;
    var src = tex.GetPixels32();
    var dest = new Color32[src.Length];
    for (int y = 0; y < h; y++)
    {
        for (int x = 0; x < w; x++)
        {
            dest[(w-x-1) * h + y] = src[y * w + x];
        }
    }
    Texture2D res = new Texture2D(h, w);
    res.SetPixels32(dest);
    res.Apply();
    return res;
}
public static Texture2D RotateTexture2dCCW(Texture2D tex)
{
    int w = tex.width;
    int h = tex.height;
    var src = tex.GetPixels32();
    var dest = new Color32[src.Length];
    for (int y = 0; y < h; y++)
    {
        for (int x = 0; x < w; x++)
        {
            dest[x * h + h-y-1] = src[y * w + x];
        }
    }
    Texture2D res = new Texture2D(h, w);
    res.SetPixels32(dest);
    res.Apply();
    return res;
}

Note i haven’t tested those two methods but it should work as expected.