Sign in to follow this  
Chris_F

Fresnel equation

Recommended Posts

Chris_F    3030

Out of curiocity I wanted to compare Schlick's approximation to the real thing, so I had my hand at implementing it, though, admittedly, I'm not very good with the maths... sleep.png so I may have made a mistake, or more.

 

float CosTheta = dot(N, L);
float SinTheta = sqrt(1 - CosTheta * CosTheta);
float temp = n1 * (SinTheta / n2);
temp = n2 * sqrt(1 - temp * temp);
temp = (n1 * CosTheta - temp) / (n1 * CosTheta + temp);
float fresnel = temp * temp;

 

I'm getting a pretty noticable difference between this and Schlick's.

Edited by Chris_F

Share this post


Link to post
Share on other sites
CryZe    773

You implemented the formula for s-polarized light. But you want to implement the formula for non-polarized light. Which is simply R = (Rs + Rp) / 2.

Share this post


Link to post
Share on other sites
Chris_F    3030

You implemented the formula for s-polarized light. But you want to implement the formula for non-polarized light. Which is simply R = (Rs + Rp) / 2.

 

Doh. Second try?

 

 

float CosTheta = dot(N, L);
float SinTheta = sqrt(1 - CosTheta * CosTheta);

float t1 = n1 * (SinTheta / n2);
t1 = sqrt(1 - t1 * t1);
float t2 = (n1 * CosTheta - n2 * t1) / (n1 * CosTheta + n2 * t1);
float Rs = t2 * t2;
t2 = (n2 * CosTheta - n1 * t1) / (n2 * CosTheta + n1 * t1);
float Rp = t2 * t2;
float R = (Rs + Rp) / 2;
Edited by Chris_F

Share this post


Link to post
Share on other sites
CryZe    773

I'd reduce unnecessary calculations even more. Also your nominators were subtracting the values instead of adding them: 

 

float cosThetaIncidence = dot(N, L);
float sinThetaIncidence = sqrt(1 - cosThetaIncidence * cosThetaIncidence);

float sinThetaTransmittance = n1 * (sinThetaIncidence / n2);
float cosThetaTransmittance = sqrt(1 - sinThetaTransmittance * sinThetaTransmittance);

float n1CosThetaTransmittance = n1 * cosThetaTransmittance;
float n2CosThetaTransmittance = n2 * cosThetaTransmittance;
float n1CosThetaIncidence = n1 * cosThetaIncidence;
float n2CosThetaIncidence = n2 * cosThetaIncidence;

float sPolarizedSqrt = (n1CosThetaIncidence - n2CosThetaTransmittance) / (n1CosThetaIncidence + n2CosThetaTransmittance);
float sPolarized = sPolarizedSqrt * sPolarizedSqrt;

float pPolarizedSqrt = (n2CosThetaIncidence - n1CosThetaTransmittance) / (n2CosThetaIncidence + n1CosThetaTransmittance);
float pPolarized = pPolarizedSqrt * pPolarizedSqrt;

float fresnel = (sPolarized + pPolarized) / 2;

 

 

Edited by CryZe

Share this post


Link to post
Share on other sites
Chris_F    3030

I'd reduce unnecessary calculations even more. Also your nominators were subtracting the values instead of adding them:

 

float CosTheta = dot(N, L);
float SinTheta = sqrt(1 - CosTheta * CosTheta);

float t1 = n1 * (SinTheta / n2);
t1 = sqrt(1 - t1 * t1);

float n1CosThetaTransmittance = n1 * t1;
float n2CosThetaTransmittance = n2 * t1;
float n1CosThetaIncidence = n1 * CosTheta;
float n2CosThetaIncidence = n2 * CosTheta;

float t2 = (n1CosThetaIncidence - n2CosThetaTransmittance) / (n1CosThetaIncidence + n2CosThetaTransmittance);
float Rs = t2 * t2;
t2 = (n2CosThetaIncidence - n1CosThetaTransmittance) / (n2CosThetaIncidence + n1CosThetaTransmittance);
float Rp = t2 * t2;
float R = (Rs + Rp) / 2;

 

Could do, but it's unlikely to make a difference as shader compilers are pretty sly.

 

Edit: For me, it seems as though this full implementation is about 24% more instructions than Schlick's method.

Edited by Chris_F

Share this post


Link to post
Share on other sites
CryZe    773

For me, it seems as though this full implementation is about 24% more instructions than Schlick's method.


Only 24% more? Isn't Schlick Fresnel ks + (1 - ks) * pow(1 - dot(L, H), 5), which would be about 9 instructions? The full fresnel equation would be about 29 instructions.

Also, why is it dot(N, L) in your code? It should be the microfacets normal and not the normal of the macro surface. Edited by CryZe

Share this post


Link to post
Share on other sites
Chris_F    3030

For me, it seems as though this full implementation is about 24% more instructions than Schlick's method.


Only 24% more? Isn't Schlick Fresnel ks + (1 - ks) * pow(1 - dot(L, H), 5), which would be about 9 instructions? The full fresnel equation would be about 29 instructions.

Also, why is it dot(N, L) in your code? It should be the microfacets normal and not the normal of the macro surface.

 

I included the instruction cost of calculating f0 with pow((n2-n1) / (n2+n1) , 2.0), and I used N.L because I was testing this with normalized Blinn-Phong, not a microfaceted BRDF.

Edited by Chris_F

Share this post


Link to post
Share on other sites
CryZe    773


I was testing this with normalized Blinn-Phong, not a microfaceted BRDF


Blinn-Phong is the microfacet version of Phong. Blinn-Phong describes the percentage of microfacets oriented towards the halfway vector, cause these are the only microfacets reflecting light into the eye. So if you are extending your already partially existing microfacet BRDF by a fresnel term, you should use the microfacets normal, aka your halfway vector. Edited by CryZe

Share this post


Link to post
Share on other sites
Chris_F    3030

OK, one last question, since as I said, I am not very familiar with the math yet. I use sqrt(1-cos^2(theta)) to get sin(theta). Is this correct? It seems to work, but as far as I know, sqrt(1-cos^2(theta)) is actually only equal to abs(sin(theta)). Assuming this works, is this method preferable to taking the length of the cross product?

Share this post


Link to post
Share on other sites
CryZe    773

abs(sin(theta)) is fine in this case. I'd use your method instead of the cross product, since the calculation of the length of the cross product requires a square root as well, so your method is faster.

Share this post


Link to post
Share on other sites
CryZe    773

I think the most important thing in optimizing a BRDF is, that you reduce linear time to constant time. Some parts of the BRDF don't need to be calculated per light. Just take a look at your version of Schlick's fresnel:

foreach (light)
{
	float f0Sqrt = (n1 - n2) / (n1 + n2);
	float f0 = f0Sqrt * f0Sqrt;
	float fresnel = f0 + (1 - f0) * pow(1 - dot(L, H), 5);
} 

 

If you implement it this way, you almost reduce the linear code by half of its instructions:

float f0Sqrt = (n1 - n2) / (n1 + n2);
float f0 = f0Sqrt * f0Sqrt;
float cf0 = 1 - f0;

foreach (light)
{
	float fresnel = f0 + cf0 * pow(1 - dot(L, H), 5);
} 

 

I've reduced my BRDF this way. And this is also the reason I'm using this reduced version of Schlick's fresnel (yes I'm using the refractive indices as well) in my BRDF, because the full fresnel equation just can't be reduced this way and is way too expensive in comparison to this one. That's also the reason why I prefer the GGX NDF over any other NDF. It's pretty damn physically accurate and can be reduced into just a few instructions. Actually it's probably even faster than Blinn-Phong.

float roughnessSqr = roughness * roughness;
float numerator = roughnessSqr / PI;
float roughnessSqrSub1 = roughnessSqr - 1;

foreach (light)
{
	float NDotH = dot(N, H);
	float NDotHSqr = NDotH * NDotH;
	float denominatorSqrt = NDotHSqr * roughnessSqrSub1 + 1;
	float denominator = denominatorSqrt * denominatorSqrt;
	float ggx = numerator / denominator;
}
Edited by CryZe

Share this post


Link to post
Share on other sites
Chris_F    3030

That's some cool info. However, going back to what you said earlier, I seem to be a bit confused now as to what actually constitutes a microfacet BRDF. Why is Blinn-Phong considered microfacet based, but Phong is not? I know you can use modified phong as a distrobution in a microfacet BRDF, but I didn't think that an ordinary Blinn-Phong shader was considered to be a microfacet BRDF.

Edited by Chris_F

Share this post


Link to post
Share on other sites
Bacterius    13165

That's some cool info. However, going back to what you said earlier, I seem to be a bit confused now as to what actually constitutes a microfacet BRDF. Why is Blinn-Phong considered microfacet based, but Phong is not? I know you can use modified phong as a distrobution in a microfacet BRDF, but I didn't think that an ordinary Blinn-Phong shader was considered to be a microfacet BRDF.

 

At a very low level, the only physical basis governing the behaviour of light on an individual planar surface is, in fact, the Fresnel equations (it's more complicated if you go deeper, or if you consider magnetic media, but in a nutshell that's pretty much it). So what BRDF's do is take into account the fact that surfaces are often not perfectly planar but somewhat rough. A diffuse BRDF is just that, taken to one extreme - the surface is maximally rough in that the incident direction of light has no bearing on its reflected direction. A perfectly specular BRDF is the other extreme. Every other BRDF is somewhere in the middle, with different geometry distributions to attempt to approximate real life materials.

 

So the only thing distinguishing a "microfacet" BRDF from a non-microfacet one is the fact that the former handles "microfacet" geometry (i.e. a disorganized mess of randomly oriented planar sections) whereas the latter could handle any other type of surface geometry (for instance, a two-layer material)

 

Blinn-Phong happens to consider microfacet geometry, therefore it is a microfacet BRDF. The specular exponent is a measure of surface roughness. In fact, there exists a direct conversion formula between a Blinn-Phong exponent and a Beckmann roughness term. Blinn-Phong is essentially a glorified microfacet distribution, and is pretty much its own BRDF since it ignores more or less every other physical effect beyond ambient reflection, diffuse reflection and microfacet specular reflection - you can very well use the Blinn-Phong distribution in a Cook-Torrance BRDF, for instance, and you'll basically get Blinn-Phong Specular + Fresnel.

 

Phong, on the other hand, is not a microfacet BRDF, because it doesn't consider microfacet geometry. In fact, it doesn't consider anything at all, quoting Wikipedia, "It is based on Bui Tuong Phong's informal observation that shiny surfaces have small intense specular highlights, while dull surfaces have large highlights that fall off more gradually.". The Phong model doesn't assume anything regarding the surface's geometry besides "this will look like metal, that will look like plastic". That's it.

Share this post


Link to post
Share on other sites
Chris_F    3030

you can very well use the Blinn-Phong distribution in a Cook-Torrance BRDF, for instance, and you'll basically get Blinn-Phong Specular + Fresnel

 

That would be Blinn-Phong specular + fresnell + geometry term wouldn't it? So simply measuring the angle of the half vector is litterally all that is nesisarry to transform Phong into a microfacet BRDF? I've never heard of anyone call Blinn a microfacet BRDF before. Usually they are refering to Cook-Torrance or similar when they bring up facets.

Share this post


Link to post
Share on other sites
Hodgman    51234
IIRC -
Phong was made just with intuition and observation, so it's an 'empirical BRDF'.
Blinn took Phong's work and recreated similar results from a theoretical basis this time, instead of from guesswork and collected data. The framework he used was microfacet theory (which says that only the facets oriented towards the half-vector will produce a visible reflection), so it's a 'microfacet BRDF'. So yes, it's the fact that the BRDF only considers the percentage of the surface that is pointing in the H direction, that makes it a 'microfacet BRDF'.

Share this post


Link to post
Share on other sites
Chris_F    3030

OK, I'm bumping this thread because I'm revisiting the Fresnel equation, this time using complex IOR values. I'm having a hard time converting this to complex numbers.

 

 

float Fresnel(float CosThetaI, float n)
{
    float CosThetaT = sqrt(max(0, 1 - (1 - CosThetaI * CosThetaI) / (n * n)));
    float NCosThetaT = n * CosThetaT;
    float NCosThetaI = n * CosThetaI;
    float Rs = pow(abs((CosThetaI - NCosThetaT) / (CosThetaI + NCosThetaT)), 2);
    float Rp = pow(abs((CosThetaT - NCosThetaI) / (CosThetaT + NCosThetaI)), 2);
    return (Rs + Rp) / 2;
}

 

This is the basic formula, but I need to re-write it so that it looks like:

 

 

float Fresnel(float CosThetaI, vec3 n, vec3 k)
{
    ...
}

 

Where n and k make up the complex IOR (n + ki). I've taken a few stabs at it, but it's gotten me nowhere. Here is my train wreck of an attempt:

 

 

vec3 Fresnel(float CosThetaI, vec3 n, vec3 k)
{
    float temp = 1 - CosThetaI * CosThetaI;

    vec3 NKSqr_real = n * n - k * k;
    vec3 NKSqr_imag = n * k * 2;

    vec3 temp2_real = (temp * NKSqr_real) / (NKSqr_real * NKSqr_real + NKSqr_imag * NKSqr_imag);
    vec3 temp2_imag = -(temp * NKSqr_imag) / (NKSqr_real * NKSqr_real + NKSqr_imag * NKSqr_imag);

    temp2_real = 1 - temp2_real;
    temp2_imag = -temp2_imag;

    vec3 CosThetaT_real = sqrt((temp2_real + sqrt(temp2_real * temp2_real + temp2_imag * temp2_imag)) / 2);
    vec3 CosThetaT_imag = sign(temp2_imag) * sqrt((-temp2_real + sqrt(temp2_real * temp2_real + temp2_imag * temp2_imag)) / 2);

    vec3 NCosThetaT_real = n * CosThetaT_real - k * CosThetaT_imag;
    vec3 NCosThetaT_imag = k * CosThetaT_real + n * CosThetaT_imag;

    vec3 NCosThetaI_real = n * CosThetaI;
    vec3 NCosThetaI_imag = k * CosThetaI;

    vec3 CosThetaI_minus_NCosThetaT_real = CosThetaI - NCosThetaT_real;
    vec3 CosThetaI_minus_NCosThetaT_imag = -NCosThetaT_imag;

    vec3 CosThetaI_plus_NCosThetaT_real = CosThetaI + NCosThetaT_real;
    vec3 CosThetaI_plus_NCosThetaT_imag = NCosThetaT_imag;

    vec3 a, b, c, d;

    a = CosThetaI_minus_NCosThetaT_real;
    b = CosThetaI_minus_NCosThetaT_imag;
    c = CosThetaI_plus_NCosThetaT_real;
    d = CosThetaI_plus_NCosThetaT_imag;

    vec3 Rs_real = (a * c + b * d) / (c * c + d * d);
    vec3 Rs_imag = (b * c + a * d) / (c * c + d * d);

    vec3 Rs = sqrt(Rs_real * Rs_real + Rs_imag * Rs_imag);
    Rs = Rs * Rs;

    vec3 CosThetaT_minus_NCosThetaI_real = CosThetaT_real - NCosThetaI_real;
    vec3 CosThetaT_minus_NCosThetaI_imag = CosThetaT_imag - NCosThetaI_imag;

    vec3 CosThetaT_plus_NCosThetaI_real = CosThetaT_real + NCosThetaI_real;
    vec3 CosThetaT_plus_NCosThetaI_imag = CosThetaT_imag + NCosThetaI_imag;

    a = CosThetaT_minus_NCosThetaI_real;
    b = CosThetaT_minus_NCosThetaI_imag;
    c = CosThetaT_plus_NCosThetaI_real;
    d = CosThetaT_plus_NCosThetaI_imag;

    vec3 Rp_real = (a * c + b * d) / (c * c + d * d);
    vec3 Rp_imag = (b * c + a * d) / (c * c + d * d);

    vec3 Rp = sqrt(Rp_real * Rp_real + Rp_imag * Rp_imag);
    Rp = Rp * Rp;

    return (Rs + Rp) / 2;
}

 

It would be so much easier if HLSL/GLSL had first class support for complex values. wacko.png

 

EDIT:

 

Never mind. I managed to come up with this.

 

 

vec2 CADD(vec2 a, vec2 b) {    return a + b; }
vec2 CSUB(vec2 a, vec2 b) {    return a - b; }
vec2 CMUL(vec2 a, vec2 b) {    return vec2(a.x * b.x - a.y * b.y, a.y * b.x + a.x * b.y); }
vec2 CDIV(vec2 a, vec2 b) {    return vec2((a.x * b.x + a.y * b.y) / (b.x * b.x + b.y * b.y), (a.y * b.x - a.x * b.y) / (b.x * b.x + b.y * b.y)); }
float CABS(vec2 a) { return sqrt(a.x * a.x + a.y * a.y); }
vec2 CSQRT(vec2 a) { return vec2(sqrt((a.x + sqrt(a.x * a.x + a.y * a.y)) / 2), sign(a.y) * sqrt((-a.x + sqrt(a.x * a.x + a.y * a.y)) / 2)); }

float _Fresnel(float _CosThetaI, vec2 n)
{
    vec2 CosThetaI = vec2(_CosThetaI, 0);
    vec2 CosThetaT = CSQRT(CSUB(vec2(1.0, 0), CDIV(CSUB(vec2(1.0, 0), CMUL(CosThetaI, CosThetaI)), CMUL(n, n))));
    vec2 NCosThetaI = CMUL(n, CosThetaI);
    vec2 NCosThetaT = CMUL(n, CosThetaT);
    float Rs = pow(CABS(CDIV(CSUB(CosThetaI, NCosThetaT), CADD(CosThetaI, NCosThetaT))), 2);
    float Rp = pow(CABS(CDIV(CSUB(CosThetaT, NCosThetaI), CADD(CosThetaT, NCosThetaI))), 2);
    return (Rs + Rp) / 2;
}
 
vec3 Fresnel(float CosThetaI, vec3 n, vec3 k)
{
    return vec3(
            _Fresnel(CosThetaI, vec2(n.r, k.r)),
            _Fresnel(CosThetaI, vec2(n.g, k.g)),
            _Fresnel(CosThetaI, vec2(n.b, k.b))
        );
}
Edited by Chris_F

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this