0

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);

Here's the result in Unity.

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.

Leonrado
  • 1
  • 1
  • I've also been working on this paper and have hit a wall in the same place. Have you continued to work on this, and have you determined how to calculate the world space UV gradient? – Aaron Cheney Apr 24 '20 at 19:28
  • Yes, I figured it out ! You can just follow the paper, but it turns out that equations (3) and (4) are inverted. So U1 = [u0, 2*v0] and U2 = [2*u0, v0]. Then it should work properly. You should get a result at equation (7) that works fine. – Leonrado Apr 24 '20 at 23:06
  • It sounds like you used a modified version of the smoothing formula then, but you're not using world space UV gradients to approximate smooth UVs? – Aaron Cheney Apr 25 '20 at 23:35
  • Sorry, I forgot to answer! So, I've looked back at this part of my project and I'm not yet using the world space UV gradients, I'm still using the screen-space ones. And I seem to get the same artifacts as the article points out. I'll probably try to implement the world-space part in a few weeks (but with a noise texture it's quite hard to see those artifacts though). I do not know if I was on the right track at the time I posted this question, as I was trying it out with the error in the article (so whatever I tried would've been wrong anyway). If you find out how it works I'd love to know! – Leonrado May 02 '20 at 09:23

0 Answers0