This is to document a work-in-progress project of rendering point clouds with depth of field effect.

First off, in order to draw big, smooth round points using GL_POINTS in cinder, I used a simple fragment shader that generates the circle geometry, since GL_POINT_SMOOTH is not supported by cinder.

void main() {
if (length(gl_PointCoord - vec2(0.5)) > 0.5) {
discard;
} else {
oColor = vColor;
}
}

Then, to render a point cloud with correct visual depth, I used the distance to camera to affect the point size (farther points are smaller).

float cameraDist = distance(ciPosition.xyz, ciModelViewInverse[3].xyz);
float distFactor = cameraDist / maxDistance;
float pointScale = clamp(1.0 - distFactor, minPointScale, maxPointScale);

To create depth of field effect, I used a linear formula to affect the point size with distance to focal plane.

float focusFactor = clamp((abs(cameraDist - focalDepth) / uAperture), 0, uMaxBlur);
gl_PointSize = clamp(80 * focusFactor * pointScale, 4, 60);
First attempt

For point clouds that have a corresponding texture map, I sampled the texture to give the points colors. The transparency of a point is also affected by how much the point is out of focus.

vec4 texel = texture(uTexture, ciTexCoord0);
vColor.rgb = texel.rgb;

float alphaFactor = (1 - focusFactor / 2);
alphaFactor = pow(alphaFactor, 2) - 0.2;
vColor.a = alphaFactor;

I rendered the mesh into a 32 bit floating point FBO using alpha blending. The effect was okay, but there are some issue related to point cloud density.

Airplane model Airplane model

Since the point cloud data I used is obtained from a triangular mesh, the point density distribution is quite uneven. As I subdivide the mesh (3 times in this case), denser parts of the mesh became super dense:

Point density issue

I would try if particle sorting could solve this problem a little bit. I also want to experiment with reconstructing point cloud from a triangular mesh using poisson disk sampling. Further more, I’m considering affecting the point size based on point density at the point’s neighbor surface.

In the screenshot above, the noticible clusters of dark particles are caused by texture sampling. I may add some random pertubation on the texture coordinate or blurring the texture map to see if this improves the visual quality.

Finally, I played with meshes that have different nature. This dog model works better because it’s organic and more consistent in mesh density.

Organic mesh model Organic mesh model

This is a super dense 3D scan point cloud which is desirable in terms of point distribtion. However, the point cloud is a bit too dense and the depth of field effect is greatly affecting visual quality.

3D scan model 3D scan model 3D scan model