Uniform Sampling on unit Hemisphere

https://blog.thomaspoulet.fr/uniform-sampling-on-unit-hemisphere/

introduction
during the rendering process, u will need to sample points over a designated surface. in this first part (meaning there will probably be a second part covering other shapes) i will try to detail sampling on unit disc and hemisphere. all the examples are generated with two million samples.

uniform?
there is two kind of sampling, uniform and non-uniform. uniform sampling guarantee that all the coordinates have the same probability to be chosen.

在这里插入图片描述
figure 1: phong illumination model applied to four different surfaces. from left to right: in perfect mirror conditions incident rays are generated using the law of reflection. in glossy materials, incident rays are generated on the unit Hemisphere with the reflection direction. in pure diffuse conditions, incident rays are sampled uniformly on the unit sphere.

As we can see on fig. 1 in the case of pure diffuse surface the rays are generated uniformly on the unit hemisphere, meaning that no direction will be preferred over another. We could use non uniform sampling, but by doing so we would give importance to random directions, leading to artefacts in the final image if not intentional or brighter spots. Pure diffuse surfaces are only theoretical, but they makes a good approximations of what we can find in the real world.

Hemisphere: Spherical Coordinates
let us try the naivest thing we can think of. the below equation is the conversion from spherical coordinates to Cartesian on unit disc.

在这里插入图片描述
在这里插入图片描述
perfect! we have two variables, phi and theta, we can generate them randomly respectively over [0, π], and [-π, π]. theta is generated over [0,π] to ensure z is positive.

# Generate a random point on a sphere using spherical coordinates
def random_spherical(u, v):
    # Correct Range [0;pi] [-pi;pi]
    theta = u * np.pi
    phi = (v * np.pi * 2) - np.pi

    # Switch to cartesian coordinates
    x = np.sin(theta) * np.cos(phi)
    y = np.sin(theta) * np.sin(phi)

    return x, y

这里的代码,可以在文章的最后:https://github.com/Lord-Nazdar/Sampling-Python
下载到。

合理的np是导入的库。
在这里插入图片描述
np.pi是π
np.sin和np.cos都是函数。
the above code is rather easy, but the result, is disappointing. the sampling is no way near to be uniform, we have a much more populated spot in the middle of the disc. back to the drawing borad. 采样非常的不均匀,因为中心点特别亮。
在这里插入图片描述
hemisphere: cosine weighted sampling
the goal of this technique is to map a square to a disk using a cosine power distribution to choose the shape and the density of distribution.

在这里插入图片描述

if u remember the introduction, the Phong Illumination uses a uniform and a spiky part for the reflection, the cosine power is perfectly suited for this spiky 尖的 effect. but as we can see on the figure, we can also produce uniform distribution with zero power.

在这里插入图片描述

this equation is not looking good, and we can have good reasons to think that it may take long to compute. but if we take the equation and place it in its context, we can reduce the amount of operations needed using some trigonometric relationship.三角函数

在这里插入图片描述
For p=0 or uniform sampling:
在这里插入图片描述
using the optimized form the computation is done in 0.1040 second versus 0.1721 second 化简了之后,节约了时间。for the general arcos form.
this simple arithmetic optimization can lead to dramatic rendering time saving since we make an extensive use of this funciton during the tracing.

# Generate a random point on a hemisphere using power cosine
def random_cosine(u, v, m=1):
    theta = np.arccos(np.power(1 - u, 1 / (1 + m)))
    phi = 2 * np.pi * v

    # Switch to cartesian coordinates
    x = np.sin(theta) * np.cos(phi)
    y = np.sin(theta) * np.sin(phi)

    return x, y

在这里插入图片描述
The result may seem non-uniform but it is due to the uncorrected disk sphere projection
这个是幂次等于0的时候的采样。不太均匀。

as we can see on the figure the result a uniform sampling on a unit hemisphere. however by varying the power we can modify the distribution and obtain the effect in the Phong Algorithm. here is some example for 5 and 50.
在这里插入图片描述
在这里插入图片描述

The code is available in Python on Github under the MIT license: https://github.com/Lord-Nazdar/Sampling-Python.

References
P. Shirley and K. Chiu, “A Low Distortion Map Between Disk and Square,” J. Graph. Tools, vol. 2, no. 3, pp. 45–52, 1997.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值