Path Tracing

Rendering function:

Lo(p,ωo)=Le(p,ωo)+ΩLi(p,ωi)f(p,ωi,ωo)cosθidωiReflected Light(Output Image)[Unknown]=Emission[Known]+Reflected Light[Unknown]BRDF[Known]Cosine of incident angle[Known]
  1. Iterate each pixel, cast ray in one pixel multiple times and take the mean of these rays' color.

Following progress is included in one single ray casting:

  1. If the ray hit the light source at depth 0, get Emission value L_emit, otherwise set it to be 0.
  2. Seperate global illumination into direct illumination(L_dir) and indirect illumination(L_indir)

(Rigidly, Only the emission directly from light sources and direct illumination on surfaces is direct illumination)

For direct illumination

In function shade(p,ωo), randomly generate N rays of ωi in pdf(wi).

L_dir(p,ωo)=1Ni=1NLlightf(p,ωi,ωo)cosθipdf(ωi)

But sample randomly in space is not efficient sampling method, we can restrict only sampling the ray to the light source.

We transform the differential of incident solid angle(dωi) to light surface's differential space(dA), now pdf=1A.

dω=dAcosθ||xx||2

image

After sample point from the light source, we also need to check whether there's obstacle between the reflecting object and light source.

This is the direct illumination part.

For indirect illumination

In function shade(p,ωo), we also simply randomly generate N rays of ωi in pdf(wi).(Sample the solid angle directly)

L_indir(p,ωo)=1Ni=1Nshade(q,ωi)f(p,ωi,ωo)cosθipdf(ωi)

Since this progress is recursively, so we only sample one ray in both two kinds of illumination(N=1). And we stop the recursion by RussianRoulette.

E=P(L_indir/P)+(1P)0=L_indir

Noticed: this sampled ray shouldn't hit the light source.

  1. Sum up two kinds of illumination
L=L_emit+L_dir+L_indir

Microfacet

BRDF

This is a kind of new material kind.

It differs in BRDF calculation.

Here is the reflection(specular) part of microfacet BRDF:

f_cooktorrance=DFG4(ωon)(ωin)

D: Normal distribution function(approximates the amount the surface's microfacets are aligned to the halfway vector)

G: Geometry function(describes the self-shadowing property)

F: Fresnel equation(the ratio of surface reflection at different surface angles)

D and G is controlled by a roughness parameter α, here I take Trowbridge-Reitz GGX as D and Schlick-GGX as G.

NDFGGXTR(n,h,α)=α2π((nh)2(α21)+1)2GSchlickGGX(n,v,k)=nv(nv)(1k)+kkdirect=(α+1)28

Self shadowing considers both view and light:

G(n,v,l,k)=Gsub(n,v,k)Gsub(n,l,k)

Fresnel equation just take Fresnel-Schlick approximation here, instead of complex equation:

FSchlick(h,v,F0)=F0+(1F0)(1(hv))5

F0 represents the base reflectivity of the surface when looking straight at its surface.

The refracted light plays an effect like diffuse light.

1=kd+ks

The sum of ratio of reflected and refracted light is 100%.

The final BRDF becomes:

fmicrofacet=kdcolorπ+ksDFG4(ωon)(ωin)

Noticed: ks is included in Fresnel.

fmicrofacet=kdcolorπ+DFG4(ωon)(ωin)

Importance Sampling

In Monte Carlo integration, if we take pdf(x) = f(x), integrated function will be a constant function:

1Ni=0N1f(x)pdf(x)

So a good approximation to integrated function is good for pdf(x) for sampled x.

When we consider the common diffuse material, We think the irradiance is reflected evenly in the whole hemisphere solid angle.

f(ωi)=Li(p,ωi)f(p,ωi,ωo)cosθidωifdiffuse=f(p,ωi,ωo)=colorπ

The variance triggered by uniform sample over solid angle ω is acceptable.

But for microfacet, the NDF is usually the dominant one(it has an extremely high peak when cosθ=1). So we sample the light with pdf=NDF.

Instead of sampling solid angle directly, we usually use spherical coordinate to sample it. So it is not the pdf respecting the solid angle that we are interested, it is the pdf respecting the spherical coordinates.

pdf(ω)=α2cosθπ(cos2θ(α21)+1)2pdf(θ,ϕ)=α2cosθsinθπ(cos2θ2(α21)+1)2pdf(θ)=02πα2cosθsinθπ(cos2θ(α21)+1)2dϕ=2α2cosθsinθ(cos2θ(α21)+1)2ϵ=cdf(θ<μ)=0μ2α2cosθsinθ(cos2θ(α21)+1)2dθ=α2cos2θ(α21)2+(α21)1α21θ=arccos1ϵϵ(α21)+1

All we need to do is randomly sample probability ϵ in uniform distribution and get the actual sampled θ, which denotes the sampled light.

Don't forget to consider this new pdf(ω) in Monte Carlo integration.

Reference

[1] GAMES101

[2] Monte Carlo Methods in Practice

[3] Sampling Microfacet BRDF

[4] learnopengl PBR Theory

[5] jackysunhz/MyPathTracer

[6] Ubpa/RenderLab