Create Your Own Volume Rendering (With Python)

Philip Mocz
The Startup
Published in
4 min readNov 21, 2020

--

For today’s recreational coding exercise, we look at a simple way to create volume renderings to visualize 3D simulation datacubes. This technique is incredibly useful when you have space-filling data you would like to visualize. Such data shows up often in astrophysical datasets but also in other areas of computer graphics and medical data (CT scans and MRIs).

You may find the accompanying Python code on github.

Before diving in, below is a gif of what running the rendering algorithm on some data may look like:

Datacube

We assume that the data is an Nx × Ny × Nz datacube of a density that we would like to visualize.

Transfer Function

First we will define a custom transfer function that will return a red, green, blue, and opacity value (r,g,b,a) as a function of input density. This function can really be any choice you want, but a nice strategy is to pick out certain density values that you are most interested in visualizing and make the transfer function opaque (a~1) around those values, and have a specific color at that value. The volume rendering algorithm, in a sense, creates a 3D iso-contour plot. A sketch of what a transfer function may look like is shown below. In this example, two contour values are picked out.

--

--

Philip Mocz
The Startup

Computational Physicist. Sharing intro tutorials on creating your own computer simulations! Harvard ’12 (A.B), ’17 (PhD). Connect with me @PMocz