float2 _SamplingDistance;
float _HeightScale;
float3 HeightToNormal(float2 uv, sampler2D tex)
{
float2 uv_smpx = uv + float2(_SamplingDistance.x, 0);
float2 uv_smpy = uv + float2(0, _SamplingDistance.y);
// Sample source and each axis with a delta
float smp_source = tex2D(tex, uv).x;
float smp_x = tex2D(tex, uv_smpx).x;
float smp_y = tex2D(tex, uv_smpy).x;
// compute dervatives
float2 derivative = float2(smp_x - smp_source, smp_y - smp_source);
// Scale the derivative
derivative *= _HeightScale / _SamplingDistance;
// Pack into a normal, then re-normalize
float3 normal = normalize(float3(derivative, 1.0));
return normal;
}