- Home /
Tangent affected by model's UV, shader uses custom texcoord
Good evening.
I'm still a big newbie when it comes to shaders, and I've been hitting my head against a wall lately. I've been trying to convert a Parallax Occlusion Mapping shader from using model UV coordinates to using worldspace coordinates.
Problem is, the shader uses the tangent in calculating.. stuff. So when the UV of the textures don't match the UV the tangent was from, well, stuff breaks completely. What should I put in place of the tangent to make it work with the position and not the UV? I am aware that the tangent points to the direction where U increases, and the bi-tangent points to the direction where V increases, but that's about where my knowledge of the subject ends.
This is how I'm doing the position based UV:
float2 uv = IN.vertex.xz;
uv += float2(IN.normal.x, IN.normal.z) * IN.vertex.y;
uv *= 0.25;
Answer by BastianUrbach · Dec 19, 2020 at 05:31 PM
I would try this:
u increases in x direction at a rate of 1 and in y direction at a rate of IN.normal.x (the 0.25 can be ignored because we're just interested in directions). So let's start with
float3 tangent = float3(1, IN.normal.x, 0);However, as far as I can tell, that vector might not actually be tangential to the surface so let's make sure it is (normal should be normalized for this but it probably already is):
tangent -= normal * dot(normal, tangent);And finally you probably want it to be normalized:
tangent = normalize(tangent);
Your answer
Follow this Question
Related Questions
Very bad float precision 0 Answers
My shader code is having an error with returns at lines 50 and 60. 1 Answer
Custom shader converting to urp need help 1 Answer
pixelated water shader 0 Answers