Hi, I’m trying to write a window shader that offsets an interior texture based on viewing angle.
Here is an example of how to do this in unreal:
The offset is done by a node called bump offset which is given the viewing direction and the texture coordinates. This is what is called parallax mapping, but with a static height instead of a hightmap.
How would I go about writing this function in cg?
I’ve tried this to translate an open gl function I found here: Learn OpenGL, extensive tutorial resource for learning Modern OpenGL
like this:
float2 ParallaxMapping(float2 texCoords, float3 viewDir)
{
float height = -1;
float2 p = viewDir.xy * (height * 0.1);
return texCoords - p;
}
and then in the vertex program set the view dir to:
mul(modelMatrix, input.vertex).xyz - _WorldSpaceCameraPos;
and then in the fragment program do this to the uv:
uv = ParallaxMapping(input.tex.xy, input.viewDir);
and it kind of works, looks great when looking up and down but moves inverted to how I want it when looking from left to right, and it moves unpredictably when the window is rotated. I think I need to take the normal of the windowpane into account somehow.
If you have done this before I’d love to get some tips to point me in the right direction!