How to rotate normal map texture

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. Texture for splatmap looks allright, but normal map dont work properly. Instead of blue color it looks red. 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);
            }            
    }

}

Short answer is : you cannot “rotate” a normal map.

A normal contains pixel orientation vectors, not simple colours.
If you rotate the map, you have to rotate the vectors as well. You could probably do this with a shader.


Extra layer of complexity :

When compressed, normal maps are not stored as RGB. The U component (red) is stored as luminance, the V component (green) is stored in alpha, while the blue component is simply discarded.


Long answer :

you can rotate the height map the normal map is generated from, but you have to update the normal map.
Have a look at Substance Designer, you can create custom Substance materials with it, fully compatible and dynamic in Unity, known as Procedural Material.
Although, you may want to be careful with this approach, as it’s been deprecated in Unity 2018.1.
Allegorithmic is said to be working on a 3rd party plugin.

Hope this helps.