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:
- It gets the worldPos of the pixel it is looking at.
- Normalizes that into a float2 between (0,0) and (1,1)
- Checks what color the reference texture has at that point.
- If that color is green it applies the grass texture to the mesh.
- 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
{
o.Albedo = tex2D(_GrassTexture, adjustedTextureCoords).rgb;
}
else
{
o.Albedo = float3(1, 0, 0);
}
}