I am trying to implement this paper (https://storage.googleapis.com/pub-tools-public-publication-data/pdf/a0ed9e70a833a3e3ef0ad9efc1d979b59eedb8c4.pdf) into Unity, but I have a hard time understanding the different equations. I especially do not understand what are the "world-space UV gradients" described in Section 4.2.
I spent time implementing the equations 1 to 7, here's what I got so far in my fragment shader:
float w = 64.0;
float2 uGrad = float2(ddx(uv.x), ddy(uv.x));
float2 vGrad = float2(ddx(uv.y), ddy(uv.y));
float2 E = float2(-log2(w * length(uGrad)), -log2(w * length(vGrad)));
float2 uv0 = float2(pow(2.0, floor(E.x)) * uv.x, pow(2.0, floor(E.y)) * uv.y);
float2 uv1 = float2(2.0 * uv0.x, uv0.y);
float2 uv2 = float2(uv0.x, 2.0 * uv0.y);
float2 uv3 = float2(2.0 * uv0.x, 2.0 * uv0.y);
float2 blendCoef = float2(Blend(E.x - floor(E.x)), Blend(E.y - floor(E.y)));
float t0 = tex2D(_DisplacementTex, uv0);
float t1 = tex2D(_DisplacementTex, uv1);
float t2 = tex2D(_DisplacementTex, uv2);
float t3 = tex2D(_DisplacementTex, uv3);
return (1.0 - blendCoef.y) * ((1.0 - blendCoef.x) * t0 + blendCoef.x * t2) + blendCoef.y * ((1.0 - blendCoef.x) * t1 + blendCoef.x * t3);
and the Blend function returns this:
pow(-2.0 * x, 3.0) + pow(3.0 * x, 2.0);
Now I am stuck at the 8th and 9th equations, that I have tried to implement in my vertex shader as:
float3 wNormal = UnityObjectToWorldNormal(v.normal);
float3 wTangent = UnityObjectToWorldDir(v.tangent);
float3 wBinormal = cross(wNormal, wTangent);
float3 vTangent = mul((float3x3)UNITY_MATRIX_V, wTangent);
float3 vBinormal = mul((float3x3)UNITY_MATRIX_V, wBinormal);
float2 uGradWS = 0.01;
float2 vGradWS = 0.01;
o.uGrad = length(uGradWS) * vTangent / length(vTangent);
o.vGrad = length(vGradWS) * vBinormal / length(vBinormal);
This is one of the first time I really try to implement something using only equations from the paper (I am usually able to find pre-existing implementation). I'd really like to be able to continue doing this in the future, but it is hard because of my lack of mathematical expertise.
Do you think that I am on the right track with what I already have?
Do you know how I could compute the world-space UV gradient? My guess is that I would need to compute it on the CPU and pass it as extra vertex data.