I don't have a software that can create normal maps from an image so I usually make a grayscale image and then let unity make the normal map from that image. But I can't save the image to use for later, Instead I have to convert it to a normal map every time I use it. So how would I make a normal map from a greyscale using a script? i prefer c# but js works fine.


1 Answer 1


Hey this function can return a normal map , form an image. It stores the exported image in the Editor Assets folder. You can also specify the strength to increase the bumpiness of the map :)

private Texture2D NormalMap(Texture2D source,float strength) 

        Texture2D normalTexture;
        float xLeft;
        float xRight;
        float yUp;
        float yDown;
        float yDelta;
        float xDelta;

        normalTexture = new Texture2D (source.width, source.height, TextureFormat.ARGB32, true);

        for (int y=0; y<normalTexture.height; y++) 
            for (int x=0; x<normalTexture.width; x++) 
                xLeft = source.GetPixel(x-1,y).grayscale*strength;
                xRight = source.GetPixel(x+1,y).grayscale*strength;
                yUp = source.GetPixel(x,y-1).grayscale*strength;
                yDown = source.GetPixel(x,y+1).grayscale*strength;
                xDelta = ((xLeft-xRight)+1)*0.5f;
                yDelta = ((yUp-yDown)+1)*0.5f;
                normalTexture.SetPixel(x,y,new Color(xDelta,yDelta,1.0f,yDelta));

        //Code for exporting the image to assets folder
        System.IO.File.WriteAllBytes( "Assets/NormalMap.png", normalTexture.EncodeToPNG());

        return normalTexture;

If you don't see the exported image immediately, then don't panic. Just minimize the editor and open it again and the image will be there.

If you face any error such as "the image is not readable" , then make sure you change the import settings of the image

  • Choose texture type as "Advanced"
  • Check "Read / Write Enable option"
  • Apply the changes
  • \$\begingroup\$ Do you have any idea how slow this will be? \$\endgroup\$
    – nights
    Commented Oct 24, 2019 at 8:51

You must log in to answer this question.