Graphics Reference
In-Depth Information
float
GetLookupDepth
(
float
inViewSpaceDepth
)
{
float
depth_range
= 128.0;
//
<
Depth range R
float
inv_curve_power
= 1.0/2.0;
//
<
Inv. power (1.0 / C)
return
pow
((
inViewSpaceDepth
/
depth_range
),
inv_curve_power
);
}
Listing 3.1.
Calculating the 3D texture lookup depth coordinate for a given view-space
depth.
We render the particles as traditional camera-facing billboards. The particle's
alpha channel will be used to define the scattering amount. To calculate the
influence a particle has on each depth slice, we use the code shown in Listing 3.2.
In the function we first calculate the 3D texture lookup depth coordinate. We
then scale the value up to a 0.0 to 15.0 range to map it within the range of our
depth slice indices. The resulting value
particle_slice_pos
defines the position
of the particle between the slices. The influence a particle has on each depth
slice is based on the absolute value of the difference between
particle_slice_pos
and each slice index. We refer to this value as
distance
because it describes the
distance of the particle to the depth slice in texture space.
If the distance is 0.0, the particle is located at the same depth as the depth
slice and the particle influence should be 1.0. A distance equal to or greater
than 1.0 means the particle is more than one slice away and the influence will
be 0.0. For distances ranging between 0.0 and 1.0, the influence will be equal to
the reversed value of
distance
. For any slice index the influence can be easily
calculated by clamping the
distance
value to a 0.0 to 1.0 range and reversing the
result to a 1.0 to 0.0 range using
f
(
x
)=1
−
x
.
float
GetAmountValue
(
float
inParticleDepth
,
float
inScatteringAmount
,
int
inSliceIndex
)
{
int
num_depth_slices
= 16;
//
<
Number of depth slices
float
particle_slice_pos
=
GetLookupDepth
(
inParticleDepth
)
(
num_depth_slices
−
1) ;
float
distance
=
abs
(
particle_slice_pos
−
inSliceIndex
);
float
influence
=1.0
−
saturate
(
distance
);
return
inScatteringAmount
influence
;
}
Listing 3.2.
Calculating the particle's scattering amount value for a single depth slice.