- Home /
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:
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);
}
}
i cant see the top of your code. its seems it might be an alignment problem. either distance or texture coordinate starting at starting at 1 ins$$anonymous$$d of zero, or something like that. you might just need a simple subtraction or addition of 1 somewhere.
another thing to check... make sure your paint program is not slightly changing the green color value around the edges when you save it. i know it sounds silly but I have seen paint programs do this in effort to make edges look smoother.
im not sure why you need world coodinates. if you just need a shader that combines / blends two textures based on a map over an object i wrote one a couple months ago that i can share. it works well
Answer by Bunny83 · Aug 05, 2017 at 12:52 AM
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.
Ty for the code critique! $$anonymous$$y script looks so much more elegant now. And it fixed the problem!
I will remember to use the floats whenever possible, makes everything alot simpler lol.
Also just wanted to mention for future peoples who may find this I found a better way to compare float3's which is using "all":
if (all(mapTextureSample == float3(0,1,0))) //(0,1,0) is green
{
}
Ty again!
Your answer
![](https://koobas.hobune.stream/wayback/20220612131942im_/https://answers.unity.com/themes/thub/images/avi.jpg)