# Weird results when trying to use different parts of texture atlas depending on surface normal

Hi all,
I’m new to 3D code and writing shaders so I’m probably doing something stupid here. I’m working on a simple Minecraft clone just to learn Unity, and I thought the easiest way to texture the ground would be to use a texture atlas with a grass + dirt texture and use a shader to choose which part of the atlas to get the texture from depending on the surface normal. i.e. for the face of a cube that points upwards, the grass texture will be used, and for all other faces the dirt texture will be used. The texture looks like this:

In my shader, if I do the following, I get the dirt texture on every face as expected:

``````float y = (IN.uv_MainTex.y / 2.0f);
float2 uv = {IN.uv_MainTex.x, y};
fixed4 c = tex2D (_MainTex, uv) * _Color;
o.Albedo = c.rgb;
``````

If I use the same code but always add 0.5f to y to get the top half, I get the grass texture everywhere as expected:

But when I try to set y based on the normal, i.e.

``````float y = (IN.uv_MainTex.y / 2.0f) + floor(IN.worldNormal.y) * 0.5f;
``````

Then I get this weird result where the top face is mostly grass but with parts of the dirt texture showing in diagonal lines:

Is IN.worldNormal the right normal to be using, or do I need to transform it into some other space? Or is the problem with how I’m using floor(), maybe? Any advice is appreciated.

Thanks,
Mitch

This looks to be a precision issue (potentially from vertex normal interpolation). There are probably smarter ways to do this, but as a simple fix you could just add a small bias to the normal, i.e;

``````    float y = (IN.uv_MainTex.y / 2.0f) + floor (IN.worldNormal.y + 0.01) * 0.5f;
``````
1 Like

That fixed it! Thanks! Instead of + 0.01 I had to change it to * 1.1 so that the bottom face would be floored down to -2 instead of -1, and thereby get the dirt texture as well, but it seems you were correct about it being a precision issue.

I’m open to suggestions on better ways to texture different faces with different textures, as well.