Search code examples
androidunity-game-enginegraphics3dnormals

Unity Normal Maps don't work on Android device


I'm an experienced native iOS developer making my first foray into Android through Unity. I'm trying to set up a custom shader, but I'm having some trouble with the Normal maps. I've got them working perfectly in the Unity simulator on my computer, but when I build to an actual device (Samsung Galaxy S8+), the Normal maps don't work at all.

I'm using Mars as my test case. Here's the model running in the simulator on my computer:

Computer version

And here's a screenshot from my device, running exactly the same code.

Android device version

I've done a LOT of research, and apparently using Normal maps on Android with Unity is not an easy thing. There are a lot of people asking about it, but almost every answer I've found has said the trick is to override the texture import settings, and force it to be "Truecolor" which seems to be "RGBA 32 Bit" according to Unity's documentation. This hasn't helped me, though.

Another thread suggested reducing the Asino Level to zero, and another suggested turning off Mip Maps. I don't know what either of those are, but neither helped.

Texture Import Settings

Here's my shader code, simplified but containing all references to Normal mapping:

void surf (Input IN, inout SurfaceOutputStandard o) {
        half4 d = tex2D (_MainTex , IN.uv_MainTex);
        half4 n = tex2D (_BumpMap , IN.uv_BumpMap);

        o.Albedo = d.rgb;
        o.Normal = UnpackNormal(n);
        o.Metallic = 0.0;
        o.Smoothness = 0.0;
    }

I've seen some threads suggesting replacements for the "UnpackNormal()" function in the shader code, indicating that it might not be the thing to do on Android or mobile in general, but none of the suggested replacements have changed anything for better or worse: the normal maps continue to work in the simulator, but not on the device.

I've even tried making my own normal maps programmatically from a grayscale heightmap, to try to circumvent any import settings I may have done wrong. Here's the code I used, and again it works in the simulator but not on the device.

public Texture2D NormalMap(Texture2D source, float strength = 10.0f) {
    Texture2D normalTexture;
    float xLeft;
    float xRight;
    float yUp;
    float yDown;
    float yDelta;
    float xDelta;

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

    for (int y=0; y<source.height; y++) {
        for (int x=0; x<source.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();

    return normalTexture;
}

Lastly, in the Build Settings, I've got the Platform set to Android and I've tried it using Texture Compression set to both "Don't Override" and "ETC (default)". The former was the original setting and the latter seemed to be Unity's suggestion both by the name and in the documentation.

I'm sure there's just some flag I haven't checked or some switch I haven't flipped, but I can't for the life of me figure out what I'm doing wrong here, or why there would be such a stubborn difference between the simulator and the device.

Can anyone help a Unity newbie out, and show me how these damn Normal maps are supposed to work on Android?


Solution

  • Check under:

    Edit -> Project Settings -> Quality

    Android is usually set to Fastest.