# Normalizing World Coordinates in Shader & Looking at Reference Texture Returns Incorrect Color

I am creating a custom shader for a plane.

It’s basic function is that it looks at a “reference texture”, which tells it what texture to apply where. A more detailed explanation is that:

1. It gets the worldPos of the pixel it is looking at.
2. Normalizes that into a float2 between (0,0) and (1,1)
3. Checks what color the reference texture has at that point.
4. If that color is green it applies the grass texture to the mesh.
5. Otherwise it applies the color red to the mesh.

This is almost completely working, the only problem is that there are two red lines where there shouldn’t be any (the reference texture is completely green). If anyone knows what could be going on I would much appreciate it!

//bottomLeftX declared & filled above
//bottomLeftZ declared & filled above
//textureSize declared & filled above

void surf(Input IN, inout SurfaceOutput o) {
uint distanceFromBottomLeftX = IN.worldPos.x - bottomLeftX;
uint distanceFromBottomLeftZ = IN.worldPos.z - bottomLeftZ;
uint mapTextureCoordX = (distanceFromBottomLeftX - (-textureSize / 2 /*min*/)) / ((textureSize / 2) - (-textureSize / 2)); //normalizes
uint mapTextureCoordZ = (distanceFromBottomLeftZ - (-textureSize / 2 /*min*/)) / ((textureSize / 2) - (-textureSize / 2)); //normalizes
float2 mapTextureCoords = float2(mapTextureCoordX, mapTextureCoordZ);
float2 adjustedTextureCoords = float2(IN.worldPos.x - .5f, IN.worldPos.z - .5f); //this just makes sure the texture aligns with the squares of the mesh

float3 mapTextureSample = tex2D(mapTexture, mapTextureCoords).rgb;
if (mapTextureSample.x == 0 && mapTextureSample.y == 1 && mapTextureSample.z == 0) //(0,1,0) is green
{
}
else
{
o.Albedo = float3(1, 0, 0);
}
}

There are several things very strange in your code. First of all, why do you use “uint” variables? This is most likely the main problem.

Since you use uint as type, your “mapTextureCoords” will always be a whole number (though due to floating point inaccuracy during conversion it might be slightly off for larger numbers). uint is an unsigned integer. That means assigning a negative value would wrap around to the largest uint value.

You really should use vector types whenever possible. Your code looks unnecessarily complicated. Also this:

... / ((textureSize / 2) - (-textureSize / 2))

is the same as

... / textureSize

Also if you want to offset a position by half of the available space it’s easier to do the offset after you divide the value. All your code basically does is this:

// untested but should work
float2 mapTextureCoords = (IN.worldPos.xz - float2(bottomLeftX, bottomLeftZ)) / textureSize;
mapTextureCoords -= .5f;

float3 mapTextureSample = tex2D(mapTexture, mapTextureCoords).rgb;
if (mapTextureSample.x == 0 && mapTextureSample.y == 1 && mapTextureSample.z == 0)
{
o.Albedo = tex2D(_GrassTexture, IN.worldPos.xz -.5f).rgb;
}
else
{
o.Albedo = float3(1, 0, 0);
}

However this time without using uint. If the use of uint should server an actual purpose you should be more clear as currently when using uint the first texture lookup would make no sense as it will always look up the same point.