Surface Shader with Normalmap&WorldUV rotationproblem

Hey guys, i have a problem with a surface shader i wrote yesterday. For my texture-UVmapping i use the world coordinates X & Z.

so my for my texture tex2D(_MainTex, -IN.worldPos.xz*scalefactor) works great.
for my bumpmap i do the same and it works at first. But when i rotate the object itself the bumpmap inverts.

For example in the pic below i rotated the plane on the right 180° in y-coordinate. Then the rocky part of the texture seems to come out of the plane, on the left side it looks like its below the grass(how it should be).

Anyone has a solution for this?

The normal maps used in games today are more accurately called Tangent Space Normal Maps. They represent left & right, up & down, and forward & back as red, green, and blue in tangent space. But what is tangent space? Tangent space is the surface normal for forward & back, and the model’s UV directions for right and up! When you’re overriding the model’s existing UVs with world space coordinates, the stored tangents for the normal maps no longer match.

So, what do you do? You have to supply your own tangents in the shader based off of world position or calculate them in the pixel shader. For a surface shader I honestly don’t know how you would go about that.

1 Like

yeah that’s the problem. i guess i’d have to make a fragment shader with a custom bumpmap… or just use multiple meshes. I hoped there’d be a faster simple workaround… rotating the mesh’s uv’s wouldnt change anything would it?

You could adjust the UVs on the mesh to match specific cases, sure, but it’d still be wrong for all the others and you end up having to make custom meshes for everything, and then what’s the point in doing world space UVs if you have to make custom meshes anyway. It might be possible to do a custom tangent direction with a vertex function on a surface shader, otherwise yeah you might need to do vert / frag style shaders.

1 Like

another idea was to assign vertex colors per 90°, so every mesh has a specific vertex color for its rotation and in the shader they will be read and added to the bumpmap. and if that doesnt work then whatever, on gamestart all the meshes will be merged together again, so i’ll just make 1 mesh per 90°. thanks for your help bgolus