光线追踪不正确的软阴影采样
Raytracing incorrect soft shadow sampling
你好,我正在研究光线追踪算法,但我被困在 monte carlo 算法上。在没有区域光的情况下渲染时,我的渲染输出是正确的,但是当我将区域光实现添加到源代码以生成软阴影时,我遇到了问题。
这是前后输出图像。
当我向下移动蓝色球体时,问题仍在继续(请注意,当球体沿白色虚线移动时,伪影仍在继续)。
请注意,此球体和区域光具有相同的 z 偏移量。当我将蓝色球体带到屏幕前时,工件消失了。我认为问题是由均匀采样锥或采样球函数引起的但不确定。
函数如下:
template <typename T>
CVector3<T> UConeSample(T u1, T u2, T costhetamax,
const CVector3<T>& x, const CVector3<T>& y, const CVector3<T>& z) {
T costheta = Math::Lerp(u1, costhetamax, T(1));
T sintheta = sqrtf(T(1) - costheta*costheta);
T phi = u2 * T(2) * T(M_PI);
return cosf(phi) * sintheta * x +
sinf(phi) * sintheta * y +
costheta * z;
}
我正在从 van Der Corput 序列生成随机浮点值 u1、u2。
这是球体采样法
CPoint3<float> CSphere::Sample(const CLightSample& ls, const CPoint3<float>& p, CVector3<float> *n) const {
// translate object to world space
CPoint3<float> pCentre = o2w(CPoint3<float>(0.0f));
CVector3<float> wc = Vector::Normalize(pCentre - p);
CVector3<float> wcx, wcy;
//create local coordinate system from wc for uniform sample cone
Vector::CoordinateSystem(wc, &wcx, &wcy);
//check if inside, epsilon val. this is true?
if (Point::DistSquare(p, pCentre) - radius*radius < 1e-4f)
return Sample(ls, n);
// Else outside evaluate cosinus theta value
float sinthetamax2 = radius * radius / Point::DistSquare(p, pCentre);
float costhetamax = sqrtf(Math::Max(0.0f, 1.0f - sinthetamax2));
// Surface properties
CSurfaceProps dg_sphere;
float thit, ray_epsilon;
CPoint3<float> ps;
//create ray direction from sampled point then send ray to sphere
CRay ray(p, Vector::UConeSample(ls.u1, ls.u2, costhetamax, wcx, wcy, wc), 1e-3f);
// Check intersection against sphere, fill surface properties and calculate hit point
if (!Intersect(ray, &thit, &ray_epsilon, &dg_sphere))
thit = Vector::Dot(pCentre - p, Vector::Normalize(ray.d));
// Evaluate surface normal
ps = ray(thit);
*n = CVector3<float>(Vector::Normalize(ps - pCentre));
//return sample point
return ps;
}
有人有什么建议吗?谢谢
我解决了问题。
- 问题是由RNG "Random Number Generator" algorithm in light sample class (require well distributed u1, u2: low discrepancy sampling)造成的。
光线追踪需要更复杂的 RNG(其中之一 "The Mersenne Twister" 伪随机数生成器)和良好的混洗算法。
希望对您有所帮助。感谢所有发表评论的人。
你好,我正在研究光线追踪算法,但我被困在 monte carlo 算法上。在没有区域光的情况下渲染时,我的渲染输出是正确的,但是当我将区域光实现添加到源代码以生成软阴影时,我遇到了问题。
这是前后输出图像。
当我向下移动蓝色球体时,问题仍在继续(请注意,当球体沿白色虚线移动时,伪影仍在继续)。 请注意,此球体和区域光具有相同的 z 偏移量。当我将蓝色球体带到屏幕前时,工件消失了。我认为问题是由均匀采样锥或采样球函数引起的但不确定。
函数如下:
template <typename T>
CVector3<T> UConeSample(T u1, T u2, T costhetamax,
const CVector3<T>& x, const CVector3<T>& y, const CVector3<T>& z) {
T costheta = Math::Lerp(u1, costhetamax, T(1));
T sintheta = sqrtf(T(1) - costheta*costheta);
T phi = u2 * T(2) * T(M_PI);
return cosf(phi) * sintheta * x +
sinf(phi) * sintheta * y +
costheta * z;
}
我正在从 van Der Corput 序列生成随机浮点值 u1、u2。 这是球体采样法
CPoint3<float> CSphere::Sample(const CLightSample& ls, const CPoint3<float>& p, CVector3<float> *n) const {
// translate object to world space
CPoint3<float> pCentre = o2w(CPoint3<float>(0.0f));
CVector3<float> wc = Vector::Normalize(pCentre - p);
CVector3<float> wcx, wcy;
//create local coordinate system from wc for uniform sample cone
Vector::CoordinateSystem(wc, &wcx, &wcy);
//check if inside, epsilon val. this is true?
if (Point::DistSquare(p, pCentre) - radius*radius < 1e-4f)
return Sample(ls, n);
// Else outside evaluate cosinus theta value
float sinthetamax2 = radius * radius / Point::DistSquare(p, pCentre);
float costhetamax = sqrtf(Math::Max(0.0f, 1.0f - sinthetamax2));
// Surface properties
CSurfaceProps dg_sphere;
float thit, ray_epsilon;
CPoint3<float> ps;
//create ray direction from sampled point then send ray to sphere
CRay ray(p, Vector::UConeSample(ls.u1, ls.u2, costhetamax, wcx, wcy, wc), 1e-3f);
// Check intersection against sphere, fill surface properties and calculate hit point
if (!Intersect(ray, &thit, &ray_epsilon, &dg_sphere))
thit = Vector::Dot(pCentre - p, Vector::Normalize(ray.d));
// Evaluate surface normal
ps = ray(thit);
*n = CVector3<float>(Vector::Normalize(ps - pCentre));
//return sample point
return ps;
}
有人有什么建议吗?谢谢
我解决了问题。
- 问题是由RNG "Random Number Generator" algorithm in light sample class (require well distributed u1, u2: low discrepancy sampling)造成的。
光线追踪需要更复杂的 RNG(其中之一 "The Mersenne Twister" 伪随机数生成器)和良好的混洗算法。
希望对您有所帮助。感谢所有发表评论的人。