What’s the correct color accumulation formula for volume rendering?

86 views Asked by At

I’m writing a pixel shader (in HLSL, for Direct3D 11) to visualize volumetric data. The source data is a 3D texture with pre-multiplied RGBA values. I need a single RGBA color on output, again with pre-multiplied alpha. I don’t need any lighting for the volume.

What’s the correct formula to accumulate translucent voxel colors along the ray?

Z order is not an issue. The source data is dense, I can iterate either back to front, or front to back direction.

The commonly used alpha blending formulae seem to fail to handle the case when the output is also transparent. Specifically, when at least 1 sampled voxel has alpha = 1.0, I want output alpha to also be 1.0, regardless on how many translucent voxels are in front of that one opaque voxel.

Here’s the main loop of the pixel shader:

// 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
0

There are 0 answers