我正在编写一个像素着色器(在 HLSL 中,用于 Direct3D 11)来可视化体积数据。源数据是具有预乘 RGBA 值的 3D 纹理。我需要在输出上使用单一的 RGBA 颜色,同样使用预乘 alpha。我不需要任何照明。
沿射线累积半透明体素颜色的正确公式是什么?
Z 顺序不是问题。源数据密集,我可以从后到前或从前到后的方向进行迭代。
常用的 alpha 混合公式似乎无法处理输出也是透明的情况。具体来说,当至少 1 个采样体素的 alpha = 1.0 时,我希望输出 alpha 也为 1.0,而不管该不透明体素前面有多少半透明体素。
这是像素着色器的主循环:
// Figure out count of layers to sample
const float inv = 1.0 / layersCount;
float s = round( layersCount );
// Initialize the accumulator
float4 acc = 0;
// Iterate over the ray, using back to front direction
for( s -= 0.5; s > 0; s -= 1.0 )
{
// Compute UV value for the sampler
float3 tc = mad( dir, s * inv, volumeNear );
// Sample from the volume texture
float4 voxel = sampleVolumeColor( tc );
// `voxel` usually has transparency, but it can also be completely opaque, or fully transparent.
// The values have pre-multiuplied alpha, voxel.rgb <= voxel.a
// TODO: correct formula to update accumulator with voxel color?
}
return acc; // SV_Target