环境光吸收式一种用于产生真实软阴影的技术。常规的阴影实现,是采用发射一条阴影测试光线,测试光线与灯是否有遮挡。而环境光吸收采用向半球面随机发射光线,利用光线与环境的交点个数来模拟阴影的深浅。
其实现的原理如下:
//法线空间
optix::Onb onb(ffnormal);
unsigned int seed = rot_seed( rnd_seeds[ launch_index ], frame );
float result = 0.0f;
const float inv_sqrt_samples = 1.0f / float(sqrt_occlusion_samples);
//发射sqrt_occulsion_sample*sqrt_occulsion_sample条测试光线
for( int i=0; i<sqrt_occlusion_samples; ++i ) {
for( int j=0; j<sqrt_occlusion_samples; ++j ) {
PerRayData_occlusion prd_occ;
prd_occ.occlusion = 0.0f;
// Stratify samples via simple jitterring
float u1 = (float(i) + rnd( seed ) )*inv_sqrt_samples;
float u2 = (float(j) + rnd( seed ) )*inv_sqrt_samples;
//计算发射方向
float3 dir;
optix::cosine_sample_hemisphere( u1, u2, dir );
onb.inverse_transform( dir );
optix::Ray occlusion_ray = optix::make_Ray( phit, dir, 2, scene_epsilon,
occlusion_distance );
rtTrace( top_object, occlusion_ray, prd_occ );
//累加结果
result += 1.0f-prd_occ.occlusion;
}
}
result /= (float)(sqrt_occlusion_samples*sqrt_occlusion_samples);