Quick question about importance sampling

Started by
2 comments, last by CulDeVu 9 years, 1 month ago

Heyyy there!

So, following PBR and the canonical rendering equation, I was trying to figure out how you would go about importance sampling by sampling a BRDF directly. You know, by generating a bunch of random rays in a hemisphere around a surface normal and sampling the BRDF, instead of cosine-weighted sampling or sampling lobes or whatnot. It's not the fastest or best way by a long shot, but it'd be useful when I'm trying out different specular reflectance functions, or playing with different BRDFs in general.

Sampling the BRDF would create a weighted distribution. I'm thinking that you could do something like:


sample N rays and give them weights equal to brdf(...) * cos(angle_between_normal_and_ray_direction)
pick one of the rays randomly, given its weight
the density function for this point is equal to (the weight of the previously picked ray) / (sum of all the weights)
the new ray to cast is the picked ray

However, I don't exactly know if that's right. I haven't really been able to find anything about it. Mostly, people talk about the PDF and how you use it to weigh different rays, but when it comes down to the derivation, it's usually left out, so I don't really know how it's supposed to be calculated, or if I'm treating the concept right. I've also never heard of a BRDF-weighted distribution, but that's probably because it's not a good idea tongue.png

I tested it out on a calculator, using a cosine-weighted distribution as a test, and the PDF seemed kinda close to what it should be. I also tried it out on a pathtracer I have laying around, and it seems like it works, but then again, I don't really trust my eyes with this stuff anymore.

So is this a correct method of generating a BRDF-weighted ray? Or is it more complicated that I think it is?

Thanks! smile.png

I'm sorry about any spelling or grammar mistakes or any undue brevity, as I'm most likely typing on my phone

"Hell, there's more evidence that we are just living in a frequency wave that flows in harmonic balance creating the universe and all its existence." ~ GDchat

Advertisement

Personally I have always followed the approach outlined in this paper by Walter et. al., although I am by no means an expert in path tracing or monte carlo techniques. Basically you generate a microfacet normal based on your normal distribution, and then you reflect the view vector about that normal to compute the sampling direction. That paper has the formulas for generating the microfacet normals for 3 distributions (Beckmann, Blinn-Phong, and GGX), and also shows you how to roll the PDF and BRDF together into a single, simplified sample weight.

If you need to combine your specular BRDF with a diffuse term, I use the technique outlined in Physically Based Rendering. Essentially for every sample, you randomly choose between specular and diffuse with equal probability. For Lambertian diffuse, you can just sample a cosine-weighted hemisphere to importance sample the BRDF.


I tested it out on a calculator, using a cosine-weighted distribution as a test, and the PDF seemed kinda close to what it should be. I also tried it out on a pathtracer I have laying around, and it seems like it works, but then again, I don't really trust my eyes with this stuff anymore.

Rendering%2BEquation%2B(hemisphere%2BFor

Of course for cosine weighted distribution, this means that you already calculate this (wi*n) factor, so weight will equal to fr(BRDF). In general you can generate samples with any distribution which is nonzero everywhere and then sample results accordingly to match the distribution you want.
But for variance reduction point of view, its more efficient to choose sampling distribution which closely matches your desired distribution. In ideal case if your samples has desired distribution then you need no external weights.

You need two main function to define BRDF:
1 - function which takes 2 directions wi and wo and returns probability that light with incident direction wi may reflect to wo direction. Note that this probability may be different for lights rays with different wavelength, so it isn't always scalar value.
2 - function which takes wi direction and generates weighted wo(cause it couldn't always generate wo with exact desired distribution)

That paper has the formulas for generating the microfacet normals for 3 distributions (Beckmann, Blinn-Phong, and GGX), and also shows you how to roll the PDF and BRDF together into a single, simplified sample weight.

If you need to combine your specular BRDF with a diffuse term, I use the technique outlined in Physically Based Rendering. Essentially for every sample, you randomly choose between specular and diffuse with equal probability. For Lambertian diffuse, you can just sample a cosine-weighted hemisphere to importance sample the BRDF.

Okay. So a relatively well known specular importance sampling and cosine weighted sampling are all you ever need? Like, if you have a gold-plated racecar with 8 or 10 layers of clear coat, there still is no need for fancier sampling methods, outside of cosine-weighted and specular-weighted?

Of course for cosine weighted distribution, this means that you already calculate this (wi*n) factor, so weight will equal to fr(BRDF). In general you can generate samples with any distribution which is nonzero everywhere and then sample results accordingly to match the distribution you want.
But for variance reduction point of view, its more efficient to choose sampling distribution which closely matches your desired distribution. In ideal case if your samples has desired distribution then you need no external weights.

Okay, thanks! That's what I was wanting to know! smile.png

I'm sorry about any spelling or grammar mistakes or any undue brevity, as I'm most likely typing on my phone

"Hell, there's more evidence that we are just living in a frequency wave that flows in harmonic balance creating the universe and all its existence." ~ GDchat

This topic is closed to new replies.

Advertisement