Parallax offset in custom shader ?

Hi everyone,

I’m wondering how to use Unity’s parallax offset (also called height mapping) in a custom surface shader.
So far I’ve found this:

float2 offset = ParallaxOffset(tex2D(_HeightMap, IN.uv_MainTex).w, _HeightIntensity, IN.viewDir);
IN.uv_MainTex.x -= offset.x;
IN.uv_MainTex.y += offset.y;
o.Albedo = tex2D (_MainTex, IN.uv_MainTex); // etc

However, this only works if the fragment’s normal is the world forward vector (0, 0, 1). How can I make it depend on the fragment’s normal ?

Thanks in advance.

Found it ! Actually no crazy vector rotation was needed, it seems in order for parallax mapping to work correctly you also need to apply it to the bumpmap. Weird, but thanksfully simple to do:

float2 offsetMainTex = ParallaxOffset(tex2D(_ParallaxMap, IN.uv_MainTex).r, _Parallax, IN.viewDir);
float2 offsetBumpMap = ParallaxOffset(tex2D(_ParallaxMap, IN.uv_MainTex).r, _Parallax, IN.viewDir);
fixed4 c = tex2D (_MainTex, IN.uv_MainTex + offsetMainTex) * _Color;
o.Albedo = c.rgb;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap + offsetBumpMap));

You don’t really need to do the same calculation twice. Also if your MainTex and Bump offset and tiling is the same:

 IN.uv_MainTex +=  ParallaxOffset(tex2D(_ParallaxMap, IN.uv_MainTex).r, _Parallax, IN.viewDir);
 o.Albedo = (tex2D (_MainTex , IN.uv_MainTex ) * _Color).rgb;
 o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex ));