Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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) 
{
        strength=Mathf.Clamp(strength,0.0F,1.0F);

        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));
            }
        }
        normalTexture.Apply();

        //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
share|improve this answer
1  
Are you sure about this? Can you post an example of a normal map that this code generates? It doesn't seem that it would work correctly. Normal maps use all 3 colors and are primarily blue. This code is not even using red or blue. (wikipedia image sample). (wikipedia description of normal map) –  Fuzzy Logic 2 days ago
    
Edited the answer :) –  Hash Buoy 2 days ago
    
Oh, yeah now with the edit it works fantastic, thanks a lot! :D –  VinnieH01 2 days ago
    
No problem :)... –  Hash Buoy 2 days ago
    
+1 That makes much more sense now. (I think the output RGB values need to be scaled down by strength, or max strength should be clamped at 1.0f instead of 10.0f) –  Fuzzy Logic 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.