Importance sampling

minggoddess發表於2016-11-17

用蒙特卡洛求解積分時 (Monte Carlo 隨機取樣對目標積分函式做近似)

importance sampling func p(x)

 p(x)值大的地方,Monte Carlo多采幾次

值小的地方,少取樣一些。

一起貢獻MC的積分值

http://blog.sina.com.cn/s/blog_4e5740460100cw5b.html  

 link1

 

 

http://statweb.stanford.edu/~owen/mc/

對 GGX的importance的理解

ImportanceSampleGGX(float2 Xi, float Roughness, float3 N)

{

float a = Roughness * Roughness;
float Phi = 2 * PI * Xi.x;
float CosTheta = sqrt( (1 - Xi.y) / ( 1 + (a*a - 1) * Xi.y ) );
float SinTheta = sqrt( 1 - CosTheta * CosTheta );
float3 H;
H.x = SinTheta * cos( Phi );
H.y = SinTheta * sin( Phi );
H.z = CosTheta;
float3 UpVector = abs(N.z) < 0.999 ? float3(0,0,1) : float3(1,0,0);
float3 TangentX = normalize( cross( UpVector, N ) );
float3 TangentY = cross( N, TangentX );
// Tangent to world space
return TangentX * H.x + TangentY * H.y + N * H.z;

}

 

http://blog.tobias-franke.eu/2014/03/30/notes_on_importance_sampling.html

link2

連結裡的程式碼,算得是極座標下的p

我這段程式碼算得三維的H 換到xyz下面

 

但為什麼p的值就變成H了呢 看著像微表面normal或者 半形向量

 因為D就是這個定義 D就是微表面normal的分佈函式

而微表面的normal朝各個方向 只有朝向l, v表徵的h方向的有貢獻,所以是h

 

整體看importanceSampleGGX的含義就是求解ggx了 公式 link2有提供

 

 感覺,就是用個函式 積個PDF再弄個什麼CDF不知道是什麼 然後就求出來了 中間出現了P() 

The NDF itself is defined as:

D(m)=α2π(cos2(nm)(α21)+1)2(11)(11)D(m)=α2π(cos2⁡(n⋅m)(α2−1)+1)2

Just like above, we start out with the PDF for GGX:

p(θ,ϕ)=α2π(cos2θ(α21)+1)2cosθsinθ(12)(12)p(θ,ϕ)=α2π(cos2⁡θ(α2−1)+1)2cos⁡θsin⁡θ

As in the case of Phong, we create two functions for θθ and ϕϕ. First let’s create p(θ)p(θ):

integrate((a^2cos(t)sin(t))/(%pi((a^2−1)cos(t)^2+1)^2), p, 0, 2*%pi)

p(θ)=2π0p(θ,ϕ)dϕ=2α2(cos2θ(α21)+1)2cosθsinθ(13)(13)p(θ)=∫02πp(θ,ϕ)dϕ=2α2(cos2⁡θ(α2−1)+1)2cos⁡θsin⁡θ

The integration for ϕϕ is the same as above, so we skip it and instead now create the CDF for p(θ)p(θ):

integrate((2a^2cos(t)sin(t))/((a^2−1)cos(t)^2+1)^2, t, 0, s)

P(sθ)=sθ0p(θ)dθ=(14)(14)P(sθ)=∫0sθp(θ)dθ=
2α2(1(2α44α2+2)cos2sθ+2α2212α42α2)(15)(15)2α2(1(2α4−4α2+2)cos2⁡sθ+2α2−2−12α4−2α2)

Setting the CDF to a random variable and solving for ss yields:

solve(2a^2(1/((2a^4−4a^2+2)cos(s)^2+2a^2−2)−1/(2a^4−2a^2)) = x, s)

P(sθ)=ξθ(16)(16)P(sθ)=ξθ
sθ=cos1(1ξθ(α21)ξθ+1−−−−−−−−−−−−√)(17)(17)sθ=cos−1(1−ξθ(α2−1)ξθ+1)

A simple GLSL function to generate important directions looks like this:

vec2 importance_sample_ggx(vec2 xi)
{
  float phi = 2.0f * PI * xi.x;
  float theta = acos(sqrt((1.0f - xi.y)/
                          ((a*a - 1.0f) * xi.y + 1.0f)
                         ));
  return vec2(phi, theta);
}

===========
http://www.cnblogs.com/xbinworld/p/4266146.html

相關文章