Deriving PDF for Hemisphere Sampling
-
Target distribution in , which is with .
-
The transformation from to :
-
The Jacobian of the transformation gives .
-
, so we have:
Jacobian Determinant Derivation
Transformation from spherical to Cartesian coordinates:
Jacobian matrix :
Compute each partial derivative:
Substituting into :
Determinant:
Using Laplace expansion along the third row:
Computing and :
Substituting:
Using :
For :
Uniformly Sampling the Unit Hemisphere
Domain and Probability Density
-
The domain, i.e., the unit hemisphere surface area, is .
Uniformly sampling the domain over implies: -
Since , we want:
Marginal and Conditional Densities
Marginal Density :
Conditional Density :
Antiderivatives
Antiderivative of :
Antiderivative of :
Inversion and Sampling
Invert to Get and :
Apply Transformation:
Apply transformation on to obtain uniformly distributed .
Cosine-Weighted Sampling on the Unit Hemisphere
Define the Target PDF
The cosine-weighted sampling is based on a probability density function (PDF) proportional to the cosine of the angle between the sample direction and the normal :
where .
Express the PDF in Spherical Coordinates
The PDF in spherical coordinates becomes:
where represents the cosine of the angle between the sample direction and the normal, and is the Jacobian determinant.
Marginal Density
To obtain the marginal density , integrate over :
Conditional Density
The conditional density of given is uniform:
Inverse Transform Sampling
Marginal
The cumulative distribution function (CDF) for is:
By setting , where is a random variable, we obtain:
Conditional
The CDF for is:
Set , where is a random variable, leading to:
Final Sampling Transformation
Using and , we compute:
Converting these spherical coordinates to Cartesian coordinates:
Why use this target PDF ?
-
Physical Background: In optics and rendering, cosine-weighted sampling is commonly used to simulate diffuse reflection of light. This kind of reflection follows Lambert's Cosine Law, which can be described as:
- The brightness of light reflected from a surface is proportional to the cosine of the angle between the light ray and the surface normal.
- The energy density of the reflected light should be weighted by to more accurately simulate diffuse reflection behavior.
-
Diffuse Energy Distribution: Consider a small area element on the unit hemisphere (measured in solid angle). The energy flux (radiance) in the direction of this element is proportional to , where is the angle between the direction and the normal.
- To satisfy the definition of a probability density function, the total probability distribution must be normalized so that its integral over the entire hemisphere equals 1.
-
Normalization Derivation:
(1) Unnormalized PDF: Since the distribution needs to be proportional to , we have .
(2) Integral over the unit hemisphere: In spherical coordinates, the solid angle element is given by . Therefore:
Substituting (where is the normalization factor), the integral becomes:
Substituting :
Separating the variables:
(3) Computing the Integral:
First, integrate with respect to :
Next, for , use :
Therefore:
Solving for , we get .
-
Final PDF:
After normalization, the probability density function becomes:
Implementation
The following C++ functions generate random directions based on uniform and cosine-weighted sampling:
vec3 random_cosine_direction() {
auto r1 = random_double();
auto r2 = random_double();
auto phi = 2 * pi * r2;
auto z = std::sqrt(1 - r1);
auto sin_theta = std::sqrt(r1);
auto x = std::cos(phi) * sin_theta;
auto y = std::sin(phi) * sin_theta;
return vec3(x, y, z);
}
vec3 random_uniform_direction() {
auto r1 = random_double();
auto r2 = random_double();
auto phi = 2 * pi * r2;
auto z = 1 - r1;
auto sin_theta = std::sqrt(1 - z * z);
auto x = std::cos(phi) * sin_theta;
auto y = std::sin(phi) * sin_theta;
return vec3(x, y, z);
}
Sampling the Unit Disk
Computing the PDF of a Transformation
To transform polar coordinates to Cartesian coordinates, we use:
The Jacobian determinant of the transformation is:
Thus, the relationship between probability densities in Cartesian and polar coordinates is:
indicating that the change in probability density from to is inversely proportional to .
Uniform PDF in Cartesian Coordinates
The proportionality constant is the area of the unit disk, . For uniform sampling, the target PDF in Cartesian coordinates is:
PDF in Polar Coordinates
Using the Jacobian determinant:
we get:
Marginal and Conditional PDFs
The marginal PDF for is:
The conditional PDF for given is:
Sampling from the PDFs
To sample and , integrate the marginal and conditional PDFs:
where . This ensures:
- is constant: all positions on a ring of radius are equally likely.
Malley's Method: A Quick Solution
Malley's method provides an easy way to generate cosine-weighted samples on the unit hemisphere. The steps are:
-
Uniform Sampling on the Unit Disk
Generate uniform samples on the unit disk. This process is well-established. -
Projection to the Hemisphere
Project the disk samples onto the surface of the hemisphere. This results in:- A sample distribution over the hemisphere's surface that is weighted by the cosine of .
Why Does This Work?
The projection ensures the resulting distribution aligns with the cosine weighting due to the geometry of the hemisphere and the relationship between the disk and spherical coordinates.
To derive this explicitly:
-
Coordinate Transformation:
Express the hemisphere's surface coordinates in terms of disk coordinates . The Jacobian determinant of the transformation accounts for the change in density. -
Jacobian Determinant:
Compute the Jacobian determinant when transforming from to . It reveals that the density transforms proportionally to , aligning with cosine weighting. -
Projection:
Uniform sampling on the disk () naturally transforms into cosine-weighted sampling () over the hemisphere due to this relationship and the geometry of the projection.
By starting with the uniform distribution on the disk and projecting, the resulting samples inherit the cosine weighting without requiring additional adjustments.
Computing the Jacobian Determinant for the Transformation
We are projecting points from the unit disk to the hemisphere , where:
- is the radial distance in the disk,
- is the azimuthal angle in the disk,
- is the polar angle in the hemisphere,
- remains unchanged.
Step 1: Define the Relationship Between and
From the projection, the radial distance maps to :
Thus, the transformation is:
with:
Step 2: Write the Jacobian Matrix
The Jacobian matrix for the transformation is:
From the transformation equations:
- ,
- ,
Thus, the Jacobian matrix becomes:
Step 3: Compute the Determinant
The Jacobian determinant is the absolute value of the determinant of :
Step 4: Adjust for Density Transformation
The relationship between densities is proportional to the inverse of the Jacobian determinant. Thus, the transformation of probability density scales proportionally to :
Final Result
The Jacobian determinant for the transformation is:
The density transforms proportionally to , aligning with the cosine-weighted sampling over the hemisphere.